From a08d6dcc7b1096c04e93bcd4a08e4787f80f0889 Mon Sep 17 00:00:00 2001
From: Kirk Webb <kwebb@flux.utah.edu>
Date: Wed, 19 Jun 2002 20:48:07 +0000
Subject: [PATCH] Fixed slothd off-by-one pid issue.  Slothd now writes its pid
 _after_ forking into the background.

---
 sensors/slothd/slothd.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/sensors/slothd/slothd.c b/sensors/slothd/slothd.c
index dd79d3297b..2d956317db 100644
--- a/sensors/slothd/slothd.c
+++ b/sensors/slothd/slothd.c
@@ -62,8 +62,6 @@ void usage(void) {
 int main(int argc, char **argv) {
 
   int exitcode = -1;
-  int pfd;
-  char pidbuf[10];
   static SLOTHD_OPTS mopts;
   static SLOTHD_PACKET mpkt;
 
@@ -75,16 +73,6 @@ int main(int argc, char **argv) {
   opts = &mopts;
   pkt = &mpkt;
 
-  /* Try to get lock.  If can't, then bail out. */
-  if ((pfd = open(PIDFILE, O_EXCL | O_CREAT | O_RDWR)) < 0) {
-    lerror("Can't create lock file, quiting.");
-    exit(1);
-  }
-  fchmod(pfd, S_IRUSR | S_IRGRP | S_IROTH);
-  sprintf(pidbuf, "%d", getpid());
-  write(pfd, pidbuf, sizeof(pidbuf));
-  close(pfd);
-
   if (parse_args(argc, argv) < 0) {
     fprintf(stderr, "Error processing arguments.\n");
   }
@@ -177,6 +165,8 @@ int parse_args(int argc, char **argv) {
 int init_slothd(void) {
 
   DIR *devs;
+  int pfd;
+  char pidbuf[10];
   char bufstr[MAXDEVLEN];
   struct dirent *dptr;
   struct hostent *hent;
@@ -257,6 +247,16 @@ int init_slothd(void) {
     return -1;
   }
 
+  /* Try to get lock.  If can't, then bail out. */
+  if ((pfd = open(PIDFILE, O_EXCL | O_CREAT | O_RDWR)) < 0) {
+    lerror("Can't create lock file, quiting.");
+    exit(1);
+  }
+  fchmod(pfd, S_IRUSR | S_IRGRP | S_IROTH);
+  sprintf(pidbuf, "%d", getpid());
+  write(pfd, pidbuf, strlen(pidbuf));
+  close(pfd);
+
   return 0;
 }
 
-- 
GitLab