Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/logsrvd/logsrv_util.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2019-2020 Todd C. Miller <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
19
#include <config.h>
20
21
#include <sys/types.h>
22
23
#include <errno.h>
24
#ifdef HAVE_STDBOOL_H
25
# include <stdbool.h>
26
#else
27
# include <compat/stdbool.h>
28
#endif /* HAVE_STDBOOL_H */
29
#if defined(HAVE_STDINT_H)
30
# include <stdint.h>
31
#elif defined(HAVE_INTTYPES_H)
32
# include <inttypes.h>
33
#endif
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <string.h>
37
#include <time.h>
38
#include <unistd.h>
39
40
#include <sudo_compat.h>
41
#include <sudo_debug.h>
42
#include <sudo_fatal.h>
43
#include <sudo_gettext.h>
44
#include <sudo_iolog.h>
45
#include <sudo_util.h>
46
47
#include "logsrv_util.h"
48
49
/*
50
* Expand buf as needed or just reset it.
51
*/
52
bool
53
expand_buf(struct connection_buffer *buf, size_t needed)
54
{
55
void *newdata;
56
debug_decl(expand_buf, SUDO_DEBUG_UTIL);
57
58
if (buf->size < needed) {
59
/* Expand buffer. */
60
const size_t newsize = sudo_pow2_roundup(needed);
61
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
62
"expanding buffer from %zu to %zu", buf->size, newsize);
63
if (newsize < needed) {
64
/* overflow */
65
errno = ENOMEM;
66
goto oom;
67
}
68
if ((newdata = malloc(newsize)) == NULL)
69
goto oom;
70
if (buf->len != buf->off)
71
memcpy(newdata, buf->data + buf->off, buf->len - buf->off);
72
free(buf->data);
73
buf->data = newdata;
74
buf->size = newsize;
75
} else {
76
/* Just reset existing buffer. */
77
if (buf->len != buf->off) {
78
memmove(buf->data, buf->data + buf->off,
79
buf->len - buf->off);
80
}
81
}
82
buf->len -= buf->off;
83
buf->off = 0;
84
85
debug_return_bool(true);
86
oom:
87
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
88
debug_return_bool(false);
89
}
90
91
/*
92
* Open any I/O log files that are present.
93
* The timing file must always exist.
94
*/
95
bool
96
iolog_open_all(int dfd, const char *iolog_dir, struct iolog_file *iolog_files,
97
const char *mode)
98
{
99
int iofd;
100
debug_decl(iolog_open_all, SUDO_DEBUG_UTIL);
101
102
/* Iterate in reverse order so we open/lock IOFD_TIMING first. */
103
for (iofd = IOFD_MAX - 1; iofd >= 0; iofd--) {
104
iolog_files[iofd].enabled = true;
105
if (!iolog_open(&iolog_files[iofd], dfd, iofd, mode)) {
106
if (errno != ENOENT) {
107
sudo_warn(U_("unable to open %s/%s"), iolog_dir,
108
iolog_fd_to_name(iofd));
109
debug_return_bool(false);
110
}
111
}
112
}
113
if (!iolog_files[IOFD_TIMING].enabled) {
114
sudo_warn(U_("unable to open %s/%s"), iolog_dir,
115
iolog_fd_to_name(IOFD_TIMING));
116
debug_return_bool(false);
117
}
118
debug_return_bool(true);
119
}
120
121
/*
122
* Seek to the specified point in time in the I/O logs.
123
*/
124
bool
125
iolog_seekto(int iolog_dir_fd, const char *iolog_path,
126
struct iolog_file *iolog_files, struct timespec *elapsed_time,
127
const struct timespec *target)
128
{
129
struct timing_closure timing;
130
off_t pos;
131
debug_decl(iolog_seekto, SUDO_DEBUG_UTIL);
132
133
if (!sudo_timespecisset(target)) {
134
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
135
"resuming at start of file [0, 0]");
136
debug_return_bool(true);
137
}
138
139
memset(&timing, 0, sizeof(timing));
140
timing.decimal = ".";
141
142
/* Parse timing file until we reach the target point. */
143
for (;;) {
144
switch (iolog_read_timing_record(&iolog_files[IOFD_TIMING], &timing)) {
145
case 0:
146
break;
147
case 1:
148
/* EOF reading timing file. */
149
sudo_warnx(U_("%s/%s: unable to find resume point [%lld, %ld]"),
150
iolog_path, "timing", (long long)target->tv_sec,
151
target->tv_nsec);
152
goto bad;
153
default:
154
/* Error printed by iolog_read_timing_record(). */
155
goto bad;
156
}
157
sudo_timespecadd(elapsed_time, &timing.delay, elapsed_time);
158
if (timing.event < IOFD_TIMING) {
159
if (!iolog_files[timing.event].enabled) {
160
/* Missing log file. */
161
sudo_warn(U_("missing I/O log file %s/%s"), iolog_path,
162
iolog_fd_to_name(timing.event));
163
goto bad;
164
}
165
pos = iolog_seek(&iolog_files[timing.event], (off_t)timing.u.nbytes,
166
SEEK_CUR);
167
if (pos == -1) {
168
sudo_warn(U_("%s/%s: unable to seek forward %zu"), iolog_path,
169
iolog_fd_to_name(timing.event), timing.u.nbytes);
170
goto bad;
171
}
172
}
173
if (sudo_timespeccmp(elapsed_time, target, >=)) {
174
if (sudo_timespeccmp(elapsed_time, target, ==))
175
break;
176
177
/* Mismatch between resume point and stored log. */
178
sudo_warnx(U_("%s/%s: unable to find resume point [%lld, %ld]"),
179
iolog_path, "timing", (long long)target->tv_sec,
180
target->tv_nsec);
181
goto bad;
182
}
183
}
184
debug_return_bool(true);
185
bad:
186
debug_return_bool(false);
187
}
188
189