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 <afs/param.h>
27 /* Define NULL-terminated array of signal name/number pairs */
33 static signal_map_t signalMap[] = {
52 static void PrintSignalList(void)
56 for (i = 0; signalMap[i].name; i++) {
57 printf("%s ", signalMap[i].name);
62 static int SignalArgToNumber(const char *sigargp)
68 /* check for numeric signal arg */
70 signo = strtol(sigargp, &endp, 0);
72 if (errno == 0 && *endp == '\0') {
76 /* check for named signal arg */
77 for (i = 0; signalMap[i].name; i++) {
78 if (!strcasecmp(signalMap[i].name, sigargp)) {
79 return signalMap[i].number;
83 return -1; /* invalid signal argument */
87 static void PrintUsage(const char *whoami)
89 printf("usage: %s [-signal] [pid] ...\n", whoami);
90 printf(" %s -l\n", whoami);
95 int 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",
155 } else if (errno == ESRCH) {
156 printf("%s: no such process %d.\n",
160 } else if (errno == EPERM) {
161 printf("%s: no privilege to signal %d.\n",
166 printf("%s: failed to signal %d (errno = %d).\n",
167 whoami, (int)pid, (int)errno);