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>
16 #include <afsconfig.h>
31 /* Define NULL-terminated array of signal name/number pairs */
37 static signal_map_t signalMap[] = {
56 static void PrintSignalList(void)
60 for (i = 0; signalMap[i].name; i++) {
61 printf("%s ", signalMap[i].name);
66 static int SignalArgToNumber(const char *sigargp)
72 /* check for numeric signal arg */
74 signo = strtol(sigargp, &endp, 0);
76 if (errno == 0 && *endp == '\0') {
80 /* check for named signal arg */
81 for (i = 0; signalMap[i].name; i++) {
82 if (!strcasecmp(signalMap[i].name, sigargp)) {
83 return signalMap[i].number;
87 return -1; /* invalid signal argument */
91 static void PrintUsage(const char *whoami)
93 printf("usage: %s [-signal] [pid] ...\n", whoami);
94 printf(" %s -l\n", whoami);
99 int main(int argc, char *argv[])
104 /* set whoami to last component of argv[0] */
105 if ((whoami = strrchr(argv[0], '/')) != NULL) {
107 } else if ((whoami = strrchr(argv[0], '\\')) != NULL) {
113 /* parse command arguments */
115 /* no arguments; print usage statement */
118 } else if (!strcmp(argv[1], "-l")) {
119 /* request to print a list of valid signals */
123 /* request to send a signal */
125 int signo = SIGTERM; /* SIGTERM is default if no signal specified */
127 for (i = 1; i < argc; i++) {
128 char *curargp = argv[i];
130 if (i == 1 && *curargp == '-') {
131 /* signal argument specified; translate to a number */
132 curargp++; /* advance past signal flag indicator ('-') */
134 if ((signo = SignalArgToNumber(curargp)) == -1) {
135 printf("%s: unknown signal %s.\n", whoami, curargp);
141 /* pid specified; send signal */
146 pid = strtol(curargp, &endp, 0);
148 if (errno != 0 || *endp != '\0' || pid <= 0) {
149 printf("%s: invalid pid value %s.\n", whoami, curargp);
152 if (kill((pid_t)pid, signo)) {
153 if (errno == EINVAL) {
154 printf("%s: invalid signal number %d.\n",
159 } else if (errno == ESRCH) {
160 printf("%s: no such process %d.\n",
164 } else if (errno == EPERM) {
165 printf("%s: no privilege to signal %d.\n",
170 printf("%s: failed to signal %d (errno = %d).\n",
171 whoami, (int)pid, (int)errno);