signals/signal.c

This is signals/signal.c (Listing 22-1, page 455), an example from the book, The Linux Programming Interface.

The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU Lesser 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.

 

Download signals/signal.c

  Cover of The Linux Programming Interface

Function list (Bold in this list means a function is not static)

/* signal.c

   An implementation of signal() using sigaction().

   Compiling with "-DOLD_SIGS" provides the older, unreliable signal handler
   semantics (which are still the default on some System V derivatives).
*/
/* Some UNIX implementations follow the BSD signal() semantics, including
   Linux, Tru64, and of course the BSD derivatives. Others, such as Solaris,
   follow the System V semantics. We'll conditionally override signal() on
   platforms following the System V semantics. For the other implementations,
   we'll provide a dummy source file that doesn't implement signal().
*/
#if defined(__sun) || defined(__sgi)
#include <signal.h>

typedef void (*sighandler_t)(int);
sighandler_t
signal(int sig, sighandler_t handler)
{
    struct sigaction newDisp, prevDisp;

    newDisp.sa_handler = handler;
    sigemptyset(&newDisp.sa_mask);
#ifdef OLD_SIGNAL
    newDisp.sa_flags = SA_RESETHAND | SA_NODEFER;
#else
    newDisp.sa_flags = SA_RESTART;
#endif

    if (sigaction(sig, &newDisp, &prevDisp) == -1)
        return SIG_ERR;
    else
        return prevDisp.sa_handler;
}

#else
/* The following declaration is provided purely to avoid gcc's
   "warning: ISO C forbids an empty source file" warnings. */

extern int signalDummyVar;

#endif

 

Download signals/signal.c

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