3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
18 #include <sys/cygwin.h>
22 static const char version[] = "$Revision$";
23 static char *prog_name;
25 static struct option longopts[] =
27 {"help", no_argument, NULL, 'h' },
28 {"list", optional_argument, NULL, 'l'},
29 {"force", no_argument, NULL, 'f'},
30 {"signal", required_argument, NULL, 's'},
31 {"version", no_argument, NULL, 'v'},
35 static char opts[] = "hl::fs:v";
38 usage (FILE *where = stderr)
41 "Usage: %s [-f] [-signal] [-s signal] pid1 [pid2 ...]\n"
43 "Send signals to processes\n"
45 " -f, --force force, using win32 interface if necessary\n"
46 " -l, --list print a list of signal names\n"
47 " -s, --signal send signal (use %s --list for a list)\n"
48 " -h, --help output usage information and exit\n"
49 " -v, --version output version information and exit\n"
50 "", prog_name, prog_name, prog_name);
51 exit (where == stderr ? 1 : 0);
57 const char *v = strchr (version, ':');
67 len = strchr (v, ' ') - v;
72 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.\n\
74 ", prog_name, len, v, __DATE__);
80 if (signo >= 0 && signo < NSIG)
81 return sys_sigabbrev[signo];
82 static char buf[sizeof ("Unknown signal") + 32];
83 sprintf (buf, "Unknown signal %d", signo);
88 getsig (const char *in_sig)
94 if (strncmp (in_sig, "SIG", 3) == 0)
98 sprintf (buf, "SIG%-.20s", in_sig);
101 intsig = strtosigno (sig) ?: atoi (in_sig);
103 if (!intsig && (strcmp (buf, "SIG0") != 0 && (strtol (in_sig, &p, 10) != 0 || *p)))
109 test_for_unknown_sig (int sig, const char *sigstr)
111 if (sig < 0 || sig > NSIG)
113 fprintf (stderr, "%s: unknown signal: %s\n", prog_name, sigstr);
120 listsig (const char *in_sig)
124 for (sig = 1; sig < NSIG - 1; sig++)
125 printf ("%s%c", strsigno (sig) + 3, (sig < NSIG - 1) ? ' ' : '\n');
128 sig = getsig (in_sig);
129 test_for_unknown_sig (sig, in_sig);
130 if (atoi (in_sig) == sig)
131 puts (strsigno (sig) + 3);
133 printf ("%d\n", sig);
138 get_debug_priv (void)
142 TOKEN_PRIVILEGES tkp;
144 if (!OpenProcessToken (GetCurrentProcess (),
145 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tok))
148 if (!LookupPrivilegeValue (NULL, SE_DEBUG_NAME, &luid))
154 tkp.PrivilegeCount = 1;
155 tkp.Privileges[0].Luid = luid;
156 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
158 AdjustTokenPrivileges (tok, FALSE, &tkp, sizeof tkp, NULL, NULL);
163 static void __stdcall
164 forcekill (int pid, int sig, int wait)
166 // try to acquire SeDebugPrivilege
169 external_pinfo *p = NULL;
170 /* cygwin_internal misinterprets negative pids (Win9x pids) */
172 p = (external_pinfo *) cygwin_internal (CW_GETPINFO_FULL, pid);
173 DWORD dwpid = p ? p->dwProcessId : (DWORD) pid;
174 HANDLE h = OpenProcess (PROCESS_TERMINATE, FALSE, (DWORD) dwpid);
177 if (!wait || GetLastError () != ERROR_INVALID_PARAMETER)
178 fprintf (stderr, "%s: couldn't open pid %u\n",
179 prog_name, (unsigned) dwpid);
182 if (!wait || WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
183 if (sig && !TerminateProcess (h, sig << 8)
184 && WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
185 fprintf (stderr, "%s: couldn't kill pid %u, %lu\n",
186 prog_name, (unsigned) dwpid, GetLastError ());
191 main (int argc, char **argv)
196 char *gotasig = NULL;
198 prog_name = strrchr (argv[0], '/');
199 if (prog_name == NULL)
200 prog_name = strrchr (argv[0], '\\');
201 if (prog_name == NULL)
212 long long int pid = 0;
217 char **av = argv + optind;
218 if ((ch = getopt_long (argc, argv, opts, longopts, NULL)) == EOF)
224 sig = getsig (gotasig);
229 optarg = argv[optind];
256 optind = 1 + av - argv;
258 sig = getsig (gotasig);
267 test_for_unknown_sig (sig, gotasig);
270 while (*argv != NULL)
273 pid = strtoll (*argv, &p, 10);
275 || (!force && (pid < LONG_MIN || pid > LONG_MAX))
276 || (force && (pid <= 0 || pid > ULONG_MAX)))
278 fprintf (stderr, "%s: illegal pid: %s\n", prog_name, *argv);
281 else if (pid <= LONG_MAX && kill ((pid_t) pid, sig) == 0)
284 forcekill ((pid_t) pid, sig, 1);
287 forcekill ((pid_t) pid, sig, 0);
291 sprintf (buf, "%s: %lld", prog_name, pid);