Skip to content

Commit

Permalink
Don't crash when stderr is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
jrosdahl committed Mar 7, 2012
1 parent 4520fd7 commit 19ba517
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions ccache.c
Expand Up @@ -1851,6 +1851,29 @@ parse_sloppiness(char *p)
return result;
}

/* Make a copy of stderr that will not be cached, so things like
distcc can send networking errors to it. */
static void
setup_uncached_err(void)
{
char *buf;
int uncached_fd;

uncached_fd = dup(2);
if (uncached_fd == -1) {
cc_log("dup(2) failed: %s", strerror(errno));
failed();
}

/* leak a pointer to the environment */
buf = format("UNCACHED_ERR_FD=%d", uncached_fd);

if (putenv(buf) == -1) {
cc_log("putenv failed: %s", strerror(errno));
failed();
}
}

/* the main ccache driver function */
static void
ccache(int argc, char *argv[])
Expand All @@ -1870,6 +1893,7 @@ ccache(int argc, char *argv[])
struct args *compiler_args;

find_compiler(argc, argv);
setup_uncached_err();

if (getenv("CCACHE_DISABLE")) {
cc_log("ccache is disabled");
Expand Down Expand Up @@ -2124,30 +2148,6 @@ ccache_main_options(int argc, char *argv[])
return 0;
}


/* Make a copy of stderr that will not be cached, so things like
distcc can send networking errors to it. */
static void
setup_uncached_err(void)
{
char *buf;
int uncached_fd;

uncached_fd = dup(2);
if (uncached_fd == -1) {
cc_log("dup(2) failed: %s", strerror(errno));
failed();
}

/* leak a pointer to the environment */
buf = format("UNCACHED_ERR_FD=%d", uncached_fd);

if (putenv(buf) == -1) {
cc_log("putenv failed: %s", strerror(errno));
failed();
}
}

int
ccache_main(int argc, char *argv[])
{
Expand Down Expand Up @@ -2213,8 +2213,6 @@ ccache_main(int argc, char *argv[])

compile_preprocessed_source_code = !getenv("CCACHE_CPP2");

setup_uncached_err();

/* make sure the cache dir exists */
if (create_dir(cache_dir) != 0) {
fprintf(stderr,
Expand Down

0 comments on commit 19ba517

Please sign in to comment.