-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibucw.c
618 lines (491 loc) · 13.5 KB
/
libucw.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
* Copyright (c) 2015, Mitja Horvat
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef __linux__
/*
* We need the RTLD_NEXT macro, therefore we must define _GNU_SOURCE to get it
*/
#define _GNU_SOURCE
#endif
#include <sys/stat.h>
#include <sys/syscall.h>
#include <fcntl.h>
#include <limits.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
/**
* Default ccache binary path; in case it's not found in PATH or is not set via UCW_CCACHE_BIN
*/
#define CCACHE_BIN "/usr/bin/ccache"
/*
* Default libc path
*/
#define LIBC_PATH "/lib/libc.so"
#define MAX_ARG 4096
extern char **environ;
typedef int execve_func_t(const char *path, char *const argv[], char *const envv[]);
static execve_func_t *libc_execve = NULL;
#if 0
void ucw_log(char *fmt, ...)
{
FILE *log;
va_list va;
va_start(va, fmt);
log = fopen("/tmp/ucw.log", "a");
fprintf(log, "%d -- ", getpid());
vfprintf(log, fmt, va);
fclose(log);
}
#else
#define ucw_log(...)
#endif
/**
* Initialize UCW wrapper
*/
void ucw_init(void)
{
if (libc_execve != NULL) return;
#ifndef __linux__
void *h;
h = dlopen(LIBC_PATH, RTLD_LAZY | RTLD_NOLOAD);
if (h == NULL)
{
ucw_log("Error!\n");
perror("");
assert(!"Unable to dlopen() libc.so: " LIBC_PATH);
}
#endif
libc_execve = dlsym(RTLD_NEXT, "execve");
if (libc_execve == NULL) assert(!"unable to resolve execve()");
#ifndef __linux__
dlclose(h);
#endif
}
/**
* Compare end of strings
*/
int strendcmp(const char *str, const char *end)
{
size_t slen;
size_t elen;
slen = strlen(str);
elen = strlen(end);
if (slen < elen) return -1;
return strcmp(str + slen - elen, end);
}
/**
* Try to find @p filename in the paths specified by the environment variable PATH and return
* a full path if found.
*/
bool resolve_path(char *out, size_t outsz, const char *filename)
{
const char *env_path;
const char *ppath;
size_t plen;
size_t flen;
char opath[PATH_MAX];
const char *psrc = NULL;
/* Do not do any PATH resolving if filename contains a '/' */
if (strchr(filename, '/') != NULL)
{
psrc = filename;
goto exit;
}
/* No PATH variable set, use some defaults */
env_path = getenv("PATH");
if (env_path == NULL)
{
env_path = ".:/bin:/usr/bin";
}
/* Split PATH by the ':' separator and append filename.
* Check if a file by that name exists and is executable */
ppath = env_path;
flen = strlen(filename);
plen = 0;
/* Traverse the PATH fields */
while (*ppath != '\0')
{
/* Move string ahead */
ppath += plen;
/* This is the last entry in the string? */
if (*ppath == '\0')
{
break;
}
/* Skip all empty ':' */
ppath += strspn(ppath, ":");
/* Caluclate path length up to the ':' delimiter or end of the string */
plen = strcspn(ppath, ":");
/* If the total resolved path exceeds opath, skip it */
if ((plen + flen + 1) >= sizeof(opath))
{
continue;
}
/* Copy the partial string from ppath to opath, and append a '\0' */
strncpy(opath, ppath, plen);
opath[plen] = '\0';
/* Append a '/' character at the end of the string */
if (opath[plen - 1] != '/') strcat(opath, "/");
/* Append the filename */
strcat(opath, filename);
/* Check if this path is accessible and is executable */
if (access(opath, X_OK) == 0)
{
psrc = opath;
goto exit;
}
}
exit:
if (psrc == NULL)
{
ucw_log("Error resolving: %s\n", filename);
return false;
}
if (outsz < (strlen(psrc) + 1))
{
/* Not enough space on target */
ucw_log("Not enough space resolving: %s\n", filename);
return false;
}
strncpy(out, psrc, outsz);
ucw_log("Name resolved: %s -> %s\n", filename, out);
return true;
}
char *ccache_path(void)
{
char *cpath_env;
static char cpath[PATH_MAX] = "";
if (cpath[0] != '\0') return cpath;
/*
* First, check the environment variable UCW_CCACHE_BIN
*/
cpath_env = getenv("UCW_CCACHE_BIN");
if (cpath_env != NULL)
{
if (strlen(cpath_env) >= sizeof(cpath))
{
printf("UCW_CCACHE_BIN too big, ignoring.\n");
}
else
{
strncpy(cpath, getenv("UCW_CCACHE_BIN"), sizeof(cpath));
return cpath;
}
}
/*
* Next, check PATH
*/
if (resolve_path(cpath, sizeof(cpath), "ccache"))
{
return cpath;
}
strcpy(cpath, CCACHE_BIN);
return cpath;
}
/**
* Check if path is a real-GCC compiler (not a ccache symlink or anything similar)
*/
bool gcc_check(const char *path, char *const argv[], char *const envv[])
{
char **argp;
struct stat st;
char she_bang[2];
bool gcc_found = false;
char *gcc_path = NULL;
int gcc_fd = -1;
/* Check if the gcc_path ends in "gcc" */
if (strendcmp(path, "gcc") != 0 &&
strendcmp(path, "g++") != 0 &&
strendcmp(path, "c++") != 0)
{
goto exit;
}
if (lstat(path, &st) == 0)
{
/* She-bang detection, if we're executing a script, do not run it through ccache */
gcc_fd = open(path, O_RDONLY);
if (gcc_fd < 0)
{
goto exit;
}
if (read(gcc_fd, she_bang, 2) >= 2)
{
if (she_bang[0] == '#' && she_bang[1] == '!')
{
goto exit;
}
}
/* It is a symbolic link, read the target path */
if (S_ISLNK(st.st_mode))
{
gcc_path = realpath(path, NULL);
}
}
if (gcc_path == NULL) gcc_path = strdup(path);
/* Check if the path is executable */
if (access(gcc_path, X_OK) != 0)
{
goto exit;
}
/*
* CCACHE doesn't know how to handle the --save-temps option which is
* used by some configure scripts to inspect temporary files produced during compilation
*/
for (argp = (char **)argv; *argp != NULL; argp++)
{
if (strendcmp(*argp, "save-temps") == 0) goto exit;
}
gcc_found = true;
exit:
if (gcc_fd >= 0) close(gcc_fd);
if (gcc_path != NULL) free(gcc_path);
return gcc_found;
}
/**
* Copy a NULL-terminated variable argument list to an array of char *
*/
bool va_to_argv(va_list va, char **argv, size_t argv_len)
{
char *carg;
char **argp = argv;
for (argp = argv; (argp - argv) < argv_len; argp++)
{
carg = va_arg(va, char *);
*argp = carg;
/* End of arguments, return */
if (carg == NULL) return true;
}
return false;
}
/**
* Run command in @p path through CCACHE
*/
int ccache_exec(const char *path, char *const argv[], char *const envv[])
{
int retval;
char *new_envv[MAX_ARG];
char *new_argv[MAX_ARG];
/*
* Mangle the environment, copy envv to new_envv:
*
* - Remove LD_PRELOAD
* - remove CCACHE_DIR
*/
char **sp = (char **)envv;
char **dp = (char **)new_envv;
while (*sp != NULL)
{
if ((dp - new_envv) >= MAX_ARG)
{
/* Error */
errno = E2BIG;
return -1;
}
/* Remove the LD_PRELOAD env variable */
if (strncmp(*sp, "LD_PRELOAD", strlen("LD_PRELOAD")) == 0)
{
}
/* Remove CCACHE_DIR */
else if (strncmp(*sp, "CCACHE_DIR", strlen("CCACHE_DIR")) == 0)
{
}
/* Copy arguments */
else
{
*dp++ = *sp;
}
sp++;
}
*dp = NULL;
/**
* Mangle the argument list:
* - As first argument insert ccache_path()
* - As second argument insert the full path
* - Ignore the first argument (the filename) from the first list
*/
sp = (char **)argv;
dp = (char **)new_argv;
/* ARGv[0] must be ccache, ARGV[1] must the full path */
*dp++ = ccache_path();
*dp++ = (char *)path;
/* Some error checking */
if (*sp == NULL)
{
return EFAULT;
}
sp++;
while (*sp != NULL)
{
if ((dp - new_argv) >= MAX_ARG)
{
errno = E2BIG;
return -1;
}
*dp++ = *sp++;
}
*dp = NULL;
#if 0
ucw_log("-----------------------------------\n");
ucw_log("CCACHE: path = %s\n", path);
dp = new_argv;
while (*dp != NULL)
{
ucw_log("ARG: %s\n", *dp++);
}
dp = new_envv;
while (*dp != NULL)
{
ucw_log("ENV: %s\n", *dp++);
}
#endif
ucw_log("CCACHE_EXEC: %s\n", new_argv[0]);
retval = libc_execve(ccache_path(), new_argv, new_envv);
if (retval != 0)
{
ucw_log("execve failed: %s %s\n", ccache_path(), strerror(errno));
ucw_log("PATH: %s -- %s %s %s\n", path, new_argv[0], new_argv[1], new_argv[2]);
dp = new_argv;
while (*dp != NULL)
{
ucw_log("ARG: %s\n", *dp++);
}
dp = new_envv;
while (*dp != NULL)
{
ucw_log("ENV: %s\n", *dp++);
}
}
return retval;
}
int execve(const char *path, char *const argv[], char *const envv[])
{
ucw_log("EXECVE: %s\n", path);
ucw_init();
if (getenv("UCC_RECURSE") != NULL)
{
ucw_log("UCC recursion detected.\n");
return libc_execve(path, argv, envv);
}
/* If we're not executing a GCC binary, just bypass it */
if (gcc_check(path, argv, envv))
{
return ccache_exec(path, argv, envv);
}
return libc_execve(path, argv, envv);
}
int execv(const char *path, char *const argv[])
{
int retval;
ucw_log("EXECV: %s\n", path);
retval = execve(path, argv, environ);
if (retval != 0)
{
ucw_log("EXECVE failed: %s\n", path);
}
return retval;
}
int execvpe(const char *path, char *const argv[], char *const envv[])
{
int retval;
ucw_log("EXECVPE: %s\n", path);
char rpath[PATH_MAX];
if (!resolve_path(rpath, sizeof(rpath), path))
{
return ENOENT;
}
retval = execve(rpath, argv, envv);
if (retval != 0 && errno == ENOEXEC)
{
/*
* Execution failed because the exec format is unknown. The manual page
* says we have to run it through /bin/sh at this point
*/
char *new_argv[MAX_ARG];
char **dp = (char **)new_argv;
char **sp = (char **)argv;
if (*sp == NULL)
{
return EFAULT;
}
*dp++ = *sp++;
*dp++ = (char *)rpath;
while (*sp != NULL)
{
if ((dp - new_argv) >= MAX_ARG) break;
*dp++ = *sp++;
}
*dp++ = NULL;
retval = execve("/bin/sh", new_argv, envv);
}
return retval;
}
int execvp(const char *path, char *const argv[])
{
ucw_log("EXECVP: %s\n", path);
return execvpe(path, argv, environ);
}
int execle(const char *path, const char *arg, ...)
{
va_list va;
char *new_argv[MAX_ARG + 1];
char **new_envv;
ucw_log("EXECLE: %s %s\n", path, arg);
va_start(va, arg);
new_argv[0] = (char *)arg;
if (!va_to_argv(va, new_argv + 1, MAX_ARG)) return -1;
/* va_to_argv() exhausted all argument va_list entries; the next one should be the
environment */
new_envv = va_arg(va, char **);
return execve(path, new_argv, new_envv);
}
int execl(const char *path, const char *arg, ...)
{
va_list va;
char *new_argv[MAX_ARG + 1];
ucw_log("EXECL: %s %s\n", path, arg);
va_start(va, arg);
new_argv[0] = (char *)arg;
if (!va_to_argv(va, new_argv + 1, MAX_ARG)) return -1;
va_end(va);
return execve(path, new_argv, environ);
}
int execlp(const char *path, const char *arg, ...)
{
va_list va;
char *new_argv[MAX_ARG + 1];
ucw_log("EXECLP: %s %s\n", path, arg);
va_start(va, arg);
new_argv[0] = (char *)arg;
if (!va_to_argv(va, new_argv + 1, MAX_ARG)) return -1;
va_end(va);
return execvpe(path, new_argv, environ);
}