add help text
[oweals/busybox.git] / procps / fuser.c
index d625b16ab36183fd952098622a4bfb9ed54d62cf..7837ff88355eb17154404c13c76576f43bfa9035 100644 (file)
@@ -4,10 +4,20 @@
  *
  * Copyright 2004 Tony J. White
  *
- * May be distributed under the conditions of the
- * GNU Library General Public License
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 
+//usage:#define fuser_trivial_usage
+//usage:       "[OPTIONS] FILE or PORT/PROTO"
+//usage:#define fuser_full_usage "\n\n"
+//usage:       "Find processes which use FILEs or PORTs\n"
+//usage:     "\nOptions:"
+//usage:     "\n       -m      Find processes which use same fs as FILEs"
+//usage:     "\n       -4,-6   Search only IPv4/IPv6 space"
+//usage:     "\n       -s      Don't display PIDs"
+//usage:     "\n       -k      Kill found processes"
+//usage:     "\n       -SIGNAL Signal to send (default: KILL)"
+
 #include "libbb.h"
 
 #define MAX_LINE 255
@@ -32,114 +42,97 @@ typedef struct pid_list {
        pid_t pid;
 } pid_list;
 
-static dev_t find_socket_dev(void)
-{
-       int fd = socket(AF_INET, SOCK_DGRAM, 0);
-       if (fd >= 0) {
-               struct stat buf;
-               int r = fstat(fd, &buf);
-               close(fd);
-               if (r == 0)
-                       return buf.st_dev;
-       }
-       return 0;
-}
 
-static int file_to_dev_inode(const char *filename, dev_t *dev, ino_t *inode)
-{
-       struct stat f_stat;
-       if (stat(filename, &f_stat))
-               return 0;
-       *inode = f_stat.st_ino;
-       *dev = f_stat.st_dev;
-       return 1;
-}
+struct globals {
+       pid_list *pid_list_head;
+       inode_list *inode_list_head;
+} FIX_ALIASING;
+#define G (*(struct globals*)&bb_common_bufsiz1)
+#define INIT_G() do { } while (0)
 
-static char *parse_net_arg(const char *arg, unsigned *port)
-{
-       char path[12], tproto[5];
-
-       if (sscanf(arg, "%u/%4s", port, tproto) != 2)
-               return NULL;
-       sprintf(path, "net/%s", tproto);
-       if (access(path, R_OK) != 0)
-               return NULL;
-       return xstrdup(tproto);
-}
 
-static pid_list *add_pid(pid_list *plist, pid_t pid)
+static void add_pid(const pid_t pid)
 {
-       pid_list *curr = plist;
-       while (curr != NULL) {
-               if (curr->pid == pid)
-                       return plist;
-               curr = curr->next;
+       pid_list **curr = &G.pid_list_head;
+
+       while (*curr) {
+               if ((*curr)->pid == pid)
+                       return;
+               curr = &(*curr)->next;
        }
-       curr = xmalloc(sizeof(pid_list));
-       curr->pid = pid;
-       curr->next = plist;
-       return curr;
+
+       *curr = xzalloc(sizeof(pid_list));
+       (*curr)->pid = pid;
 }
 
-static inode_list *add_inode(inode_list *ilist, dev_t dev, ino_t inode)
+static void add_inode(const struct stat *st)
 {
-       inode_list *curr = ilist;
-       while (curr != NULL) {
-               if (curr->inode == inode && curr->dev == dev)
-                       return ilist;
-               curr = curr->next;
+       inode_list **curr = &G.inode_list_head;
+
+       while (*curr) {
+               if ((*curr)->dev == st->st_dev
+                && (*curr)->inode == st->st_ino
+               ) {
+                       return;
+               }
+               curr = &(*curr)->next;
        }
-       curr = xmalloc(sizeof(inode_list));
-       curr->dev = dev;
-       curr->inode = inode;
-       curr->next = ilist;
-       return curr;
+
+       *curr = xzalloc(sizeof(inode_list));
+       (*curr)->dev = st->st_dev;
+       (*curr)->inode = st->st_ino;
 }
 
-static inode_list *scan_proc_net(const char *proto,
-                               unsigned port, inode_list *ilist)
+static void scan_proc_net(const char *path, unsigned port)
 {
-       char path[12], line[MAX_LINE + 1];
-       char addr[128];
-       ino_t tmp_inode;
-       dev_t tmp_dev;
+       char line[MAX_LINE + 1];
        long long uint64_inode;
        unsigned tmp_port;
        FILE *f;
+       struct stat st;
+       int fd;
 
-       tmp_dev = find_socket_dev();
+       /* find socket dev */
+       st.st_dev = 0;
+       fd = socket(AF_INET, SOCK_DGRAM, 0);
+       if (fd >= 0) {
+               fstat(fd, &st);
+               close(fd);
+       }
 
-       sprintf(path, "net/%s", proto);
-       f = fopen(path, "r");
+       f = fopen_for_read(path);
        if (!f)
-               return ilist;
+               return;
 
        while (fgets(line, MAX_LINE, f)) {
+               char addr[68];
                if (sscanf(line, "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
                                "%*x:%*x %*x %*d %*d %llu",
                                addr, &tmp_port, &uint64_inode) == 3
                ) {
-                       if (strlen(addr) == 8 && (option_mask32 & OPT_IP6))
+                       int len = strlen(addr);
+                       if (len == 8 && (option_mask32 & OPT_IP6))
                                continue;
-                       if (strlen(addr) > 8 && (option_mask32 & OPT_IP4))
+                       if (len > 8 && (option_mask32 & OPT_IP4))
                                continue;
                        if (tmp_port == port) {
-                               tmp_inode = uint64_inode;
-                               ilist = add_inode(ilist, tmp_dev, tmp_inode);
+                               st.st_ino = uint64_inode;
+                               add_inode(&st);
                        }
                }
        }
        fclose(f);
-       return ilist;
 }
 
-static int search_dev_inode(inode_list *ilist, dev_t dev, ino_t inode)
+static int search_dev_inode(const struct stat *st)
 {
+       inode_list *ilist = G.inode_list_head;
+
        while (ilist) {
-               if (ilist->dev == dev) {
+               if (ilist->dev == st->st_dev) {
                        if (option_mask32 & OPT_MOUNT)
                                return 1;
-                       if (ilist->inode == inode)
+                       if (ilist->inode == st->st_ino)
                                return 1;
                }
                ilist = ilist->next;
@@ -147,48 +140,42 @@ static int search_dev_inode(inode_list *ilist, dev_t dev, ino_t inode)
        return 0;
 }
 
-static pid_list *scan_pid_maps(const char *fname, pid_t pid,
-                               inode_list *ilist, pid_list *plist)
+static void scan_pid_maps(const char *fname, pid_t pid)
 {
        FILE *file;
        char line[MAX_LINE + 1];
        int major, minor;
-       ino_t inode;
        long long uint64_inode;
-       dev_t dev;
+       struct stat st;
 
-       file = fopen(fname, "r");
+       file = fopen_for_read(fname);
        if (!file)
-               return plist;
+               return;
+
        while (fgets(line, MAX_LINE, file)) {
                if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3)
                        continue;
-               inode = uint64_inode;
-               if (major == 0 && minor == 0 && inode == 0)
+               st.st_ino = uint64_inode;
+               if (major == 0 && minor == 0 && st.st_ino == 0)
                        continue;
-               dev = makedev(major, minor);
-               if (search_dev_inode(ilist, dev, inode))
-                       plist = add_pid(plist, pid);
+               st.st_dev = makedev(major, minor);
+               if (search_dev_inode(&st))
+                       add_pid(pid);
        }
        fclose(file);
-       return plist;
 }
 
-static pid_list *scan_link(const char *lname, pid_t pid,
-                               inode_list *ilist, pid_list *plist)
+static void scan_link(const char *lname, pid_t pid)
 {
-       ino_t inode;
-       dev_t dev;
+       struct stat st;
 
-       if (!file_to_dev_inode(lname, &dev, &inode))
-               return plist;
-       if (search_dev_inode(ilist, dev, inode))
-               plist = add_pid(plist, pid);
-       return plist;
+       if (stat(lname, &st) >= 0) {
+               if (search_dev_inode(&st))
+                       add_pid(pid);
+       }
 }
 
-static pid_list *scan_dir_links(const char *dname, pid_t pid,
-                               inode_list *ilist, pid_list *plist)
+static void scan_dir_links(const char *dname, pid_t pid)
 {
        DIR *d;
        struct dirent *de;
@@ -196,100 +183,73 @@ static pid_list *scan_dir_links(const char *dname, pid_t pid,
 
        d = opendir(dname);
        if (!d)
-               return plist;
+               return;
+
        while ((de = readdir(d)) != NULL) {
                lname = concat_subpath_file(dname, de->d_name);
                if (lname == NULL)
                        continue;
-               plist = scan_link(lname, pid, ilist, plist);
+               scan_link(lname, pid);
                free(lname);
        }
        closedir(d);
-       return plist;
 }
 
-static pid_list *scan_proc_pids(inode_list *ilist)
+/* NB: does chdir internally */
+static void scan_proc_pids(void)
 {
        DIR *d;
        struct dirent *de;
        pid_t pid;
-       pid_list *plist;
 
-       d = opendir(".");
+       xchdir("/proc");
+       d = opendir("/proc");
        if (!d)
-               return NULL;
+               return;
 
-       plist = NULL;
        while ((de = readdir(d)) != NULL) {
                pid = (pid_t)bb_strtou(de->d_name, NULL, 10);
                if (errno)
                        continue;
                if (chdir(de->d_name) < 0)
                        continue;
-               plist = scan_link("cwd", pid, ilist, plist);
-               plist = scan_link("exe", pid, ilist, plist);
-               plist = scan_link("root", pid, ilist, plist);
-               plist = scan_dir_links("fd", pid, ilist, plist);
-               plist = scan_dir_links("lib", pid, ilist, plist);
-               plist = scan_dir_links("mmap", pid, ilist, plist);
-               plist = scan_pid_maps("maps", pid, ilist, plist);
-               xchdir("/proc");
-       }
-       closedir(d);
-       return plist;
-}
-
-static int print_pid_list(pid_list *plist)
-{
-       while (plist != NULL) {
-               printf("%u ", (unsigned)plist->pid);
-               plist = plist->next;
-       }
-       bb_putchar('\n');
-       return 1;
-}
+               scan_link("cwd", pid);
+               scan_link("exe", pid);
+               scan_link("root", pid);
 
-static int kill_pid_list(pid_list *plist, int sig)
-{
-       pid_t mypid = getpid();
-       int success = 1;
+               scan_dir_links("fd", pid);
+               scan_dir_links("lib", pid);
+               scan_dir_links("mmap", pid);
 
-       while (plist != NULL) {
-               if (plist->pid != mypid) {
-                       if (kill(plist->pid, sig) != 0) {
-                               bb_perror_msg("kill pid %u", (unsigned)plist->pid);
-                               success = 0;
-                       }
-               }
-               plist = plist->next;
+               scan_pid_maps("maps", pid);
+               xchdir("/proc");
        }
-       return success;
+       closedir(d);
 }
 
 int fuser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int fuser_main(int argc, char **argv)
+int fuser_main(int argc UNUSED_PARAM, char **argv)
 {
        pid_list *plist;
-       inode_list *ilist;
+       pid_t mypid;
        char **pp;
-       dev_t dev;
-       ino_t inode;
+       struct stat st;
        unsigned port;
        int opt;
-       int success;
+       int exitcode;
        int killsig;
 /*
-fuser [options] FILEs or PORT/PROTOs
+fuser [OPTIONS] FILE or PORT/PROTO
 Find processes which use FILEs or PORTs
         -m      Find processes which use same fs as FILEs
         -4      Search only IPv4 space
         -6      Search only IPv6 space
-        -s      Silent: just exit with 0 if any processes are found
-        -k      Kill found processes (otherwise display PIDs)
-        -SIGNAL Signal to send (default: TERM)
+        -s      Don't display PIDs
+        -k      Kill found processes
+        -SIGNAL Signal to send (default: KILL)
 */
        /* Handle -SIGNAL. Oh my... */
-       killsig = SIGTERM;
+       killsig = SIGKILL; /* yes, the default is not SIGTERM */
        pp = argv;
        while (*++pp) {
                char *arg = *pp;
@@ -311,35 +271,58 @@ Find processes which use FILEs or PORTs
                break;
        }
 
+       opt_complementary = "-1"; /* at least one param */
        opt = getopt32(argv, OPTION_STRING);
        argv += optind;
 
-       xchdir("/proc");
-
-       ilist = NULL;
        pp = argv;
        while (*pp) {
-               char *proto = parse_net_arg(*pp, &port);
-               if (proto) { /* PORT/PROTO */
-                       ilist = scan_proc_net(proto, port, ilist);
-                       free(proto);
+               /* parse net arg */
+               char path[20], tproto[5];
+               if (sscanf(*pp, "%u/%4s", &port, tproto) != 2)
+                       goto file;
+               sprintf(path, "/proc/net/%s", tproto);
+               if (access(path, R_OK) != 0) { /* PORT/PROTO */
+                       scan_proc_net(path, port);
                } else { /* FILE */
-                       if (!file_to_dev_inode(*pp, &dev, &inode))
-                               bb_perror_msg_and_die("can't open %s", *pp);
-                       ilist = add_inode(ilist, dev, inode);
+ file:
+                       xstat(*pp, &st);
+                       add_inode(&st);
                }
                pp++;
        }
 
-       plist = scan_proc_pids(ilist);
+       scan_proc_pids(); /* changes dir to "/proc" */
 
-       if (!plist)
-               return EXIT_FAILURE;
-       success = 1;
-       if (opt & OPT_KILL) {
-               success = kill_pid_list(plist, killsig);
-       } else if (!(opt & OPT_SILENT)) {
-               success = print_pid_list(plist);
+       mypid = getpid();
+       plist = G.pid_list_head;
+       while (1) {
+               if (!plist)
+                       return EXIT_FAILURE;
+               if (plist->pid != mypid)
+                       break;
+               plist = plist->next;
        }
-       return (success != 1); /* 0 == success */
+
+       exitcode = EXIT_SUCCESS;
+       do {
+               if (plist->pid != mypid) {
+                       if (opt & OPT_KILL) {
+                               if (kill(plist->pid, killsig) != 0) {
+                                       bb_perror_msg("kill pid %u", (unsigned)plist->pid);
+                                       exitcode = EXIT_FAILURE;
+                               }
+                       }
+                       if (!(opt & OPT_SILENT)) {
+                               printf("%u ", (unsigned)plist->pid);
+                       }
+               }
+               plist = plist->next;
+       } while (plist);
+
+       if (!(opt & (OPT_SILENT))) {
+               bb_putchar('\n');
+       }
+
+       return exitcode;
 }