pgkill for windows - Mailing list pgsql-patches

From Andrew Dunstan
Subject pgkill for windows
Date
Msg-id 409FAE96.2010302@dunslane.net
Whole thread Raw
Responses Re: pgkill for windows  (Peter Eisentraut <peter_e@gmx.net>)
Re: pgkill for windows  (Bruce Momjian <pgman@candle.pha.pa.us>)
List pgsql-patches

Attached are 2 files needed to create a pgkill facility for Windows, and
a complementary patch for src/bin/Makefile

It is adapted from the facility on the Win32 page, but uses the builtin
signal definitions rather than using its own.

This facility is needed for shutting down postgres cleanly as part of
'make check', as we don't really use signals in Windows, we only emulate
them using our own scheme, so any native kill program doesn't have the
desired effect.

cheers

andrew
#-------------------------------------------------------------------------
#
# Makefile for src/bin/pgkill
#
# Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
# Portions Copyright (c) 1994, Regents of the University of California
#
# $PostgreSQL: pgsql-server/src/bin/pgkill/Makefile,v 1.36 2004/04/26 17:40:48 momjian Exp $
#
#-------------------------------------------------------------------------

subdir = src/bin/pgkill
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global

override CPPFLAGS := -DPGBINDIR=\"$(bindir)\" -DPGDATADIR=\"$(datadir)\" -I$(libpq_srcdir) $(CPPFLAGS)

OBJS=    pgkill.o

all: pgkill

pgkill: $(OBJS)
    $(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) -o $@$(X)

install: all installdirs
    $(INSTALL_PROGRAM) pgkill$(X) $(DESTDIR)$(bindir)/pgkill$(X)

installdirs:
    $(mkinstalldirs) $(DESTDIR)$(bindir)

uninstall:
    rm -f $(DESTDIR)$(bindir)/pgkill$(X)

clean distclean maintainer-clean:
    rm -f pgkill$(X) $(OBJS)


# ensure that changes in bindir etc. propagate into object file
pgkill.o: pgkill.c $(top_builddir)/src/Makefile.global
Index: src/bin/Makefile
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/bin/Makefile,v
retrieving revision 1.43
diff -c -r1.43 Makefile
*** src/bin/Makefile    20 Apr 2004 00:40:06 -0000    1.43
--- src/bin/Makefile    10 May 2004 16:17:52 -0000
***************
*** 15,20 ****
--- 15,24 ----

  DIRS := initdb initlocation ipcclean pg_ctl pg_dump \
      psql scripts pg_config pg_controldata pg_resetxlog
+ ifeq ($(host_os), mingw32)
+ DIRS := $(DIRS) pgkill
+ endif
+

  all install installdirs uninstall depend distprep:
      @for dir in $(DIRS); do $(MAKE) -C $$dir $@ || exit; done
#include <stdio.h>
#include <stdlib.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <signal.h>
#include "pqsignal.h"

static void Usage(void) {
  printf("Usage: pgkill <signal> <pid>\n");
  printf("Signal must be uppercase..\n");
  exit(1);
}

static int
pgkill(int pid, int sig)
{
        char            pipename[128];
        BYTE            sigData = sig;
        BYTE            sigRet = 0;
        DWORD           bytes;

        wsprintf(pipename, "\\\\.\\pipe\\pgsignal_%i", pid);
        if (!CallNamedPipe(pipename, &sigData, 1, &sigRet, 1, &bytes, 1000))
        {
          printf("Failed in call: %ld\n",GetLastError());
                return -1;
        }
        if (bytes != 1 || sigRet != sig)
        {
          printf("Invalid return value!\n");
                return -1;
        }

        return 0;
}

int main(int argc, char *argv[]) {
  int sig = -1;
  int pid = 0;

  if (argc != 3)
    Usage();

  if (!strcmp(argv[1],"HUP"))
    sig = SIGHUP;
  else if (!strcmp(argv[1],"INT"))
    sig = SIGINT;
  else if (!strcmp(argv[1],"QUIT"))
    sig = SIGQUIT;
  else if (!strcmp(argv[1],"ABRT"))
    sig = SIGABRT;
  else if (!strcmp(argv[1],"KILL"))
    sig = SIGKILL;
  else if (!strcmp(argv[1],"TERM"))
    sig = SIGTERM;
  else if (!strcmp(argv[1],"USR1"))
    sig = SIGUSR1;
  else if (!strcmp(argv[1],"USR2"))
    sig = SIGUSR2;

  if (sig == -1)
    Usage();


  pid = atoi(argv[2]);
  if (pid == 0)
    Usage();

  printf("Got: %i\n",pgkill(pid,sig));
  exit(0);
}

pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: mingw configure failure detection
Next
From: Andrew Dunstan
Date:
Subject: patch for different join result order on regression test for win32