Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 2 | /* |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 3 | * tiny fuser implementation |
| 4 | * |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 5 | * Copyright 2004 Tony J. White |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame^] | 10 | //usage:#define fuser_trivial_usage |
| 11 | //usage: "[OPTIONS] FILE or PORT/PROTO" |
| 12 | //usage:#define fuser_full_usage "\n\n" |
| 13 | //usage: "Find processes which use FILEs or PORTs\n" |
| 14 | //usage: "\nOptions:" |
| 15 | //usage: "\n -m Find processes which use same fs as FILEs" |
| 16 | //usage: "\n -4,-6 Search only IPv4/IPv6 space" |
| 17 | //usage: "\n -s Don't display PIDs" |
| 18 | //usage: "\n -k Kill found processes" |
| 19 | //usage: "\n -SIGNAL Signal to send (default: KILL)" |
| 20 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 21 | #include "libbb.h" |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 22 | |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 23 | #define MAX_LINE 255 |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 25 | #define OPTION_STRING "mks64" |
| 26 | enum { |
| 27 | OPT_MOUNT = (1 << 0), |
| 28 | OPT_KILL = (1 << 1), |
| 29 | OPT_SILENT = (1 << 2), |
| 30 | OPT_IP6 = (1 << 3), |
| 31 | OPT_IP4 = (1 << 4), |
| 32 | }; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 33 | |
| 34 | typedef struct inode_list { |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 35 | struct inode_list *next; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 36 | ino_t inode; |
| 37 | dev_t dev; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 38 | } inode_list; |
| 39 | |
| 40 | typedef struct pid_list { |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 41 | struct pid_list *next; |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 42 | pid_t pid; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 43 | } pid_list; |
| 44 | |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 45 | |
| 46 | struct globals { |
| 47 | pid_list *pid_list_head; |
| 48 | inode_list *inode_list_head; |
Denys Vlasenko | e8d0a14 | 2011-01-16 11:21:15 +0100 | [diff] [blame] | 49 | } FIX_ALIASING; |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 50 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 51 | #define INIT_G() do { } while (0) |
| 52 | |
| 53 | |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 54 | static void add_pid(const pid_t pid) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 55 | { |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 56 | pid_list **curr = &G.pid_list_head; |
| 57 | |
| 58 | while (*curr) { |
| 59 | if ((*curr)->pid == pid) |
| 60 | return; |
| 61 | curr = &(*curr)->next; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 62 | } |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 63 | |
| 64 | *curr = xzalloc(sizeof(pid_list)); |
| 65 | (*curr)->pid = pid; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 68 | static void add_inode(const struct stat *st) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 69 | { |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 70 | inode_list **curr = &G.inode_list_head; |
| 71 | |
| 72 | while (*curr) { |
| 73 | if ((*curr)->dev == st->st_dev |
| 74 | && (*curr)->inode == st->st_ino |
| 75 | ) { |
| 76 | return; |
| 77 | } |
| 78 | curr = &(*curr)->next; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 79 | } |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 80 | |
| 81 | *curr = xzalloc(sizeof(inode_list)); |
| 82 | (*curr)->dev = st->st_dev; |
| 83 | (*curr)->inode = st->st_ino; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 86 | static void scan_proc_net(const char *path, unsigned port) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 87 | { |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 88 | char line[MAX_LINE + 1]; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 89 | long long uint64_inode; |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 90 | unsigned tmp_port; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 91 | FILE *f; |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 92 | struct stat st; |
Maksym Kryzhanovskyy | e3657dc | 2010-06-06 22:56:12 +0200 | [diff] [blame] | 93 | int fd; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 94 | |
Maksym Kryzhanovskyy | e3657dc | 2010-06-06 22:56:12 +0200 | [diff] [blame] | 95 | /* find socket dev */ |
| 96 | st.st_dev = 0; |
| 97 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 98 | if (fd >= 0) { |
| 99 | fstat(fd, &st); |
| 100 | close(fd); |
| 101 | } |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 102 | |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 103 | f = fopen_for_read(path); |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 104 | if (!f) |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 105 | return; |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 106 | |
| 107 | while (fgets(line, MAX_LINE, f)) { |
Denis Vlasenko | 6b79718 | 2008-07-12 09:32:38 +0000 | [diff] [blame] | 108 | char addr[68]; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 109 | if (sscanf(line, "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x " |
| 110 | "%*x:%*x %*x %*d %*d %llu", |
| 111 | addr, &tmp_port, &uint64_inode) == 3 |
| 112 | ) { |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 113 | int len = strlen(addr); |
| 114 | if (len == 8 && (option_mask32 & OPT_IP6)) |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 115 | continue; |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 116 | if (len > 8 && (option_mask32 & OPT_IP4)) |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 117 | continue; |
| 118 | if (tmp_port == port) { |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 119 | st.st_ino = uint64_inode; |
| 120 | add_inode(&st); |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 123 | } |
| 124 | fclose(f); |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 127 | static int search_dev_inode(const struct stat *st) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 128 | { |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 129 | inode_list *ilist = G.inode_list_head; |
| 130 | |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 131 | while (ilist) { |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 132 | if (ilist->dev == st->st_dev) { |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 133 | if (option_mask32 & OPT_MOUNT) |
| 134 | return 1; |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 135 | if (ilist->inode == st->st_ino) |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 136 | return 1; |
| 137 | } |
| 138 | ilist = ilist->next; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 139 | } |
| 140 | return 0; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 143 | static void scan_pid_maps(const char *fname, pid_t pid) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 144 | { |
| 145 | FILE *file; |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 146 | char line[MAX_LINE + 1]; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 147 | int major, minor; |
Eric Andersen | a68ea1c | 2006-01-30 22:48:39 +0000 | [diff] [blame] | 148 | long long uint64_inode; |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 149 | struct stat st; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 150 | |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 151 | file = fopen_for_read(fname); |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 152 | if (!file) |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 153 | return; |
| 154 | |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 155 | while (fgets(line, MAX_LINE, file)) { |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 156 | if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3) |
| 157 | continue; |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 158 | st.st_ino = uint64_inode; |
| 159 | if (major == 0 && minor == 0 && st.st_ino == 0) |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 160 | continue; |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 161 | st.st_dev = makedev(major, minor); |
| 162 | if (search_dev_inode(&st)) |
| 163 | add_pid(pid); |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 164 | } |
| 165 | fclose(file); |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 168 | static void scan_link(const char *lname, pid_t pid) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 169 | { |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 170 | struct stat st; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 171 | |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 172 | if (stat(lname, &st) >= 0) { |
| 173 | if (search_dev_inode(&st)) |
| 174 | add_pid(pid); |
| 175 | } |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 178 | static void scan_dir_links(const char *dname, pid_t pid) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 179 | { |
| 180 | DIR *d; |
| 181 | struct dirent *de; |
| 182 | char *lname; |
| 183 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 184 | d = opendir(dname); |
| 185 | if (!d) |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 186 | return; |
| 187 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 188 | while ((de = readdir(d)) != NULL) { |
| 189 | lname = concat_subpath_file(dname, de->d_name); |
| 190 | if (lname == NULL) |
| 191 | continue; |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 192 | scan_link(lname, pid); |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 193 | free(lname); |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 194 | } |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 195 | closedir(d); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 196 | } |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 197 | |
Denis Vlasenko | 5de8a13 | 2008-05-28 12:44:22 +0000 | [diff] [blame] | 198 | /* NB: does chdir internally */ |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 199 | static void scan_proc_pids(void) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 200 | { |
| 201 | DIR *d; |
| 202 | struct dirent *de; |
| 203 | pid_t pid; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 204 | |
Denis Vlasenko | cd9d4c8 | 2008-05-28 14:57:58 +0000 | [diff] [blame] | 205 | xchdir("/proc"); |
Denis Vlasenko | 5de8a13 | 2008-05-28 12:44:22 +0000 | [diff] [blame] | 206 | d = opendir("/proc"); |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 207 | if (!d) |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 208 | return; |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 209 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 210 | while ((de = readdir(d)) != NULL) { |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 211 | pid = (pid_t)bb_strtou(de->d_name, NULL, 10); |
| 212 | if (errno) |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 213 | continue; |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 214 | if (chdir(de->d_name) < 0) |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 215 | continue; |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 216 | scan_link("cwd", pid); |
| 217 | scan_link("exe", pid); |
| 218 | scan_link("root", pid); |
| 219 | |
| 220 | scan_dir_links("fd", pid); |
| 221 | scan_dir_links("lib", pid); |
| 222 | scan_dir_links("mmap", pid); |
| 223 | |
| 224 | scan_pid_maps("maps", pid); |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 225 | xchdir("/proc"); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 226 | } |
| 227 | closedir(d); |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 230 | int fuser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 231 | int fuser_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | 31642d7 | 2006-03-14 21:45:38 +0000 | [diff] [blame] | 232 | { |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 233 | pid_list *plist; |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 234 | pid_t mypid; |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 235 | char **pp; |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 236 | struct stat st; |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 237 | unsigned port; |
| 238 | int opt; |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 239 | int exitcode; |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 240 | int killsig; |
| 241 | /* |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 242 | fuser [OPTIONS] FILE or PORT/PROTO |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 243 | Find processes which use FILEs or PORTs |
| 244 | -m Find processes which use same fs as FILEs |
| 245 | -4 Search only IPv4 space |
| 246 | -6 Search only IPv6 space |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 247 | -s Don't display PIDs |
| 248 | -k Kill found processes |
| 249 | -SIGNAL Signal to send (default: KILL) |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 250 | */ |
| 251 | /* Handle -SIGNAL. Oh my... */ |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 252 | killsig = SIGKILL; /* yes, the default is not SIGTERM */ |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 253 | pp = argv; |
| 254 | while (*++pp) { |
| 255 | char *arg = *pp; |
| 256 | if (arg[0] != '-') |
| 257 | continue; |
| 258 | if (arg[1] == '-' && arg[2] == '\0') /* "--" */ |
| 259 | break; |
| 260 | if ((arg[1] == '4' || arg[1] == '6') && arg[2] == '\0') |
| 261 | continue; /* it's "-4" or "-6" */ |
| 262 | opt = get_signum(&arg[1]); |
| 263 | if (opt < 0) |
| 264 | continue; |
| 265 | /* "-SIGNAL" option found. Remove it and bail out */ |
| 266 | killsig = opt; |
| 267 | do { |
| 268 | pp[0] = arg = pp[1]; |
| 269 | pp++; |
| 270 | } while (arg); |
| 271 | break; |
| 272 | } |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 273 | |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 274 | opt_complementary = "-1"; /* at least one param */ |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 275 | opt = getopt32(argv, OPTION_STRING); |
| 276 | argv += optind; |
Mike Frysinger | 70cbb6e | 2006-04-21 22:04:05 +0000 | [diff] [blame] | 277 | |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 278 | pp = argv; |
| 279 | while (*pp) { |
Maksym Kryzhanovskyy | e3657dc | 2010-06-06 22:56:12 +0200 | [diff] [blame] | 280 | /* parse net arg */ |
| 281 | char path[20], tproto[5]; |
| 282 | if (sscanf(*pp, "%u/%4s", &port, tproto) != 2) |
| 283 | goto file; |
| 284 | sprintf(path, "/proc/net/%s", tproto); |
| 285 | if (access(path, R_OK) != 0) { /* PORT/PROTO */ |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 286 | scan_proc_net(path, port); |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 287 | } else { /* FILE */ |
Maksym Kryzhanovskyy | e3657dc | 2010-06-06 22:56:12 +0200 | [diff] [blame] | 288 | file: |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 289 | xstat(*pp, &st); |
| 290 | add_inode(&st); |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 291 | } |
Denis Vlasenko | afc4113 | 2008-03-17 08:44:58 +0000 | [diff] [blame] | 292 | pp++; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 293 | } |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 294 | |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 295 | scan_proc_pids(); /* changes dir to "/proc" */ |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 296 | |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 297 | mypid = getpid(); |
| 298 | plist = G.pid_list_head; |
| 299 | while (1) { |
| 300 | if (!plist) |
| 301 | return EXIT_FAILURE; |
| 302 | if (plist->pid != mypid) |
| 303 | break; |
| 304 | plist = plist->next; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 305 | } |
Maksym Kryzhanovskyy | fef9ee7 | 2010-05-22 20:41:08 +0200 | [diff] [blame] | 306 | |
| 307 | exitcode = EXIT_SUCCESS; |
| 308 | do { |
| 309 | if (plist->pid != mypid) { |
| 310 | if (opt & OPT_KILL) { |
| 311 | if (kill(plist->pid, killsig) != 0) { |
| 312 | bb_perror_msg("kill pid %u", (unsigned)plist->pid); |
| 313 | exitcode = EXIT_FAILURE; |
| 314 | } |
| 315 | } |
| 316 | if (!(opt & OPT_SILENT)) { |
| 317 | printf("%u ", (unsigned)plist->pid); |
| 318 | } |
| 319 | } |
| 320 | plist = plist->next; |
| 321 | } while (plist); |
| 322 | |
| 323 | if (!(opt & (OPT_SILENT))) { |
| 324 | bb_putchar('\n'); |
| 325 | } |
| 326 | |
| 327 | return exitcode; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 328 | } |