diff options
author | Paul Smith <psmith@gnu.org> | 2022-10-15 16:34:54 -0400 |
---|---|---|
committer | Paul Smith <psmith@gnu.org> | 2022-10-15 18:39:32 -0400 |
commit | 18c4b508ef24596470b64ddf058c0115ac75fbc2 (patch) | |
tree | 70410cfe71c32ac0235089e24fe02fa970bcef49 /src/main.c | |
parent | 383eb3a923b95ee9959446a03e34d870f9d1f427 (diff) | |
download | make-git-18c4b508ef24596470b64ddf058c0115ac75fbc2.tar.gz |
[SV 63157] Ensure temporary files are removed when signaled
Original patch from Dmitry Goncharov <dgoncharov@users.sf.net>.
When handling a fatal signal ensure the temporary files for
stdin and the jobserver fifo (if in use) are deleted.
* src/makeint.h (temp_stdin_unlink): Declare a new method.
* src/main.c (temp_stdin_unlink): Delete the stdin temporary file
if it exists. If the unlink fails and we're not handling a signal
then show an error.
(main): Call temp_stdin_unlink() instead of unlinking by hand.
* src/commands.c (fatal_error_signal): Invoke cleanup methods if
we're handling a fatal signal.
* tests/scripts/features/output-sync: Test signal handling during
output sync and jobserver with FIFO.
* tests/scripts/features/temp_stdin: Test signal handling when
makefiles are read from stdin.
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 39 |
1 files changed, 20 insertions, 19 deletions
@@ -1137,6 +1137,23 @@ reset_jobserver (void) jobserver_auth = NULL; } +void +temp_stdin_unlink () +{ + /* This function is called from a signal handler. Keep async-signal-safe. + If there is a temp file from reading from stdin, get rid of it. */ + if (stdin_offset >= 0) + { + const char *nm = makefiles->list[stdin_offset]; + int r = 0; + + stdin_offset = -1; + EINTRLOOP(r, unlink (nm)); + if (r < 0 && errno != ENOENT && !handling_fatal_signal) + perror_with_name (_("unlink (temporary file): "), nm); + } +} + #ifdef _AMIGA int main (int argc, char **argv) @@ -2776,9 +2793,7 @@ main (int argc, char **argv, char **envp) #endif jobserver_post_child(1); - /* Get rid of any stdin temp file. */ - if (stdin_offset >= 0) - unlink (makefiles->list[stdin_offset]); + temp_stdin_unlink (); _exit (127); } @@ -2804,15 +2819,7 @@ main (int argc, char **argv, char **envp) } } - /* If there is a temp file from reading a makefile from stdin, get rid of - it now. */ - if (stdin_offset >= 0) - { - const char *nm = makefiles->list[stdin_offset]; - if (unlink (nm) < 0 && errno != ENOENT) - perror_with_name (_("unlink (temporary file): "), nm); - stdin_offset = -1; - } + temp_stdin_unlink (); /* If there were no command-line goals, use the default. */ if (goals == 0) @@ -3731,13 +3738,7 @@ die (int status) print_version (); /* Get rid of a temp file from reading a makefile from stdin. */ - if (stdin_offset >= 0) - { - const char *nm = makefiles->list[stdin_offset]; - if (unlink (nm) < 0 && errno != ENOENT) - perror_with_name (_("unlink (temporary file): "), nm); - stdin_offset = -1; - } + temp_stdin_unlink (); /* Wait for children to die. */ err = (status != 0); |