summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorPaul Smith <psmith@gnu.org>2022-10-15 16:34:54 -0400
committerPaul Smith <psmith@gnu.org>2022-10-15 18:39:32 -0400
commit18c4b508ef24596470b64ddf058c0115ac75fbc2 (patch)
tree70410cfe71c32ac0235089e24fe02fa970bcef49 /src/main.c
parent383eb3a923b95ee9959446a03e34d870f9d1f427 (diff)
downloadmake-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.c39
1 files changed, 20 insertions, 19 deletions
diff --git a/src/main.c b/src/main.c
index afca0656..05b11c63 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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);