2 * tiny fuser implementation
4 * Copyright 2004 Tony J. White
6 * May be distributed under the conditions of the
7 * GNU Library General Public License
10 /* this define is needed for asprintf() */
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
25 #include <sys/socket.h>
28 #define FUSER_PROC_DIR "/proc"
29 #define FUSER_MAX_LINE 255
31 #define FUSER_OPT_MOUNT 1
32 #define FUSER_OPT_KILL 2
33 #define FUSER_OPT_SILENT 4
34 #define FUSER_OPT_IP6 8
35 #define FUSER_OPT_IP4 16
37 typedef struct inode_list {
40 struct inode_list *next;
43 typedef struct pid_list {
45 struct pid_list *next;
48 static int fuser_option(char *option)
52 if(!(strlen(option))) return 0;
53 if(option[0] != '-') return 0;
55 while(*option != '\0') {
56 if(*option == 'm') opt |= FUSER_OPT_MOUNT;
57 else if(*option == 'k') opt |= FUSER_OPT_KILL;
58 else if(*option == 's') opt |= FUSER_OPT_SILENT;
59 else if(*option == '6') opt |= FUSER_OPT_IP6;
60 else if(*option == '4') opt |= FUSER_OPT_IP4;
63 "Unsupported option '%c'", *option);
70 static int fuser_file_to_dev_inode(const char *filename,
71 dev_t *dev, ino_t *inode)
74 if((stat(filename, &f_stat)) < 0) return 0;
75 memcpy(inode, &f_stat.st_ino, sizeof(ino_t));
76 memcpy(dev, &f_stat.st_dev, sizeof(dev_t));
80 static int fuser_find_socket_dev(dev_t *dev) {
81 int fd = socket(PF_INET, SOCK_DGRAM,0);
84 if (fd >= 0 && (fstat(fd, &buf)) == 0) {
85 memcpy(dev, &buf.st_dev, sizeof(dev_t));
92 static int fuser_parse_net_arg(const char *filename,
93 const char **proto, int *port)
96 char path[PATH_MAX+1], tproto[5];
98 if((sscanf(filename, "%d/%4s", &tport, &tproto[0])) != 2) return 0;
99 strncpy(path, FUSER_PROC_DIR, sizeof(FUSER_PROC_DIR));
100 strncat(path, "/net/", 5);
101 strncat(path, tproto, strlen(tproto));
102 if((access(path, R_OK)) != 0) return 0;
103 *proto = bb_xstrndup(tproto, strlen(tproto));
104 memcpy(port, &tport, sizeof(int));
108 static int fuser_add_pid(pid_list *plist, pid_t pid)
110 pid_list *curr = NULL, *last = NULL;
112 if(plist->pid == 0) plist->pid = pid;
114 while(curr != NULL) {
115 if(curr->pid == pid) return 1;
119 curr = xmalloc(sizeof(pid_list));
126 static int fuser_add_inode(inode_list *ilist, dev_t dev, ino_t inode)
128 inode_list *curr = NULL, *last = NULL;
130 if(!ilist->inode && !ilist->dev) {
132 ilist->inode = inode;
135 while(curr != NULL) {
136 if(curr->inode == inode && curr->dev == dev) return 1;
140 curr = xmalloc(sizeof(inode_list));
148 static int fuser_scan_proc_net(int opts, const char *proto,
149 int port, inode_list *ilist)
151 char path[PATH_MAX+1], line[FUSER_MAX_LINE+1];
158 if(!fuser_find_socket_dev(&tmp_dev)) tmp_dev = 0;
160 strncpy(path, FUSER_PROC_DIR, sizeof(FUSER_PROC_DIR));
161 strncat(path, "/net/", 5);
162 strncat(path, proto, strlen(proto));
164 if (!(f = fopen(path, "r"))) return 0;
165 while(fgets(line, FUSER_MAX_LINE, f)) {
167 "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
168 "%*x:%*x %*x %*d %*d %lu",
169 &addr[0], &tmp_port, &tmp_inode) == 3) {
170 if((strlen(addr) == 8) &&
171 (opts & FUSER_OPT_IP6)) continue;
172 else if((strlen(addr) > 8) &&
173 (opts & FUSER_OPT_IP4)) continue;
174 if(tmp_port == port) {
175 fuser_add_inode(ilist, tmp_dev, tmp_inode);
184 static int fuser_search_dev_inode(int opts, inode_list *ilist,
185 dev_t dev, ino_t inode)
191 if((opts & FUSER_OPT_MOUNT) && curr->dev == dev)
193 if(curr->inode == inode && curr->dev == dev)
200 static int fuser_scan_pid_maps(int opts, const char *fname, pid_t pid,
201 inode_list *ilist, pid_list *plist)
204 char line[FUSER_MAX_LINE + 1];
209 if (!(file = fopen(fname, "r"))) return 0;
210 while (fgets(line, FUSER_MAX_LINE, file)) {
211 if(sscanf(line, "%*s %*s %*s %x:%x %ld",
212 &major, &minor, &inode) != 3) continue;
213 if(major == 0 && minor == 0 && inode == 0) continue;
214 dev = makedev(major, minor);
215 if(fuser_search_dev_inode(opts, ilist, dev, inode)) {
216 fuser_add_pid(plist, pid);
224 static int fuser_scan_link(int opts, const char *lname, pid_t pid,
225 inode_list *ilist, pid_list *plist)
230 if(!fuser_file_to_dev_inode(lname, &dev, &inode)) return 0;
231 if(fuser_search_dev_inode(opts, ilist, dev, inode))
232 fuser_add_pid(plist, pid);
236 static int fuser_scan_dir_links(int opts, const char *dname, pid_t pid,
237 inode_list *ilist, pid_list *plist)
243 if((d = opendir(dname))) {
244 while((de = readdir(d)) != NULL) {
245 if(!(strcmp(de->d_name, "."))) continue;
246 if(!(strcmp(de->d_name, ".."))) continue;
247 if(asprintf(&lname, "%s/%s", dname, de->d_name) < 0) {
251 fuser_scan_link(opts, lname, pid, ilist, plist);
261 static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist)
268 if(!(d = opendir(FUSER_PROC_DIR))) return 0;
269 while((de = readdir(d)) != NULL) {
270 pid = (pid_t)atoi(de->d_name);
272 if(asprintf(&dname, "%s/%d/", FUSER_PROC_DIR, pid) < 0) {
276 if(chdir(dname) < 0) {
281 fuser_scan_link(opts, "cwd", pid, ilist, plist);
282 fuser_scan_link(opts, "exe", pid, ilist, plist);
283 fuser_scan_link(opts, "root", pid, ilist, plist);
284 fuser_scan_dir_links(opts, "fd", pid, ilist, plist);
285 fuser_scan_dir_links(opts, "lib", pid, ilist, plist);
286 fuser_scan_dir_links(opts, "mmap", pid, ilist, plist);
287 fuser_scan_pid_maps(opts, "maps", pid, ilist, plist);
293 static int fuser_print_pid_list(pid_list *plist) {
297 if(plist == NULL) return 0;
298 while(curr != NULL) {
299 if(curr->pid > 0) printf("%d ", curr->pid);
306 static int fuser_kill_pid_list(pid_list *plist, int sig) {
309 pid_t mypid = getpid();
312 if(plist == NULL) return 0;
313 while(curr != NULL) {
314 if(curr->pid > 0 && curr->pid != mypid) {
315 if (kill(curr->pid, sig) != 0) {
317 "Could not kill pid '%d'", curr->pid);
326 extern int fuser_main(int argc, char **argv) {
328 int* fni; /* file name indexes of argv */
329 int fnic = 0; /* file name index count */
331 static int opt = 0; /* FUSER_OPT_ */
336 int killsig = SIGTERM;
339 fni = xmalloc(sizeof(int));
340 for(i=1;i<argc;i++) {
341 optn = fuser_option(argv[i]);
342 if(optn) opt |= optn;
343 else if(argv[i][0] == '-') {
344 if(!(u_signal_names(argv[i]+1, &killsig, 0)))
348 fni = xrealloc(fni, sizeof(int) * (fnic+2));
354 pids = xmalloc(sizeof(pid_list));
355 inodes = xmalloc(sizeof(inode_list));
356 for(i=0;i<fnic;i++) {
357 if(fuser_parse_net_arg(argv[fni[i]], &proto, &port)) {
358 fuser_scan_proc_net(opt, proto, port, inodes);
361 if(!fuser_file_to_dev_inode(
362 argv[fni[i]], &dev, &inode)) {
365 bb_perror_msg_and_die(
366 "Could not open '%s'", argv[fni[i]]);
368 fuser_add_inode(inodes, dev, inode);
371 success = fuser_scan_proc_pids(opt, inodes, pids);
372 /* if the first pid in the list is 0, none have been found */
373 if(pids->pid == 0) success = 0;
375 if(opt & FUSER_OPT_KILL) {
376 success = fuser_kill_pid_list(pids, killsig);
378 else if(!(opt & FUSER_OPT_SILENT)) {
379 success = fuser_print_pid_list(pids);
384 /* return 0 on (success == 1) 1 otherwise */
385 return (success != 1);