~ubuntu-branches/debian/sid/ccache/sid

« back to all changes in this revision

Viewing changes to ccache.c

  • Committer: Package Import Robot
  • Author(s): Joel Rosdahl
  • Date: 2015-10-08 21:48:51 UTC
  • mfrom: (1.2.13)
  • Revision ID: package-import@ubuntu.com-20151008214851-rucjv972rs2jnay2
Tags: 3.2.4-1
* New upstream release 3.2.4
* Use debhelper compat level 9

Show diffs side-by-side

added added

removed removed

Lines of Context:
239
239
/* Temporary files to remove at program exit. */
240
240
static struct pending_tmp_file *pending_tmp_files = NULL;
241
241
 
 
242
static sigset_t fatal_signal_set;
 
243
 
 
244
/* PID of currently executing compiler that we have started, if any. 0 means no
 
245
 * ongoing compilation. */
 
246
static pid_t compiler_pid = 0;
 
247
 
242
248
/*
243
249
 * This is a string that identifies the current "version" of the hash sum
244
250
 * computed by ccache. If, for any reason, we want to force the hash sum to be
314
320
        return path;
315
321
}
316
322
 
 
323
void
 
324
block_signals(void)
 
325
{
 
326
        sigprocmask(SIG_BLOCK, &fatal_signal_set, NULL);
 
327
}
 
328
 
 
329
void
 
330
unblock_signals(void)
 
331
{
 
332
        sigset_t empty;
 
333
        sigemptyset(&empty);
 
334
        sigprocmask(SIG_SETMASK, &empty, NULL);
 
335
}
 
336
 
317
337
static void
318
338
add_pending_tmp_file(const char *path)
319
339
{
320
 
        struct pending_tmp_file *e = x_malloc(sizeof(*e));
 
340
        struct pending_tmp_file *e;
 
341
 
 
342
        block_signals();
 
343
        e = x_malloc(sizeof(*e));
321
344
        e->path = x_strdup(path);
322
345
        e->next = pending_tmp_files;
323
346
        pending_tmp_files = e;
 
347
        unblock_signals();
324
348
}
325
349
 
326
350
static void
327
 
clean_up_pending_tmp_files(void)
 
351
do_clean_up_pending_tmp_files(void)
328
352
{
329
353
        struct pending_tmp_file *p = pending_tmp_files;
330
354
        while (p) {
337
361
}
338
362
 
339
363
static void
340
 
signal_handler(int signo)
341
 
{
342
 
        (void)signo;
343
 
        clean_up_pending_tmp_files();
344
 
        _exit(1);
 
364
clean_up_pending_tmp_files(void)
 
365
{
 
366
        block_signals();
 
367
        do_clean_up_pending_tmp_files();
 
368
        unblock_signals();
 
369
}
 
370
 
 
371
static void
 
372
signal_handler(int signum)
 
373
{
 
374
        /* Unregister handler for this signal so that we can send the signal to
 
375
         * ourselves at the end of the handler. */
 
376
        signal(signum, SIG_DFL);
 
377
 
 
378
        /* If ccache was killed explicitly, then bring the compiler subprocess (if
 
379
         * any) with us as well. */
 
380
        if (signum == SIGTERM
 
381
            && compiler_pid != 0
 
382
            && waitpid(compiler_pid, NULL, WNOHANG) == 0) {
 
383
                kill(compiler_pid, signum);
 
384
        }
 
385
 
 
386
        do_clean_up_pending_tmp_files();
 
387
 
 
388
        if (compiler_pid != 0) {
 
389
                /* Wait for compiler subprocess to exit before we snuff it. */
 
390
                waitpid(compiler_pid, NULL, 0);
 
391
        }
 
392
 
 
393
        /* Resend signal to ourselves to exit properly after returning from the
 
394
         * handler. */
 
395
        kill(getpid(), signum);
 
396
}
 
397
 
 
398
static void
 
399
register_signal_handler(int signum)
 
400
{
 
401
        struct sigaction act;
 
402
        memset(&act, 0, sizeof(act));
 
403
        act.sa_handler = signal_handler;
 
404
        act.sa_mask = fatal_signal_set;
 
405
        act.sa_flags = SA_RESTART;
 
406
        sigaction(signum, &act, NULL);
 
407
}
 
408
 
 
409
static void
 
410
set_up_signal_handlers(void)
 
411
{
 
412
        sigemptyset(&fatal_signal_set);
 
413
        sigaddset(&fatal_signal_set, SIGINT);
 
414
        sigaddset(&fatal_signal_set, SIGTERM);
 
415
#ifdef SIGHUP
 
416
        sigaddset(&fatal_signal_set, SIGHUP);
 
417
#endif
 
418
#ifdef SIGQUIT
 
419
        sigaddset(&fatal_signal_set, SIGQUIT);
 
420
#endif
 
421
 
 
422
        register_signal_handler(SIGINT);
 
423
        register_signal_handler(SIGTERM);
 
424
#ifdef SIGHUP
 
425
        register_signal_handler(SIGHUP);
 
426
#endif
 
427
#ifdef SIGQUIT
 
428
        register_signal_handler(SIGQUIT);
 
429
#endif
345
430
}
346
431
 
347
432
static void
719
804
                x_unlink(dest);
720
805
                ret = link(source, dest);
721
806
        } else {
722
 
                ret = copy_file(source, dest, conf->compression);
 
807
                ret = copy_file(
 
808
                  source, dest, conf->compression ? conf->compression_level : 0);
723
809
        }
724
810
        if (ret != 0) {
725
811
                cc_log("Failed to %s %s to %s: %s",
877
963
        }
878
964
 
879
965
        cc_log("Running real compiler");
880
 
        status = execute(args->argv, tmp_stdout_fd, tmp_stderr_fd);
 
966
        status = execute(args->argv, tmp_stdout_fd, tmp_stderr_fd, &compiler_pid);
881
967
        args_pop(args, 3);
882
968
 
883
969
        if (x_stat(tmp_stdout, &st) != 0) {
1140
1226
                args_add(args, "-E");
1141
1227
                args_add(args, input_file);
1142
1228
                cc_log("Running preprocessor");
1143
 
                status = execute(args->argv, path_stdout_fd, path_stderr_fd);
 
1229
                status = execute(args->argv, path_stdout_fd, path_stderr_fd, &compiler_pid);
1144
1230
                args_pop(args, 2);
1145
1231
        }
1146
1232
 
1263
1349
compiler_is_clang(struct args *args)
1264
1350
{
1265
1351
        char *name = basename(args->argv[0]);
1266
 
        bool is = strstr(name, "clang");
 
1352
        bool is = strstr(name, "clang") != NULL;
1267
1353
        free(name);
1268
1354
        return is;
1269
1355
}
2858
2944
        /* Arguments to send to the real compiler. */
2859
2945
        struct args *compiler_args;
2860
2946
 
 
2947
        set_up_signal_handlers();
 
2948
 
2861
2949
        orig_args = args_init(argc, argv);
2862
2950
 
2863
2951
        initialize();
2864
2952
        find_compiler(argv);
2865
2953
 
2866
 
#ifndef _WIN32
2867
 
        signal(SIGHUP, signal_handler);
2868
 
#endif
2869
 
        signal(SIGINT, signal_handler);
2870
 
        signal(SIGTERM, signal_handler);
2871
 
 
2872
2954
        if (str_eq(conf->temporary_dir, "")) {
2873
2955
                clean_up_internal_tempdir();
2874
2956
        }