From ca4035bdacfd83f815323c9c597f2683fc4aa218 Mon Sep 17 00:00:00 2001 From: Elie De Brauwer Date: Sun, 16 Dec 2012 13:43:36 +0100 Subject: Extend killall with support for -v and -i --- lib/lib.c | 6 +++--- lib/lib.h | 2 +- toys/lsb/killall.c | 17 ++++++++++++++--- toys/lsb/pidof.c | 2 +- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index 148a7652..5b591d44 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -158,7 +158,7 @@ void xprintf(char *format, ...) void xputs(char *s) { - if (EOF == puts(s)) perror_exit("write"); + if (EOF == puts(s) || fflush(stdout)) perror_exit("write"); } void xputc(char c) @@ -975,7 +975,7 @@ int yesno(char *prompt, int def) } // Execute a callback for each PID that matches a process name from a list. -void for_each_pid_with_name_in(char **names, int (*callback)(pid_t pid)) +void for_each_pid_with_name_in(char **names, int (*callback)(pid_t pid, char *name)) { DIR *dp; struct dirent *entry; @@ -999,7 +999,7 @@ void for_each_pid_with_name_in(char **names, int (*callback)(pid_t pid)) for (curname = names; *curname; curname++) if (!strcmp(basename(cmd), *curname)) - if (!callback(atol(entry->d_name))) goto done; + if (!callback(atol(entry->d_name), *curname)) goto done; } done: closedir(dp); diff --git a/lib/lib.h b/lib/lib.h index 7cc4d5ca..17aaea25 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -144,7 +144,7 @@ void replace_tempfile(int fdin, int fdout, char **tempname); void crc_init(unsigned int *crc_table, int little_endian); void terminal_size(unsigned *x, unsigned *y); int yesno(char *prompt, int def); -void for_each_pid_with_name_in(char **names, int (*callback)(pid_t pid)); +void for_each_pid_with_name_in(char **names, int (*callback)(pid_t pid, char *name)); unsigned long xstrtoul(const char *nptr, char **endptr, int base); // getmountlist.c diff --git a/toys/lsb/killall.c b/toys/lsb/killall.c index 2294f61d..655ed734 100644 --- a/toys/lsb/killall.c +++ b/toys/lsb/killall.c @@ -1,20 +1,23 @@ + /* killall.c - Send signal (default: TERM) to all processes with given names. * * Copyright 2012 Andreas Heck * * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/killall.html -USE_KILLALL(NEWTOY(killall, "<1?lq", TOYFLAG_USR|TOYFLAG_BIN)) +USE_KILLALL(NEWTOY(killall, "<1?lqvi", TOYFLAG_USR|TOYFLAG_BIN)) config KILLALL bool "killall" default y help - usage: killall [-l] [-q] [-SIG] PROCESS_NAME... + usage: killall [-l] [-qv] [-SIG] PROCESS_NAME... Send a signal (default: TERM) to all processes with the given names. -l print list of all available signals + -i ask for confirmation before killing + -v report if the signal was successfully sent -q don't print any warnings or error messages */ @@ -25,12 +28,20 @@ GLOBALS( int signum; ) -static int kill_process(pid_t pid) +static int kill_process(pid_t pid, char *name) { int ret; + if(toys.optflags & FLAG_i) { + snprintf(toybuf, sizeof(toybuf), "Signal %s(%d) ?", name, pid); + if (yesno(toybuf, 0) == 0) return 1; + } + toys.exitval = 0; + ret = kill(pid, TT.signum); + if (toys.optflags & FLAG_v) + printf("Killed %s(%d) with signal %d\n", name, pid, TT.signum); if (ret == -1 && !(toys.optflags & FLAG_q)) perror("kill"); return 1; diff --git a/toys/lsb/pidof.c b/toys/lsb/pidof.c index 533e922a..7285b6ae 100644 --- a/toys/lsb/pidof.c +++ b/toys/lsb/pidof.c @@ -25,7 +25,7 @@ GLOBALS( char *omit; ) -static int print_pid(pid_t pid) +static int print_pid(pid_t pid, char * name) { char * res; int len; -- cgit v1.2.3