Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/lib/iolog/iolog_timing.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2009-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 <stdio.h>
22
#include <stdlib.h>
23
#ifdef HAVE_STDBOOL_H
24
# include <stdbool.h>
25
#else
26
# include <compat/stdbool.h>
27
#endif /* HAVE_STDBOOL_H */
28
#include <string.h>
29
#include <signal.h>
30
#include <unistd.h>
31
#include <ctype.h>
32
#include <errno.h>
33
#include <limits.h>
34
#include <fcntl.h>
35
#include <time.h>
36
37
#include <sudo_compat.h>
38
#include <sudo_debug.h>
39
#include <sudo_eventlog.h>
40
#include <sudo_fatal.h>
41
#include <sudo_gettext.h>
42
#include <sudo_iolog.h>
43
#include <sudo_util.h>
44
45
static int timing_event_adj;
46
47
void
48
iolog_adjust_delay(struct timespec *delay, struct timespec *max_delay,
49
double scale_factor)
50
{
51
debug_decl(iolog_adjust_delay, SUDO_DEBUG_UTIL);
52
53
/* Avoid division by zero or negative delays. */
54
if (scale_factor <= 0.0) {
55
sudo_timespecclear(delay);
56
debug_return;
57
}
58
59
if (scale_factor != 1.0) {
60
/* Order is important: we don't want to double the remainder. */
61
const double seconds = (double)delay->tv_sec / scale_factor;
62
const double nseconds = (double)delay->tv_nsec / scale_factor;
63
delay->tv_sec = (time_t)seconds;
64
delay->tv_nsec = (long)nseconds;
65
delay->tv_nsec += (long)((seconds - (double)delay->tv_sec) * 1000000000);
66
while (delay->tv_nsec >= 1000000000) {
67
delay->tv_sec++;
68
delay->tv_nsec -= 1000000000;
69
}
70
}
71
72
/* Clamp to max delay. */
73
if (max_delay != NULL) {
74
if (sudo_timespeccmp(delay, max_delay, >)) {
75
delay->tv_sec = max_delay->tv_sec;
76
delay->tv_nsec = max_delay->tv_nsec;
77
}
78
}
79
80
debug_return;
81
}
82
83
/*
84
* Parse the delay as seconds and nanoseconds: %lld.%09ld
85
* Sudo used to write this as a double, but since timing data is logged
86
* in the C locale this may not match the current locale.
87
*/
88
char *
89
iolog_parse_delay(const char *cp, struct timespec *delay,
90
const char *decimal_point)
91
{
92
char numbuf[STRLEN_MAX_SIGNED(long long) + 1];
93
const char *errstr, *ep;
94
long long llval;
95
size_t len;
96
debug_decl(iolog_parse_delay, SUDO_DEBUG_UTIL);
97
98
/* Parse seconds (whole number portion). */
99
for (ep = cp; isdigit((unsigned char)*ep); ep++)
100
continue;
101
len = (size_t)(ep - cp);
102
if (len >= sizeof(numbuf)) {
103
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
104
"%s: number of seconds is too large", cp);
105
debug_return_ptr(NULL);
106
}
107
memcpy(numbuf, cp, len);
108
numbuf[len] = '\0';
109
delay->tv_sec = (time_t)sudo_strtonum(numbuf, 0, TIME_T_MAX, &errstr);
110
if (errstr != NULL) {
111
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
112
"%s: number of seconds is %s", numbuf, errstr);
113
debug_return_ptr(NULL);
114
}
115
116
/* Radix may be in user's locale for sudo < 1.7.4 so accept that too. */
117
if (*ep != '.' && *ep != *decimal_point) {
118
if (*ep == '\0' || isspace((unsigned char)*ep)) {
119
/* No fractional part. */
120
delay->tv_nsec = 0;
121
goto done;
122
}
123
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
124
"invalid characters after seconds: %s", ep);
125
debug_return_ptr(NULL);
126
}
127
cp = ep + 1;
128
129
/* Parse fractional part, we may read more precision than we can store. */
130
for (ep = cp; isdigit((unsigned char)*ep); ep++)
131
continue;
132
len = (size_t)(ep - cp);
133
if (len >= sizeof(numbuf)) {
134
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
135
"%s: number of nanoseconds is too large", cp);
136
debug_return_ptr(NULL);
137
}
138
memcpy(numbuf, cp, len);
139
numbuf[len] = '\0';
140
llval = sudo_strtonum(numbuf, 0, LLONG_MAX, &errstr);
141
if (errstr != NULL) {
142
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
143
"%s: number of nanoseconds is %s", numbuf, errstr);
144
debug_return_ptr(NULL);
145
}
146
147
/* Adjust fractional part to nanosecond precision. */
148
if (len < 9) {
149
/* Convert to nanosecond precision. */
150
do {
151
llval *= 10;
152
} while (++len < 9);
153
} else if (len > 9) {
154
/* Clamp to nanoseconds. */
155
do {
156
llval /= 10;
157
} while (--len > 9);
158
}
159
delay->tv_nsec = (long)llval;
160
161
done:
162
/* Advance to the next field. */
163
while (isspace((unsigned char)*ep))
164
ep++;
165
166
debug_return_str((char *)ep);
167
}
168
169
/*
170
* Parse a timing line, which is formatted as:
171
* IO_EVENT_TTYOUT sleep_time num_bytes
172
* IO_EVENT_WINSIZE sleep_time lines cols
173
* IO_EVENT_SUSPEND sleep_time signo
174
* Where type is IO_EVENT_*, sleep_time is the number of seconds to sleep
175
* before writing the data and num_bytes is the number of bytes to output.
176
* Returns true on success and false on failure.
177
*/
178
bool
179
iolog_parse_timing(const char *line, struct timing_closure *timing)
180
{
181
unsigned long ulval;
182
char *cp, *ep;
183
debug_decl(iolog_parse_timing, SUDO_DEBUG_UTIL);
184
185
/* Clear iolog descriptor. */
186
timing->iol = NULL;
187
188
/* Parse event type. */
189
ulval = strtoul(line, &ep, 10);
190
if (ep == line || !isspace((unsigned char) *ep))
191
goto bad;
192
if (ulval >= IO_EVENT_COUNT)
193
goto bad;
194
if (ulval == IO_EVENT_TTYOUT_1_8_7) {
195
/* work around a bug in timing files generated by sudo 1.8.7 */
196
timing_event_adj = 2;
197
}
198
timing->event = (int)ulval - timing_event_adj;
199
for (cp = ep + 1; isspace((unsigned char) *cp); cp++)
200
continue;
201
202
/* Parse delay, returns the next field or NULL on error. */
203
if ((cp = iolog_parse_delay(cp, &timing->delay, timing->decimal)) == NULL)
204
goto bad;
205
206
switch (timing->event) {
207
case IO_EVENT_SUSPEND:
208
/* Signal name (no leading SIG prefix) or number. */
209
if (str2sig(cp, &timing->u.signo) == -1)
210
goto bad;
211
break;
212
case IO_EVENT_WINSIZE:
213
ulval = strtoul(cp, &ep, 10);
214
if (ep == cp || !isspace((unsigned char) *ep))
215
goto bad;
216
if (ulval > INT_MAX)
217
goto bad;
218
timing->u.winsize.lines = (int)ulval;
219
for (cp = ep + 1; isspace((unsigned char) *cp); cp++)
220
continue;
221
222
ulval = strtoul(cp, &ep, 10);
223
if (ep == cp || *ep != '\0')
224
goto bad;
225
if (ulval > INT_MAX)
226
goto bad;
227
timing->u.winsize.cols = (int)ulval;
228
break;
229
default:
230
errno = 0;
231
ulval = strtoul(cp, &ep, 10);
232
if (ep == cp || *ep != '\0')
233
goto bad;
234
/* Note: assumes SIZE_MAX == ULONG_MAX */
235
if (errno == ERANGE && ulval == ULONG_MAX)
236
goto bad;
237
timing->u.nbytes = (size_t)ulval;
238
break;
239
}
240
241
debug_return_bool(true);
242
bad:
243
debug_return_bool(false);
244
}
245
246
/*
247
* Read the next record from the timing file.
248
* Return 0 on success, 1 on EOF and -1 on error.
249
*/
250
int
251
iolog_read_timing_record(struct iolog_file *iol, struct timing_closure *timing)
252
{
253
char line[LINE_MAX];
254
const char *errstr;
255
char *nl;
256
debug_decl(iolog_read_timing_record, SUDO_DEBUG_UTIL);
257
258
/* Read next record from timing file. */
259
if (iolog_gets(iol, line, sizeof(line), &errstr) == NULL) {
260
/* EOF or error reading timing file, we are done. */
261
if (iolog_eof(iol))
262
debug_return_int(1);
263
sudo_warnx(U_("error reading timing file: %s"), errstr);
264
debug_return_int(-1);
265
}
266
267
/*
268
* All timing file records must end with a newline.
269
* A missing newline may indicate a line longer than LINE_MAX.
270
*/
271
nl = strchr(line, '\n');
272
if (nl == NULL) {
273
goto invalid;
274
}
275
*nl = '\0';
276
277
/* Parse timing file record. */
278
if (!iolog_parse_timing(line, timing)) {
279
goto invalid;
280
}
281
282
debug_return_int(0);
283
invalid:
284
/* Truncate invalid timing file line at 128 bytes. */
285
line[128] = '\0';
286
sudo_warnx(U_("invalid timing file line: %s"), line);
287
debug_return_int(-1);
288
}
289
290