pgsjc/disc_SIGHUP.c

This is pgsjc/disc_SIGHUP.c (Listing 34-4, page 712), 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
+/* disc_SIGHUP.c
+
+   This program demonstrates that when a "terminal disconnect" occurs, SIGHUP
+   is sent to all the members of the foreground process group for this terminal.
+
+   Try using the following command to run this program in an X-window, and then
+   closing the window:
+
+        exec disc_SIGHUP > sig.log 2>&1
+
+   (Since the above will replace the shell with this program, it will be the
+   controlling process for the terminal.)
+*/
 #define _GNU_SOURCE     /* Get strsignal() declaration from <string.h> */
 #include <string.h>
 #include <signal.h>
 #include "tlpi_hdr.h"
 
 static void             /* Handler for SIGHUP */
 handler(int sig)
 {
     printf("PID %ld: caught signal %2d (%s)\n", (long) getpid(),
             sig, strsignal(sig));
                         /* UNSAFE (see Section 21.1.2) */
 }
 
 int
 main(int argc, char *argv[])
 {
     pid_t parentPid, childPid;
     int j;
     struct sigaction sa;
 
     if (argc < 2 || strcmp(argv[1], "--help") == 0)
         usageErr("%s {d|s}... [ > sig.log 2>&1 ]\n", argv[0]);
 
     setbuf(stdout, NULL);               /* Make stdout unbuffered */
 
     parentPid = getpid();
     printf("PID of parent process is:       %ld\n", (long) parentPid);
     printf("Foreground process group ID is: %ld\n",
             (long) tcgetpgrp(STDIN_FILENO));
 
     for (j = 1; j < argc; j++) {        /* Create child processes */
         childPid = fork();
         if (childPid == -1)
             errExit("fork");
 
         if (childPid == 0) {            /* If child... */
             if (argv[j][0] == 'd')      /* 'd' --> to different pgrp */
                 if (setpgid(0, 0) == -1)
                     errExit("setpgid");
 
             sigemptyset(&sa.sa_mask);
             sa.sa_flags = 0;
             sa.sa_handler = handler;
             if (sigaction(SIGHUP, &sa, NULL) == -1)
                 errExit("sigaction");
             break;                      /* Child exits loop */
         }
     }
 
     /* All processes fall through to here */
 
     alarm(60);      /* Ensure each process eventually terminates */
 
     printf("PID=%ld PGID=%ld\n", (long) getpid(), (long) getpgrp());
     for (;;)
         pause();        /* Wait for signals */
 }

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