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
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <sys/sysmacros.h>
24 #define FUSER_PROC_DIR "/proc"
25 #define FUSER_MAX_LINE 255
27 #define FUSER_OPT_MOUNT 1
28 #define FUSER_OPT_KILL 2
29 #define FUSER_OPT_SILENT 4
30 #define FUSER_OPT_IP6 8
31 #define FUSER_OPT_IP4 16
33 typedef struct inode_list {
36 struct inode_list *next;
39 typedef struct pid_list {
41 struct pid_list *next;
44 static int fuser_option(char *option)
48 if(!(strlen(option))) return 0;
49 if(option[0] != '-') return 0;
51 while(*option != '\0') {
52 if(*option == 'm') opt |= FUSER_OPT_MOUNT;
53 else if(*option == 'k') opt |= FUSER_OPT_KILL;
54 else if(*option == 's') opt |= FUSER_OPT_SILENT;
55 else if(*option == '6') opt |= FUSER_OPT_IP6;
56 else if(*option == '4') opt |= FUSER_OPT_IP4;
59 "Unsupported option '%c'", *option);
66 static int fuser_file_to_dev_inode(const char *filename,
67 dev_t *dev, ino_t *inode)
70 if((stat(filename, &f_stat)) < 0) return 0;
71 *inode = f_stat.st_ino;
76 static int fuser_find_socket_dev(dev_t *dev)
78 int fd = socket(PF_INET, SOCK_DGRAM,0);
81 if (fd >= 0 && (fstat(fd, &buf)) == 0) {
89 static int fuser_parse_net_arg(const char *filename,
90 const char **proto, int *port)
92 char path[sizeof(FUSER_PROC_DIR)+12], tproto[5];
94 if((sscanf(filename, "%d/%4s", port, tproto)) != 2) return 0;
95 sprintf(path, "%s/net/%s", FUSER_PROC_DIR, tproto);
96 if((access(path, R_OK)) != 0) return 0;
97 *proto = bb_xstrdup(tproto);
101 static int fuser_add_pid(pid_list *plist, pid_t pid)
103 pid_list *curr = NULL, *last = NULL;
105 if(plist->pid == 0) plist->pid = pid;
107 while(curr != NULL) {
108 if(curr->pid == pid) return 1;
112 curr = xmalloc(sizeof(pid_list));
119 static int fuser_add_inode(inode_list *ilist, dev_t dev, ino_t inode)
121 inode_list *curr = NULL, *last = NULL;
123 if(!ilist->inode && !ilist->dev) {
125 ilist->inode = inode;
128 while(curr != NULL) {
129 if(curr->inode == inode && curr->dev == dev) return 1;
133 curr = xmalloc(sizeof(inode_list));
141 static int fuser_scan_proc_net(int opts, const char *proto,
142 int port, inode_list *ilist)
144 char path[sizeof(FUSER_PROC_DIR)+12], line[FUSER_MAX_LINE+1];
148 long long uint64_inode;
152 if(!fuser_find_socket_dev(&tmp_dev)) tmp_dev = 0;
153 sprintf(path, "%s/net/%s", FUSER_PROC_DIR, proto);
155 if (!(f = fopen(path, "r"))) return 0;
156 while(fgets(line, FUSER_MAX_LINE, f)) {
158 "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
159 "%*x:%*x %*x %*d %*d %llu",
160 addr, &tmp_port, &uint64_inode) == 3) {
161 if((strlen(addr) == 8) &&
162 (opts & FUSER_OPT_IP6)) continue;
163 else if((strlen(addr) > 8) &&
164 (opts & FUSER_OPT_IP4)) continue;
165 if(tmp_port == port) {
166 tmp_inode = uint64_inode;
167 fuser_add_inode(ilist, tmp_dev, tmp_inode);
176 static int fuser_search_dev_inode(int opts, inode_list *ilist,
177 dev_t dev, ino_t inode)
183 if((opts & FUSER_OPT_MOUNT) && curr->dev == dev)
185 if(curr->inode == inode && curr->dev == dev)
192 static int fuser_scan_pid_maps(int opts, const char *fname, pid_t pid,
193 inode_list *ilist, pid_list *plist)
196 char line[FUSER_MAX_LINE + 1];
199 long long uint64_inode;
202 if (!(file = fopen(fname, "r"))) return 0;
203 while (fgets(line, FUSER_MAX_LINE, file)) {
204 if(sscanf(line, "%*s %*s %*s %x:%x %llu",
205 &major, &minor, &uint64_inode) != 3) continue;
206 inode = uint64_inode;
207 if(major == 0 && minor == 0 && inode == 0) continue;
208 dev = makedev(major, minor);
209 if(fuser_search_dev_inode(opts, ilist, dev, inode)) {
210 fuser_add_pid(plist, pid);
218 static int fuser_scan_link(int opts, const char *lname, pid_t pid,
219 inode_list *ilist, pid_list *plist)
224 if(!fuser_file_to_dev_inode(lname, &dev, &inode)) return 0;
225 if(fuser_search_dev_inode(opts, ilist, dev, inode))
226 fuser_add_pid(plist, pid);
230 static int fuser_scan_dir_links(int opts, const char *dname, pid_t pid,
231 inode_list *ilist, pid_list *plist)
237 if((d = opendir(dname))) {
238 while((de = readdir(d)) != NULL) {
239 lname = concat_subpath_file(dname, de->d_name);
242 fuser_scan_link(opts, lname, pid, ilist, plist);
252 static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist)
259 if(!(d = opendir(FUSER_PROC_DIR))) return 0;
260 while((de = readdir(d)) != NULL) {
261 pid = (pid_t)atoi(de->d_name);
263 dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name);
264 if(chdir(dname) < 0) {
269 fuser_scan_link(opts, "cwd", pid, ilist, plist);
270 fuser_scan_link(opts, "exe", pid, ilist, plist);
271 fuser_scan_link(opts, "root", pid, ilist, plist);
272 fuser_scan_dir_links(opts, "fd", pid, ilist, plist);
273 fuser_scan_dir_links(opts, "lib", pid, ilist, plist);
274 fuser_scan_dir_links(opts, "mmap", pid, ilist, plist);
275 fuser_scan_pid_maps(opts, "maps", pid, ilist, plist);
282 static int fuser_print_pid_list(pid_list *plist)
284 pid_list *curr = plist;
286 if(plist == NULL) return 0;
287 while(curr != NULL) {
288 if(curr->pid > 0) printf("%d ", curr->pid);
295 static int fuser_kill_pid_list(pid_list *plist, int sig)
297 pid_list *curr = plist;
298 pid_t mypid = getpid();
301 if(plist == NULL) return 0;
302 while(curr != NULL) {
303 if(curr->pid > 0 && curr->pid != mypid) {
304 if (kill(curr->pid, sig) != 0) {
306 "Could not kill pid '%d'", curr->pid);
315 int fuser_main(int argc, char **argv)
318 int* fni; /* file name indexes of argv */
319 int fnic = 0; /* file name index count */
321 static int opt = 0; /* FUSER_OPT_ */
326 int killsig = SIGTERM;
329 fni = xmalloc(sizeof(int));
330 for(i=1;i<argc;i++) {
331 optn = fuser_option(argv[i]);
332 if(optn) opt |= optn;
333 else if(argv[i][0] == '-') {
334 if(!(u_signal_names(argv[i]+1, &killsig, 0)))
338 fni = xrealloc(fni, sizeof(int) * (fnic+2));
344 pids = xmalloc(sizeof(pid_list));
345 inodes = xmalloc(sizeof(inode_list));
346 for(i=0;i<fnic;i++) {
347 if(fuser_parse_net_arg(argv[fni[i]], &proto, &port)) {
348 fuser_scan_proc_net(opt, proto, port, inodes);
351 if(!fuser_file_to_dev_inode(
352 argv[fni[i]], &dev, &inode)) {
355 bb_perror_msg_and_die(
356 "Could not open '%s'", argv[fni[i]]);
358 fuser_add_inode(inodes, dev, inode);
361 success = fuser_scan_proc_pids(opt, inodes, pids);
362 /* if the first pid in the list is 0, none have been found */
363 if(pids->pid == 0) success = 0;
365 if(opt & FUSER_OPT_KILL) {
366 success = fuser_kill_pid_list(pids, killsig);
368 else if(!(opt & FUSER_OPT_SILENT)) {
369 success = fuser_print_pid_list(pids);
374 /* return 0 on (success == 1) 1 otherwise */
375 return (success != 1);