1 /* vi: set sw=4 ts=4: */
3 * tiny fuser implementation
5 * Copyright 2004 Tony J. White
7 * May be distributed under the conditions of the
8 * GNU Library General Public License
13 #define FUSER_PROC_DIR "/proc"
14 #define FUSER_MAX_LINE 255
16 #define FUSER_OPT_MOUNT 1
17 #define FUSER_OPT_KILL 2
18 #define FUSER_OPT_SILENT 4
19 #define FUSER_OPT_IP6 8
20 #define FUSER_OPT_IP4 16
22 typedef struct inode_list {
25 struct inode_list *next;
28 typedef struct pid_list {
30 struct pid_list *next;
33 static int fuser_option(char *option)
37 if(!(strlen(option))) return 0;
38 if(option[0] != '-') return 0;
40 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;
48 "Unsupported option '%c'", *option);
55 static int fuser_file_to_dev_inode(const char *filename,
56 dev_t *dev, ino_t *inode)
59 if((stat(filename, &f_stat)) < 0) return 0;
60 *inode = f_stat.st_ino;
65 static int fuser_find_socket_dev(dev_t *dev)
67 int fd = socket(PF_INET, SOCK_DGRAM,0);
70 if (fd >= 0 && (fstat(fd, &buf)) == 0) {
78 static int fuser_parse_net_arg(const char *filename,
79 const char **proto, int *port)
81 char path[sizeof(FUSER_PROC_DIR)+12], tproto[5];
83 if((sscanf(filename, "%d/%4s", port, tproto)) != 2) return 0;
84 sprintf(path, "%s/net/%s", FUSER_PROC_DIR, tproto);
85 if((access(path, R_OK)) != 0) return 0;
86 *proto = xstrdup(tproto);
90 static int fuser_add_pid(pid_list *plist, pid_t pid)
92 pid_list *curr = NULL, *last = NULL;
94 if(plist->pid == 0) plist->pid = pid;
97 if(curr->pid == pid) return 1;
101 curr = xmalloc(sizeof(pid_list));
108 static int fuser_add_inode(inode_list *ilist, dev_t dev, ino_t inode)
110 inode_list *curr = NULL, *last = NULL;
112 if(!ilist->inode && !ilist->dev) {
114 ilist->inode = inode;
117 while(curr != NULL) {
118 if(curr->inode == inode && curr->dev == dev) return 1;
122 curr = xmalloc(sizeof(inode_list));
130 static int fuser_scan_proc_net(int opts, const char *proto,
131 int port, inode_list *ilist)
133 char path[sizeof(FUSER_PROC_DIR)+12], line[FUSER_MAX_LINE+1];
137 long long uint64_inode;
141 if(!fuser_find_socket_dev(&tmp_dev)) tmp_dev = 0;
142 sprintf(path, "%s/net/%s", FUSER_PROC_DIR, proto);
144 if (!(f = fopen(path, "r"))) return 0;
145 while(fgets(line, FUSER_MAX_LINE, f)) {
147 "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
148 "%*x:%*x %*x %*d %*d %llu",
149 addr, &tmp_port, &uint64_inode) == 3) {
150 if((strlen(addr) == 8) &&
151 (opts & FUSER_OPT_IP6)) continue;
152 else if((strlen(addr) > 8) &&
153 (opts & FUSER_OPT_IP4)) continue;
154 if(tmp_port == port) {
155 tmp_inode = uint64_inode;
156 fuser_add_inode(ilist, tmp_dev, tmp_inode);
165 static int fuser_search_dev_inode(int opts, inode_list *ilist,
166 dev_t dev, ino_t inode)
172 if((opts & FUSER_OPT_MOUNT) && curr->dev == dev)
174 if(curr->inode == inode && curr->dev == dev)
181 static int fuser_scan_pid_maps(int opts, const char *fname, pid_t pid,
182 inode_list *ilist, pid_list *plist)
185 char line[FUSER_MAX_LINE + 1];
188 long long uint64_inode;
191 if (!(file = fopen(fname, "r"))) return 0;
192 while (fgets(line, FUSER_MAX_LINE, file)) {
193 if(sscanf(line, "%*s %*s %*s %x:%x %llu",
194 &major, &minor, &uint64_inode) != 3) continue;
195 inode = uint64_inode;
196 if(major == 0 && minor == 0 && inode == 0) continue;
197 dev = makedev(major, minor);
198 if(fuser_search_dev_inode(opts, ilist, dev, inode)) {
199 fuser_add_pid(plist, pid);
207 static int fuser_scan_link(int opts, const char *lname, pid_t pid,
208 inode_list *ilist, pid_list *plist)
213 if(!fuser_file_to_dev_inode(lname, &dev, &inode)) return 0;
214 if(fuser_search_dev_inode(opts, ilist, dev, inode))
215 fuser_add_pid(plist, pid);
219 static int fuser_scan_dir_links(int opts, const char *dname, pid_t pid,
220 inode_list *ilist, pid_list *plist)
226 if((d = opendir(dname))) {
227 while((de = readdir(d)) != NULL) {
228 lname = concat_subpath_file(dname, de->d_name);
231 fuser_scan_link(opts, lname, pid, ilist, plist);
241 static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist)
248 if(!(d = opendir(FUSER_PROC_DIR))) return 0;
249 while((de = readdir(d)) != NULL) {
250 pid = (pid_t)atoi(de->d_name);
252 dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name);
253 if(chdir(dname) < 0) {
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);
271 static int fuser_print_pid_list(pid_list *plist)
273 pid_list *curr = plist;
275 if(plist == NULL) return 0;
276 while(curr != NULL) {
277 if(curr->pid > 0) printf("%d ", curr->pid);
284 static int fuser_kill_pid_list(pid_list *plist, int sig)
286 pid_list *curr = plist;
287 pid_t mypid = getpid();
290 if(plist == NULL) return 0;
291 while(curr != NULL) {
292 if(curr->pid > 0 && curr->pid != mypid) {
293 if (kill(curr->pid, sig) != 0) {
295 "cannot kill pid '%d'", curr->pid);
304 int fuser_main(int argc, char **argv)
307 int* fni; /* file name indexes of argv */
308 int fnic = 0; /* file name index count */
310 static int opt = 0; /* FUSER_OPT_ */
315 int killsig = SIGTERM;
321 fni = xmalloc(sizeof(int));
322 for(i=1;i<argc;i++) {
323 optn = fuser_option(argv[i]);
324 if(optn) opt |= optn;
325 else if(argv[i][0] == '-') {
326 killsig = get_signum(argv[i]+1);
331 fni = xrealloc(fni, sizeof(int) * (fnic+2));
337 inodes = xmalloc(sizeof(inode_list));
338 for(i=0;i<fnic;i++) {
339 if(fuser_parse_net_arg(argv[fni[i]], &proto, &port)) {
340 fuser_scan_proc_net(opt, proto, port, inodes);
343 if(!fuser_file_to_dev_inode(
344 argv[fni[i]], &dev, &inode)) {
345 if (ENABLE_FEATURE_CLEAN_UP) free(inodes);
346 bb_perror_msg_and_die("cannot open '%s'", argv[fni[i]]);
348 fuser_add_inode(inodes, dev, inode);
351 pids = xmalloc(sizeof(pid_list));
352 success = fuser_scan_proc_pids(opt, inodes, pids);
353 /* if the first pid in the list is 0, none have been found */
354 if(pids->pid == 0) success = 0;
356 if(opt & FUSER_OPT_KILL) {
357 success = fuser_kill_pid_list(pids, killsig);
359 else if(!(opt & FUSER_OPT_SILENT)) {
360 success = fuser_print_pid_list(pids);
365 /* return 0 on (success == 1) 1 otherwise */
366 return (success != 1);