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