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