2019-09-06 15:34:26 +02:00
|
|
|
#include <AK/String.h>
|
2018-11-06 10:46:40 +01:00
|
|
|
#include <signal.h>
|
2019-06-07 11:49:31 +02:00
|
|
|
#include <stdio.h>
|
2018-11-06 10:46:40 +01:00
|
|
|
#include <stdlib.h>
|
2019-06-07 11:49:31 +02:00
|
|
|
#include <unistd.h>
|
2018-10-31 01:06:57 +01:00
|
|
|
|
2018-11-06 10:46:40 +01:00
|
|
|
static void print_usage_and_exit()
|
|
|
|
{
|
|
|
|
printf("usage: kill [-signal] <PID>\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2018-10-31 01:06:57 +01:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2018-11-06 10:46:40 +01:00
|
|
|
if (argc != 2 && argc != 3)
|
|
|
|
print_usage_and_exit();
|
2018-10-31 01:06:57 +01:00
|
|
|
bool ok;
|
2018-11-06 10:46:40 +01:00
|
|
|
unsigned signum = SIGTERM;
|
|
|
|
int pid_argi = 1;
|
|
|
|
if (argc == 3) {
|
|
|
|
pid_argi = 2;
|
|
|
|
if (argv[1][0] != '-')
|
|
|
|
print_usage_and_exit();
|
2019-05-08 19:21:51 +02:00
|
|
|
signum = String(&argv[1][1]).to_uint(ok);
|
2018-11-06 10:46:40 +01:00
|
|
|
if (!ok) {
|
2018-11-08 02:07:08 +01:00
|
|
|
printf("'%s' is not a valid signal number\n", &argv[1][1]);
|
2018-11-06 10:46:40 +01:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
2019-05-08 19:21:51 +02:00
|
|
|
unsigned pid = String(argv[pid_argi]).to_uint(ok);
|
2018-10-31 01:06:57 +01:00
|
|
|
if (!ok) {
|
2018-11-08 02:07:08 +01:00
|
|
|
printf("'%s' is not a valid PID\n", argv[pid_argi]);
|
2018-11-06 10:46:40 +01:00
|
|
|
return 3;
|
2018-10-31 01:06:57 +01:00
|
|
|
}
|
|
|
|
|
2019-02-28 11:45:45 +01:00
|
|
|
int rc = kill((pid_t)pid, signum);
|
|
|
|
if (rc < 0)
|
|
|
|
perror("kill");
|
2018-10-31 01:06:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|