#include <signal.h>
#include "c.h"
-#include "kill.h"
#include "nls.h"
#include "closestream.h"
+#include "procutils.h"
#include "strutils.h"
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);
int errors, numsig, pid;
char *ep, *arg;
int do_pid, do_kill, check_all;
- pid_t *pids, *ip;
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
/* 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;
}
static int kill_verbose (char *procname, pid_t pid, int sig)
{
- int rc;
+ int rc = 0;
if (sig < 0) {
printf ("%ld\n", (long)pid);
+++ /dev/null
-/*
- * 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);
-}
-