Mercurial > hg > toybox
changeset 643:7bdebd2af1d6
Add signal handler to clean up tempfile.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 30 Jul 2012 01:01:33 -0500 |
parents | 812afb883392 |
children | 6a096902309d |
files | lib/lib.c lib/lib.h |
diffstat | 2 files changed, 30 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/lib/lib.c Tue Jul 24 02:52:36 2012 -0500 +++ b/lib/lib.c Mon Jul 30 01:01:33 2012 -0500 @@ -708,7 +708,7 @@ continue; } function(fd, *argv); - if (!flags) close(fd); + if (flags == O_RDONLY) close(fd); } while (*++argv); } @@ -772,6 +772,13 @@ return rc; } +static char *tempfile2zap; +static void tempfile_handler(int i) +{ + if (1 < (long)tempfile2zap) unlink(tempfile2zap); + _exit(1); +} + // Open a temporary file to copy an existing file into. int copy_tempfile(int fdin, char *name, char **tempname) { @@ -781,6 +788,8 @@ *tempname = xstrndup(name, strlen(name)+6); strcat(*tempname,"XXXXXX"); if(-1 == (fd = mkstemp(*tempname))) error_exit("no temp file"); + if (!tempfile2zap) sigatexit(tempfile_handler); + tempfile2zap = *tempname; // Set permissions of output file @@ -796,6 +805,7 @@ close(fdin); close(fdout); unlink(*tempname); + tempfile2zap = (char *)1; free(*tempname); *tempname = NULL; } @@ -812,6 +822,7 @@ } xclose(fdout); rename(*tempname, temp); + tempfile2zap = (char *)1; free(*tempname); free(temp); *tempname = NULL; @@ -927,17 +938,28 @@ #define SIGNIFY(x) {SIG##x, #x} static struct signame signames[] = { - SIGNIFY(ABRT), SIGNIFY(ALRM), SIGNIFY(BUS), SIGNIFY(CHLD), SIGNIFY(CONT), + SIGNIFY(ABRT), SIGNIFY(ALRM), SIGNIFY(BUS), SIGNIFY(FPE), SIGNIFY(HUP), SIGNIFY(ILL), SIGNIFY(INT), SIGNIFY(KILL), - SIGNIFY(PIPE), SIGNIFY(QUIT), SIGNIFY(SEGV), SIGNIFY(STOP), SIGNIFY(TERM), - SIGNIFY(TSTP), SIGNIFY(TTIN), SIGNIFY(TTOU), SIGNIFY(USR1), SIGNIFY(USR2), - SIGNIFY(SYS), SIGNIFY(TRAP), SIGNIFY(URG), SIGNIFY(VTALRM), SIGNIFY(XCPU), - SIGNIFY(XFSZ) + SIGNIFY(PIPE), SIGNIFY(QUIT), SIGNIFY(SEGV), SIGNIFY(TERM), + SIGNIFY(USR1), SIGNIFY(USR2), SIGNIFY(SYS), SIGNIFY(TRAP), + SIGNIFY(VTALRM), SIGNIFY(XCPU), SIGNIFY(XFSZ), + + // Start of non-terminal signals + + SIGNIFY(CHLD), SIGNIFY(CONT), SIGNIFY(STOP), SIGNIFY(TSTP), + SIGNIFY(TTIN), SIGNIFY(TTOU), SIGNIFY(URG) }; // not in posix: SIGNIFY(STKFLT), SIGNIFY(WINCH), SIGNIFY(IO), SIGNIFY(PWR) // obsolete: SIGNIFY(PROF) SIGNIFY(POLL) +// Install the same handler on every signal that defaults to killing the process +void sigatexit(void *handler) +{ + int i; + for (i=0; signames[i].num != SIGCHLD; i++) + signal(signames[i].num, handler); +} // Convert name to signal number. If name == NULL print names. int sig_to_num(char *pidstr) { @@ -967,7 +989,7 @@ return NULL; } - +// premute mode bits based on posix mode strings. mode_t string_to_mode(char *modestr, mode_t mode) { char *whos = "ogua", *hows = "=+-", *whats = "xwrstX", *whys = "ogu";