From: Guy Harris Date: Sun, 13 Dec 2015 22:29:40 +0000 (-0800) Subject: Check for fork/vfork failing. X-Git-Tag: tcpdump-4.8.0~63 X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/18200d6eead9cd1a9dc7a5fb2ec5bcec0f75a8e2 Check for fork/vfork failing. Clean up some other stuff while we're at it. --- diff --git a/tcpdump.c b/tcpdump.c index 4f42f4b2..de8f6330 100644 --- a/tcpdump.c +++ b/tcpdump.c @@ -1884,18 +1884,31 @@ info(register int verbose) } #if defined(HAVE_FORK) || defined(HAVE_VFORK) +#ifdef HAVE_FORK +#define fork_subprocess() fork() +#else +#define fork_subprocess() vfork() +#endif static void compress_savefile(const char *filename) { -# ifdef HAVE_FORK - #define test_fork fork() -# else - #define test_fork vfork() -# endif - if(test_fork) + pid_t child; + + child = fork_subprocess(); + if (child == -1) { + fprintf(stderr, + "compress_savefile: fork failed: %s\n", + pcap_strerror(errno)); return; + } + if (child != 0) { + /* Parent process. */ + return; + } + /* - * Set to lowest priority so that this doesn't disturb the capture + * Child process. + * Set to lowest priority so that this doesn't disturb the capture. */ #ifdef NZERO setpriority(PRIO_PROCESS, 0, NZERO - 1); @@ -1904,15 +1917,15 @@ compress_savefile(const char *filename) #endif if (execlp(zflag, zflag, filename, (char *)NULL) == -1) fprintf(stderr, - "compress_savefile:execlp(%s, %s): %s\n", + "compress_savefile: execlp(%s, %s) failed: %s\n", zflag, filename, pcap_strerror(errno)); -# ifdef HAVE_FORK +#ifdef HAVE_FORK exit(1); -# else +#else _exit(1); -# endif +#endif } #else /* HAVE_FORK && HAVE_VFORK */ static void