blob: 8afa958b63f268108d5f9ab62e84de4cd120598e [file] [log] [blame]
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00001/* vi: set sw=4 ts=4: */
Rob Landleyaa872762005-10-28 13:05:12 +00002/*
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003 * tiny fuser implementation
4 *
Rob Landleyaa872762005-10-28 13:05:12 +00005 * Copyright 2004 Tony J. White
6 *
7 * May be distributed under the conditions of the
8 * GNU Library General Public License
9 */
10
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000011#include "libbb.h"
Rob Landleyaa872762005-10-28 13:05:12 +000012
Denis Vlasenkoafc41132008-03-17 08:44:58 +000013#define MAX_LINE 255
Rob Landleyaa872762005-10-28 13:05:12 +000014
Denis Vlasenkoafc41132008-03-17 08:44:58 +000015#define OPTION_STRING "mks64"
16enum {
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 Landleyaa872762005-10-28 13:05:12 +000023
24typedef struct inode_list {
Denis Vlasenkoafc41132008-03-17 08:44:58 +000025 struct inode_list *next;
Rob Landleyaa872762005-10-28 13:05:12 +000026 ino_t inode;
27 dev_t dev;
Rob Landleyaa872762005-10-28 13:05:12 +000028} inode_list;
29
30typedef struct pid_list {
Rob Landleyaa872762005-10-28 13:05:12 +000031 struct pid_list *next;
Denis Vlasenkoafc41132008-03-17 08:44:58 +000032 pid_t pid;
Rob Landleyaa872762005-10-28 13:05:12 +000033} pid_list;
34
Denis Vlasenkoafc41132008-03-17 08:44:58 +000035static dev_t find_socket_dev(void)
Rob Landleyaa872762005-10-28 13:05:12 +000036{
Denis Vlasenkoafc41132008-03-17 08:44:58 +000037 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 Landleyaa872762005-10-28 13:05:12 +000044 }
Denis Vlasenkoafc41132008-03-17 08:44:58 +000045 return 0;
Rob Landleyaa872762005-10-28 13:05:12 +000046}
47
Denis Vlasenkoafc41132008-03-17 08:44:58 +000048static int file_to_dev_inode(const char *filename, dev_t *dev, ino_t *inode)
Rob Landleyaa872762005-10-28 13:05:12 +000049{
50 struct stat f_stat;
Denis Vlasenkoafc41132008-03-17 08:44:58 +000051 if (stat(filename, &f_stat))
Denis Vlasenko50f7f442007-04-11 23:20:53 +000052 return 0;
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +000053 *inode = f_stat.st_ino;
54 *dev = f_stat.st_dev;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000055 return 1;
Rob Landleyaa872762005-10-28 13:05:12 +000056}
57
Denis Vlasenkoafc41132008-03-17 08:44:58 +000058static char *parse_net_arg(const char *arg, unsigned *port)
Rob Landley31642d72006-03-14 21:45:38 +000059{
Denis Vlasenko1b2058f2008-03-29 17:59:27 +000060 char path[20], tproto[5];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000061
Denis Vlasenkoafc41132008-03-17 08:44:58 +000062 if (sscanf(arg, "%u/%4s", port, tproto) != 2)
63 return NULL;
Denis Vlasenko1b2058f2008-03-29 17:59:27 +000064 sprintf(path, "/proc/net/%s", tproto);
Denis Vlasenkoafc41132008-03-17 08:44:58 +000065 if (access(path, R_OK) != 0)
66 return NULL;
67 return xstrdup(tproto);
Rob Landleyaa872762005-10-28 13:05:12 +000068}
69
Denis Vlasenkoafc41132008-03-17 08:44:58 +000070static pid_list *add_pid(pid_list *plist, pid_t pid)
Rob Landleyaa872762005-10-28 13:05:12 +000071{
Denis Vlasenkoafc41132008-03-17 08:44:58 +000072 pid_list *curr = plist;
Denis Vlasenko50f7f442007-04-11 23:20:53 +000073 while (curr != NULL) {
74 if (curr->pid == pid)
Denis Vlasenkoafc41132008-03-17 08:44:58 +000075 return plist;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000076 curr = curr->next;
77 }
Denis Vlasenkoafc41132008-03-17 08:44:58 +000078 curr = xmalloc(sizeof(pid_list));
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000079 curr->pid = pid;
Denis Vlasenkoafc41132008-03-17 08:44:58 +000080 curr->next = plist;
81 return curr;
Rob Landleyaa872762005-10-28 13:05:12 +000082}
83
Denis Vlasenkoafc41132008-03-17 08:44:58 +000084static inode_list *add_inode(inode_list *ilist, dev_t dev, ino_t inode)
Rob Landleyaa872762005-10-28 13:05:12 +000085{
Denis Vlasenkoafc41132008-03-17 08:44:58 +000086 inode_list *curr = ilist;
Denis Vlasenko50f7f442007-04-11 23:20:53 +000087 while (curr != NULL) {
88 if (curr->inode == inode && curr->dev == dev)
Denis Vlasenkoafc41132008-03-17 08:44:58 +000089 return ilist;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000090 curr = curr->next;
91 }
Denis Vlasenkoafc41132008-03-17 08:44:58 +000092 curr = xmalloc(sizeof(inode_list));
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000093 curr->dev = dev;
94 curr->inode = inode;
Denis Vlasenkoafc41132008-03-17 08:44:58 +000095 curr->next = ilist;
96 return curr;
Rob Landleyaa872762005-10-28 13:05:12 +000097}
98
Denis Vlasenkoafc41132008-03-17 08:44:58 +000099static inode_list *scan_proc_net(const char *proto,
100 unsigned port, inode_list *ilist)
Rob Landleyaa872762005-10-28 13:05:12 +0000101{
Denis Vlasenko1b2058f2008-03-29 17:59:27 +0000102 char path[20], line[MAX_LINE + 1];
Rob Landleyaa872762005-10-28 13:05:12 +0000103 char addr[128];
104 ino_t tmp_inode;
105 dev_t tmp_dev;
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000106 long long uint64_inode;
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000107 unsigned tmp_port;
Rob Landleyaa872762005-10-28 13:05:12 +0000108 FILE *f;
109
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000110 tmp_dev = find_socket_dev();
Rob Landleyaa872762005-10-28 13:05:12 +0000111
Denis Vlasenko1b2058f2008-03-29 17:59:27 +0000112 sprintf(path, "/proc/net/%s", proto);
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000113 f = fopen(path, "r");
114 if (!f)
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000115 return ilist;
116
117 while (fgets(line, MAX_LINE, f)) {
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000118 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 Vlasenkoafc41132008-03-17 08:44:58 +0000122 if (strlen(addr) == 8 && (option_mask32 & OPT_IP6))
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000123 continue;
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000124 if (strlen(addr) > 8 && (option_mask32 & OPT_IP4))
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000125 continue;
126 if (tmp_port == port) {
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +0000127 tmp_inode = uint64_inode;
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000128 ilist = add_inode(ilist, tmp_dev, tmp_inode);
Rob Landleyaa872762005-10-28 13:05:12 +0000129 }
130 }
Rob Landleyaa872762005-10-28 13:05:12 +0000131 }
132 fclose(f);
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000133 return ilist;
Rob Landleyaa872762005-10-28 13:05:12 +0000134}
135
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000136static int search_dev_inode(inode_list *ilist, dev_t dev, ino_t inode)
Rob Landleyaa872762005-10-28 13:05:12 +0000137{
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000138 while (ilist) {
139 if (ilist->dev == dev) {
140 if (option_mask32 & OPT_MOUNT)
141 return 1;
142 if (ilist->inode == inode)
143 return 1;
144 }
145 ilist = ilist->next;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000146 }
147 return 0;
Rob Landleyaa872762005-10-28 13:05:12 +0000148}
149
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000150static pid_list *scan_pid_maps(const char *fname, pid_t pid,
151 inode_list *ilist, pid_list *plist)
Rob Landleyaa872762005-10-28 13:05:12 +0000152{
153 FILE *file;
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000154 char line[MAX_LINE + 1];
Rob Landleyaa872762005-10-28 13:05:12 +0000155 int major, minor;
156 ino_t inode;
Eric Andersena68ea1c2006-01-30 22:48:39 +0000157 long long uint64_inode;
Rob Landleyaa872762005-10-28 13:05:12 +0000158 dev_t dev;
159
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000160 file = fopen(fname, "r");
161 if (!file)
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000162 return plist;
163 while (fgets(line, MAX_LINE, file)) {
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000164 if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3)
165 continue;
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +0000166 inode = uint64_inode;
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000167 if (major == 0 && minor == 0 && inode == 0)
168 continue;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000169 dev = makedev(major, minor);
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000170 if (search_dev_inode(ilist, dev, inode))
171 plist = add_pid(plist, pid);
Rob Landleyaa872762005-10-28 13:05:12 +0000172 }
173 fclose(file);
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000174 return plist;
Rob Landleyaa872762005-10-28 13:05:12 +0000175}
176
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000177static pid_list *scan_link(const char *lname, pid_t pid,
178 inode_list *ilist, pid_list *plist)
Rob Landleyaa872762005-10-28 13:05:12 +0000179{
180 ino_t inode;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000181 dev_t dev;
Rob Landleyaa872762005-10-28 13:05:12 +0000182
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000183 if (!file_to_dev_inode(lname, &dev, &inode))
184 return plist;
185 if (search_dev_inode(ilist, dev, inode))
186 plist = add_pid(plist, pid);
187 return plist;
Rob Landleyaa872762005-10-28 13:05:12 +0000188}
189
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000190static pid_list *scan_dir_links(const char *dname, pid_t pid,
191 inode_list *ilist, pid_list *plist)
Rob Landleyaa872762005-10-28 13:05:12 +0000192{
193 DIR *d;
194 struct dirent *de;
195 char *lname;
196
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000197 d = opendir(dname);
198 if (!d)
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000199 return plist;
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000200 while ((de = readdir(d)) != NULL) {
201 lname = concat_subpath_file(dname, de->d_name);
202 if (lname == NULL)
203 continue;
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000204 plist = scan_link(lname, pid, ilist, plist);
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000205 free(lname);
Rob Landleyaa872762005-10-28 13:05:12 +0000206 }
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000207 closedir(d);
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000208 return plist;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000209}
Rob Landleyaa872762005-10-28 13:05:12 +0000210
Denis Vlasenko5de8a132008-05-28 12:44:22 +0000211/* NB: does chdir internally */
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000212static pid_list *scan_proc_pids(inode_list *ilist)
Rob Landleyaa872762005-10-28 13:05:12 +0000213{
214 DIR *d;
215 struct dirent *de;
216 pid_t pid;
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000217 pid_list *plist;
Rob Landleyaa872762005-10-28 13:05:12 +0000218
Denis Vlasenkocd9d4c82008-05-28 14:57:58 +0000219 xchdir("/proc");
Denis Vlasenko5de8a132008-05-28 12:44:22 +0000220 d = opendir("/proc");
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000221 if (!d)
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000222 return NULL;
223
224 plist = NULL;
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000225 while ((de = readdir(d)) != NULL) {
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000226 pid = (pid_t)bb_strtou(de->d_name, NULL, 10);
227 if (errno)
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000228 continue;
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000229 if (chdir(de->d_name) < 0)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000230 continue;
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000231 plist = scan_link("cwd", pid, ilist, plist);
232 plist = scan_link("exe", pid, ilist, plist);
233 plist = scan_link("root", pid, ilist, plist);
234 plist = scan_dir_links("fd", pid, ilist, plist);
235 plist = scan_dir_links("lib", pid, ilist, plist);
236 plist = scan_dir_links("mmap", pid, ilist, plist);
237 plist = scan_pid_maps("maps", pid, ilist, plist);
238 xchdir("/proc");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000239 }
240 closedir(d);
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000241 return plist;
Rob Landleyaa872762005-10-28 13:05:12 +0000242}
243
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000244static int print_pid_list(pid_list *plist)
Rob Landley31642d72006-03-14 21:45:38 +0000245{
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000246 while (plist != NULL) {
247 printf("%u ", (unsigned)plist->pid);
248 plist = plist->next;
Rob Landleyaa872762005-10-28 13:05:12 +0000249 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000250 bb_putchar('\n');
Rob Landleyaa872762005-10-28 13:05:12 +0000251 return 1;
252}
253
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000254static int kill_pid_list(pid_list *plist, int sig)
Rob Landley31642d72006-03-14 21:45:38 +0000255{
Rob Landleyaa872762005-10-28 13:05:12 +0000256 pid_t mypid = getpid();
257 int success = 1;
258
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000259 while (plist != NULL) {
260 if (plist->pid != mypid) {
261 if (kill(plist->pid, sig) != 0) {
262 bb_perror_msg("kill pid %u", (unsigned)plist->pid);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000263 success = 0;
Rob Landleyaa872762005-10-28 13:05:12 +0000264 }
265 }
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000266 plist = plist->next;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000267 }
268 return success;
Rob Landleyaa872762005-10-28 13:05:12 +0000269}
270
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000271int fuser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000272int fuser_main(int argc UNUSED_PARAM, char **argv)
Rob Landley31642d72006-03-14 21:45:38 +0000273{
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000274 pid_list *plist;
275 inode_list *ilist;
276 char **pp;
Rob Landleyaa872762005-10-28 13:05:12 +0000277 dev_t dev;
278 ino_t inode;
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000279 unsigned port;
280 int opt;
281 int success;
282 int killsig;
283/*
284fuser [options] FILEs or PORT/PROTOs
285Find processes which use FILEs or PORTs
286 -m Find processes which use same fs as FILEs
287 -4 Search only IPv4 space
288 -6 Search only IPv6 space
289 -s Silent: just exit with 0 if any processes are found
290 -k Kill found processes (otherwise display PIDs)
291 -SIGNAL Signal to send (default: TERM)
292*/
293 /* Handle -SIGNAL. Oh my... */
294 killsig = SIGTERM;
295 pp = argv;
296 while (*++pp) {
297 char *arg = *pp;
298 if (arg[0] != '-')
299 continue;
300 if (arg[1] == '-' && arg[2] == '\0') /* "--" */
301 break;
302 if ((arg[1] == '4' || arg[1] == '6') && arg[2] == '\0')
303 continue; /* it's "-4" or "-6" */
304 opt = get_signum(&arg[1]);
305 if (opt < 0)
306 continue;
307 /* "-SIGNAL" option found. Remove it and bail out */
308 killsig = opt;
309 do {
310 pp[0] = arg = pp[1];
311 pp++;
312 } while (arg);
313 break;
314 }
Rob Landleyaa872762005-10-28 13:05:12 +0000315
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000316 opt = getopt32(argv, OPTION_STRING);
317 argv += optind;
Mike Frysinger70cbb6e2006-04-21 22:04:05 +0000318
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000319 ilist = NULL;
320 pp = argv;
321 while (*pp) {
322 char *proto = parse_net_arg(*pp, &port);
323 if (proto) { /* PORT/PROTO */
324 ilist = scan_proc_net(proto, port, ilist);
325 free(proto);
326 } else { /* FILE */
327 if (!file_to_dev_inode(*pp, &dev, &inode))
328 bb_perror_msg_and_die("can't open %s", *pp);
329 ilist = add_inode(ilist, dev, inode);
Rob Landleyaa872762005-10-28 13:05:12 +0000330 }
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000331 pp++;
Rob Landleyaa872762005-10-28 13:05:12 +0000332 }
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000333
Denis Vlasenko5de8a132008-05-28 12:44:22 +0000334 plist = scan_proc_pids(ilist); /* changes dir to "/proc" */
Rob Landleyaa872762005-10-28 13:05:12 +0000335
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000336 if (!plist)
337 return EXIT_FAILURE;
338 success = 1;
339 if (opt & OPT_KILL) {
340 success = kill_pid_list(plist, killsig);
341 } else if (!(opt & OPT_SILENT)) {
342 success = print_pid_list(plist);
Rob Landleyaa872762005-10-28 13:05:12 +0000343 }
Denis Vlasenkoafc41132008-03-17 08:44:58 +0000344 return (success != 1); /* 0 == success */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000345}