#include "getopt_long.h"
 #include "libpq-fe.h"
-#include "libpq/pqsignal.h"
 #include "portability/instr_time.h"
 
 #include <ctype.h>
 
 #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"
 
 /*-------------------------------------------------------------------------
  *
  * 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
  * 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"
 
 
        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 */
 
 #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;
 
 
 #include "postgres.h"
 
-#include <libpq/pqsignal.h>
+#include "libpq/pqsignal.h"
 
 /*
  * These are exported for use by the UNBLOCKED_SIGNAL_QUEUE() macro.
 }
 
 
-/* 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)
 {
 
 
 #include "postgres.h"
 
-#include "libpq/pqsignal.h"
-
 
 /* Communication area for inter-thread communication */
 typedef struct timerCA
 
 #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"
 
 
 #include <sys/time.h>
 
-#include "libpq/pqsignal.h"
 #include "storage/proc.h"
 #include "utils/timeout.h"
 #include "utils/timestamp.h"
 
 /encnames.c
-/pqsignal.c
 /localtime.c
 
 /initdb
 
 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/%
    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
 
 #include <signal.h>
 #include <time.h>
 
-#include "libpq/pqsignal.h"
 #include "mb/pg_wchar.h"
 #include "getaddrinfo.h"
 #include "getopt_long.h"
 
 
 #include "postgres_fe.h"
 #include "libpq-fe.h"
-#include "libpq/pqsignal.h"
 #include "access/xlog_internal.h"
 
 #include "receivelog.h"
 
 #include <sys/resource.h>
 #endif
 
-#include "libpq/pqsignal.h"
 #include "getopt_long.h"
 #include "miscadmin.h"
 
 
 
 #include "portability/instr_time.h"
 
-#include "pqsignal.h"
-
 #include "settings.h"
 #include "command.h"
 #include "copy.h"
 
 
 #include "libpq-fe.h"
 #include "pqexpbuffer.h"
-#include "pqsignal.h"
 #include "dumputils.h"
 
 #include "settings.h"
 
 #include <locale.h>
 
 #include "catalog/pg_type.h"
-#include "pqsignal.h"
 
 #include "common.h"
 #include "mbprint.h"
 
 #include <unistd.h>
 
 #include "common.h"
-#include "libpq/pqsignal.h"
 
 static void SetCancelConn(PGconn *conn);
 static void ResetCancelConn(void);
 
 /*-------------------------------------------------------------------------
  *
  * 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
 #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 */
 
 /* 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);
 
 
 # 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
 
    -@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"
    "$(INTDIR)\fe-secure.obj" \
    "$(INTDIR)\libpq-events.obj" \
    "$(INTDIR)\pqexpbuffer.obj" \
-   "$(INTDIR)\pqsignal.obj" \
    "$(INTDIR)\wchar.obj" \
    "$(INTDIR)\encnames.obj" \
    "$(INTDIR)\snprintf.obj" \
 
 
 #include "libpq-fe.h"
 #include "libpq-int.h"
-#include "pqsignal.h"
 #include "mb/pg_wchar.h"
 #include "pg_config_paths.h"
 
 
 
 #include "libpq-fe.h"
 #include "libpq-int.h"
-#include "pqsignal.h"
 
 
 static void do_field(const PQprintOpt *po, const PGresult *res,
 
 
 #include "libpq-fe.h"
 #include "fe-auth.h"
-#include "pqsignal.h"
 #include "libpq-int.h"
 
 #ifdef WIN32
 
+++ /dev/null
-/*-------------------------------------------------------------------------
- *
- * 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 */
-}
 
+++ /dev/null
-/*-------------------------------------------------------------------------
- *
- * 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 */
 
    -@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"
    "$(INTDIR)\fe-secure.obj" \
    "$(INTDIR)\libpq-events.obj" \
    "$(INTDIR)\pqexpbuffer.obj" \
-   "$(INTDIR)\pqsignal.obj" \
    "$(INTDIR)\wchar.obj" \
    "$(INTDIR)\encnames.obj" \
    "$(INTDIR)\snprintf.obj" \
 
 #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"
 
 
 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
 
--- /dev/null
+/*-------------------------------------------------------------------------
+ *
+ * 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 */