2018-11-06 09:46:40 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
2018-12-03 23:27:16 +00:00
|
|
|
#include <AK/AKString.h>
|
2018-10-31 00:06:57 +00:00
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
static unsigned parse_uint(const String& str, bool& ok)
|
2018-10-31 00:06:57 +00:00
|
|
|
{
|
2018-12-21 01:10:45 +00:00
|
|
|
if (str.is_empty()) {
|
2018-11-08 01:07:08 +00:00
|
|
|
ok = false;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-10-31 00:06:57 +00:00
|
|
|
unsigned value = 0;
|
2019-02-25 21:06:55 +00:00
|
|
|
for (int i = 0; i < str.length(); ++i) {
|
2018-10-31 00:06:57 +00:00
|
|
|
if (str[i] < '0' || str[i] > '9') {
|
|
|
|
ok = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
value = value * 10;
|
2019-02-25 21:06:55 +00:00
|
|
|
value += (unsigned)(str[i] - '0');
|
2018-10-31 00:06:57 +00:00
|
|
|
}
|
|
|
|
ok = true;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2018-11-06 09:46:40 +00:00
|
|
|
static void print_usage_and_exit()
|
|
|
|
{
|
|
|
|
printf("usage: kill [-signal] <PID>\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2018-10-31 00:06:57 +00:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2018-11-06 09:46:40 +00:00
|
|
|
if (argc != 2 && argc != 3)
|
|
|
|
print_usage_and_exit();
|
2018-10-31 00:06:57 +00:00
|
|
|
bool ok;
|
2018-11-06 09:46:40 +00:00
|
|
|
unsigned signum = SIGTERM;
|
|
|
|
int pid_argi = 1;
|
|
|
|
if (argc == 3) {
|
|
|
|
pid_argi = 2;
|
|
|
|
if (argv[1][0] != '-')
|
|
|
|
print_usage_and_exit();
|
2019-01-31 16:31:23 +00:00
|
|
|
signum = parse_uint(&argv[1][1], ok);
|
2018-11-06 09:46:40 +00:00
|
|
|
if (!ok) {
|
2018-11-08 01:07:08 +00:00
|
|
|
printf("'%s' is not a valid signal number\n", &argv[1][1]);
|
2018-11-06 09:46:40 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
2019-01-31 16:31:23 +00:00
|
|
|
unsigned pid = parse_uint(argv[pid_argi], ok);
|
2018-10-31 00:06:57 +00:00
|
|
|
if (!ok) {
|
2018-11-08 01:07:08 +00:00
|
|
|
printf("'%s' is not a valid PID\n", argv[pid_argi]);
|
2018-11-06 09:46:40 +00:00
|
|
|
return 3;
|
2018-10-31 00:06:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 10:45:45 +00:00
|
|
|
int rc = kill((pid_t)pid, signum);
|
|
|
|
if (rc < 0)
|
|
|
|
perror("kill");
|
2018-10-31 00:06:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|