static int
ask (char *name, pid_t pid, const int signal)
{
- int res;
- size_t len;
- char *line;
-
- line = NULL;
- len = 0;
-
- do {
- if (signal == SIGTERM)
- printf (_("Kill %s(%s%d) ? (y/N) "), name, process_group ? "pgid " : "",
- pid);
- else
- printf (_("Signal %s(%s%d) ? (y/N) "), name, process_group ? "pgid " : "",
- pid);
-
- fflush (stdout);
-
- if (getline (&line, &len, stdin) < 0)
- return 0;
- /* Check for default */
- if (line[0] == '\n') {
- free(line);
- return 0;
- }
- res = rpmatch(line);
- if (res >= 0) {
- free(line);
- return res;
- }
- } while(1);
- /* Never should get here */
+ int res;
+ size_t len;
+ char *line;
+
+ line = NULL;
+ len = 0;
+
+ do {
+ if (signal == SIGTERM)
+ printf (_("Kill %s(%s%d) ? (y/N) "), name, process_group ? "pgid " : "",
+ pid);
+ else
+ printf (_("Signal %s(%s%d) ? (y/N) "), name, process_group ? "pgid " : "",
+ pid);
+
+ fflush (stdout);
+
+ if (getline (&line, &len, stdin) < 0)
+ return 0;
+ /* Check for default */
+ if (line[0] == '\n') {
+ free(line);
+ return 0;
+ }
+ res = rpmatch(line);
+ if (res >= 0) {
+ free(line);
+ return res;
+ }
+ } while(1);
+ /* Never should get here */
}
static double
uptime()
{
- char * savelocale;
- char buf[2048];
- FILE* file;
- if (!(file=fopen( PROC_BASE "/uptime", "r"))) {
- fprintf(stderr, "killall: error opening uptime file\n");
- exit(1);
- }
- savelocale = setlocale(LC_NUMERIC,"C");
- if (fscanf(file, "%2047s", buf) == EOF) perror("uptime");
- fclose(file);
- setlocale(LC_NUMERIC,savelocale);
- return atof(buf);
+ char * savelocale;
+ char buf[2048];
+ FILE* file;
+ if (!(file=fopen( PROC_BASE "/uptime", "r"))) {
+ fprintf(stderr, "killall: error opening uptime file\n");
+ exit(1);
+ }
+ savelocale = setlocale(LC_NUMERIC,"C");
+ if (fscanf(file, "%2047s", buf) == EOF) perror("uptime");
+ fclose(file);
+ setlocale(LC_NUMERIC,savelocale);
+ return atof(buf);
}
/* process age from jiffies to seconds via uptime */
static double process_age(const unsigned long long jf)
{
- double age;
- double sc_clk_tck = sysconf(_SC_CLK_TCK);
- assert(sc_clk_tck > 0);
- age = uptime() - jf / sc_clk_tck;
- if (age < 0L)
- return 0L;
- return age;
+ double age;
+ double sc_clk_tck = sysconf(_SC_CLK_TCK);
+ assert(sc_clk_tck > 0);
+ age = uptime() - jf / sc_clk_tck;
+ if (age < 0L)
+ return 0L;
+ return age;
}
/* returns requested time interval in seconds,
- negative indicates error has occurred
- */
+ negative indicates error has occurred
+ */
static long
parse_time_units(const char* age)
{
- char *unit;
- long num;
-
- num = strtol(age,&unit,10);
- if (age == unit) /* no digits found */
- return -1;
- if (unit[0] == '\0') /* no units found */
- return -1;
-
- switch(unit[0]) {
- case 's':
- return num;
- case 'm':
- return (num * 60);
- case 'h':
- return (num * 60 * 60);
- case 'd':
- return (num * 60 * 60 * 24);
- case 'w':
- return (num * 60 * 60 * 24 * 7);
- case 'M':
- return (num * 60 * 60 * 24 * 7 * 4);
- case 'y':
- return (num * 60 * 60 * 24 * 7 * 4 * 12);
- }
- return -1;
+ char *unit;
+ long num;
+
+ num = strtol(age,&unit,10);
+ if (age == unit) /* no digits found */
+ return -1;
+ if (unit[0] == '\0') /* no units found */
+ return -1;
+
+ switch(unit[0]) {
+ case 's':
+ return num;
+ case 'm':
+ return (num * 60);
+ case 'h':
+ return (num * 60 * 60);
+ case 'd':
+ return (num * 60 * 60 * 24);
+ case 'w':
+ return (num * 60 * 60 * 24 * 7);
+ case 'M':
+ return (num * 60 * 60 * 24 * 7 * 4);
+ case 'y':
+ return (num * 60 * 60 * 24 * 7 * 4 * 12);
+ }
+ return -1;
}
enum ns_type {
}
static int get_ns(pid_t pid, int id) {
- struct stat st;
- char buff[50];
- snprintf(buff, sizeof(buff), "/proc/%i/ns/%s", pid, get_ns_name(id));
- if (stat(buff, &st))
- return 0;
- else
- return st.st_ino;
+ struct stat st;
+ char buff[50];
+ snprintf(buff, sizeof(buff), "/proc/%i/ns/%s", pid, get_ns_name(id));
+ if (stat(buff, &st))
+ return 0;
+ else
+ return st.st_ino;
}
static int
match_process_uid(pid_t pid, uid_t uid)
{
- char buf[128];
- uid_t puid;
- FILE *f;
- int re = -1;
-
- snprintf (buf, sizeof buf, PROC_BASE "/%d/status", pid);
- if (!(f = fopen (buf, "r")))
- return 0;
-
- while (fgets(buf, sizeof buf, f))
- {
- if (sscanf (buf, "Uid:\t%d", &puid))
- {
- re = uid==puid;
- break;
- }
- }
- fclose(f);
- if (re==-1)
- {
- fprintf(stderr, _("killall: Cannot get UID from process status\n"));
- exit(1);
- }
- return re;
+ char buf[128];
+ uid_t puid;
+ FILE *f;
+ int re = -1;
+
+ snprintf (buf, sizeof buf, PROC_BASE "/%d/status", pid);
+ if (!(f = fopen (buf, "r")))
+ return 0;
+
+ while (fgets(buf, sizeof buf, f))
+ {
+ if (sscanf (buf, "Uid:\t%d", &puid))
+ {
+ re = uid==puid;
+ break;
+ }
+ }
+ fclose(f);
+ if (re==-1)
+ {
+ fprintf(stderr, _("killall: Cannot get UID from process status\n"));
+ exit(1);
+ }
+ return re;
}
static void
free_regexp_list(regex_t *reglist, int names)
{
- int i;
- for (i = 0; i < names; i++)
- regfree(®list[i]);
- free(reglist);
+ int i;
+ for (i = 0; i < names; i++)
+ regfree(®list[i]);
+ free(reglist);
}
static regex_t *
build_regexp_list(int names, char **namelist)
{
- int i;
- regex_t *reglist;
- int flag = REG_EXTENDED|REG_NOSUB;
-
- if (!(reglist = malloc (sizeof (regex_t) * names)))
- {
- perror ("malloc");
- exit (1);
- }
-
- if (ignore_case)
- flag |= REG_ICASE;
-
- for (i = 0; i < names; i++)
- {
- if (regcomp(®list[i], namelist[i], flag) != 0)
- {
- fprintf(stderr, _("killall: Bad regular expression: %s\n"), namelist[i]);
- free_regexp_list(reglist, i);
- exit (1);
- }
- }
- return reglist;
+ int i;
+ regex_t *reglist;
+ int flag = REG_EXTENDED|REG_NOSUB;
+
+ if (!(reglist = malloc (sizeof (regex_t) * names)))
+ {
+ perror ("malloc");
+ exit (1);
+ }
+
+ if (ignore_case)
+ flag |= REG_ICASE;
+
+ for (i = 0; i < names; i++)
+ {
+ if (regcomp(®list[i], namelist[i], flag) != 0)
+ {
+ fprintf(stderr, _("killall: Bad regular expression: %s\n"), namelist[i]);
+ free_regexp_list(reglist, i);
+ exit (1);
+ }
+ }
+ return reglist;
}
static NAMEINFO *
int i;
NAMEINFO *ni = NULL;
if ( (ni = malloc(sizeof(NAMEINFO) * names)) == NULL)
- return NULL;
+ return NULL;
for (i = 0; i < names; i++)
{
- ni[i].name = namelist[i];
- ni[i].st.st_dev = 0;
- if (!strchr (namelist[i], '/'))
- {
- ni[i].name_length = strlen (namelist[i]);
- }
- else if (stat (namelist[i], &(ni[i].st)) < 0)
- {
- perror (namelist[i]);
- free(ni);
- return NULL;
- }
+ ni[i].name = namelist[i];
+ ni[i].st.st_dev = 0;
+ if (!strchr (namelist[i], '/'))
+ {
+ ni[i].name_length = strlen (namelist[i]);
+ }
+ else if (stat (namelist[i], &(ni[i].st)) < 0)
+ {
+ perror (namelist[i]);
+ free(ni);
+ return NULL;
+ }
}
return ni;
}
static int
load_process_name_and_age(char *comm, double *process_age_sec,
- const pid_t pid, int load_age)
+ const pid_t pid, int load_age)
{
FILE *file;
char *path;
*process_age_sec = 0;
if (asprintf (&path, PROC_BASE "/%d/stat", pid) < 0)
- return -1;
+ return -1;
if (!(file = fopen (path, "r")))
{
- free(path);
- return -1;
+ free(path);
+ return -1;
}
free (path);
if (fgets(buf, 1024, file) == NULL)
startcomm = strchr(buf, '(') + 1;
endcomm = strrchr(startcomm, ')');
lencomm = endcomm - startcomm;
- if (lencomm > 15)
- lencomm = 15;
+ if (lencomm < 0)
+ lencomm = 0;
+ if (lencomm > sizeof comm -1)
+ lencomm = sizeof comm -1;
strncpy(comm, startcomm, lencomm);
comm[lencomm] = '\0';
int okay;
if (asprintf (&path, PROC_BASE "/%d/cmdline", pid) < 0)
- return -1;
+ return -1;
if (!(file = fopen (path, "r")))
{
- free (path);
- return -1;
+ free (path);
+ return -1;
}
free(path);
if ( (command_buf = (char *)malloc (cmd_size)) == NULL)
- exit(1);
+ exit(1);
while (1)
{
- /* look for actual command so we skip over initial "sh" if any */
+ /* look for actual command so we skip over initial "sh" if any */
/* 'cmdline' has arguments separated by nulls */
for (p=command_buf; ; p++)
- {
- int c;
- if (p == (command_buf + cmd_size))
- {
- char *new_command_buf;
- int cur_size = cmd_size;
- cmd_size *= 2;
- new_command_buf = (char *)realloc(command_buf, cmd_size);
- if (!new_command_buf) {
- if (command_buf)
- free(command_buf);
- exit (1);
- }
- command_buf = new_command_buf;
- p = command_buf + cur_size;
- }
- c = fgetc(file);
- if (c == EOF || c == '\0')
- {
- *p = '\0';
- break;
- } else {
- *p = c;
- }
- }
- if (strlen(command_buf) == 0) {
- okay = 0;
- break;
- }
- p = strrchr(command_buf,'/');
- p = p ? p+1 : command_buf;
- if (strncmp(p, comm, COMM_LEN-1) == 0) {
- okay = 1;
- if (!(*command = strdup(p))) {
- free(command_buf);
- exit(1);
- }
- break;
- }
+ {
+ int c;
+ if (p == (command_buf + cmd_size))
+ {
+ char *new_command_buf;
+ int cur_size = cmd_size;
+ cmd_size *= 2;
+ new_command_buf = (char *)realloc(command_buf, cmd_size);
+ if (!new_command_buf) {
+ if (command_buf)
+ free(command_buf);
+ exit (1);
+ }
+ command_buf = new_command_buf;
+ p = command_buf + cur_size;
+ }
+ c = fgetc(file);
+ if (c == EOF || c == '\0')
+ {
+ *p = '\0';
+ break;
+ } else {
+ *p = c;
+ }
+ }
+ if (strlen(command_buf) == 0) {
+ okay = 0;
+ break;
+ }
+ p = strrchr(command_buf,'/');
+ p = p ? p+1 : command_buf;
+ if (strncmp(p, comm, COMM_LEN-1) == 0) {
+ okay = 1;
+ if (!(*command = strdup(p))) {
+ free(command_buf);
+ exit(1);
+ }
+ break;
+ }
}
(void) fclose(file);
free(command_buf);
command_buf = NULL;
-
+
if (exact && !okay)
{
- if (verbose)
- fprintf (stderr, _("killall: skipping partial match %s(%d)\n"),
- comm, pid);
- *got_long = okay;
- return -1;
+ if (verbose)
+ fprintf (stderr, _("killall: skipping partial match %s(%d)\n"),
+ comm, pid);
+ *got_long = okay;
+ return -1;
}
*got_long = okay;
return 0;
self = getpid ();
if (!(dir = opendir (PROC_BASE)))
{
- perror (PROC_BASE);
- exit (1);
+ perror (PROC_BASE);
+ exit (1);
}
*max_pids = 256;
pid_table = malloc (*max_pids * sizeof (pid_t));
while ( (de = readdir (dir)) != NULL)
{
if (!(pid = (pid_t) atoi (de->d_name)) || pid == self)
- continue;
- if (*pids == *max_pids)
- {
- if (!(pid_table = realloc (pid_table, 2 * *pids * sizeof (pid_t))))
- {
- perror ("realloc");
- exit (1);
- }
- *max_pids *= 2;
- }
- pid_table[(*pids)++] = pid;
+ continue;
+ if (*pids == *max_pids)
+ {
+ if (!(pid_table = realloc (pid_table, 2 * *pids * sizeof (pid_t))))
+ {
+ perror ("realloc");
+ exit (1);
+ }
+ *max_pids *= 2;
+ }
+ pid_table[(*pids)++] = pid;
}
- (void) closedir (dir);
- return pid_table;
+ (void) closedir (dir);
+ return pid_table;
}
#ifdef WITH_SELINUX
static int
kill_all(int signal, int name_count, char **namelist, struct passwd *pwent,
- regex_t *scontext )
+ regex_t *scontext )
#else /*WITH_SELINUX*/
static int
kill_all (int signal, int name_count, char **namelist, struct passwd *pwent)
#endif /*WITH_SELINUX*/
if (opt_ns_pid)
- ns_ino = get_ns(opt_ns_pid, PIDNS);
+ ns_ino = get_ns(opt_ns_pid, PIDNS);
if (name_count && reg)
- reglist = build_regexp_list(name_count, namelist);
+ reglist = build_regexp_list(name_count, namelist);
else
- if ( (name_info = build_nameinfo(name_count, namelist)) == NULL)
- exit(1);
+ if ( (name_info = build_nameinfo(name_count, namelist)) == NULL)
+ exit(1);
pid_table = create_pid_table(&max_pids, &pids);
found = 0;
pids_killed = 0;
pid_killed = malloc (max_pids * sizeof (pid_t));
- if (!pid_killed)
+ if (!pid_killed)
{
- perror ("malloc");
- exit (1);
+ perror ("malloc");
+ exit (1);
+ }
+ if (process_group)
+ {
+ pgids = calloc (pids, sizeof (pid_t));
+ if (!pgids)
+ {
+ perror ("malloc");
+ exit (1);
+ }
}
- if (process_group)
- {
- pgids = calloc (pids, sizeof (pid_t));
- if (!pgids)
- {
- perror ("malloc");
- exit (1);
- }
- }
got_long = 0;
for (i = 0; i < pids; i++)
{
- pid_t id;
- int found_name = -1;
- double process_age_sec = 0;
- /* match by UID */
+ pid_t id;
+ int found_name = -1;
+ double process_age_sec = 0;
+ /* match by UID */
if (pwent && match_process_uid(pid_table[i], pwent->pw_uid)==0)
- continue;
- if (opt_ns_pid && ns_ino && ns_ino != get_ns(pid_table[i], PIDNS))
- continue;
+ continue;
+ if (opt_ns_pid && ns_ino && ns_ino != get_ns(pid_table[i], PIDNS))
+ continue;
#ifdef WITH_SELINUX
/* match by SELinux context */
{
if (getpidcon(pid_table[i], &lcontext) < 0)
continue;
- if (regexec(scontext, lcontext, 0, NULL, 0) != 0) {
+ if (regexec(scontext, lcontext, 0, NULL, 0) != 0) {
freecon(lcontext);
continue;
}
#endif /*WITH_SELINUX*/
length = load_process_name_and_age(comm, &process_age_sec, pid_table[i], (younger_than||older_than));
if (length < 0)
- continue;
+ continue;
- /* test for process age, if required */
- if ( younger_than && (process_age_sec > younger_than ) )
- continue;
- if ( older_than && (process_age_sec < older_than ) )
- continue;
+ /* test for process age, if required */
+ if ( younger_than && (process_age_sec > younger_than ) )
+ continue;
+ if ( older_than && (process_age_sec < older_than ) )
+ continue;
got_long = 0;
if (command) {
command = NULL;
}
if (length == COMM_LEN - 1)
- if (load_proc_cmdline(pid_table[i], comm, &command, &got_long) < 0)
- continue;
-
- /* match by process name */
+ if (load_proc_cmdline(pid_table[i], comm, &command, &got_long) < 0)
+ continue;
+
+ /* match by process name */
for (j = 0; j < name_count; j++)
- {
- if (reg)
- {
- if (regexec (®list[j], got_long ? command : comm, 0, NULL, 0) != 0)
- continue;
- }
- else /* non-regex */
- {
- if (!name_info[j].st.st_dev)
- {
- if (length != COMM_LEN - 1 || name_info[j].name_length < COMM_LEN - 1)
- {
- if (ignore_case == 1)
- {
- if (strcasecmp (namelist[j], comm))
- continue;
- } else {
- if (strcmp(namelist[j], comm))
- continue;
- }
- } else {
- if (ignore_case == 1)
- {
- if (got_long ? strcasecmp (namelist[j], command) :
- strncasecmp (namelist[j], comm, COMM_LEN - 1))
- continue;
- } else {
- if (got_long ? strcmp (namelist[j], command) :
- strncmp (namelist[j], comm, COMM_LEN - 1))
- continue;
- }
- }
- } else {
- int ok = 1;
- if (asprintf (&path, PROC_BASE "/%d/exe", pid_table[i]) < 0)
- continue;
- if (stat (path, &st) < 0)
- ok = 0;
- else if (name_info[j].st.st_dev != st.st_dev ||
- name_info[j].st.st_ino != st.st_ino)
- {
- /* maybe the binary has been modified and std[j].st_ino
- * is not reliable anymore. We need to compare paths.
- */
- size_t len = strlen(namelist[j]);
- char *linkbuf = malloc(len + 1);
-
- if (!linkbuf ||
- readlink(path, linkbuf, len + 1) != (ssize_t)len ||
- memcmp(namelist[j], linkbuf, len))
- ok = 0;
- free(linkbuf);
- }
- free(path);
- if (!ok)
- continue;
- }
- } /* non-regex */
- found_name = j;
- break;
- }
- if (name_count && found_name==-1)
- continue; /* match by process name faild */
-
+ {
+ if (reg)
+ {
+ if (regexec (®list[j], got_long ? command : comm, 0, NULL, 0) != 0)
+ continue;
+ }
+ else /* non-regex */
+ {
+ if (!name_info[j].st.st_dev)
+ {
+ if (length != COMM_LEN - 1 || name_info[j].name_length < COMM_LEN - 1)
+ {
+ if (ignore_case == 1)
+ {
+ if (strcasecmp (namelist[j], comm))
+ continue;
+ } else {
+ if (strcmp(namelist[j], comm))
+ continue;
+ }
+ } else {
+ if (ignore_case == 1)
+ {
+ if (got_long ? strcasecmp (namelist[j], command) :
+ strncasecmp (namelist[j], comm, COMM_LEN - 1))
+ continue;
+ } else {
+ if (got_long ? strcmp (namelist[j], command) :
+ strncmp (namelist[j], comm, COMM_LEN - 1))
+ continue;
+ }
+ }
+ } else {
+ int ok = 1;
+ if (asprintf (&path, PROC_BASE "/%d/exe", pid_table[i]) < 0)
+ continue;
+ if (stat (path, &st) < 0)
+ ok = 0;
+ else if (name_info[j].st.st_dev != st.st_dev ||
+ name_info[j].st.st_ino != st.st_ino)
+ {
+ /* maybe the binary has been modified and std[j].st_ino
+ * is not reliable anymore. We need to compare paths.
+ */
+ size_t len = strlen(namelist[j]);
+ char *linkbuf = malloc(len + 1);
+
+ if (!linkbuf ||
+ readlink(path, linkbuf, len + 1) != (ssize_t)len ||
+ memcmp(namelist[j], linkbuf, len))
+ ok = 0;
+ free(linkbuf);
+ }
+ free(path);
+ if (!ok)
+ continue;
+ }
+ } /* non-regex */
+ found_name = j;
+ break;
+ }
+ if (name_count && found_name==-1)
+ continue; /* match by process name faild */
+
/* check for process group */
- if (!process_group)
- id = pid_table[i];
- else
- {
- int j;
-
- id = getpgid (pid_table[i]);
- pgids[i] = id;
- if (id < 0)
- {
- fprintf (stderr, "killall: getpgid(%d): %s\n",
- pid_table[i], strerror (errno));
- }
- for (j = 0; j < i; j++)
- if (pgids[j] == id)
- break;
- if (j < i)
- continue;
- }
- if (interactive && !ask (comm, id, signal))
- continue;
- if (kill (process_group ? -id : id, signal) >= 0)
- {
- if (verbose)
- fprintf (stderr, _("Killed %s(%s%d) with signal %d\n"), got_long ? command :
- comm, process_group ? "pgid " : "", id, signal);
- if (found_name >= 0)
- /* mark item of namelist */
- found |= 1UL << found_name;
- pid_killed[pids_killed++] = id;
- }
- else if (errno != ESRCH || interactive)
- fprintf (stderr, "%s(%d): %s\n", got_long ? command :
- comm, id, strerror (errno));
+ if (!process_group)
+ id = pid_table[i];
+ else
+ {
+ int j;
+
+ id = getpgid (pid_table[i]);
+ pgids[i] = id;
+ if (id < 0)
+ {
+ fprintf (stderr, "killall: getpgid(%d): %s\n",
+ pid_table[i], strerror (errno));
+ }
+ for (j = 0; j < i; j++)
+ if (pgids[j] == id)
+ break;
+ if (j < i)
+ continue;
+ }
+ if (interactive && !ask (comm, id, signal))
+ continue;
+ if (kill (process_group ? -id : id, signal) >= 0)
+ {
+ if (verbose)
+ fprintf (stderr, _("Killed %s(%s%d) with signal %d\n"), got_long ? command :
+ comm, process_group ? "pgid " : "", id, signal);
+ if (found_name >= 0)
+ /* mark item of namelist */
+ found |= 1UL << found_name;
+ pid_killed[pids_killed++] = id;
+ }
+ else if (errno != ESRCH || interactive)
+ fprintf (stderr, "%s(%d): %s\n", got_long ? command :
+ comm, id, strerror (errno));
}
if (command)
- free(command);
+ free(command);
if (reglist)
- free_regexp_list(reglist, name_count);
+ free_regexp_list(reglist, name_count);
free(pgids);
if (!quiet)
- for (i = 0; i < name_count; i++)
- if (!(found & (1UL << i)))
- fprintf (stderr, _("%s: no process found\n"), namelist[i]);
+ for (i = 0; i < name_count; i++)
+ if (!(found & (1UL << i)))
+ fprintf (stderr, _("%s: no process found\n"), namelist[i]);
if (name_count)
/* killall returns a zero return code if at least one process has
* been killed for each listed command. */
*/
while (pids_killed && wait_until_dead)
{
- for (i = 0; i < pids_killed;)
- {
- if (kill (process_group ? -pid_killed[i] : pid_killed[i], 0) < 0 &&
- errno == ESRCH)
- {
- pid_killed[i] = pid_killed[--pids_killed];
- continue;
- }
- i++;
- }
- sleep (1); /* wait a bit longer */
+ for (i = 0; i < pids_killed;)
+ {
+ if (kill (process_group ? -pid_killed[i] : pid_killed[i], 0) < 0 &&
+ errno == ESRCH)
+ {
+ pid_killed[i] = pid_killed[--pids_killed];
+ continue;
+ }
+ i++;
+ }
+ sleep (1); /* wait a bit longer */
}
free(pid_killed);
free(pid_table);
static void
usage (const char *msg)
{
- if (msg != NULL)
- fprintf(stderr, "%s\n", msg);
+ if (msg != NULL)
+ fprintf(stderr, "%s\n", msg);
#ifdef WITH_SELINUX
- fprintf(stderr, _(
- "Usage: killall [ -Z CONTEXT ] [ -u USER ] [ -y TIME ] [ -o TIME ] [ -eIgiqrvw ]\n"
- " [ -s SIGNAL | -SIGNAL ] NAME...\n"));
+ fprintf(stderr, _(
+ "Usage: killall [ -Z CONTEXT ] [ -u USER ] [ -y TIME ] [ -o TIME ] [ -eIgiqrvw ]\n"
+ " [ -s SIGNAL | -SIGNAL ] NAME...\n"));
#else /*WITH_SELINUX*/
- fprintf(stderr, _(
- "Usage: killall [OPTION]... [--] NAME...\n"));
+ fprintf(stderr, _(
+ "Usage: killall [OPTION]... [--] NAME...\n"));
#endif /*WITH_SELINUX*/
- fprintf(stderr, _(
- " killall -l, --list\n"
- " killall -V, --version\n\n"
- " -e,--exact require exact match for very long names\n"
- " -I,--ignore-case case insensitive process name match\n"
- " -g,--process-group kill process group instead of process\n"
- " -y,--younger-than kill processes younger than TIME\n"
- " -o,--older-than kill processes older than TIME\n"
- " -i,--interactive ask for confirmation before killing\n"
- " -l,--list list all known signal names\n"
- " -q,--quiet don't print complaints\n"
- " -r,--regexp interpret NAME as an extended regular expression\n"
- " -s,--signal SIGNAL send this signal instead of SIGTERM\n"
- " -u,--user USER kill only process(es) running as USER\n"
- " -v,--verbose report if the signal was successfully sent\n"
- " -V,--version display version information\n"
- " -w,--wait wait for processes to die\n"
- " -n,--ns PID match processes that belong to the same namespaces\n"
- " as PID\n"));
+ fprintf(stderr, _(
+ " killall -l, --list\n"
+ " killall -V, --version\n\n"
+ " -e,--exact require exact match for very long names\n"
+ " -I,--ignore-case case insensitive process name match\n"
+ " -g,--process-group kill process group instead of process\n"
+ " -y,--younger-than kill processes younger than TIME\n"
+ " -o,--older-than kill processes older than TIME\n"
+ " -i,--interactive ask for confirmation before killing\n"
+ " -l,--list list all known signal names\n"
+ " -q,--quiet don't print complaints\n"
+ " -r,--regexp interpret NAME as an extended regular expression\n"
+ " -s,--signal SIGNAL send this signal instead of SIGTERM\n"
+ " -u,--user USER kill only process(es) running as USER\n"
+ " -v,--verbose report if the signal was successfully sent\n"
+ " -V,--version display version information\n"
+ " -w,--wait wait for processes to die\n"
+ " -n,--ns PID match processes that belong to the same namespaces\n"
+ " as PID\n"));
#ifdef WITH_SELINUX
- fprintf(stderr, _(
- " -Z,--context REGEXP kill only process(es) having context\n"
- " (must precede other arguments)\n"));
+ fprintf(stderr, _(
+ " -Z,--context REGEXP kill only process(es) having context\n"
+ " (must precede other arguments)\n"));
#endif /*WITH_SELINUX*/
- fputc('\n', stderr);
- exit(1);
+ fputc('\n', stderr);
+ exit(1);
}
void print_version()
{
- fprintf(stderr, "killall (PSmisc) %s\n", VERSION);
- fprintf(stderr, _(
- "Copyright (C) 1993-2017 Werner Almesberger and Craig Small\n\n"));
- fprintf(stderr, _(
- "PSmisc comes with ABSOLUTELY NO WARRANTY.\n"
- "This is free software, and you are welcome to redistribute it under\n"
- "the terms of the GNU General Public License.\n"
- "For more information about these matters, see the files named COPYING.\n"));
+ fprintf(stderr, "killall (PSmisc) %s\n", VERSION);
+ fprintf(stderr, _(
+ "Copyright (C) 1993-2017 Werner Almesberger and Craig Small\n\n"));
+ fprintf(stderr, _(
+ "PSmisc comes with ABSOLUTELY NO WARRANTY.\n"
+ "This is free software, and you are welcome to redistribute it under\n"
+ "the terms of the GNU General Public License.\n"
+ "For more information about these matters, see the files named COPYING.\n"));
}
static int
have_proc_self_stat (void)
{
- char filename[128];
- struct stat isproc;
- pid_t pid = getpid();
+ char filename[128];
+ struct stat isproc;
+ pid_t pid = getpid();
- snprintf(filename, sizeof(filename), PROC_BASE"/%d/stat", (int) pid);
- return stat(filename, &isproc) == 0;
+ snprintf(filename, sizeof(filename), PROC_BASE"/%d/stat", (int) pid);
+ return stat(filename, &isproc) == 0;
}
int
main (int argc, char **argv)
{
- char *name;
- int sig_num;
- int optc;
- int myoptind;
- int skip_error=0;
- struct passwd *pwent = NULL;
- char yt[16];
- char ot[16];
-
- //int optsig = 0;
-
- struct option options[] = {
- {"exact", 0, NULL, 'e'},
- {"ignore-case", 0, NULL, 'I'},
- {"process-group", 0, NULL, 'g'},
- {"younger-than", 1, NULL, 'y'},
- {"older-than", 1, NULL, 'o'},
- {"interactive", 0, NULL, 'i'},
- {"list-signals", 0, NULL, 'l'},
- {"quiet", 0, NULL, 'q'},
- {"regexp", 0, NULL, 'r'},
- {"signal", 1, NULL, 's'},
- {"user", 1, NULL, 'u'},
- {"verbose", 0, NULL, 'v'},
- {"wait", 0, NULL, 'w'},
- {"ns", 1, NULL, 'n' },
+ char *name;
+ int sig_num;
+ int optc;
+ int myoptind;
+ int skip_error=0;
+ struct passwd *pwent = NULL;
+ char yt[COMM_LEN];
+ char ot[COMM_LEN];
+
+ //int optsig = 0;
+
+ struct option options[] = {
+ {"exact", 0, NULL, 'e'},
+ {"ignore-case", 0, NULL, 'I'},
+ {"process-group", 0, NULL, 'g'},
+ {"younger-than", 1, NULL, 'y'},
+ {"older-than", 1, NULL, 'o'},
+ {"interactive", 0, NULL, 'i'},
+ {"list-signals", 0, NULL, 'l'},
+ {"quiet", 0, NULL, 'q'},
+ {"regexp", 0, NULL, 'r'},
+ {"signal", 1, NULL, 's'},
+ {"user", 1, NULL, 'u'},
+ {"verbose", 0, NULL, 'v'},
+ {"wait", 0, NULL, 'w'},
+ {"ns", 1, NULL, 'n' },
#ifdef WITH_SELINUX
- {"context", 1, NULL, 'Z'},
+ {"context", 1, NULL, 'Z'},
#endif /*WITH_SELINUX*/
- {"version", 0, NULL, 'V'},
- {0,0,0,0 }};
+ {"version", 0, NULL, 'V'},
+ {0,0,0,0 }};
- /* Setup the i18n */
+ /* Setup the i18n */
#ifdef ENABLE_NLS
- setlocale(LC_ALL, "");
- bindtextdomain(PACKAGE, LOCALEDIR);
- textdomain(PACKAGE);
+ setlocale(LC_ALL, "");
+ bindtextdomain(PACKAGE, LOCALEDIR);
+ textdomain(PACKAGE);
#endif
#ifdef WITH_SELINUX
- security_context_t scontext = NULL;
- regex_t scontext_reg;
+ security_context_t scontext = NULL;
+ regex_t scontext_reg;
- if ( argc < 2 ) usage(NULL); /* do the obvious thing... */
+ if ( argc < 2 ) usage(NULL); /* do the obvious thing... */
#endif /*WITH_SELINUX*/
- name = strrchr (*argv, '/');
- if (name)
- name++;
- else
- name = *argv;
- sig_num = SIGTERM;
+ name = strrchr (*argv, '/');
+ if (name)
+ name++;
+ else
+ name = *argv;
+ sig_num = SIGTERM;
- opterr = 0;
+ opterr = 0;
#ifdef WITH_SELINUX
- while ( (optc = getopt_long(argc,argv,"egy:o:ilqrs:u:vwZ:VIn:",options,NULL)) != -1) {
+ while ( (optc = getopt_long(argc,argv,"egy:o:ilqrs:u:vwZ:VIn:",options,NULL)) != -1) {
#else
- while ( (optc = getopt_long(argc,argv,"egy:o:ilqrs:u:vwVIn:",options,NULL)) != -1) {
+ while ( (optc = getopt_long(argc,argv,"egy:o:ilqrs:u:vwVIn:",options,NULL)) != -1) {
#endif
- switch (optc) {
- case 'e':
- exact = 1;
- break;
- case 'g':
- process_group = 1;
- break;
- case 'y':
- strncpy(yt, optarg, 16);
- yt[15] = '\0';
- if ( 0 >= (younger_than = parse_time_units(yt) ) )
- usage(_("Invalid time format"));
- break;
- case 'o':
- strncpy(ot, optarg, 16);
- ot[15] = '\0';
- if ( 0 >= (older_than = parse_time_units(ot) ) )
- usage(_("Invalid time format"));
- break;
- case 'i':
- interactive = 1;
- break;
- case 'l':
- list_signals();
- return 0;
- break;
- case 'q':
- quiet = 1;
- break;
- case 'r':
- reg = 1;
- break;
- case 's':
- sig_num = get_signal (optarg, "killall");
- break;
- case 'u':
- if (!(pwent = getpwnam(optarg))) {
- fprintf (stderr, _("Cannot find user %s\n"), optarg);
- exit (1);
- }
- break;
- case 'v':
- verbose = 1;
- break;
- case 'w':
- wait_until_dead = 1;
- break;
- case 'I':
- /* option check is optind-1 but sig name is optind */
- if (strcmp(argv[optind-1],"-I") == 0 || strncmp(argv[optind-1],"--",2) == 0) {
- ignore_case = 1;
- } else {
- sig_num = get_signal (argv[optind]+1, "killall");
- skip_error=1;
- }
- break;
- case 'V':
- /* option check is optind-1 but sig name is optind */
- if (strcmp(argv[optind-1],"-V") == 0 || strncmp(argv[optind-1],"--",2) == 0) {
- print_version();
- return 0;
- }
- sig_num = get_signal (argv[optind]+1, "killall");
- break;
- case 'n':
- opt_ns_pid = atoi(optarg);
- if (opt_ns_pid == 0)
- usage(_("Invalid namespace PID"));
- break;
+ switch (optc) {
+ case 'e':
+ exact = 1;
+ break;
+ case 'g':
+ process_group = 1;
+ break;
+ case 'y':
+ strncpy(yt, optarg, sizeof yt -1);
+ yt[sizeof yt -1] = '\0';
+ if ( 0 >= (younger_than = parse_time_units(yt) ) )
+ usage(_("Invalid time format"));
+ break;
+ case 'o':
+ strncpy(ot, optarg, sizeof ot - 1);
+ ot[sizeof ot -1] = '\0';
+ if ( 0 >= (older_than = parse_time_units(ot) ) )
+ usage(_("Invalid time format"));
+ break;
+ case 'i':
+ interactive = 1;
+ break;
+ case 'l':
+ list_signals();
+ return 0;
+ break;
+ case 'q':
+ quiet = 1;
+ break;
+ case 'r':
+ reg = 1;
+ break;
+ case 's':
+ sig_num = get_signal (optarg, "killall");
+ break;
+ case 'u':
+ if (!(pwent = getpwnam(optarg))) {
+ fprintf (stderr, _("Cannot find user %s\n"), optarg);
+ exit (1);
+ }
+ break;
+ case 'v':
+ verbose = 1;
+ break;
+ case 'w':
+ wait_until_dead = 1;
+ break;
+ case 'I':
+ /* option check is optind-1 but sig name is optind */
+ if (strcmp(argv[optind-1],"-I") == 0 || strncmp(argv[optind-1],"--",2) == 0) {
+ ignore_case = 1;
+ } else {
+ sig_num = get_signal (argv[optind]+1, "killall");
+ skip_error=1;
+ }
+ break;
+ case 'V':
+ /* option check is optind-1 but sig name is optind */
+ if (strcmp(argv[optind-1],"-V") == 0 || strncmp(argv[optind-1],"--",2) == 0) {
+ print_version();
+ return 0;
+ }
+ sig_num = get_signal (argv[optind]+1, "killall");
+ break;
+ case 'n':
+ opt_ns_pid = atoi(optarg);
+ if (opt_ns_pid == 0)
+ usage(_("Invalid namespace PID"));
+ break;
#ifdef WITH_SELINUX
- case 'Z':
- if (is_selinux_enabled()>0) {
- scontext=optarg;
- if (regcomp(&scontext_reg, scontext, REG_EXTENDED|REG_NOSUB) != 0) {
- fprintf(stderr, _("Bad regular expression: %s\n"), scontext);
- exit (1);
- }
- } else
- fprintf(stderr, "Warning: -Z (--context) ignored. Requires an SELinux enabled kernel\n");
- break;
+ case 'Z':
+ if (is_selinux_enabled()>0) {
+ scontext=optarg;
+ if (regcomp(&scontext_reg, scontext, REG_EXTENDED|REG_NOSUB) != 0) {
+ fprintf(stderr, _("Bad regular expression: %s\n"), scontext);
+ exit (1);
+ }
+ } else
+ fprintf(stderr, "Warning: -Z (--context) ignored. Requires an SELinux enabled kernel\n");
+ break;
#endif /*WITH_SELINUX*/
- case '?':
- /* Signal names are in uppercase, so check to see if the argv
- * is upper case */
- if (argv[optind-1][1] >= 'A' && argv[optind-1][1] <= 'Z') {
- sig_num = get_signal (argv[optind-1]+1, "killall");
- } else {
- /* Might also be a -## signal too */
- if (argv[optind-1][1] >= '0' && argv[optind-1][1] <= '9') {
- sig_num = atoi(argv[optind-1]+1);
- } else {
- if (skip_error)
- skip_error=0;
- else
- usage(NULL);
+ case '?':
+ /* Signal names are in uppercase, so check to see if the argv
+ * is upper case */
+ if (argv[optind-1][1] >= 'A' && argv[optind-1][1] <= 'Z') {
+ sig_num = get_signal (argv[optind-1]+1, "killall");
+ } else {
+ /* Might also be a -## signal too */
+ if (argv[optind-1][1] >= '0' && argv[optind-1][1] <= '9') {
+ sig_num = atoi(argv[optind-1]+1);
+ } else {
+ if (skip_error)
+ skip_error=0;
+ else
+ usage(NULL);
+ }
+ }
+ break;
+ }
}
- }
- break;
- }
- }
- myoptind = optind;
+ myoptind = optind;
#ifdef WITH_SELINUX
- if ((argc - myoptind < 1) && pwent==NULL && scontext==NULL)
+ if ((argc - myoptind < 1) && pwent==NULL && scontext==NULL)
#else
- if ((argc - myoptind < 1) && pwent==NULL)
+ if ((argc - myoptind < 1) && pwent==NULL)
#endif
- usage(NULL);
-
- if (argc - myoptind > MAX_NAMES) {
- fprintf (stderr, _("killall: Maximum number of names is %d\n"),
- MAX_NAMES);
- exit (1);
- }
- if (!have_proc_self_stat()) {
- fprintf (stderr, _("killall: %s lacks process entries (not mounted ?)\n"),
- PROC_BASE);
- exit (1);
- }
- argv = argv + myoptind;
+ usage(NULL);
+
+ if (argc - myoptind > MAX_NAMES) {
+ fprintf (stderr, _("killall: Maximum number of names is %d\n"),
+ MAX_NAMES);
+ exit (1);
+ }
+ if (!have_proc_self_stat()) {
+ fprintf (stderr, _("killall: %s lacks process entries (not mounted ?)\n"),
+ PROC_BASE);
+ exit (1);
+ }
+ argv = argv + myoptind;
#ifdef WITH_SELINUX
- return kill_all(sig_num,argc - myoptind, argv, pwent,
- scontext ? &scontext_reg : NULL);
+ return kill_all(sig_num,argc - myoptind, argv, pwent,
+ scontext ? &scontext_reg : NULL);
#else /*WITH_SELINUX*/
- return kill_all(sig_num,argc - myoptind, argv, pwent);
+ return kill_all(sig_num,argc - myoptind, argv, pwent);
#endif /*WITH_SELINUX*/
-}
+ }