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.
This commit is contained in:
Tom Lane 2013-03-17 12:06:42 -04:00
parent d43837d030
commit da5aeccf64
30 changed files with 99 additions and 184 deletions

View File

@ -35,7 +35,6 @@
#include "getopt_long.h"
#include "libpq-fe.h"
#include "libpq/pqsignal.h"
#include "portability/instr_time.h"
#include <ctype.h>

View File

@ -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"

View File

@ -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 <signal.h>
#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 */

View File

@ -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;

View File

@ -13,7 +13,7 @@
#include "postgres.h"
#include <libpq/pqsignal.h>
#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)
{

View File

@ -18,8 +18,6 @@
#include "postgres.h"
#include "libpq/pqsignal.h"
/* Communication area for inter-thread communication */
typedef struct timerCA

View File

@ -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"

View File

@ -16,7 +16,6 @@
#include <sys/time.h>
#include "libpq/pqsignal.h"
#include "storage/proc.h"
#include "utils/timeout.h"
#include "utils/timestamp.h"

View File

@ -1,5 +1,4 @@
/encnames.c
/pqsignal.c
/localtime.c
/initdb

View File

@ -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

View File

@ -56,7 +56,6 @@
#include <signal.h>
#include <time.h>
#include "libpq/pqsignal.h"
#include "mb/pg_wchar.h"
#include "getaddrinfo.h"
#include "getopt_long.h"

View File

@ -14,7 +14,6 @@
#include "postgres_fe.h"
#include "libpq-fe.h"
#include "libpq/pqsignal.h"
#include "access/xlog_internal.h"
#include "receivelog.h"

View File

@ -33,7 +33,6 @@
#include <sys/resource.h>
#endif
#include "libpq/pqsignal.h"
#include "getopt_long.h"
#include "miscadmin.h"

View File

@ -19,8 +19,6 @@
#include "portability/instr_time.h"
#include "pqsignal.h"
#include "settings.h"
#include "command.h"
#include "copy.h"

View File

@ -18,7 +18,6 @@
#include "libpq-fe.h"
#include "pqexpbuffer.h"
#include "pqsignal.h"
#include "dumputils.h"
#include "settings.h"

View File

@ -23,7 +23,6 @@
#include <locale.h>
#include "catalog/pg_type.h"
#include "pqsignal.h"
#include "common.h"
#include "mbprint.h"

View File

@ -19,7 +19,6 @@
#include <unistd.h>
#include "common.h"
#include "libpq/pqsignal.h"
static void SetCancelConn(PGconn *conn);
static void ResetCancelConn(void);

View File

@ -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 */

View File

@ -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);

View File

@ -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

View File

@ -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" \

View File

@ -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"

View File

@ -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,

View File

@ -30,7 +30,6 @@
#include "libpq-fe.h"
#include "fe-auth.h"
#include "pqsignal.h"
#include "libpq-int.h"
#ifdef WIN32

View File

@ -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 <signal.h>
#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 */
}

View File

@ -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 */

View File

@ -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" \

View File

@ -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"

View File

@ -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

76
src/port/pqsignal.c Normal file
View File

@ -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 <signal.h>
#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 */