blob: 4906797cf075c2b13d5d80ca232bfe04ee60c997 [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
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000011#include "busybox.h"
Rob Landleyaa872762005-10-28 13:05:12 +000012
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 Rikerc1ef7bd2006-01-25 00:08:53 +000018#define FUSER_OPT_SILENT 4
19#define FUSER_OPT_IP6 8
20#define FUSER_OPT_IP4 16
Rob Landleyaa872762005-10-28 13:05:12 +000021
22typedef struct inode_list {
23 ino_t inode;
24 dev_t dev;
25 struct inode_list *next;
26} inode_list;
27
28typedef struct pid_list {
29 pid_t pid;
30 struct pid_list *next;
31} pid_list;
32
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000033static int fuser_option(char *option)
Rob Landleyaa872762005-10-28 13:05:12 +000034{
35 int opt = 0;
36
37 if(!(strlen(option))) return 0;
38 if(option[0] != '-') return 0;
Paul Foxf9d40d62006-01-20 21:48:06 +000039 ++option;
Rob Landleyaa872762005-10-28 13:05:12 +000040 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 Rikerc1ef7bd2006-01-25 00:08:53 +000048 "Unsupported option '%c'", *option);
Rob Landleyaa872762005-10-28 13:05:12 +000049 }
Paul Foxf9d40d62006-01-20 21:48:06 +000050 ++option;
Rob Landleyaa872762005-10-28 13:05:12 +000051 }
52 return opt;
53}
54
55static 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"7eb8e452005-10-28 16:08:47 +000060 *inode = f_stat.st_ino;
61 *dev = f_stat.st_dev;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000062 return 1;
Rob Landleyaa872762005-10-28 13:05:12 +000063}
64
Rob Landley31642d72006-03-14 21:45:38 +000065static int fuser_find_socket_dev(dev_t *dev)
66{
Rob Landleyaa872762005-10-28 13:05:12 +000067 int fd = socket(PF_INET, SOCK_DGRAM,0);
68 struct stat buf;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000069
Rob Landleyaa872762005-10-28 13:05:12 +000070 if (fd >= 0 && (fstat(fd, &buf)) == 0) {
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +000071 *dev = buf.st_dev;
Rob Landleyaa872762005-10-28 13:05:12 +000072 close(fd);
73 return 1;
74 }
75 return 0;
76}
77
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000078static int fuser_parse_net_arg(const char *filename,
79 const char **proto, int *port)
Rob Landleyaa872762005-10-28 13:05:12 +000080{
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +000081 char path[sizeof(FUSER_PROC_DIR)+12], tproto[5];
Rob Landleyaa872762005-10-28 13:05:12 +000082
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +000083 if((sscanf(filename, "%d/%4s", port, tproto)) != 2) return 0;
84 sprintf(path, "%s/net/%s", FUSER_PROC_DIR, tproto);
Rob Landleyaa872762005-10-28 13:05:12 +000085 if((access(path, R_OK)) != 0) return 0;
Rob Landley86b4d642006-08-03 17:58:17 +000086 *proto = xstrdup(tproto);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000087 return 1;
Rob Landleyaa872762005-10-28 13:05:12 +000088}
89
90static int fuser_add_pid(pid_list *plist, pid_t pid)
91{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000092 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 Landleyaa872762005-10-28 13:05:12 +0000104 curr->next = NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000105 return 1;
Rob Landleyaa872762005-10-28 13:05:12 +0000106}
107
108static int fuser_add_inode(inode_list *ilist, dev_t dev, ino_t inode)
109{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000110 inode_list *curr = NULL, *last = NULL;
111
112 if(!ilist->inode && !ilist->dev) {
Rob Landleyaa872762005-10-28 13:05:12 +0000113 ilist->dev = dev;
114 ilist->inode = inode;
115 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000116 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 Landleyaa872762005-10-28 13:05:12 +0000126 curr->next = NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000127 return 1;
Rob Landleyaa872762005-10-28 13:05:12 +0000128}
129
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000130static int fuser_scan_proc_net(int opts, const char *proto,
131 int port, inode_list *ilist)
Rob Landleyaa872762005-10-28 13:05:12 +0000132{
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +0000133 char path[sizeof(FUSER_PROC_DIR)+12], line[FUSER_MAX_LINE+1];
Rob Landleyaa872762005-10-28 13:05:12 +0000134 char addr[128];
135 ino_t tmp_inode;
136 dev_t tmp_dev;
Eric Andersena68ea1c2006-01-30 22:48:39 +0000137 long long uint64_inode;
Rob Landleyaa872762005-10-28 13:05:12 +0000138 int tmp_port;
139 FILE *f;
140
141 if(!fuser_find_socket_dev(&tmp_dev)) tmp_dev = 0;
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +0000142 sprintf(path, "%s/net/%s", FUSER_PROC_DIR, proto);
Rob Landleyaa872762005-10-28 13:05:12 +0000143
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"7eb8e452005-10-28 16:08:47 +0000148 "%*x:%*x %*x %*d %*d %llu",
149 addr, &tmp_port, &uint64_inode) == 3) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000150 if((strlen(addr) == 8) &&
Rob Landleyaa872762005-10-28 13:05:12 +0000151 (opts & FUSER_OPT_IP6)) continue;
152 else if((strlen(addr) > 8) &&
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000153 (opts & FUSER_OPT_IP4)) continue;
Rob Landleyaa872762005-10-28 13:05:12 +0000154 if(tmp_port == port) {
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +0000155 tmp_inode = uint64_inode;
Rob Landleyaa872762005-10-28 13:05:12 +0000156 fuser_add_inode(ilist, tmp_dev, tmp_inode);
157 }
158 }
159
160 }
161 fclose(f);
162 return 1;
163}
164
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000165static int fuser_search_dev_inode(int opts, inode_list *ilist,
166 dev_t dev, ino_t inode)
Rob Landleyaa872762005-10-28 13:05:12 +0000167{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000168 inode_list *curr;
169 curr = ilist;
Rob Landleyaa872762005-10-28 13:05:12 +0000170
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000171 while(curr) {
Rob Landleyaa872762005-10-28 13:05:12 +0000172 if((opts & FUSER_OPT_MOUNT) && curr->dev == dev)
173 return 1;
174 if(curr->inode == inode && curr->dev == dev)
175 return 1;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000176 curr = curr->next;
177 }
178 return 0;
Rob Landleyaa872762005-10-28 13:05:12 +0000179}
180
181static int fuser_scan_pid_maps(int opts, const char *fname, pid_t pid,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000182 inode_list *ilist, pid_list *plist)
Rob Landleyaa872762005-10-28 13:05:12 +0000183{
184 FILE *file;
185 char line[FUSER_MAX_LINE + 1];
186 int major, minor;
187 ino_t inode;
Eric Andersena68ea1c2006-01-30 22:48:39 +0000188 long long uint64_inode;
Rob Landleyaa872762005-10-28 13:05:12 +0000189 dev_t dev;
190
191 if (!(file = fopen(fname, "r"))) return 0;
192 while (fgets(line, FUSER_MAX_LINE, file)) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000193 if(sscanf(line, "%*s %*s %*s %x:%x %llu",
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +0000194 &major, &minor, &uint64_inode) != 3) continue;
195 inode = uint64_inode;
Rob Landleyaa872762005-10-28 13:05:12 +0000196 if(major == 0 && minor == 0 && inode == 0) continue;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000197 dev = makedev(major, minor);
Rob Landleyaa872762005-10-28 13:05:12 +0000198 if(fuser_search_dev_inode(opts, ilist, dev, inode)) {
199 fuser_add_pid(plist, pid);
200 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000201
Rob Landleyaa872762005-10-28 13:05:12 +0000202 }
203 fclose(file);
204 return 1;
205}
206
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000207static int fuser_scan_link(int opts, const char *lname, pid_t pid,
208 inode_list *ilist, pid_list *plist)
Rob Landleyaa872762005-10-28 13:05:12 +0000209{
210 ino_t inode;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000211 dev_t dev;
Rob Landleyaa872762005-10-28 13:05:12 +0000212
213 if(!fuser_file_to_dev_inode(lname, &dev, &inode)) return 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000214 if(fuser_search_dev_inode(opts, ilist, dev, inode))
Rob Landleyaa872762005-10-28 13:05:12 +0000215 fuser_add_pid(plist, pid);
216 return 1;
217}
218
219static 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"7eb8e452005-10-28 16:08:47 +0000228 lname = concat_subpath_file(dname, de->d_name);
229 if(lname == NULL)
Rob Landleyaa872762005-10-28 13:05:12 +0000230 continue;
Rob Landleyaa872762005-10-28 13:05:12 +0000231 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 Rikerc1ef7bd2006-01-25 00:08:53 +0000239}
Rob Landleyaa872762005-10-28 13:05:12 +0000240
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000241static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist)
Rob Landleyaa872762005-10-28 13:05:12 +0000242{
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"7eb8e452005-10-28 16:08:47 +0000252 dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name);
Rob Landleyaa872762005-10-28 13:05:12 +0000253 if(chdir(dname) < 0) {
254 free(dname);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000255 continue;
Rob Landleyaa872762005-10-28 13:05:12 +0000256 }
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"7eb8e452005-10-28 16:08:47 +0000265 chdir("..");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000266 }
267 closedir(d);
Rob Landleyaa872762005-10-28 13:05:12 +0000268 return 1;
269}
270
Rob Landley31642d72006-03-14 21:45:38 +0000271static int fuser_print_pid_list(pid_list *plist)
272{
Paul Foxf9d40d62006-01-20 21:48:06 +0000273 pid_list *curr = plist;
Rob Landleyaa872762005-10-28 13:05:12 +0000274
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 Landley31642d72006-03-14 21:45:38 +0000284static int fuser_kill_pid_list(pid_list *plist, int sig)
285{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000286 pid_list *curr = plist;
Rob Landleyaa872762005-10-28 13:05:12 +0000287 pid_t mypid = getpid();
288 int success = 1;
289
290 if(plist == NULL) return 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000291 while(curr != NULL) {
Rob Landleyaa872762005-10-28 13:05:12 +0000292 if(curr->pid > 0 && curr->pid != mypid) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000293 if (kill(curr->pid, sig) != 0) {
294 bb_perror_msg(
Denis Vlasenkoea620772006-10-14 02:23:43 +0000295 "cannot kill pid '%d'", curr->pid);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000296 success = 0;
Rob Landleyaa872762005-10-28 13:05:12 +0000297 }
298 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000299 curr = curr->next;
300 }
301 return success;
Rob Landleyaa872762005-10-28 13:05:12 +0000302}
303
Rob Landley31642d72006-03-14 21:45:38 +0000304int fuser_main(int argc, char **argv)
305{
Rob Landleyaa872762005-10-28 13:05:12 +0000306 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 Frysinger70cbb6e2006-04-21 22:04:05 +0000318 if (argc < 2)
319 bb_show_usage();
320
Rob Landleyaa872762005-10-28 13:05:12 +0000321 fni = xmalloc(sizeof(int));
322 for(i=1;i<argc;i++) {
323 optn = fuser_option(argv[i]);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000324 if(optn) opt |= optn;
Rob Landleyaa872762005-10-28 13:05:12 +0000325 else if(argv[i][0] == '-') {
Rob Landleyc9c1a412006-07-12 19:17:55 +0000326 if(0>(killsig = get_signum(argv[i]+1)))
Rob Landleyaa872762005-10-28 13:05:12 +0000327 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 Landleyaa872762005-10-28 13:05:12 +0000336 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 Landleyc9c1a412006-07-12 19:17:55 +0000344 if (ENABLE_FEATURE_CLEAN_UP) free(inodes);
Denis Vlasenkoea620772006-10-14 02:23:43 +0000345 bb_perror_msg_and_die("cannot open '%s'", argv[fni[i]]);
Rob Landleyaa872762005-10-28 13:05:12 +0000346 }
347 fuser_add_inode(inodes, dev, inode);
348 }
349 }
Rob Landleyc9c1a412006-07-12 19:17:55 +0000350 pids = xmalloc(sizeof(pid_list));
Rob Landleyaa872762005-10-28 13:05:12 +0000351 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 Rikerc1ef7bd2006-01-25 00:08:53 +0000366}