Header And Logo

PostgreSQL
| The world's most advanced open source database.

pqsignal.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * pqsignal.c
00004  *    reliable BSD-style signal(2) routine stolen from RWW who stole it
00005  *    from Stevens...
00006  *
00007  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00008  * Portions Copyright (c) 1994, Regents of the University of California
00009  *
00010  *
00011  * IDENTIFICATION
00012  *    src/port/pqsignal.c
00013  *
00014  *  A NOTE ABOUT SIGNAL HANDLING ACROSS THE VARIOUS PLATFORMS.
00015  *
00016  *  pg_config.h defines the macro HAVE_POSIX_SIGNALS for some platforms and
00017  *  not for others.  We use that here to decide how to handle signalling.
00018  *
00019  *  Ultrix and SunOS provide BSD signal(2) semantics by default.
00020  *
00021  *  SVID2 and POSIX signal(2) semantics differ from BSD signal(2)
00022  *  semantics.  We can use the POSIX sigaction(2) on systems that
00023  *  allow us to request restartable signals (SA_RESTART).
00024  *
00025  *  Some systems don't allow restartable signals at all unless we
00026  *  link to a special BSD library.
00027  *
00028  *  We devoutly hope that there aren't any Unix-oid systems that provide
00029  *  neither POSIX signals nor BSD signals.  The alternative is to do
00030  *  signal-handler reinstallation, which doesn't work well at all.
00031  *
00032  *  Windows, of course, is resolutely in a class by itself.  In the backend,
00033  *  we don't use this file at all; src/backend/port/win32/signal.c provides
00034  *  pqsignal() for the backend environment.  Frontend programs can use
00035  *  this version of pqsignal() if they wish, but beware that Windows
00036  *  requires signal-handler reinstallation, because indeed it provides
00037  *  neither POSIX signals nor BSD signals :-(
00038  * ------------------------------------------------------------------------
00039  */
00040 
00041 #include "c.h"
00042 
00043 #include <signal.h>
00044 
00045 #if !defined(WIN32) || defined(FRONTEND)
00046 
00047 /*
00048  * Set up a signal handler for signal "signo"
00049  *
00050  * Returns the previous handler.
00051  */
00052 pqsigfunc
00053 pqsignal(int signo, pqsigfunc func)
00054 {
00055 #if !defined(HAVE_POSIX_SIGNALS)
00056     return signal(signo, func);
00057 #else
00058     struct sigaction act,
00059                 oact;
00060 
00061     act.sa_handler = func;
00062     sigemptyset(&act.sa_mask);
00063     act.sa_flags = 0;
00064     if (signo != SIGALRM)
00065         act.sa_flags |= SA_RESTART;
00066 #ifdef SA_NOCLDSTOP
00067     if (signo == SIGCHLD)
00068         act.sa_flags |= SA_NOCLDSTOP;
00069 #endif
00070     if (sigaction(signo, &act, &oact) < 0)
00071         return SIG_ERR;
00072     return oact.sa_handler;
00073 #endif   /* !HAVE_POSIX_SIGNALS */
00074 }
00075 
00076 #endif /* !defined(WIN32) || defined(FRONTEND) */