Thread: pgkill for windows

pgkill for windows

From
Andrew Dunstan
Date:

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

Re: pgkill for windows

From
Peter Eisentraut
Date:
Andrew Dunstan wrote:
> Attached are 2 files needed to create a pgkill facility for Windows,
> and a complementary patch for src/bin/Makefile

If it's only for the regression tests, then it should go into that
directory.


Re: pgkill for windows

From
"Magnus Hagander"
Date:
> Andrew Dunstan wrote:
> > Attached are 2 files needed to create a pgkill facility for
> Windows,
> > and a complementary patch for src/bin/Makefile
>
> If it's only for the regression tests, then it should go into
> that directory.

It is not. It is a tool required to do any "controlled backend
termination" on win32. The regression tests is just one user of it.
Regression tests is just one user of it.

(You can shutdown the postmaster using Ctrl-C or using the Service
Control Manager, but pgkill is required for individual backend
signalling).


//Magnus

Re: pgkill for windows

From
Andrew Dunstan
Date:

Peter Eisentraut wrote:

>Andrew Dunstan wrote:
>
>
>>Attached are 2 files needed to create a pgkill facility for Windows,
>>and a complementary patch for src/bin/Makefile
>>
>>
>
>If it's only for the regression tests, then it should go into that
>directory.
>
>
>
>

In effect you need this on Windows anywhere that in Unix you would want
to use kill (1)  on a postgres process.

cheers

andrew


Re: pgkill for windows

From
Bruce Momjian
Date:
Andrew Dunstan wrote:
> >>Attached are 2 files needed to create a pgkill facility for Windows,
> >>and a complementary patch for src/bin/Makefile
> >>
> >>
> >
> >If it's only for the regression tests, then it should go into that
> >directory.
> >
> >
> >
> >
>
> In effect you need this on Windows anywhere that in Unix you would want
> to use kill (1)  on a postgres process.

Is this something that should be part of the Win32 pg_ctl C code
instead?

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

Re: pgkill for windows

From
"Magnus Hagander"
Date:
> > >>Attached are 2 files needed to create a pgkill facility
> for Windows,
> > >>and a complementary patch for src/bin/Makefile
> > >>
> > >>
> > >
> > >If it's only for the regression tests, then it should go into that
> > >directory.
> > >
> > >
> > >
> > >
> >
> > In effect you need this on Windows anywhere that in Unix you would
> > want to use kill (1)  on a postgres process.
>
> Is this something that should be part of the Win32 pg_ctl C
> code instead?

I'd say also, not instead.
pg_ctl still only works on the postmaster.

//Magnus


Re: pgkill for windows

From
Bruce Momjian
Date:
I was hoping to avoid platform-specific binaries.  Once pg_ctl is done
in C, it can start/stop the postmaster, but not individual backends.
Can we add a flag to pg_ctl so it can send arbitrary signals to
processed on Win32?  That would be best, I think.

---------------------------------------------------------------------------

Andrew Dunstan wrote:
>
>
> 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


>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

Re: pgkill for windows

From
"Andrew Dunstan"
Date:
Bruce Momjian said:
>
> I was hoping to avoid platform-specific binaries.  Once pg_ctl is done
> in C, it can start/stop the postmaster, but not individual backends.
> Can we add a flag to pg_ctl so it can send arbitrary signals to
> processed on Win32?  That would be best, I think.
>

Ok, that makes sense.

I am working on pg_ctl and hope to have a first cut in a day or two
(everything is done except the startup code).

cheers

andrew