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 | * |
| 7 | * May be distributed under the conditions of the |
| 8 | * GNU Library General Public License |
| 9 | */ |
| 10 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 11 | #include "busybox.h" |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 12 | |
| 13 | #define FUSER_PROC_DIR "/proc" |
| 14 | #define FUSER_MAX_LINE 255 |
| 15 | |
| 16 | #define FUSER_OPT_MOUNT 1 |
| 17 | #define FUSER_OPT_KILL 2 |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 18 | #define FUSER_OPT_SILENT 4 |
| 19 | #define FUSER_OPT_IP6 8 |
| 20 | #define FUSER_OPT_IP4 16 |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 21 | |
| 22 | typedef struct inode_list { |
| 23 | ino_t inode; |
| 24 | dev_t dev; |
| 25 | struct inode_list *next; |
| 26 | } inode_list; |
| 27 | |
| 28 | typedef struct pid_list { |
| 29 | pid_t pid; |
| 30 | struct pid_list *next; |
| 31 | } pid_list; |
| 32 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 33 | static int fuser_option(char *option) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 34 | { |
| 35 | int opt = 0; |
| 36 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 37 | if (!option[0]) |
| 38 | return 0; |
| 39 | if (option[0] != '-') |
| 40 | return 0; |
Paul Fox | f9d40d6 | 2006-01-20 21:48:06 +0000 | [diff] [blame] | 41 | ++option; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 42 | while (*option != '\0') { |
| 43 | if (*option == 'm') opt |= FUSER_OPT_MOUNT; |
| 44 | else if (*option == 'k') opt |= FUSER_OPT_KILL; |
| 45 | else if (*option == 's') opt |= FUSER_OPT_SILENT; |
| 46 | else if (*option == '6') opt |= FUSER_OPT_IP6; |
| 47 | else if (*option == '4') opt |= FUSER_OPT_IP4; |
| 48 | else |
| 49 | bb_error_msg_and_die("unsupported option '%c'", *option); |
Paul Fox | f9d40d6 | 2006-01-20 21:48:06 +0000 | [diff] [blame] | 50 | ++option; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 51 | } |
| 52 | return opt; |
| 53 | } |
| 54 | |
| 55 | static int fuser_file_to_dev_inode(const char *filename, |
| 56 | dev_t *dev, ino_t *inode) |
| 57 | { |
| 58 | struct stat f_stat; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 59 | if ((stat(filename, &f_stat)) < 0) |
| 60 | return 0; |
"Vladimir N. Oleynik" | 7eb8e45 | 2005-10-28 16:08:47 +0000 | [diff] [blame] | 61 | *inode = f_stat.st_ino; |
| 62 | *dev = f_stat.st_dev; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 63 | return 1; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Rob Landley | 31642d7 | 2006-03-14 21:45:38 +0000 | [diff] [blame] | 66 | static int fuser_find_socket_dev(dev_t *dev) |
| 67 | { |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 68 | int fd = socket(PF_INET, SOCK_DGRAM,0); |
| 69 | struct stat buf; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 70 | |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 71 | if (fd >= 0 && (fstat(fd, &buf)) == 0) { |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 72 | *dev = buf.st_dev; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 73 | close(fd); |
| 74 | return 1; |
| 75 | } |
| 76 | return 0; |
| 77 | } |
| 78 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 79 | static int fuser_parse_net_arg(const char *filename, |
| 80 | const char **proto, int *port) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 81 | { |
"Vladimir N. Oleynik" | 7eb8e45 | 2005-10-28 16:08:47 +0000 | [diff] [blame] | 82 | char path[sizeof(FUSER_PROC_DIR)+12], tproto[5]; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 83 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 84 | if ((sscanf(filename, "%d/%4s", port, tproto)) != 2) |
| 85 | return 0; |
| 86 | sprintf(path, FUSER_PROC_DIR "/net/%s", tproto); |
| 87 | if ((access(path, R_OK)) != 0) |
| 88 | return 0; |
Rob Landley | 86b4d64 | 2006-08-03 17:58:17 +0000 | [diff] [blame] | 89 | *proto = xstrdup(tproto); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 90 | return 1; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static int fuser_add_pid(pid_list *plist, pid_t pid) |
| 94 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 95 | pid_list *curr = NULL, *last = NULL; |
| 96 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 97 | if (plist->pid == 0) |
| 98 | plist->pid = pid; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 99 | curr = plist; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 100 | while (curr != NULL) { |
| 101 | if (curr->pid == pid) |
| 102 | return 1; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 103 | last = curr; |
| 104 | curr = curr->next; |
| 105 | } |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 106 | curr = xzalloc(sizeof(pid_list)); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 107 | last->next = curr; |
| 108 | curr->pid = pid; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 109 | /*curr->next = NULL;*/ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 110 | return 1; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static int fuser_add_inode(inode_list *ilist, dev_t dev, ino_t inode) |
| 114 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 115 | inode_list *curr = NULL, *last = NULL; |
| 116 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 117 | if (!ilist->inode && !ilist->dev) { |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 118 | ilist->dev = dev; |
| 119 | ilist->inode = inode; |
| 120 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 121 | curr = ilist; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 122 | while (curr != NULL) { |
| 123 | if (curr->inode == inode && curr->dev == dev) |
| 124 | return 1; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 125 | last = curr; |
| 126 | curr = curr->next; |
| 127 | } |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 128 | curr = xzalloc(sizeof(inode_list)); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 129 | last->next = curr; |
| 130 | curr->dev = dev; |
| 131 | curr->inode = inode; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 132 | /*curr->next = NULL;*/ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 133 | return 1; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 136 | static int fuser_scan_proc_net(int opts, const char *proto, |
| 137 | int port, inode_list *ilist) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 138 | { |
"Vladimir N. Oleynik" | 7eb8e45 | 2005-10-28 16:08:47 +0000 | [diff] [blame] | 139 | char path[sizeof(FUSER_PROC_DIR)+12], line[FUSER_MAX_LINE+1]; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 140 | char addr[128]; |
| 141 | ino_t tmp_inode; |
| 142 | dev_t tmp_dev; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 143 | long long uint64_inode; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 144 | int tmp_port; |
| 145 | FILE *f; |
| 146 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 147 | if (!fuser_find_socket_dev(&tmp_dev)) |
| 148 | tmp_dev = 0; |
| 149 | sprintf(path, FUSER_PROC_DIR "/net/%s", proto); |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 150 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 151 | f = fopen(path, "r"); |
| 152 | if (!f) |
| 153 | return 0; |
| 154 | while (fgets(line, FUSER_MAX_LINE, f)) { |
| 155 | if (sscanf(line, "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x " |
| 156 | "%*x:%*x %*x %*d %*d %llu", |
| 157 | addr, &tmp_port, &uint64_inode) == 3 |
| 158 | ) { |
| 159 | if (strlen(addr) == 8 && (opts & FUSER_OPT_IP6)) |
| 160 | continue; |
| 161 | if (strlen(addr) > 8 && (opts & FUSER_OPT_IP4)) |
| 162 | continue; |
| 163 | if (tmp_port == port) { |
"Vladimir N. Oleynik" | 7eb8e45 | 2005-10-28 16:08:47 +0000 | [diff] [blame] | 164 | tmp_inode = uint64_inode; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 165 | fuser_add_inode(ilist, tmp_dev, tmp_inode); |
| 166 | } |
| 167 | } |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 168 | } |
| 169 | fclose(f); |
| 170 | return 1; |
| 171 | } |
| 172 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 173 | static int fuser_search_dev_inode(int opts, inode_list *ilist, |
| 174 | dev_t dev, ino_t inode) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 175 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 176 | inode_list *curr; |
| 177 | curr = ilist; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 178 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 179 | while (curr) { |
| 180 | if ((opts & FUSER_OPT_MOUNT) && curr->dev == dev) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 181 | return 1; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 182 | if (curr->inode == inode && curr->dev == dev) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 183 | return 1; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 184 | curr = curr->next; |
| 185 | } |
| 186 | return 0; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static int fuser_scan_pid_maps(int opts, const char *fname, pid_t pid, |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 190 | inode_list *ilist, pid_list *plist) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 191 | { |
| 192 | FILE *file; |
| 193 | char line[FUSER_MAX_LINE + 1]; |
| 194 | int major, minor; |
| 195 | ino_t inode; |
Eric Andersen | a68ea1c | 2006-01-30 22:48:39 +0000 | [diff] [blame] | 196 | long long uint64_inode; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 197 | dev_t dev; |
| 198 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 199 | file = fopen(fname, "r"); |
| 200 | if (!file) |
| 201 | return 0; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 202 | while (fgets(line, FUSER_MAX_LINE, file)) { |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 203 | if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3) |
| 204 | continue; |
"Vladimir N. Oleynik" | 7eb8e45 | 2005-10-28 16:08:47 +0000 | [diff] [blame] | 205 | inode = uint64_inode; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 206 | if (major == 0 && minor == 0 && inode == 0) |
| 207 | continue; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 208 | dev = makedev(major, minor); |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 209 | if (fuser_search_dev_inode(opts, ilist, dev, inode)) { |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 210 | fuser_add_pid(plist, pid); |
| 211 | } |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 212 | } |
| 213 | fclose(file); |
| 214 | return 1; |
| 215 | } |
| 216 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 217 | static int fuser_scan_link(int opts, const char *lname, pid_t pid, |
| 218 | inode_list *ilist, pid_list *plist) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 219 | { |
| 220 | ino_t inode; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 221 | dev_t dev; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 222 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 223 | if (!fuser_file_to_dev_inode(lname, &dev, &inode)) |
| 224 | return 0; |
| 225 | if (fuser_search_dev_inode(opts, ilist, dev, inode)) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 226 | fuser_add_pid(plist, pid); |
| 227 | return 1; |
| 228 | } |
| 229 | |
| 230 | static int fuser_scan_dir_links(int opts, const char *dname, pid_t pid, |
| 231 | inode_list *ilist, pid_list *plist) |
| 232 | { |
| 233 | DIR *d; |
| 234 | struct dirent *de; |
| 235 | char *lname; |
| 236 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 237 | d = opendir(dname); |
| 238 | if (!d) |
| 239 | return 0; |
| 240 | while ((de = readdir(d)) != NULL) { |
| 241 | lname = concat_subpath_file(dname, de->d_name); |
| 242 | if (lname == NULL) |
| 243 | continue; |
| 244 | fuser_scan_link(opts, lname, pid, ilist, plist); |
| 245 | free(lname); |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 246 | } |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 247 | closedir(d); |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 248 | return 1; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 249 | } |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 250 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 251 | static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 252 | { |
| 253 | DIR *d; |
| 254 | struct dirent *de; |
| 255 | pid_t pid; |
| 256 | char *dname; |
| 257 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 258 | d = opendir(FUSER_PROC_DIR); |
| 259 | if (!d) |
| 260 | return 0; |
| 261 | while ((de = readdir(d)) != NULL) { |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 262 | pid = (pid_t)atoi(de->d_name); |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 263 | if (!pid) |
| 264 | continue; |
"Vladimir N. Oleynik" | 7eb8e45 | 2005-10-28 16:08:47 +0000 | [diff] [blame] | 265 | dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name); |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 266 | if (chdir(dname) < 0) { |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 267 | free(dname); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 268 | continue; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 269 | } |
| 270 | free(dname); |
| 271 | fuser_scan_link(opts, "cwd", pid, ilist, plist); |
| 272 | fuser_scan_link(opts, "exe", pid, ilist, plist); |
| 273 | fuser_scan_link(opts, "root", pid, ilist, plist); |
| 274 | fuser_scan_dir_links(opts, "fd", pid, ilist, plist); |
| 275 | fuser_scan_dir_links(opts, "lib", pid, ilist, plist); |
| 276 | fuser_scan_dir_links(opts, "mmap", pid, ilist, plist); |
| 277 | fuser_scan_pid_maps(opts, "maps", pid, ilist, plist); |
"Vladimir N. Oleynik" | 7eb8e45 | 2005-10-28 16:08:47 +0000 | [diff] [blame] | 278 | chdir(".."); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 279 | } |
| 280 | closedir(d); |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 281 | return 1; |
| 282 | } |
| 283 | |
Rob Landley | 31642d7 | 2006-03-14 21:45:38 +0000 | [diff] [blame] | 284 | static int fuser_print_pid_list(pid_list *plist) |
| 285 | { |
Paul Fox | f9d40d6 | 2006-01-20 21:48:06 +0000 | [diff] [blame] | 286 | pid_list *curr = plist; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 287 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 288 | if (plist == NULL) |
| 289 | return 0; |
| 290 | while (curr != NULL) { |
| 291 | if (curr->pid > 0) |
| 292 | printf("%d ", curr->pid); |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 293 | curr = curr->next; |
| 294 | } |
Denis Vlasenko | c6f188d | 2006-10-26 00:37:00 +0000 | [diff] [blame] | 295 | puts(""); |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 296 | return 1; |
| 297 | } |
| 298 | |
Rob Landley | 31642d7 | 2006-03-14 21:45:38 +0000 | [diff] [blame] | 299 | static int fuser_kill_pid_list(pid_list *plist, int sig) |
| 300 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 301 | pid_list *curr = plist; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 302 | pid_t mypid = getpid(); |
| 303 | int success = 1; |
| 304 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 305 | if (plist == NULL) |
| 306 | return 0; |
| 307 | while (curr != NULL) { |
| 308 | if (curr->pid > 0 && curr->pid != mypid) { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 309 | if (kill(curr->pid, sig) != 0) { |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 310 | bb_perror_msg("kill pid '%d'", curr->pid); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 311 | success = 0; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 314 | curr = curr->next; |
| 315 | } |
| 316 | return success; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 319 | int fuser_main(int argc, char **argv); |
Rob Landley | 31642d7 | 2006-03-14 21:45:38 +0000 | [diff] [blame] | 320 | int fuser_main(int argc, char **argv) |
| 321 | { |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 322 | /*static -- huh???*/ int opt = 0; /* FUSER_OPT_ */ |
| 323 | |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 324 | int port, i, optn; |
| 325 | int* fni; /* file name indexes of argv */ |
| 326 | int fnic = 0; /* file name index count */ |
| 327 | const char *proto; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 328 | dev_t dev; |
| 329 | ino_t inode; |
| 330 | pid_list *pids; |
| 331 | inode_list *inodes; |
| 332 | int killsig = SIGTERM; |
| 333 | int success = 1; |
| 334 | |
Mike Frysinger | 70cbb6e | 2006-04-21 22:04:05 +0000 | [diff] [blame] | 335 | if (argc < 2) |
| 336 | bb_show_usage(); |
| 337 | |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 338 | fni = xmalloc(sizeof(int)); |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 339 | for (i = 1; i < argc; i++) { |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 340 | optn = fuser_option(argv[i]); |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 341 | if (optn) |
| 342 | opt |= optn; |
| 343 | else if (argv[i][0] == '-') { |
Denis Vlasenko | 2a813e2 | 2006-12-23 01:06:21 +0000 | [diff] [blame] | 344 | killsig = get_signum(argv[i]+1); |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 345 | if (killsig < 0) |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 346 | killsig = SIGTERM; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 347 | } else { |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 348 | fni = xrealloc(fni, sizeof(int) * (fnic+2)); |
| 349 | fni[fnic++] = i; |
| 350 | } |
| 351 | } |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 352 | |
| 353 | if (!fnic) |
| 354 | return 1; |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 355 | |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 356 | inodes = xmalloc(sizeof(inode_list)); |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 357 | for (i = 0; i < fnic; i++) { |
| 358 | if (fuser_parse_net_arg(argv[fni[i]], &proto, &port)) { |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 359 | fuser_scan_proc_net(opt, proto, port, inodes); |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 360 | } else { |
| 361 | if (!fuser_file_to_dev_inode(argv[fni[i]], &dev, &inode)) { |
| 362 | if (ENABLE_FEATURE_CLEAN_UP) |
| 363 | free(inodes); |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 364 | bb_perror_msg_and_die("cannot open '%s'", argv[fni[i]]); |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 365 | } |
| 366 | fuser_add_inode(inodes, dev, inode); |
| 367 | } |
| 368 | } |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 369 | pids = xmalloc(sizeof(pid_list)); |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 370 | success = fuser_scan_proc_pids(opt, inodes, pids); |
| 371 | /* if the first pid in the list is 0, none have been found */ |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 372 | if (pids->pid == 0) |
| 373 | success = 0; |
| 374 | if (success) { |
| 375 | if (opt & FUSER_OPT_KILL) { |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 376 | success = fuser_kill_pid_list(pids, killsig); |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 377 | } else if (!(opt & FUSER_OPT_SILENT)) { |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 378 | success = fuser_print_pid_list(pids); |
| 379 | } |
| 380 | } |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 381 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 382 | free(pids); |
| 383 | free(inodes); |
| 384 | } |
Rob Landley | aa87276 | 2005-10-28 13:05:12 +0000 | [diff] [blame] | 385 | /* return 0 on (success == 1) 1 otherwise */ |
| 386 | return (success != 1); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 387 | } |