2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* Kill command based on process management library (procmgmt.h).
11 * Intended for use on Windows NT, where it will only signal processes
12 * linked with the process management library (except SIGKILL).
15 #include <afsconfig.h>
16 #include <afs/param.h>
23 /* Define NULL-terminated array of signal name/number pairs */
29 static signal_map_t signalMap
[] = {
53 for (i
= 0; signalMap
[i
].name
; i
++) {
54 printf("%s ", signalMap
[i
].name
);
60 SignalArgToNumber(const char *sigargp
)
66 /* check for numeric signal arg */
68 signo
= strtol(sigargp
, &endp
, 0);
70 if (errno
== 0 && *endp
== '\0') {
74 /* check for named signal arg */
75 for (i
= 0; signalMap
[i
].name
; i
++) {
76 if (!strcasecmp(signalMap
[i
].name
, sigargp
)) {
77 return signalMap
[i
].number
;
81 return -1; /* invalid signal argument */
86 PrintUsage(const char *whoami
)
88 printf("usage: %s [-signal] [pid] ...\n", whoami
);
89 printf(" %s -l\n", whoami
);
95 main(int argc
, char *argv
[])
100 /* set whoami to last component of argv[0] */
101 if ((whoami
= strrchr(argv
[0], '/')) != NULL
) {
103 } else if ((whoami
= strrchr(argv
[0], '\\')) != NULL
) {
109 /* parse command arguments */
111 /* no arguments; print usage statement */
114 } else if (!strcmp(argv
[1], "-l")) {
115 /* request to print a list of valid signals */
119 /* request to send a signal */
121 int signo
= SIGTERM
; /* SIGTERM is default if no signal specified */
123 for (i
= 1; i
< argc
; i
++) {
124 char *curargp
= argv
[i
];
126 if (i
== 1 && *curargp
== '-') {
127 /* signal argument specified; translate to a number */
128 curargp
++; /* advance past signal flag indicator ('-') */
130 if ((signo
= SignalArgToNumber(curargp
)) == -1) {
131 printf("%s: unknown signal %s.\n", whoami
, curargp
);
137 /* pid specified; send signal */
142 pid
= strtol(curargp
, &endp
, 0);
144 if (errno
!= 0 || *endp
!= '\0' || pid
<= 0) {
145 printf("%s: invalid pid value %s.\n", whoami
, curargp
);
148 if (kill((pid_t
) pid
, signo
)) {
149 if (errno
== EINVAL
) {
150 printf("%s: invalid signal number %d.\n", whoami
,
155 } else if (errno
== ESRCH
) {
156 printf("%s: no such process %d.\n", whoami
,
160 } else if (errno
== EPERM
) {
161 printf("%s: no privilege to signal %d.\n", whoami
,
166 printf("%s: failed to signal %d (errno = %d).\n",
167 whoami
, (int)pid
, (int)errno
);