filelock/create_pid_file.c

This is filelock/create_pid_file.c (Listing 55-4, page 1143), an example from the book, The Linux Programming Interface.

The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU General Public License, version 3.

This page shows the "distribution" or "book" version of the file (why are there two versions?), or the differences between the two versions. You can switch between the views using the tabs below.

In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions.

  Cover of The Linux Programming Interface
+/* create_pid_file.c
+
+   Implement a function that can be used by a daemon (or indeed any program)
+   to ensure that only one instance of the program is running.
+*/
 #include <sys/stat.h>
 #include <fcntl.h>
 #include "region_locking.h"             /* For lockRegion() */
 #include "create_pid_file.h"            /* Declares createPidFile() and
                                            defines CPF_CLOEXEC */
 #include "tlpi_hdr.h"
 
 #define BUF_SIZE 100            /* Large enough to hold maximum PID as string */
 
 /* Open/create the file named in 'pidFile', lock it, optionally set the
    close-on-exec flag for the file descriptor, write our PID into the file,
    and (in case the caller is interested) return the file descriptor
    referring to the locked file. The caller is responsible for deleting
    'pidFile' file (just) before process termination. 'progName' should be the
    name of the calling program (i.e., argv[0] or similar), and is used only for
    diagnostic messages. If we can't open 'pidFile', or we encounter some other
    error, then we print an appropriate diagnostic and terminate. */
 
 int
 createPidFile(const char *progName, const char *pidFile, int flags)
 {
     int fd;
     char buf[BUF_SIZE];
 
     fd = open(pidFile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
     if (fd == -1)
         errExit("Could not open PID file %s", pidFile);
 
     if (flags & CPF_CLOEXEC) {
 
         /* Set the close-on-exec file descriptor flag */
 
+        /* Instead of the following steps, we could (on Linux) have opened the
+           file with O_CLOEXEC flag. However, not all systems support open()
+           O_CLOEXEC (which was standardized only in SUSv4), so instead we use
+           fcntl() to set the close-on-exec flag after opening the file */
+
         flags = fcntl(fd, F_GETFD);                     /* Fetch flags */
         if (flags == -1)
             errExit("Could not get flags for PID file %s", pidFile);
 
         flags |= FD_CLOEXEC;                            /* Turn on FD_CLOEXEC */
 
         if (fcntl(fd, F_SETFD, flags) == -1)            /* Update flags */
             errExit("Could not set flags for PID file %s", pidFile);
     }
 
     if (lockRegion(fd, F_WRLCK, SEEK_SET, 0, 0) == -1) {
         if (errno  == EAGAIN || errno == EACCES)
             fatal("PID file '%s' is locked; probably "
                      "'%s' is already running", pidFile, progName);
         else
             errExit("Unable to lock PID file '%s'", pidFile);
     }
 
     if (ftruncate(fd, 0) == -1)
         errExit("Could not truncate PID file '%s'", pidFile);
 
     snprintf(buf, BUF_SIZE, "%ld\n", (long) getpid());
     if (write(fd, buf, strlen(buf)) != strlen(buf))
         fatal("Writing to PID file '%s'", pidFile);
 
     return fd;
 }

Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.

Valid XHTML 1.1