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