killall.c revision c0e56edaf256adb6c60c5a052525a1ffbb927901
1ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley/* vi: set sw=4 ts=4: 2ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley * 3f42e11b6712f7bd17d1ee2e548f54a2f6d9aed46Rob Landley * killall.c - Send signal (default: TERM) to all processes with given names. 4ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley * 5ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley * Copyright 2012 Andreas Heck <aheck@gmx.de> 6ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley * 7f91b7c89bc852868692b9518185421ebb52d67b3Rob Landley * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/killall.html 8ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 9f42e11b6712f7bd17d1ee2e548f54a2f6d9aed46Rob LandleyUSE_KILLALL(NEWTOY(killall, "<1?lq", TOYFLAG_USR|TOYFLAG_BIN)) 10ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 11ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landleyconfig KILLALL 12ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley bool "killall" 13ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley default y 14ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley help 15ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley usage: killall [-l] [-q] [-SIG] PROCESS_NAME... 16ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 17ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley Send a signal (default: TERM) to all processes with the given names. 18ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 19ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley -l print list of all available signals 20ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley -q don't print any warnings or error messages 21ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley*/ 22ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 23c0e56edaf256adb6c60c5a052525a1ffbb927901Rob Landley#define FOR_killall 24ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley#include "toys.h" 25ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 26c0e56edaf256adb6c60c5a052525a1ffbb927901Rob LandleyGLOBALS( 27f42e11b6712f7bd17d1ee2e548f54a2f6d9aed46Rob Landley int signum; 28ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley) 29ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 30f42e11b6712f7bd17d1ee2e548f54a2f6d9aed46Rob Landleystatic void kill_process(pid_t pid) 31f42e11b6712f7bd17d1ee2e548f54a2f6d9aed46Rob Landley{ 32ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley int ret; 33ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 344696bfc4057e87ea8a66bd64aafb9ca14a64290eRob Landley toys.exitval = 0; 35ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley ret = kill(pid, TT.signum); 36ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 37f42e11b6712f7bd17d1ee2e548f54a2f6d9aed46Rob Landley if (ret == -1 && !(toys.optflags & FLAG_q)) perror("kill"); 38ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley} 39ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 40ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landleyvoid killall_main(void) 41ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley{ 42ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley char **names; 43ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 44ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley if (toys.optflags & FLAG_l) { 454696bfc4057e87ea8a66bd64aafb9ca14a64290eRob Landley sig_to_num(NULL); 46f42e11b6712f7bd17d1ee2e548f54a2f6d9aed46Rob Landley return; 47ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley } 48ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 494696bfc4057e87ea8a66bd64aafb9ca14a64290eRob Landley TT.signum = SIGTERM; 504696bfc4057e87ea8a66bd64aafb9ca14a64290eRob Landley toys.exitval++; 514696bfc4057e87ea8a66bd64aafb9ca14a64290eRob Landley 52ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley if (!*toys.optargs) { 53ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley toys.exithelp = 1; 54ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley error_exit("Process name missing!"); 55ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley } 56ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 57ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley names = toys.optargs; 58ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 59f42e11b6712f7bd17d1ee2e548f54a2f6d9aed46Rob Landley if (**names == '-') { 604696bfc4057e87ea8a66bd64aafb9ca14a64290eRob Landley if (0 > (TT.signum = sig_to_num((*names)+1))) { 614696bfc4057e87ea8a66bd64aafb9ca14a64290eRob Landley if (toys.optflags & FLAG_q) exit(1); 624696bfc4057e87ea8a66bd64aafb9ca14a64290eRob Landley error_exit("Invalid signal"); 63ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley } 64f42e11b6712f7bd17d1ee2e548f54a2f6d9aed46Rob Landley names++; 65ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 664696bfc4057e87ea8a66bd64aafb9ca14a64290eRob Landley if (!*names) { 674696bfc4057e87ea8a66bd64aafb9ca14a64290eRob Landley toys.exithelp++; 684696bfc4057e87ea8a66bd64aafb9ca14a64290eRob Landley error_exit("Process name missing!"); 694696bfc4057e87ea8a66bd64aafb9ca14a64290eRob Landley } 70ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley } 71ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 72ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley for_each_pid_with_name_in(names, kill_process); 73ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley 744696bfc4057e87ea8a66bd64aafb9ca14a64290eRob Landley if (toys.exitval && !(toys.optflags & FLAG_q)) 754696bfc4057e87ea8a66bd64aafb9ca14a64290eRob Landley error_exit("No such process"); 76ff9ee8fc15e1a41bffe06bfcee30368e7c117601Rob Landley} 77