~ubuntu-branches/ubuntu/maverick/evolution-data-server/maverick-proposed

« back to all changes in this revision

Viewing changes to libdb/build_win32/dbkill.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-05-17 17:02:06 UTC
  • mfrom: (1.10.5 upstream)
  • mto: This revision was merged to the branch mainline in revision 128.
  • Revision ID: james.westby@ubuntu.com-20100517170206-xu1wmjuy40nt2sk0
Tags: upstream-2.30.1
ImportĀ upstreamĀ versionĀ 2.30.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*-
2
 
 * See the file LICENSE for redistribution information.
3
 
 *
4
 
 * Copyright (c) 1999-2002
5
 
 *      Sleepycat Software.  All rights reserved.
6
 
 *
7
 
 * $Id$
8
 
 */
9
 
/*
10
 
 * Kill -
11
 
 * Simulate Unix kill on Windows/NT and Windows/9X.
12
 
 * This good enough to support the Berkeley DB test suite,
13
 
 * but may be missing some favorite features.
14
 
 *
15
 
 * Would have used MKS kill, but it didn't seem to work well
16
 
 * on Win/9X.  Cygnus kill works within the Gnu/Cygnus environment
17
 
 * (where processes are given small pids, with presumably a translation
18
 
 * table between small pids and actual process handles), but our test
19
 
 * environment, via Tcl, does not use the Cygnus environment.
20
 
 *
21
 
 * Compile this and install it as c:/tools/kill.exe (or as indicated
22
 
 * by build_win32/include.tcl ).
23
 
 */
24
 
 
25
 
#include <windows.h>
26
 
#include <stdio.h>
27
 
#include <stdlib.h>
28
 
#include <limits.h>
29
 
 
30
 
/*
31
 
 * Like atol, with specified base.  Would use stdlib, but
32
 
 * strtol("0xFFFF1234", NULL, 16) returns 0x7FFFFFFF and
33
 
 * strtol("4294712487", NULL, 16) returns 0x7FFFFFFF w/ VC++
34
 
 */
35
 
long
36
 
myatol(char *s, int base)
37
 
{
38
 
        long result = 0;
39
 
        char ch;
40
 
        int sign = 1;  /* + */
41
 
        if (base == 0)
42
 
                base = 10;
43
 
        if (base != 10 && base != 16)
44
 
                return LONG_MAX;
45
 
        while ((ch = *s++) != '\0') {
46
 
                if (ch == '-') {
47
 
                        sign = -sign;
48
 
                }
49
 
                else if (ch >= '0' && ch <= '9') {
50
 
                        result = result * base + (ch - '0');
51
 
                }
52
 
                else if (ch == 'x' || ch == 'X') {
53
 
                        /* Allow leading 0x..., and switch to base 16 */
54
 
                        base = 16;
55
 
                }
56
 
                else if (base == 16 && ch >= 'a' && ch <= 'f') {
57
 
                        result = result * base + (ch - 'a' + 10);
58
 
                }
59
 
                else if (base == 16 && ch >= 'A' && ch <= 'F') {
60
 
                        result = result * base + (ch - 'A' + 10);
61
 
                }
62
 
                else {
63
 
                        if (sign > 1)
64
 
                                return LONG_MAX;
65
 
                        else
66
 
                                return LONG_MIN;
67
 
                }
68
 
        }
69
 
        return sign * result;
70
 
}
71
 
 
72
 
void
73
 
usage_exit()
74
 
{
75
 
        fprintf(stderr, "Usage: kill [ -sig ] pid\n");
76
 
        fprintf(stderr, "       for win32, sig must be or 0, 15 (TERM)\n");
77
 
        exit(EXIT_FAILURE);
78
 
}
79
 
 
80
 
int
81
 
main(int argc, char **argv)
82
 
{
83
 
        HANDLE hProcess ;
84
 
        DWORD accessflag;
85
 
        long pid;
86
 
        int sig = 15;
87
 
 
88
 
        if (argc > 2) {
89
 
                if (argv[1][0] != '-')
90
 
                        usage_exit();
91
 
 
92
 
                if (strcmp(argv[1], "-TERM") == 0)
93
 
                        sig = 15;
94
 
                else {
95
 
                        /* currently sig is more or less ignored,
96
 
                         * we only care if it is zero or not
97
 
                         */
98
 
                        sig = atoi(&argv[1][1]);
99
 
                        if (sig < 0)
100
 
                                usage_exit();
101
 
                }
102
 
                argc--;
103
 
                argv++;
104
 
        }
105
 
        if (argc < 2)
106
 
                usage_exit();
107
 
 
108
 
        pid = myatol(argv[1], 10);
109
 
        /*printf("pid = %ld (0x%lx) (command line %s)\n", pid, pid, argv[1]);*/
110
 
        if (pid == LONG_MAX || pid == LONG_MIN)
111
 
                usage_exit();
112
 
 
113
 
        if (sig == 0)
114
 
                accessflag = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
115
 
        else
116
 
                accessflag = STANDARD_RIGHTS_REQUIRED | PROCESS_TERMINATE;
117
 
        hProcess = OpenProcess(accessflag, FALSE, pid);
118
 
        if (hProcess == NULL) {
119
 
                fprintf(stderr, "dbkill: %s: no such process\n", argv[1]);
120
 
                exit(EXIT_FAILURE);
121
 
        }
122
 
        if (sig == 0)
123
 
                exit(EXIT_SUCCESS);
124
 
        if (!TerminateProcess(hProcess, 99)) {
125
 
                DWORD err = GetLastError();
126
 
                fprintf(stderr,
127
 
                    "dbkill: cannot kill process: error %d (0x%lx)\n", err, err);
128
 
                exit(EXIT_FAILURE);
129
 
        }
130
 
        return EXIT_SUCCESS;
131
 
}