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>
23 #define FUSER_PROC_DIR "/proc"
24 #define FUSER_MAX_LINE 255
26 #define FUSER_OPT_MOUNT 1
27 #define FUSER_OPT_KILL 2
28 #define FUSER_OPT_SILENT 4
29 #define FUSER_OPT_IP6 8
30 #define FUSER_OPT_IP4 16
32 typedef struct inode_list {
35 struct inode_list *next;
38 typedef struct pid_list {
40 struct pid_list *next;
43 static int fuser_option(char *option)
47 if(!(strlen(option))) return 0;
48 if(option[0] != '-') return 0;
50 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;
58 "Unsupported option '%c'", *option);
65 static int fuser_file_to_dev_inode(const char *filename,
66 dev_t *dev, ino_t *inode)
69 if((stat(filename, &f_stat)) < 0) return 0;
70 *inode = f_stat.st_ino;
75 static int fuser_find_socket_dev(dev_t *dev) {
76 int fd = socket(PF_INET, SOCK_DGRAM,0);
79 if (fd >= 0 && (fstat(fd, &buf)) == 0) {
87 static int fuser_parse_net_arg(const char *filename,
88 const char **proto, int *port)
90 char path[sizeof(FUSER_PROC_DIR)+12], tproto[5];
92 if((sscanf(filename, "%d/%4s", port, tproto)) != 2) return 0;
93 sprintf(path, "%s/net/%s", FUSER_PROC_DIR, tproto);
94 if((access(path, R_OK)) != 0) return 0;
95 *proto = bb_xstrdup(tproto);
99 static int fuser_add_pid(pid_list *plist, pid_t pid)
101 pid_list *curr = NULL, *last = NULL;
103 if(plist->pid == 0) plist->pid = pid;
105 while(curr != NULL) {
106 if(curr->pid == pid) return 1;
110 curr = xmalloc(sizeof(pid_list));
117 static int fuser_add_inode(inode_list *ilist, dev_t dev, ino_t inode)
119 inode_list *curr = NULL, *last = NULL;
121 if(!ilist->inode && !ilist->dev) {
123 ilist->inode = inode;
126 while(curr != NULL) {
127 if(curr->inode == inode && curr->dev == dev) return 1;
131 curr = xmalloc(sizeof(inode_list));
139 static int fuser_scan_proc_net(int opts, const char *proto,
140 int port, inode_list *ilist)
142 char path[sizeof(FUSER_PROC_DIR)+12], line[FUSER_MAX_LINE+1];
146 long long uint64_inode;
150 if(!fuser_find_socket_dev(&tmp_dev)) tmp_dev = 0;
151 sprintf(path, "%s/net/%s", FUSER_PROC_DIR, proto);
153 if (!(f = fopen(path, "r"))) return 0;
154 while(fgets(line, FUSER_MAX_LINE, f)) {
156 "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
157 "%*x:%*x %*x %*d %*d %llu",
158 addr, &tmp_port, &uint64_inode) == 3) {
159 if((strlen(addr) == 8) &&
160 (opts & FUSER_OPT_IP6)) continue;
161 else if((strlen(addr) > 8) &&
162 (opts & FUSER_OPT_IP4)) continue;
163 if(tmp_port == port) {
164 tmp_inode = uint64_inode;
165 fuser_add_inode(ilist, tmp_dev, tmp_inode);
174 static int fuser_search_dev_inode(int opts, inode_list *ilist,
175 dev_t dev, ino_t inode)
181 if((opts & FUSER_OPT_MOUNT) && curr->dev == dev)
183 if(curr->inode == inode && curr->dev == dev)
190 static int fuser_scan_pid_maps(int opts, const char *fname, pid_t pid,
191 inode_list *ilist, pid_list *plist)
194 char line[FUSER_MAX_LINE + 1];
197 long long uint64_inode;
200 if (!(file = fopen(fname, "r"))) return 0;
201 while (fgets(line, FUSER_MAX_LINE, file)) {
202 if(sscanf(line, "%*s %*s %*s %x:%x %llu",
203 &major, &minor, &uint64_inode) != 3) continue;
204 inode = uint64_inode;
205 if(major == 0 && minor == 0 && inode == 0) continue;
206 dev = makedev(major, minor);
207 if(fuser_search_dev_inode(opts, ilist, dev, inode)) {
208 fuser_add_pid(plist, pid);
216 static int fuser_scan_link(int opts, const char *lname, pid_t pid,
217 inode_list *ilist, pid_list *plist)
222 if(!fuser_file_to_dev_inode(lname, &dev, &inode)) return 0;
223 if(fuser_search_dev_inode(opts, ilist, dev, inode))
224 fuser_add_pid(plist, pid);
228 static int fuser_scan_dir_links(int opts, const char *dname, pid_t pid,
229 inode_list *ilist, pid_list *plist)
235 if((d = opendir(dname))) {
236 while((de = readdir(d)) != NULL) {
237 lname = concat_subpath_file(dname, de->d_name);
240 fuser_scan_link(opts, lname, pid, ilist, plist);
250 static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist)
257 if(!(d = opendir(FUSER_PROC_DIR))) return 0;
258 while((de = readdir(d)) != NULL) {
259 pid = (pid_t)atoi(de->d_name);
261 dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name);
262 if(chdir(dname) < 0) {
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);
280 static int fuser_print_pid_list(pid_list *plist) {
281 pid_list *curr = plist;
283 if(plist == NULL) return 0;
284 while(curr != NULL) {
285 if(curr->pid > 0) printf("%d ", curr->pid);
292 static int fuser_kill_pid_list(pid_list *plist, int sig) {
293 pid_list *curr = plist;
294 pid_t mypid = getpid();
297 if(plist == NULL) return 0;
298 while(curr != NULL) {
299 if(curr->pid > 0 && curr->pid != mypid) {
300 if (kill(curr->pid, sig) != 0) {
302 "Could not kill pid '%d'", curr->pid);
311 extern int fuser_main(int argc, char **argv) {
313 int* fni; /* file name indexes of argv */
314 int fnic = 0; /* file name index count */
316 static int opt = 0; /* FUSER_OPT_ */
321 int killsig = SIGTERM;
324 fni = xmalloc(sizeof(int));
325 for(i=1;i<argc;i++) {
326 optn = fuser_option(argv[i]);
327 if(optn) opt |= optn;
328 else if(argv[i][0] == '-') {
329 if(!(u_signal_names(argv[i]+1, &killsig, 0)))
333 fni = xrealloc(fni, sizeof(int) * (fnic+2));
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);
346 if(!fuser_file_to_dev_inode(
347 argv[fni[i]], &dev, &inode)) {
350 bb_perror_msg_and_die(
351 "Could not open '%s'", argv[fni[i]]);
353 fuser_add_inode(inodes, dev, inode);
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;
360 if(opt & FUSER_OPT_KILL) {
361 success = fuser_kill_pid_list(pids, killsig);
363 else if(!(opt & FUSER_OPT_SILENT)) {
364 success = fuser_print_pid_list(pids);
369 /* return 0 on (success == 1) 1 otherwise */
370 return (success != 1);