-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeclient.c
470 lines (388 loc) · 9.97 KB
/
timeclient.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
* timeclient.c
*
* run a program under timserver
*
* timeexec program args
*
* is the same as:
* LD_PRELOAD=./timeclient.so program args
* where timeclient.so is from this file
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/msg.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <dlfcn.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "timecontrol.h"
/*
* number of queue and client, log file
*/
int queue;
long client;
char logfile[1000];
char *timeclient;
/*
* logging
*
* cannot be done with regular io stream operations, and especially not opening
* a file only once in the constructor, since the application may close file
* descriptors at will; this is what cronie does: before running a task, it
* closes all file descriptors > stderr but does not fclose their FILE*, making
* fopen/fprintf unusable
*
* since multiple processes may write concurrently, this function should be
* regulated by a semaphore; but O_APPEND seems enough for now
*
* the log file has to be rw-rw-rw because cron may run processes as regular
* users, and the log file is initially owned by root
*/
int logprintf(char *fmt, ...) {
mode_t m;
int fd;
char line[500];
va_list va;
m = umask(0);
fd = open(logfile,
O_WRONLY | O_CREAT | O_APPEND,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (fd == -1) {
perror(logfile);
return -1;
}
umask(m);
va_start(va, fmt);
vsnprintf(line, 500, fmt, va);
write(fd, line, strlen(line));
va_end(va);
close(fd);
return 0;
}
/*
* original system calls
*/
unsigned int (* sleep_orig)(unsigned int seconds);
int (* nanosleep_orig)(const struct timespec *req, struct timespec *rem);
time_t (*time_orig)(time_t *tloc);
int (* gettimeofday_orig)(struct timeval *restrict tp, void *restrict tzp);
int (* clock_gettime_orig)(clockid_t clock_id, struct timespec *tp);
pid_t (* fork_orig)(void);
void (* _exit_orig)(int status);
void (* exit_group_orig)(int status);
int (* execve_orig)(const char *filename, char *const argv[],
char *const envp[]);
int (* execle_orig)(const char *filename, const char *arg, ...);
/*
* new system calls for time
*/
void cancel() {
int res;
logprintf("%d: cancel()\n", getpid());
msg.mtype = CANCEL;
msg.client = client;
res = msgsnd(queue, &msg, msgsize, 0);
if (res == -1) {
logprintf("\tmsgsnd: %s\n", strerror(errno));
return;
}
/* here we really need the wakeup message before returning, since the
* program may then sleep again; it will wake up immediately if we had
* not consumed the reply to the cancel message */
do {
res = msgrcv(queue, &msg, msgsize, WAKE(client), 0);
if (res == -1)
logprintf("\tmsgrcv: %s\n", strerror(errno));
} while (res == -1 && errno == EINTR);
}
unsigned int sleep(unsigned int seconds) {
int res;
long start, left;
pid_t pid;
pid = getpid();
logprintf("%d: sleep(%u)\n", pid, seconds);
start = time(NULL);
msg.mtype = SLEEP;
msg.client = client;
msg.time = seconds;
res = msgsnd(queue, &msg, msgsize, 0);
if (res == -1) {
logprintf("\tmsgsnd: %s\n", strerror(errno));
logprintf("\tsleep_orig(%d)\n", seconds);
return sleep_orig(seconds);
}
res = msgrcv(queue, &msg, msgsize, WAKE(client), 0);
if (res == -1) {
logprintf("%d:\t\tsleep, msgrcv: %s\n", pid, strerror(errno));
if (errno != EINTR)
return sleep_orig(seconds);
cancel();
left = start + seconds - time(NULL);
logprintf("%d:\t\tsleep, left: %d\n", pid, left);
return left;
}
logprintf("%d: woken(%u): %ld\n", pid, seconds, msg.time);
return 0;
}
int nanosleep(const struct timespec *req, struct timespec *rem) {
int res;
logprintf("%d: nanosleep(%d,...)\n", getpid(), req->tv_sec);
res = sleep(req->tv_sec);
if (res == 0)
return 0;
if (rem != NULL) {
rem->tv_sec = res;
rem->tv_nsec = 121;
}
return -1;
}
time_t time(time_t *tloc) {
int res;
pid_t pid;
pid = getpid();
logprintf("%d: time()\n", pid);
msg.mtype = QUERY;
msg.client = client;
res = msgsnd(queue, &msg, msgsize, 0);
if (res == -1) {
logprintf("%d:\t\ttime, msgrcv: %s\n", pid, strerror(errno));
return time_orig(tloc);
}
res = msgrcv(queue, &msg, msgsize, TIME, 0);
if (res == -1) {
logprintf("%d:\t\ttime, msgrcv: %s\n", pid, strerror(errno));
return time_orig(tloc);
}
logprintf("%d: time(): %ld\n", pid, msg.time);
return msg.time;
}
int gettimeofday(struct timeval *restrict tp, void *restrict tzp) {
time_t t;
t = time(NULL);
if (tp) {
tp->tv_sec = t;
tp->tv_usec = 12;
}
if (tzp) {
/* unspecified behavior, this is */
(void) tzp;
}
return 0;
}
int clock_gettime(clockid_t clock_id, struct timespec *tp) {
time_t t;
(void) clock_id;
t = time(NULL);
tp->tv_sec = t;
tp->tv_nsec = 1234;
return 0;
}
/*
* client registration and unregistration
*/
void registerclient() {
key_t key;
int res;
pid_t pid;
pid = getpid();
logprintf("%d: registerclient()\n", pid);
/* open queue */
key = ftok(KEYFILE, TIMESERVER);
if (key == -1) {
logprintf("%d:\t\t%s: %s\n", pid, KEYFILE, strerror(errno));
queue = -1;
return;
}
queue = msgget(key, 0700);
if (queue == -1) {
logprintf("%d:\t\tmsgget: %s\n", pid, strerror(errno));
return;
}
/* request client number */
msg.mtype = REGISTER;
res = msgsnd(queue, &msg, msgsize, 0);
if (res == -1) {
logprintf("%d:\t\tmsgsnd: %s\n", pid, strerror(errno));
return;
}
/* obtain client id */
res = msgrcv(queue, &msg, msgsize, CLIENTID, 0);
if (res == -1) {
logprintf("%d:\t\tmsgrcv: %s\n", pid, strerror(errno));
return;
}
client = msg.client;
logprintf("%d: client(): %ld\n", getpid(), client);
if (client == -1) {
logprintf("%d:\t\tcannot register\n", pid);
return;
}
/* send pid */
msg.mtype = PID;
msg.client = client;
msg.time = getpid();
msgsnd(queue, &msg, msgsize, 0);
}
void unregisterclient() {
int res;
pid_t pid;
if (queue == -1)
return;
pid = getpid();
logprintf("%d: unregister(%ld)\n", pid, client);
msg.mtype = UNREGISTER;
msg.client = client;
res = msgsnd(queue, &msg, msgsize, 0);
if (res == -1) {
logprintf("%d:\t\tmsgsnd: %s\n", pid, strerror(errno));
return;
}
}
/*
* process-handling system calls
*/
pid_t fork(void) {
pid_t ret;
logprintf("%d: fork()\n", getpid());
ret = fork_orig();
if (ret == 0) {
logprintf("%d: child\n", getpid());
registerclient();
}
return ret;
}
void _exit(int status) {
logprintf("%d: _exit(%d)\n", getpid(), status);
unregisterclient();
_exit_orig(status);
_exit(status); /* avoid warning */
}
void exit_group(int status) {
logprintf("%d: exit_group(%d)\n", getpid(), status);
unregisterclient();
exit_group_orig(status);
exit_group(status); /* avoid warning */
}
int str2cmp(char *a, char *b) {
if (a == NULL)
return -1;
if (strlen(a) < strlen(b))
return -1;
return memcmp(a, b, strlen(b));
}
int execve(const char *filename, char *const argv[],
char *const envp[]) {
int i;
char **newenvp;
char ldpreload[1020], logfilename[1020];
int oldld, oldlog;
int res;
logprintf("%d: execve(%s,...)\n", getpid(), filename);
for (i = 0; i == 0 || argv[i - 1]; i++)
logprintf("\targv[%d]: %s\n", i, argv[i]);
logprintf("\t------------\n");
oldld = 0;
oldlog = 0;
for (i = 0; i == 0 || envp[i - 1]; i++) {
logprintf("\tenvp[%d]: %s\n", i, envp[i]);
if (! str2cmp(envp[i], "LD_PRELOAD="))
oldld = 1;
if (! str2cmp(envp[i], "TIMECLIENTLOGFILE="))
oldlog = 1;
}
logprintf("\t------------\n");
/* add LD_PRELOAD again, since the application may call
* execve() with an arbitrary environment */
newenvp = malloc((i + 2) * sizeof(char *));
memcpy (newenvp, envp, i * sizeof(char *));
snprintf(ldpreload, 1020, "LD_PRELOAD=%s", timeclient);
snprintf(logfilename, 1020, "TIMECLIENTLOGFILE=%s", logfile);
i--;
if (! oldld)
newenvp[i++] = ldpreload;
if (! oldlog)
newenvp[i++] = logfilename;
newenvp[i++] = NULL;
for (i = 0; i == 0 || newenvp[i - 1]; i++)
logprintf("\tnewenvp[%d]: %s\n", i, newenvp[i]);
/* cannot keep client_id across an execve();
* just unregister for now; if execve() fails, register again */
unregisterclient();
res = execve_orig(filename, argv, newenvp);
registerclient();
return res;
}
/* for some reason, redefining execve only is not enough */
int execle(const char *path, const char *arg, ...) {
va_list ap;
char *s;
char **envp;
char **res, **argv;
int argn;
int err;
logprintf("%d: execle(%s,...)\n", getpid(), path);
argn = 1;
argv = malloc((argn + 1) * sizeof(char *));
argv[argn - 1] = (char *) arg;
va_start(ap, arg);
for (argn++; NULL != (s = va_arg(ap, char *)); argn++) {
res = realloc(argv, (argn + 1) * sizeof(char *));
if (res == NULL) {
va_end(ap);
free(argv);
errno = ENOMEM;
return -1;
}
argv = res;
argv[argn - 1] = s;
}
argv[argn - 1] = NULL;
envp = va_arg(ap, char **);
va_end(ap);
execve(path, argv, envp);
err = errno;
free(argv);
errno = err;
return -1;
}
/*
* constructor and destructor
*/
static void __attribute__((constructor)) init() {
char *ldpreload, *envlogfile, cwd[1000];
ldpreload = getenv("LD_PRELOAD");
if (ldpreload[0] != '.')
timeclient = strdup(ldpreload);
else {
getcwd(cwd, 1000);
timeclient = malloc(strlen(cwd) + strlen(ldpreload) + 2);
sprintf(timeclient, "%s/%s", cwd, ldpreload);
}
envlogfile = getenv("TIMECLIENTLOGFILE");
if (envlogfile == NULL)
snprintf(logfile, 1000, "/tmp/timeclient.%d", getpid());
else
snprintf(logfile, 1000, "%s", envlogfile);
sleep_orig = dlsym(RTLD_NEXT, "sleep");
nanosleep_orig = dlsym(RTLD_NEXT, "nanosleep");
time_orig = dlsym(RTLD_NEXT, "time");
gettimeofday_orig = dlsym(RTLD_NEXT, "gettimeofday");
clock_gettime_orig = dlsym(RTLD_NEXT, "clock_gettime_orig");
fork_orig = dlsym(RTLD_NEXT, "fork");
_exit_orig = dlsym(RTLD_NEXT, "_exit");
exit_group_orig = dlsym(RTLD_NEXT, "exit_group");
execve_orig = dlsym(RTLD_NEXT, "execve");
execle_orig = dlsym(RTLD_NEXT, "execle");
registerclient();
}
static void __attribute__((destructor)) fini() {
unregisterclient();
}