From: Tom Lane Date: Sun, 17 Mar 2013 16:06:42 +0000 (-0400) Subject: Move pqsignal() to libpgport. X-Git-Tag: REL9_3_BETA1~220 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=da5aeccf64b37a8e9bd3cb605848590595dbcbf8;p=postgresql Move pqsignal() to libpgport. We had two copies of this function in the backend and libpq, which was already pretty bogus, but it turns out that we need it in some other programs that don't use libpq (such as pg_test_fsync). So put it where it probably should have been all along. The signal-mask-initialization support in src/backend/libpq/pqsignal.c stays where it is, though, since we only need that in the backend. --- diff --git a/contrib/pgbench/pgbench.c b/contrib/pgbench/pgbench.c index d5f8f73190..8deabe4a5a 100644 --- a/contrib/pgbench/pgbench.c +++ b/contrib/pgbench/pgbench.c @@ -35,7 +35,6 @@ #include "getopt_long.h" #include "libpq-fe.h" -#include "libpq/pqsignal.h" #include "portability/instr_time.h" #include diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index a02eebcb27..8e7341ba45 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -35,7 +35,6 @@ #include "catalog/catversion.h" #include "catalog/pg_control.h" #include "catalog/pg_database.h" -#include "libpq/pqsignal.h" #include "miscadmin.h" #include "pgstat.h" #include "postmaster/bgwriter.h" diff --git a/src/backend/libpq/pqsignal.c b/src/backend/libpq/pqsignal.c index d088daeb7d..b621ba7add 100644 --- a/src/backend/libpq/pqsignal.c +++ b/src/backend/libpq/pqsignal.c @@ -1,8 +1,7 @@ /*------------------------------------------------------------------------- * * pqsignal.c - * reliable BSD-style signal(2) routine stolen from RWW who stole it - * from Stevens... + * Backend signal(2) support (see also src/port/pqsignal.c) * * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California @@ -11,38 +10,11 @@ * IDENTIFICATION * src/backend/libpq/pqsignal.c * - * NOTES - * This shouldn't be in libpq, but the monitor and some other - * things need it... - * - * A NOTE ABOUT SIGNAL HANDLING ACROSS THE VARIOUS PLATFORMS. - * - * pg_config.h defines the macro HAVE_POSIX_SIGNALS for some platforms and - * not for others. This file and pqsignal.h use that macro to decide - * how to handle signalling. - * - * signal(2) handling - this is here because it affects some of - * the frontend commands as well as the backend processes. - * - * Ultrix and SunOS provide BSD signal(2) semantics by default. - * - * SVID2 and POSIX signal(2) semantics differ from BSD signal(2) - * semantics. We can use the POSIX sigaction(2) on systems that - * allow us to request restartable signals (SA_RESTART). - * - * Some systems don't allow restartable signals at all unless we - * link to a special BSD library. - * - * We devoutly hope that there aren't any systems that provide - * neither POSIX signals nor BSD signals. The alternative - * is to do signal-handler reinstallation, which doesn't work well - * at all. - * ------------------------------------------------------------------------*/ + * ------------------------------------------------------------------------ + */ #include "postgres.h" -#include - #include "libpq/pqsignal.h" @@ -145,36 +117,3 @@ pqinitmask(void) sigmask(SIGWINCH) | sigmask(SIGFPE); #endif } - - -/* Win32 signal handling is in backend/port/win32/signal.c */ -#ifndef WIN32 - -/* - * Set up a signal handler - */ -pqsigfunc -pqsignal(int signo, pqsigfunc func) -{ -#if !defined(HAVE_POSIX_SIGNALS) - return signal(signo, func); -#else - struct sigaction act, - oact; - - act.sa_handler = func; - sigemptyset(&act.sa_mask); - act.sa_flags = 0; - if (signo != SIGALRM) - act.sa_flags |= SA_RESTART; -#ifdef SA_NOCLDSTOP - if (signo == SIGCHLD) - act.sa_flags |= SA_NOCLDSTOP; -#endif - if (sigaction(signo, &act, &oact) < 0) - return SIG_ERR; - return oact.sa_handler; -#endif /* !HAVE_POSIX_SIGNALS */ -} - -#endif /* WIN32 */ diff --git a/src/backend/main/main.c b/src/backend/main/main.c index 1173bda620..8d4218e00b 100644 --- a/src/backend/main/main.c +++ b/src/backend/main/main.c @@ -41,9 +41,6 @@ #include "utils/help_config.h" #include "utils/pg_locale.h" #include "utils/ps_status.h" -#ifdef WIN32 -#include "libpq/pqsignal.h" -#endif const char *progname; diff --git a/src/backend/port/win32/signal.c b/src/backend/port/win32/signal.c index 2c406bc7f2..8b2d98141b 100644 --- a/src/backend/port/win32/signal.c +++ b/src/backend/port/win32/signal.c @@ -13,7 +13,7 @@ #include "postgres.h" -#include +#include "libpq/pqsignal.h" /* * These are exported for use by the UNBLOCKED_SIGNAL_QUEUE() macro. @@ -158,7 +158,11 @@ pqsigsetmask(int mask) } -/* signal manipulation. Only called on main thread, no sync required */ +/* + * Unix-like signal handler installation + * + * Only called on main thread, no sync required + */ pqsigfunc pqsignal(int signum, pqsigfunc handler) { diff --git a/src/backend/port/win32/timer.c b/src/backend/port/win32/timer.c index 1394104456..333beb44a8 100644 --- a/src/backend/port/win32/timer.c +++ b/src/backend/port/win32/timer.c @@ -18,8 +18,6 @@ #include "postgres.h" -#include "libpq/pqsignal.h" - /* Communication area for inter-thread communication */ typedef struct timerCA diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index 266cd64f6f..c05bb1e081 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -49,7 +49,6 @@ #include "funcapi.h" #include "libpq/libpq.h" #include "libpq/pqformat.h" -#include "libpq/pqsignal.h" #include "miscadmin.h" #include "nodes/replnodes.h" #include "replication/basebackup.h" diff --git a/src/backend/utils/misc/timeout.c b/src/backend/utils/misc/timeout.c index 2ee6e00ed2..b5a3c8f5df 100644 --- a/src/backend/utils/misc/timeout.c +++ b/src/backend/utils/misc/timeout.c @@ -16,7 +16,6 @@ #include -#include "libpq/pqsignal.h" #include "storage/proc.h" #include "utils/timeout.h" #include "utils/timestamp.h" diff --git a/src/bin/initdb/.gitignore b/src/bin/initdb/.gitignore index c0ed2e8d16..0f74727d8f 100644 --- a/src/bin/initdb/.gitignore +++ b/src/bin/initdb/.gitignore @@ -1,5 +1,4 @@ /encnames.c -/pqsignal.c /localtime.c /initdb diff --git a/src/bin/initdb/Makefile b/src/bin/initdb/Makefile index 4a6037933e..458b455373 100644 --- a/src/bin/initdb/Makefile +++ b/src/bin/initdb/Makefile @@ -23,23 +23,20 @@ ifneq (,$(with_system_tzdata)) override CPPFLAGS += '-DSYSTEMTZDIR="$(with_system_tzdata)"' endif -OBJS= initdb.o findtimezone.o localtime.o encnames.o pqsignal.o $(WIN32RES) +OBJS= initdb.o findtimezone.o localtime.o encnames.o $(WIN32RES) all: initdb initdb: $(OBJS) | submake-libpgport $(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X) -# We used to pull in all of libpq to get encnames and pqsignal, but that +# We used to pull in all of libpq to get encnames.c, but that # exposes us to risks of version skew if we link to a shared library. # Do it the hard way, instead, so that we're statically linked. encnames.c: % : $(top_srcdir)/src/backend/utils/mb/% rm -f $@ && $(LN_S) $< . -pqsignal.c: % : $(top_srcdir)/src/interfaces/libpq/% - rm -f $@ && $(LN_S) $< . - # Likewise, pull in localtime.c from src/timezones localtime.c: % : $(top_srcdir)/src/timezone/% @@ -55,7 +52,7 @@ uninstall: rm -f '$(DESTDIR)$(bindir)/initdb$(X)' clean distclean maintainer-clean: - rm -f initdb$(X) $(OBJS) encnames.c pqsignal.c localtime.c + rm -f initdb$(X) $(OBJS) encnames.c localtime.c # ensure that changes in datadir propagate into object file diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index b0b346f72a..e16f3e3c80 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -56,7 +56,6 @@ #include #include -#include "libpq/pqsignal.h" #include "mb/pg_wchar.h" #include "getaddrinfo.h" #include "getopt_long.h" diff --git a/src/bin/pg_basebackup/pg_receivexlog.c b/src/bin/pg_basebackup/pg_receivexlog.c index 352ff35376..e65f127d1d 100644 --- a/src/bin/pg_basebackup/pg_receivexlog.c +++ b/src/bin/pg_basebackup/pg_receivexlog.c @@ -14,7 +14,6 @@ #include "postgres_fe.h" #include "libpq-fe.h" -#include "libpq/pqsignal.h" #include "access/xlog_internal.h" #include "receivelog.h" diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index 7d5e16856e..537d173e89 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -33,7 +33,6 @@ #include #endif -#include "libpq/pqsignal.h" #include "getopt_long.h" #include "miscadmin.h" diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index dd183cacc1..be5e34a369 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -19,8 +19,6 @@ #include "portability/instr_time.h" -#include "pqsignal.h" - #include "settings.h" #include "command.h" #include "copy.h" diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c index a97795f943..57cbf92816 100644 --- a/src/bin/psql/copy.c +++ b/src/bin/psql/copy.c @@ -18,7 +18,6 @@ #include "libpq-fe.h" #include "pqexpbuffer.h" -#include "pqsignal.h" #include "dumputils.h" #include "settings.h" diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c index 0722c984a8..7e1f27ac9e 100644 --- a/src/bin/psql/print.c +++ b/src/bin/psql/print.c @@ -23,7 +23,6 @@ #include #include "catalog/pg_type.h" -#include "pqsignal.h" #include "common.h" #include "mbprint.h" diff --git a/src/bin/scripts/common.c b/src/bin/scripts/common.c index 03193b6fcd..4645bc137f 100644 --- a/src/bin/scripts/common.c +++ b/src/bin/scripts/common.c @@ -19,7 +19,6 @@ #include #include "common.h" -#include "libpq/pqsignal.h" static void SetCancelConn(PGconn *conn); static void ResetCancelConn(void); diff --git a/src/include/libpq/pqsignal.h b/src/include/libpq/pqsignal.h index bc0bcef040..1889dca4b9 100644 --- a/src/include/libpq/pqsignal.h +++ b/src/include/libpq/pqsignal.h @@ -1,18 +1,13 @@ /*------------------------------------------------------------------------- * * pqsignal.h - * prototypes for the reliable BSD-style signal(2) routine. - * + * Backend signal(2) support (see also src/port/pqsignal.c) * * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * src/include/libpq/pqsignal.h * - * NOTES - * This shouldn't be in libpq, but the monitor and some other - * things need it... - * *------------------------------------------------------------------------- */ #ifndef PQSIGNAL_H @@ -42,10 +37,6 @@ int pqsigsetmask(int mask); #define sigdelset(set, signum) (*(set) &= ~(sigmask(signum))) #endif /* not HAVE_SIGPROCMASK */ -typedef void (*pqsigfunc) (int); - extern void pqinitmask(void); -extern pqsigfunc pqsignal(int signo, pqsigfunc func); - #endif /* PQSIGNAL_H */ diff --git a/src/include/port.h b/src/include/port.h index c5d0e0a970..cde4ad5607 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -462,6 +462,13 @@ extern int pg_check_dir(const char *dir); /* port/pgmkdirp.c */ extern int pg_mkdir_p(char *path, int omode); +/* port/pqsignal.c */ +/* On Windows, we can emulate pqsignal in the backend, but not frontend */ +#if !defined(WIN32) || !defined(FRONTEND) +typedef void (*pqsigfunc) (int signo); +extern pqsigfunc pqsignal(int signo, pqsigfunc func); +#endif + /* port/quotes.c */ extern char *escape_single_quotes_ascii(const char *src); diff --git a/src/interfaces/libpq/Makefile b/src/interfaces/libpq/Makefile index f594f7e499..c45856efb6 100644 --- a/src/interfaces/libpq/Makefile +++ b/src/interfaces/libpq/Makefile @@ -32,7 +32,7 @@ LIBS := $(LIBS:-lpgport=) # We can't use Makefile variables here because the MSVC build system scrapes # OBJS from this file. OBJS= fe-auth.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-lobj.o \ - fe-protocol2.o fe-protocol3.o pqexpbuffer.o pqsignal.o fe-secure.o \ + fe-protocol2.o fe-protocol3.o pqexpbuffer.o fe-secure.o \ libpq-events.o # libpgport C files we always use OBJS += chklocale.o inet_net_ntop.o noblock.o pgstrcasecmp.o thread.o diff --git a/src/interfaces/libpq/bcc32.mak b/src/interfaces/libpq/bcc32.mak index 441fb5e76f..6282196446 100644 --- a/src/interfaces/libpq/bcc32.mak +++ b/src/interfaces/libpq/bcc32.mak @@ -95,7 +95,6 @@ CLEAN : -@erase "$(INTDIR)\fe-secure.obj" -@erase "$(INTDIR)\libpq-events.obj" -@erase "$(INTDIR)\pqexpbuffer.obj" - -@erase "$(INTDIR)\pqsignal.obj" -@erase "$(INTDIR)\win32.obj" -@erase "$(INTDIR)\wchar.obj" -@erase "$(INTDIR)\encnames.obj" @@ -140,7 +139,6 @@ LIB32_OBJS= \ "$(INTDIR)\fe-secure.obj" \ "$(INTDIR)\libpq-events.obj" \ "$(INTDIR)\pqexpbuffer.obj" \ - "$(INTDIR)\pqsignal.obj" \ "$(INTDIR)\wchar.obj" \ "$(INTDIR)\encnames.obj" \ "$(INTDIR)\snprintf.obj" \ diff --git a/src/interfaces/libpq/fe-misc.c b/src/interfaces/libpq/fe-misc.c index cea3776c33..6be3249638 100644 --- a/src/interfaces/libpq/fe-misc.c +++ b/src/interfaces/libpq/fe-misc.c @@ -55,7 +55,6 @@ #include "libpq-fe.h" #include "libpq-int.h" -#include "pqsignal.h" #include "mb/pg_wchar.h" #include "pg_config_paths.h" diff --git a/src/interfaces/libpq/fe-print.c b/src/interfaces/libpq/fe-print.c index 5c86f037d7..b0fab6a839 100644 --- a/src/interfaces/libpq/fe-print.c +++ b/src/interfaces/libpq/fe-print.c @@ -35,7 +35,6 @@ #include "libpq-fe.h" #include "libpq-int.h" -#include "pqsignal.h" static void do_field(const PQprintOpt *po, const PGresult *res, diff --git a/src/interfaces/libpq/fe-secure.c b/src/interfaces/libpq/fe-secure.c index 574d3bae8f..174cf426f0 100644 --- a/src/interfaces/libpq/fe-secure.c +++ b/src/interfaces/libpq/fe-secure.c @@ -30,7 +30,6 @@ #include "libpq-fe.h" #include "fe-auth.h" -#include "pqsignal.h" #include "libpq-int.h" #ifdef WIN32 diff --git a/src/interfaces/libpq/pqsignal.c b/src/interfaces/libpq/pqsignal.c deleted file mode 100644 index 26e203b669..0000000000 --- a/src/interfaces/libpq/pqsignal.c +++ /dev/null @@ -1,49 +0,0 @@ -/*------------------------------------------------------------------------- - * - * pqsignal.c - * reliable BSD-style signal(2) routine stolen from RWW who stole it - * from Stevens... - * - * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group - * Portions Copyright (c) 1994, Regents of the University of California - * - * - * IDENTIFICATION - * src/interfaces/libpq/pqsignal.c - * - * NOTES - * This shouldn't be in libpq, but the monitor and some other - * things need it... - * - *------------------------------------------------------------------------- - */ -#include "postgres_fe.h" - -#include - -#include "pqsignal.h" - - -pqsigfunc -pqsignal(int signo, pqsigfunc func) -{ -#if !defined(HAVE_POSIX_SIGNALS) - return signal(signo, func); -#else - struct sigaction act, - oact; - - act.sa_handler = func; - sigemptyset(&act.sa_mask); - act.sa_flags = 0; - if (signo != SIGALRM) - act.sa_flags |= SA_RESTART; -#ifdef SA_NOCLDSTOP - if (signo == SIGCHLD) - act.sa_flags |= SA_NOCLDSTOP; -#endif - if (sigaction(signo, &act, &oact) < 0) - return SIG_ERR; - return oact.sa_handler; -#endif /* !HAVE_POSIX_SIGNALS */ -} diff --git a/src/interfaces/libpq/pqsignal.h b/src/interfaces/libpq/pqsignal.h deleted file mode 100644 index 2a72327d79..0000000000 --- a/src/interfaces/libpq/pqsignal.h +++ /dev/null @@ -1,25 +0,0 @@ -/*------------------------------------------------------------------------- - * - * pqsignal.h - * prototypes for the reliable BSD-style signal(2) routine. - * - * - * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group - * Portions Copyright (c) 1994, Regents of the University of California - * - * src/interfaces/libpq/pqsignal.h - * - * NOTES - * This shouldn't be in libpq, but the monitor and some other - * things need it... - * - *------------------------------------------------------------------------- - */ -#ifndef PQSIGNAL_H -#define PQSIGNAL_H - -typedef void (*pqsigfunc) (int); - -extern pqsigfunc pqsignal(int signo, pqsigfunc func); - -#endif /* PQSIGNAL_H */ diff --git a/src/interfaces/libpq/win32.mak b/src/interfaces/libpq/win32.mak index 9355de23ce..49d51d11fb 100644 --- a/src/interfaces/libpq/win32.mak +++ b/src/interfaces/libpq/win32.mak @@ -102,7 +102,6 @@ CLEAN : -@erase "$(INTDIR)\fe-secure.obj" -@erase "$(INTDIR)\libpq-events.obj" -@erase "$(INTDIR)\pqexpbuffer.obj" - -@erase "$(INTDIR)\pqsignal.obj" -@erase "$(INTDIR)\win32.obj" -@erase "$(INTDIR)\wchar.obj" -@erase "$(INTDIR)\encnames.obj" @@ -150,7 +149,6 @@ LIB32_OBJS= \ "$(INTDIR)\fe-secure.obj" \ "$(INTDIR)\libpq-events.obj" \ "$(INTDIR)\pqexpbuffer.obj" \ - "$(INTDIR)\pqsignal.obj" \ "$(INTDIR)\wchar.obj" \ "$(INTDIR)\encnames.obj" \ "$(INTDIR)\snprintf.obj" \ diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c index 1463618e42..ef56a4fab4 100644 --- a/src/pl/plperl/plperl.c +++ b/src/pl/plperl/plperl.c @@ -24,7 +24,6 @@ #include "commands/trigger.h" #include "executor/spi.h" #include "funcapi.h" -#include "libpq/pqsignal.h" #include "mb/pg_wchar.h" #include "miscadmin.h" #include "nodes/makefuncs.h" diff --git a/src/port/Makefile b/src/port/Makefile index 0774e33f9f..a032acca1c 100644 --- a/src/port/Makefile +++ b/src/port/Makefile @@ -32,7 +32,8 @@ LIBS += $(PTHREAD_LIBS) OBJS = $(LIBOBJS) chklocale.o dirmod.o erand48.o exec.o fls.o inet_net_ntop.o \ noblock.o path.o pgcheckdir.o pg_crc.o pgmkdirp.o pgsleep.o \ - pgstrcasecmp.o qsort.o qsort_arg.o quotes.o sprompt.o tar.o thread.o \ + pgstrcasecmp.o pqsignal.o \ + qsort.o qsort_arg.o quotes.o sprompt.o tar.o thread.o \ wait_error.o # foo_srv.o and foo.o are both built from foo.c, but only foo.o has -DFRONTEND diff --git a/src/port/pqsignal.c b/src/port/pqsignal.c new file mode 100644 index 0000000000..ffb6f84330 --- /dev/null +++ b/src/port/pqsignal.c @@ -0,0 +1,76 @@ +/*------------------------------------------------------------------------- + * + * pqsignal.c + * reliable BSD-style signal(2) routine stolen from RWW who stole it + * from Stevens... + * + * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/port/pqsignal.c + * + * A NOTE ABOUT SIGNAL HANDLING ACROSS THE VARIOUS PLATFORMS. + * + * pg_config.h defines the macro HAVE_POSIX_SIGNALS for some platforms and + * not for others. We use that here to decide how to handle signalling. + * + * Ultrix and SunOS provide BSD signal(2) semantics by default. + * + * SVID2 and POSIX signal(2) semantics differ from BSD signal(2) + * semantics. We can use the POSIX sigaction(2) on systems that + * allow us to request restartable signals (SA_RESTART). + * + * Some systems don't allow restartable signals at all unless we + * link to a special BSD library. + * + * We devoutly hope that there aren't any Unix-oid systems that provide + * neither POSIX signals nor BSD signals. The alternative is to do + * signal-handler reinstallation, which doesn't work well at all. + * + * Windows, of course, is resolutely in a class by itself. This file + * should not get compiled at all on Windows. We have an emulation of + * pqsignal() in src/backend/port/win32/signal.c for the backend + * environment; frontend programs are out of luck. + * ------------------------------------------------------------------------ + */ + +#include "c.h" + +#include + +#ifndef WIN32 + +/* + * Set up a signal handler for signal "signo" + * + * Returns the previous handler. It's expected that the installed handler + * will persist across multiple deliveries of the signal (unlike the original + * POSIX definition of signal(2)). + */ +pqsigfunc +pqsignal(int signo, pqsigfunc func) +{ +#if !defined(HAVE_POSIX_SIGNALS) + return signal(signo, func); +#else + struct sigaction act, + oact; + + act.sa_handler = func; + sigemptyset(&act.sa_mask); + act.sa_flags = 0; + if (signo != SIGALRM) + act.sa_flags |= SA_RESTART; +#ifdef SA_NOCLDSTOP + if (signo == SIGCHLD) + act.sa_flags |= SA_NOCLDSTOP; +#endif + if (sigaction(signo, &act, &oact) < 0) + return SIG_ERR; + return oact.sa_handler; +#endif /* !HAVE_POSIX_SIGNALS */ +} + +#endif /* WIN32 */