blob: c0101e6355c82d2af969c9a09154e74d7045de64 [file] [log] [blame]
Rob Landleyaa872762005-10-28 13:05:12 +00001/*
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002 * tiny fuser implementation
3 *
Rob Landleyaa872762005-10-28 13:05:12 +00004 * Copyright 2004 Tony J. White
5 *
6 * May be distributed under the conditions of the
7 * GNU Library General Public License
8 */
9
Rob Landleyaa872762005-10-28 13:05:12 +000010#include <stdio.h>
11#include <stdlib.h>
12#include <unistd.h>
13#include <string.h>
14#include <limits.h>
15#include <dirent.h>
16#include <signal.h>
17#include <sys/types.h>
18#include <sys/ioctl.h>
19#include <sys/stat.h>
20#include <sys/socket.h>
21#include "busybox.h"
22
23#define FUSER_PROC_DIR "/proc"
24#define FUSER_MAX_LINE 255
25
26#define FUSER_OPT_MOUNT 1
27#define FUSER_OPT_KILL 2
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000028#define FUSER_OPT_SILENT 4
29#define FUSER_OPT_IP6 8
30#define FUSER_OPT_IP4 16
Rob Landleyaa872762005-10-28 13:05:12 +000031
32typedef struct inode_list {
33 ino_t inode;
34 dev_t dev;
35 struct inode_list *next;
36} inode_list;
37
38typedef struct pid_list {
39 pid_t pid;
40 struct pid_list *next;
41} pid_list;
42
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000043static int fuser_option(char *option)
Rob Landleyaa872762005-10-28 13:05:12 +000044{
45 int opt = 0;
46
47 if(!(strlen(option))) return 0;
48 if(option[0] != '-') return 0;
Paul Foxf9d40d62006-01-20 21:48:06 +000049 ++option;
Rob Landleyaa872762005-10-28 13:05:12 +000050 while(*option != '\0') {
51 if(*option == 'm') opt |= FUSER_OPT_MOUNT;
52 else if(*option == 'k') opt |= FUSER_OPT_KILL;
53 else if(*option == 's') opt |= FUSER_OPT_SILENT;
54 else if(*option == '6') opt |= FUSER_OPT_IP6;
55 else if(*option == '4') opt |= FUSER_OPT_IP4;
56 else {
57 bb_error_msg_and_die(
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000058 "Unsupported option '%c'", *option);
Rob Landleyaa872762005-10-28 13:05:12 +000059 }
Paul Foxf9d40d62006-01-20 21:48:06 +000060 ++option;
Rob Landleyaa872762005-10-28 13:05:12 +000061 }
62 return opt;
63}
64
65static int fuser_file_to_dev_inode(const char *filename,
66 dev_t *dev, ino_t *inode)
67{
68 struct stat f_stat;
69 if((stat(filename, &f_stat)) < 0) return 0;
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +000070 *inode = f_stat.st_ino;
71 *dev = f_stat.st_dev;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000072 return 1;
Rob Landleyaa872762005-10-28 13:05:12 +000073}
74
75static int fuser_find_socket_dev(dev_t *dev) {
76 int fd = socket(PF_INET, SOCK_DGRAM,0);
77 struct stat buf;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000078
Rob Landleyaa872762005-10-28 13:05:12 +000079 if (fd >= 0 && (fstat(fd, &buf)) == 0) {
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +000080 *dev = buf.st_dev;
Rob Landleyaa872762005-10-28 13:05:12 +000081 close(fd);
82 return 1;
83 }
84 return 0;
85}
86
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000087static int fuser_parse_net_arg(const char *filename,
88 const char **proto, int *port)
Rob Landleyaa872762005-10-28 13:05:12 +000089{
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +000090 char path[sizeof(FUSER_PROC_DIR)+12], tproto[5];
Rob Landleyaa872762005-10-28 13:05:12 +000091
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +000092 if((sscanf(filename, "%d/%4s", port, tproto)) != 2) return 0;
93 sprintf(path, "%s/net/%s", FUSER_PROC_DIR, tproto);
Rob Landleyaa872762005-10-28 13:05:12 +000094 if((access(path, R_OK)) != 0) return 0;
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +000095 *proto = bb_xstrdup(tproto);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000096 return 1;
Rob Landleyaa872762005-10-28 13:05:12 +000097}
98
99static int fuser_add_pid(pid_list *plist, pid_t pid)
100{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000101 pid_list *curr = NULL, *last = NULL;
102
103 if(plist->pid == 0) plist->pid = pid;
104 curr = plist;
105 while(curr != NULL) {
106 if(curr->pid == pid) return 1;
107 last = curr;
108 curr = curr->next;
109 }
110 curr = xmalloc(sizeof(pid_list));
111 last->next = curr;
112 curr->pid = pid;
Rob Landleyaa872762005-10-28 13:05:12 +0000113 curr->next = NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000114 return 1;
Rob Landleyaa872762005-10-28 13:05:12 +0000115}
116
117static int fuser_add_inode(inode_list *ilist, dev_t dev, ino_t inode)
118{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000119 inode_list *curr = NULL, *last = NULL;
120
121 if(!ilist->inode && !ilist->dev) {
Rob Landleyaa872762005-10-28 13:05:12 +0000122 ilist->dev = dev;
123 ilist->inode = inode;
124 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000125 curr = ilist;
126 while(curr != NULL) {
127 if(curr->inode == inode && curr->dev == dev) return 1;
128 last = curr;
129 curr = curr->next;
130 }
131 curr = xmalloc(sizeof(inode_list));
132 last->next = curr;
133 curr->dev = dev;
134 curr->inode = inode;
Rob Landleyaa872762005-10-28 13:05:12 +0000135 curr->next = NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000136 return 1;
Rob Landleyaa872762005-10-28 13:05:12 +0000137}
138
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000139static int fuser_scan_proc_net(int opts, const char *proto,
140 int port, inode_list *ilist)
Rob Landleyaa872762005-10-28 13:05:12 +0000141{
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +0000142 char path[sizeof(FUSER_PROC_DIR)+12], line[FUSER_MAX_LINE+1];
Rob Landleyaa872762005-10-28 13:05:12 +0000143 char addr[128];
144 ino_t tmp_inode;
145 dev_t tmp_dev;
Eric Andersena68ea1c2006-01-30 22:48:39 +0000146 long long uint64_inode;
Rob Landleyaa872762005-10-28 13:05:12 +0000147 int tmp_port;
148 FILE *f;
149
150 if(!fuser_find_socket_dev(&tmp_dev)) tmp_dev = 0;
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +0000151 sprintf(path, "%s/net/%s", FUSER_PROC_DIR, proto);
Rob Landleyaa872762005-10-28 13:05:12 +0000152
153 if (!(f = fopen(path, "r"))) return 0;
154 while(fgets(line, FUSER_MAX_LINE, f)) {
155 if(sscanf(line,
156 "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +0000157 "%*x:%*x %*x %*d %*d %llu",
158 addr, &tmp_port, &uint64_inode) == 3) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000159 if((strlen(addr) == 8) &&
Rob Landleyaa872762005-10-28 13:05:12 +0000160 (opts & FUSER_OPT_IP6)) continue;
161 else if((strlen(addr) > 8) &&
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000162 (opts & FUSER_OPT_IP4)) continue;
Rob Landleyaa872762005-10-28 13:05:12 +0000163 if(tmp_port == port) {
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +0000164 tmp_inode = uint64_inode;
Rob Landleyaa872762005-10-28 13:05:12 +0000165 fuser_add_inode(ilist, tmp_dev, tmp_inode);
166 }
167 }
168
169 }
170 fclose(f);
171 return 1;
172}
173
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000174static int fuser_search_dev_inode(int opts, inode_list *ilist,
175 dev_t dev, ino_t inode)
Rob Landleyaa872762005-10-28 13:05:12 +0000176{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000177 inode_list *curr;
178 curr = ilist;
Rob Landleyaa872762005-10-28 13:05:12 +0000179
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000180 while(curr) {
Rob Landleyaa872762005-10-28 13:05:12 +0000181 if((opts & FUSER_OPT_MOUNT) && curr->dev == dev)
182 return 1;
183 if(curr->inode == inode && curr->dev == dev)
184 return 1;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000185 curr = curr->next;
186 }
187 return 0;
Rob Landleyaa872762005-10-28 13:05:12 +0000188}
189
190static int fuser_scan_pid_maps(int opts, const char *fname, pid_t pid,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000191 inode_list *ilist, pid_list *plist)
Rob Landleyaa872762005-10-28 13:05:12 +0000192{
193 FILE *file;
194 char line[FUSER_MAX_LINE + 1];
195 int major, minor;
196 ino_t inode;
Eric Andersena68ea1c2006-01-30 22:48:39 +0000197 long long uint64_inode;
Rob Landleyaa872762005-10-28 13:05:12 +0000198 dev_t dev;
199
200 if (!(file = fopen(fname, "r"))) return 0;
201 while (fgets(line, FUSER_MAX_LINE, file)) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000202 if(sscanf(line, "%*s %*s %*s %x:%x %llu",
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +0000203 &major, &minor, &uint64_inode) != 3) continue;
204 inode = uint64_inode;
Rob Landleyaa872762005-10-28 13:05:12 +0000205 if(major == 0 && minor == 0 && inode == 0) continue;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000206 dev = makedev(major, minor);
Rob Landleyaa872762005-10-28 13:05:12 +0000207 if(fuser_search_dev_inode(opts, ilist, dev, inode)) {
208 fuser_add_pid(plist, pid);
209 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000210
Rob Landleyaa872762005-10-28 13:05:12 +0000211 }
212 fclose(file);
213 return 1;
214}
215
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000216static int fuser_scan_link(int opts, const char *lname, pid_t pid,
217 inode_list *ilist, pid_list *plist)
Rob Landleyaa872762005-10-28 13:05:12 +0000218{
219 ino_t inode;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000220 dev_t dev;
Rob Landleyaa872762005-10-28 13:05:12 +0000221
222 if(!fuser_file_to_dev_inode(lname, &dev, &inode)) return 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000223 if(fuser_search_dev_inode(opts, ilist, dev, inode))
Rob Landleyaa872762005-10-28 13:05:12 +0000224 fuser_add_pid(plist, pid);
225 return 1;
226}
227
228static int fuser_scan_dir_links(int opts, const char *dname, pid_t pid,
229 inode_list *ilist, pid_list *plist)
230{
231 DIR *d;
232 struct dirent *de;
233 char *lname;
234
235 if((d = opendir(dname))) {
236 while((de = readdir(d)) != NULL) {
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +0000237 lname = concat_subpath_file(dname, de->d_name);
238 if(lname == NULL)
Rob Landleyaa872762005-10-28 13:05:12 +0000239 continue;
Rob Landleyaa872762005-10-28 13:05:12 +0000240 fuser_scan_link(opts, lname, pid, ilist, plist);
241 free(lname);
242 }
243 closedir(d);
244 }
245 else return 0;
246 return 1;
247
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000248}
Rob Landleyaa872762005-10-28 13:05:12 +0000249
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000250static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist)
Rob Landleyaa872762005-10-28 13:05:12 +0000251{
252 DIR *d;
253 struct dirent *de;
254 pid_t pid;
255 char *dname;
256
257 if(!(d = opendir(FUSER_PROC_DIR))) return 0;
258 while((de = readdir(d)) != NULL) {
259 pid = (pid_t)atoi(de->d_name);
260 if(!pid) continue;
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +0000261 dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name);
Rob Landleyaa872762005-10-28 13:05:12 +0000262 if(chdir(dname) < 0) {
263 free(dname);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000264 continue;
Rob Landleyaa872762005-10-28 13:05:12 +0000265 }
266 free(dname);
267 fuser_scan_link(opts, "cwd", pid, ilist, plist);
268 fuser_scan_link(opts, "exe", pid, ilist, plist);
269 fuser_scan_link(opts, "root", pid, ilist, plist);
270 fuser_scan_dir_links(opts, "fd", pid, ilist, plist);
271 fuser_scan_dir_links(opts, "lib", pid, ilist, plist);
272 fuser_scan_dir_links(opts, "mmap", pid, ilist, plist);
273 fuser_scan_pid_maps(opts, "maps", pid, ilist, plist);
"Vladimir N. Oleynik"7eb8e452005-10-28 16:08:47 +0000274 chdir("..");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000275 }
276 closedir(d);
Rob Landleyaa872762005-10-28 13:05:12 +0000277 return 1;
278}
279
280static int fuser_print_pid_list(pid_list *plist) {
Paul Foxf9d40d62006-01-20 21:48:06 +0000281 pid_list *curr = plist;
Rob Landleyaa872762005-10-28 13:05:12 +0000282
283 if(plist == NULL) return 0;
284 while(curr != NULL) {
285 if(curr->pid > 0) printf("%d ", curr->pid);
286 curr = curr->next;
287 }
288 printf("\n");
289 return 1;
290}
291
292static int fuser_kill_pid_list(pid_list *plist, int sig) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000293 pid_list *curr = plist;
Rob Landleyaa872762005-10-28 13:05:12 +0000294 pid_t mypid = getpid();
295 int success = 1;
296
297 if(plist == NULL) return 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000298 while(curr != NULL) {
Rob Landleyaa872762005-10-28 13:05:12 +0000299 if(curr->pid > 0 && curr->pid != mypid) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000300 if (kill(curr->pid, sig) != 0) {
301 bb_perror_msg(
Rob Landleyaa872762005-10-28 13:05:12 +0000302 "Could not kill pid '%d'", curr->pid);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000303 success = 0;
Rob Landleyaa872762005-10-28 13:05:12 +0000304 }
305 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000306 curr = curr->next;
307 }
308 return success;
Rob Landleyaa872762005-10-28 13:05:12 +0000309}
310
311extern int fuser_main(int argc, char **argv) {
312 int port, i, optn;
313 int* fni; /* file name indexes of argv */
314 int fnic = 0; /* file name index count */
315 const char *proto;
316 static int opt = 0; /* FUSER_OPT_ */
317 dev_t dev;
318 ino_t inode;
319 pid_list *pids;
320 inode_list *inodes;
321 int killsig = SIGTERM;
322 int success = 1;
323
324 fni = xmalloc(sizeof(int));
325 for(i=1;i<argc;i++) {
326 optn = fuser_option(argv[i]);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000327 if(optn) opt |= optn;
Rob Landleyaa872762005-10-28 13:05:12 +0000328 else if(argv[i][0] == '-') {
329 if(!(u_signal_names(argv[i]+1, &killsig, 0)))
330 killsig = SIGTERM;
331 }
332 else {
333 fni = xrealloc(fni, sizeof(int) * (fnic+2));
334 fni[fnic++] = i;
335 }
336 }
337 if(!fnic) return 1;
338
339 pids = xmalloc(sizeof(pid_list));
340 inodes = xmalloc(sizeof(inode_list));
341 for(i=0;i<fnic;i++) {
342 if(fuser_parse_net_arg(argv[fni[i]], &proto, &port)) {
343 fuser_scan_proc_net(opt, proto, port, inodes);
344 }
345 else {
346 if(!fuser_file_to_dev_inode(
347 argv[fni[i]], &dev, &inode)) {
348 free(pids);
349 free(inodes);
350 bb_perror_msg_and_die(
351 "Could not open '%s'", argv[fni[i]]);
352 }
353 fuser_add_inode(inodes, dev, inode);
354 }
355 }
356 success = fuser_scan_proc_pids(opt, inodes, pids);
357 /* if the first pid in the list is 0, none have been found */
358 if(pids->pid == 0) success = 0;
359 if(success) {
360 if(opt & FUSER_OPT_KILL) {
361 success = fuser_kill_pid_list(pids, killsig);
362 }
363 else if(!(opt & FUSER_OPT_SILENT)) {
364 success = fuser_print_pid_list(pids);
365 }
366 }
367 free(pids);
368 free(inodes);
369 /* return 0 on (success == 1) 1 otherwise */
370 return (success != 1);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000371}