]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
kill: use new API from lib/procutils.c
authorKarel Zak <kzak@redhat.com>
Tue, 12 Mar 2013 16:40:31 +0000 (17:40 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 12 Mar 2013 16:40:31 +0000 (17:40 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/Makemodule.am
misc-utils/kill.c
misc-utils/kill.h [deleted file]
misc-utils/procs.c [deleted file]

index 2634b3d4c8e6ab0571e735f2f7bccb250c95a96b..3846d9c1e257f3076bcbd33c761ba9b30de70d15 100644 (file)
@@ -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
index 051f56d8ff8d80ae0a12a69a049a55bc2d820d54..f6b3f84daae99a98ece97d7f032d12a06cf6c405 100644 (file)
@@ -50,9 +50,9 @@
 #include <signal.h>
 
 #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 (file)
index 107f801..0000000
+++ /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 (file)
index dff7a70..0000000
+++ /dev/null
@@ -1,126 +0,0 @@
-/*
- *  procs.c -- functions to parse the linux /proc filesystem.
- *  (c) 1994 salvatore valente <svalente@mit.edu>
- *
- *   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 <sys/types.h>
-#include <sys/stat.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <dirent.h>
-#include <ctype.h>
-#include <unistd.h>
-#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);
-}
-