From dcaf992bf1d5cfeda9a5842e69d9340b67846c29 Mon Sep 17 00:00:00 2001 From: Quentin Smith Date: Wed, 26 Jun 2019 01:59:34 -0400 Subject: [PATCH] Write PID file after forking and before exiting. --- zhm/zhm.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/zhm/zhm.c b/zhm/zhm.c index ec4696b7..409e737b 100644 --- a/zhm/zhm.c +++ b/zhm/zhm.c @@ -414,12 +414,13 @@ init_hm(void) #ifndef DEBUG if (!inetd && !nofork) detach(); - - /* Write pid to file */ - fp = fopen(PidFile, "w"); - if (fp != NULL) { + else { + /* Write pid to file */ + fp = fopen(PidFile, "w"); + if (fp != NULL) { fprintf(fp, "%d\n", getpid()); fclose(fp); + } } #endif /* DEBUG */ @@ -469,11 +470,18 @@ detach(void) /* detach from terminal and fork. */ register int i, x = ZGetFD(); register long size; + FILE *fp; i = fork(); if (i) { if (i < 0) - perror("fork"); + perror("fork"); + /* Write pid to file */ + fp = fopen(PidFile, "w"); + if (fp != NULL) { + fprintf(fp, "%d\n", i); + fclose(fp); + } exit(0); } #ifdef _POSIX_VERSION