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