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>
30 /* Define NULL-terminated array of signal name/number pairs */
36 static signal_map_t signalMap[] = {
60 for (i = 0; signalMap[i].name; i++) {
61 printf("%s ", signalMap[i].name);
67 SignalArgToNumber(const char *sigargp)
73 /* check for numeric signal arg */
75 signo = strtol(sigargp, &endp, 0);
77 if (errno == 0 && *endp == '\0') {
81 /* check for named signal arg */
82 for (i = 0; signalMap[i].name; i++) {
83 if (!strcasecmp(signalMap[i].name, sigargp)) {
84 return signalMap[i].number;
88 return -1; /* invalid signal argument */
93 PrintUsage(const char *whoami)
95 printf("usage: %s [-signal] [pid] ...\n", whoami);
96 printf(" %s -l\n", whoami);
102 main(int argc, char *argv[])
107 /* set whoami to last component of argv[0] */
108 if ((whoami = strrchr(argv[0], '/')) != NULL) {
110 } else if ((whoami = strrchr(argv[0], '\\')) != NULL) {
116 /* parse command arguments */
118 /* no arguments; print usage statement */
121 } else if (!strcmp(argv[1], "-l")) {
122 /* request to print a list of valid signals */
126 /* request to send a signal */
128 int signo = SIGTERM; /* SIGTERM is default if no signal specified */
130 for (i = 1; i < argc; i++) {
131 char *curargp = argv[i];
133 if (i == 1 && *curargp == '-') {
134 /* signal argument specified; translate to a number */
135 curargp++; /* advance past signal flag indicator ('-') */
137 if ((signo = SignalArgToNumber(curargp)) == -1) {
138 printf("%s: unknown signal %s.\n", whoami, curargp);
144 /* pid specified; send signal */
149 pid = strtol(curargp, &endp, 0);
151 if (errno != 0 || *endp != '\0' || pid <= 0) {
152 printf("%s: invalid pid value %s.\n", whoami, curargp);
155 if (kill((pid_t) pid, signo)) {
156 if (errno == EINVAL) {
157 printf("%s: invalid signal number %d.\n", whoami,
162 } else if (errno == ESRCH) {
163 printf("%s: no such process %d.\n", whoami,
167 } else if (errno == EPERM) {
168 printf("%s: no privilege to signal %d.\n", whoami,
173 printf("%s: failed to signal %d (errno = %d).\n",
174 whoami, (int)pid, (int)errno);