From: Karel Zak Date: Tue, 12 Mar 2013 16:40:31 +0000 (+0100) Subject: kill: use new API from lib/procutils.c X-Git-Tag: v2.23-rc1~69 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f1e7f7d54e232ceba0c739633183a0608529fb90;p=thirdparty%2Futil-linux.git kill: use new API from lib/procutils.c Signed-off-by: Karel Zak --- diff --git a/misc-utils/Makemodule.am b/misc-utils/Makemodule.am index 2634b3d4c8..3846d9c1e2 100644 --- a/misc-utils/Makemodule.am +++ b/misc-utils/Makemodule.am @@ -145,10 +145,7 @@ endif # BUILD_LIBMOUNT if BUILD_KILL bin_PROGRAMS += kill -kill_SOURCES = \ - misc-utils/kill.c \ - misc-utils/kill.h \ - misc-utils/procs.c +kill_SOURCES = misc-utils/kill.c kill_LDADD = $(LDADD) libcommon.la dist_man_MANS += misc-utils/kill.1 endif diff --git a/misc-utils/kill.c b/misc-utils/kill.c index 051f56d8ff..f6b3f84daa 100644 --- a/misc-utils/kill.c +++ b/misc-utils/kill.c @@ -50,9 +50,9 @@ #include #include "c.h" -#include "kill.h" #include "nls.h" #include "closestream.h" +#include "procutils.h" #include "strutils.h" struct signv { @@ -139,8 +139,6 @@ struct signv { #endif }; -extern char *mybasename(char *); - static int arg_to_signum (char *arg, int mask); static void nosig (char *name); static void printsig (int sig); @@ -158,7 +156,6 @@ int main (int argc, char *argv[]) int errors, numsig, pid; char *ep, *arg; int do_pid, do_kill, check_all; - pid_t *pids, *ip; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); @@ -269,23 +266,35 @@ int main (int argc, char *argv[]) /* we're done with the options. the rest of the arguments should be process ids and names. kill them. */ - for (errors = EXIT_SUCCESS; (arg = *argv) != NULL; argv++) { + for (errors = 0; (arg = *argv) != NULL; argv++) { pid = strtol (arg, &ep, 10); if (! *ep) errors += kill_verbose (arg, pid, numsig); - else { - pids = get_pids (arg, check_all); - if (! pids) { + else { + struct proc_processes *ps = proc_open_processes(); + int ct = 0; + + if (!ps) + continue; + + if (!check_all) + proc_processes_filter_by_uid(ps, getuid()); + + proc_processes_filter_by_name(ps, arg); + + while (proc_next_pid(ps, &pid) == 0) { + errors += kill_verbose(arg, pid, numsig); + ct++; + } + + if (!ct) { errors++; warnx (_("cannot find process \"%s\""), arg); - continue; } - for (ip = pids; *ip >= 0; ip++) - errors += kill_verbose (arg, *ip, numsig); - free (pids); + proc_close_processes(ps); } } - if (errors != EXIT_SUCCESS) + if (errors != 0) errors = EXIT_FAILURE; return errors; } @@ -424,7 +433,7 @@ static int usage(int status) static int kill_verbose (char *procname, pid_t pid, int sig) { - int rc; + int rc = 0; if (sig < 0) { printf ("%ld\n", (long)pid); diff --git a/misc-utils/kill.h b/misc-utils/kill.h deleted file mode 100644 index 107f8019b3..0000000000 --- a/misc-utils/kill.h +++ /dev/null @@ -1 +0,0 @@ -extern pid_t *get_pids (char *process_name, int get_all); diff --git a/misc-utils/procs.c b/misc-utils/procs.c deleted file mode 100644 index dff7a7063b..0000000000 --- a/misc-utils/procs.c +++ /dev/null @@ -1,126 +0,0 @@ -/* - * procs.c -- functions to parse the linux /proc filesystem. - * (c) 1994 salvatore valente - * - * this program is free software. you can redistribute it and - * modify it under the terms of the gnu general public license. - * there is no warranty. - * - * faith - * 1.2 - * 1995/02/23 01:20:40 - * - */ - -#define _POSIX_SOURCE 1 - -#include -#include -#include -#include -#include -#include -#include -#include -#include "kill.h" -#include "xalloc.h" - -extern char *mybasename (char *); -static char *parse_parens (char *buf); - -pid_t * -get_pids (char *process_name, int get_all) { - DIR *dir; - struct dirent *ent; - int status; - char fname[100], *cp, buf[256], *end; - struct stat st; - uid_t uid; - FILE *fp; - pid_t pid, *pids, num_pids, pids_size; - - dir = opendir ("/proc"); - if (! dir) { - warn ("opendir /proc"); - return NULL; - } - uid = getuid (); - pids = NULL; - num_pids = pids_size = 0; - - while ((ent = readdir (dir)) != NULL) { - pid = strtol(ent->d_name, &end, 10); - if (errno || ent->d_name == end || (end && *end)) - continue; - sprintf (fname, "/proc/%ld/cmdline", (long)pid); - /* get the process owner */ - status = stat (fname, &st); - if (status != 0) continue; - if (! get_all && uid != st.st_uid) continue; - /* get the command line */ - fp = fopen (fname, "r"); - if (! fp) continue; - cp = fgets (buf, sizeof (buf), fp); - fclose (fp); - /* an empty command line means the process is swapped out */ - if (! cp || ! *cp) { - /* get the process name from the statfile */ - sprintf (fname, "/proc/%ld/stat", (long)pid); - fp = fopen (fname, "r"); - if (! fp) continue; - cp = fgets (buf, sizeof (buf), fp); - fclose (fp); - if (cp == NULL) continue; - cp = parse_parens (buf); - if (cp == NULL) continue; - } - /* ok, we got the process name. */ - if (strcmp (process_name, mybasename (cp))) continue; - while (pids_size < num_pids + 2) { - pids_size += 5; - pids = xrealloc (pids, sizeof(pid_t) * pids_size); - } - if (pids) { - pids[num_pids++] = pid; - pids[num_pids] = -1; - } - } - closedir (dir); - return pids; -} - -/* - * parse_parens () -- return an index just past the first open paren in - * buf, and terminate the string at the matching close paren. - */ -static char *parse_parens (char *buf) -{ - char *cp, *ip; - int depth; - - cp = strchr (buf, '('); - if (cp == NULL) return NULL; - cp++; - depth = 1; - for (ip = cp; *ip; ip++) { - if (*ip == '(') - depth++; - if (*ip == ')') { - depth--; - if (depth == 0) { - *ip = 0; - break; - } - } - } - return cp; -} - -char *mybasename (char *path) -{ - char *cp; - - cp = strrchr (path, '/'); - return (cp ? cp + 1 : path); -} -