Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

dbkill.cpp

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1999-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: dbkill.cpp,v 12.1 2005/06/16 20:20:43 bostic Exp $
00008  */
00009 /*
00010  * Kill -
00011  * Simulate Unix kill on Windows/NT and Windows/9X.
00012  * This good enough to support the Berkeley DB test suite,
00013  * but may be missing some favorite features.
00014  *
00015  * Would have used MKS kill, but it didn't seem to work well
00016  * on Win/9X.  Cygnus kill works within the Gnu/Cygnus environment
00017  * (where processes are given small pids, with presumably a translation
00018  * table between small pids and actual process handles), but our test
00019  * environment, via Tcl, does not use the Cygnus environment.
00020  *
00021  * Compile this and install it as c:/tools/kill.exe (or as indicated
00022  * by build_win32/include.tcl ).
00023  */
00024 
00025 #include <windows.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <limits.h>
00029 
00030 /*
00031  * Like atol, with specified base.  Would use stdlib, but
00032  * strtol("0xFFFF1234", NULL, 16) returns 0x7FFFFFFF and
00033  * strtol("4294712487", NULL, 16) returns 0x7FFFFFFF w/ VC++
00034  */
00035 long
00036 myatol(char *s, int base)
00037 {
00038         long result = 0;
00039         char ch;
00040         int sign = 1;  /* + */
00041         if (base == 0)
00042                 base = 10;
00043         if (base != 10 && base != 16)
00044                 return LONG_MAX;
00045         while ((ch = *s++) != '\0') {
00046                 if (ch == '-') {
00047                         sign = -sign;
00048                 }
00049                 else if (ch >= '0' && ch <= '9') {
00050                         result = result * base + (ch - '0');
00051                 }
00052                 else if (ch == 'x' || ch == 'X') {
00053                         /* Allow leading 0x..., and switch to base 16 */
00054                         base = 16;
00055                 }
00056                 else if (base == 16 && ch >= 'a' && ch <= 'f') {
00057                         result = result * base + (ch - 'a' + 10);
00058                 }
00059                 else if (base == 16 && ch >= 'A' && ch <= 'F') {
00060                         result = result * base + (ch - 'A' + 10);
00061                 }
00062                 else {
00063                         if (sign > 1)
00064                                 return LONG_MAX;
00065                         else
00066                                 return LONG_MIN;
00067                 }
00068         }
00069         return sign * result;
00070 }
00071 
00072 void
00073 usage_exit()
00074 {
00075         fprintf(stderr, "Usage: kill [ -sig ] pid\n");
00076         fprintf(stderr, "       for win32, sig must be or 0, 15 (TERM)\n");
00077         exit(EXIT_FAILURE);
00078 }
00079 
00080 int
00081 main(int argc, char **argv)
00082 {
00083         HANDLE hProcess ;
00084         DWORD accessflag;
00085         long pid;
00086         int sig = 15;
00087 
00088         if (argc > 2) {
00089                 if (argv[1][0] != '-')
00090                         usage_exit();
00091 
00092                 if (strcmp(argv[1], "-TERM") == 0)
00093                         sig = 15;
00094                 else {
00095                         /* currently sig is more or less ignored,
00096                          * we only care if it is zero or not
00097                          */
00098                         sig = atoi(&argv[1][1]);
00099                         if (sig < 0)
00100                                 usage_exit();
00101                 }
00102                 argc--;
00103                 argv++;
00104         }
00105         if (argc < 2)
00106                 usage_exit();
00107 
00108         pid = myatol(argv[1], 10);
00109         /*printf("pid = %ld (0x%lx) (command line %s)\n", pid, pid, argv[1]);*/
00110         if (pid == LONG_MAX || pid == LONG_MIN)
00111                 usage_exit();
00112 
00113         if (sig == 0)
00114                 accessflag = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
00115         else
00116                 accessflag = STANDARD_RIGHTS_REQUIRED | PROCESS_TERMINATE;
00117         hProcess = OpenProcess(accessflag, FALSE, pid);
00118         if (hProcess == NULL) {
00119                 fprintf(stderr, "dbkill: %s: no such process\n", argv[1]);
00120                 exit(EXIT_FAILURE);
00121         }
00122         if (sig == 0)
00123                 exit(EXIT_SUCCESS);
00124         if (!TerminateProcess(hProcess, 99)) {
00125                 DWORD err = GetLastError();
00126                 fprintf(stderr,
00127                     "dbkill: cannot kill process: error %d (0x%lx)\n", err, err);
00128                 exit(EXIT_FAILURE);
00129         }
00130         return EXIT_SUCCESS;
00131 }

Generated on Sun Dec 25 12:14:17 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2