Skip to content

Commit

Permalink
kill: add signum argument
Browse files Browse the repository at this point in the history
  • Loading branch information
mosmeh committed Aug 31, 2024
1 parent 57c66ea commit 4fdf95c
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions userland/kill.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char* argv[]) {
if (argc != 2) {
dprintf(STDERR_FILENO, "Usage: kill PID\n");
return EXIT_FAILURE;
static void usage(void) {
dprintf(STDERR_FILENO, "Usage: kill [-SIGNUM] PID\n");
exit(EXIT_FAILURE);
}

static int parse_signum(const char* s) {
if (str_is_uint(s))
return atoi(s);
for (int i = 0; i < NSIG; ++i) {
if (!strcmp(s, sys_signame[i]))
return i;
}
const char* pid_str = argv[1];
if (!str_is_uint(pid_str)) {
dprintf(STDERR_FILENO, "Illegal pid: %s\n", pid_str);
return EXIT_FAILURE;
return -1;
}

int main(int argc, char* argv[]) {
if (argc < 2)
usage();

pid_t pid = -1;
int signum = SIGTERM;
for (int i = 1; i < argc; ++i) {
const char* arg = argv[i];
if (arg[0] == '-') {
signum = parse_signum(arg + 1);
if (signum < 0 || signum >= NSIG)
usage();
} else if (str_is_uint(arg)) {
pid = atoi(arg);
} else {
usage();
}
}
pid_t pid = atoi(pid_str);
if (kill(pid, SIGTERM) < 0) {
if (pid < 0)
usage();

if (kill(pid, signum) < 0) {
perror("kill");
return EXIT_FAILURE;
}
Expand Down

0 comments on commit 4fdf95c

Please sign in to comment.