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