]> granicus.if.org Git - pgbouncer/commitdiff
Improve pid file processing
authorPeter Eisentraut <peter@eisentraut.org>
Mon, 24 Jun 2019 20:46:10 +0000 (22:46 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Mon, 24 Jun 2019 20:46:10 +0000 (22:46 +0200)
Remove some useless code that triggers a TOCTOU warning from
Coverity.  Make error messages more accurate.

src/main.c

index e76c773e2dae9d5c5f5afa5d15f49f4f11e5811b..49a87e4ffd50779219f7f1ee432d57870c3e92d6 100644 (file)
@@ -582,28 +582,23 @@ static void remove_pidfile(void)
 static void check_pidfile(void)
 {
        char buf[128 + 1];
-       struct stat st;
        pid_t pid = 0;
        int fd, res, err;
 
        if (!cf_pidfile || !cf_pidfile[0])
                return;
 
-       /* check if pidfile exists */
-       if (stat(cf_pidfile, &st) < 0) {
-               if (errno != ENOENT)
-                       fatal_perror("stat");
-               return;
-       }
-
        /* read old pid */
        fd = open(cf_pidfile, O_RDONLY);
-       if (fd < 0)
-               goto locked_pidfile;
+       if (fd < 0) {
+               if (errno == ENOENT)
+                       return;
+               fatal_perror("could not open pidfile");
+       }
        res = read(fd, buf, sizeof(buf) - 1);
        close(fd);
        if (res <= 0)
-               goto locked_pidfile;
+               fatal_perror("could not read pidfile");
 
        /* parse pid */
        buf[res] = 0;
@@ -621,7 +616,7 @@ static void check_pidfile(void)
        log_info("stale pidfile, removing");
        err = unlink(cf_pidfile);
        if (err != 0)
-               fatal_perror("cannot remove stale pidfile");
+               fatal_perror("could not remove stale pidfile");
        return;
 
 locked_pidfile: