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)
42 while (*option != '\0') {
43 if (*option == 'm') opt |= FUSER_OPT_MOUNT;
44 else if (*option == 'k') opt |= FUSER_OPT_KILL;
45 else if (*option == 's') opt |= FUSER_OPT_SILENT;
46 else if (*option == '6') opt |= FUSER_OPT_IP6;
47 else if (*option == '4') opt |= FUSER_OPT_IP4;
49 bb_error_msg_and_die("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)
61 *inode = f_stat.st_ino;
66 static int fuser_find_socket_dev(dev_t *dev)
68 int fd = socket(PF_INET, SOCK_DGRAM,0);
71 if (fd >= 0 && (fstat(fd, &buf)) == 0) {
79 static int fuser_parse_net_arg(const char *filename,
80 const char **proto, int *port)
82 char path[sizeof(FUSER_PROC_DIR)+12], tproto[5];
84 if ((sscanf(filename, "%d/%4s", port, tproto)) != 2)
86 sprintf(path, FUSER_PROC_DIR "/net/%s", tproto);
87 if ((access(path, R_OK)) != 0)
89 *proto = xstrdup(tproto);
93 static int fuser_add_pid(pid_list *plist, pid_t pid)
95 pid_list *curr = NULL, *last = NULL;
100 while (curr != NULL) {
101 if (curr->pid == pid)
106 curr = xzalloc(sizeof(pid_list));
109 /*curr->next = NULL;*/
113 static int fuser_add_inode(inode_list *ilist, dev_t dev, ino_t inode)
115 inode_list *curr = NULL, *last = NULL;
117 if (!ilist->inode && !ilist->dev) {
119 ilist->inode = inode;
122 while (curr != NULL) {
123 if (curr->inode == inode && curr->dev == dev)
128 curr = xzalloc(sizeof(inode_list));
132 /*curr->next = NULL;*/
136 static int fuser_scan_proc_net(int opts, const char *proto,
137 int port, inode_list *ilist)
139 char path[sizeof(FUSER_PROC_DIR)+12], line[FUSER_MAX_LINE+1];
143 long long uint64_inode;
147 if (!fuser_find_socket_dev(&tmp_dev))
149 sprintf(path, FUSER_PROC_DIR "/net/%s", proto);
151 f = fopen(path, "r");
154 while (fgets(line, FUSER_MAX_LINE, f)) {
155 if (sscanf(line, "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
156 "%*x:%*x %*x %*d %*d %llu",
157 addr, &tmp_port, &uint64_inode) == 3
159 if (strlen(addr) == 8 && (opts & FUSER_OPT_IP6))
161 if (strlen(addr) > 8 && (opts & FUSER_OPT_IP4))
163 if (tmp_port == port) {
164 tmp_inode = uint64_inode;
165 fuser_add_inode(ilist, tmp_dev, tmp_inode);
173 static int fuser_search_dev_inode(int opts, inode_list *ilist,
174 dev_t dev, ino_t inode)
180 if ((opts & FUSER_OPT_MOUNT) && curr->dev == dev)
182 if (curr->inode == inode && curr->dev == dev)
189 static int fuser_scan_pid_maps(int opts, const char *fname, pid_t pid,
190 inode_list *ilist, pid_list *plist)
193 char line[FUSER_MAX_LINE + 1];
196 long long uint64_inode;
199 file = fopen(fname, "r");
202 while (fgets(line, FUSER_MAX_LINE, file)) {
203 if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3)
205 inode = uint64_inode;
206 if (major == 0 && minor == 0 && inode == 0)
208 dev = makedev(major, minor);
209 if (fuser_search_dev_inode(opts, ilist, dev, inode)) {
210 fuser_add_pid(plist, pid);
217 static int fuser_scan_link(int opts, const char *lname, pid_t pid,
218 inode_list *ilist, pid_list *plist)
223 if (!fuser_file_to_dev_inode(lname, &dev, &inode))
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)
240 while ((de = readdir(d)) != NULL) {
241 lname = concat_subpath_file(dname, de->d_name);
244 fuser_scan_link(opts, lname, pid, ilist, plist);
251 static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist)
258 d = opendir(FUSER_PROC_DIR);
261 while ((de = readdir(d)) != NULL) {
262 pid = (pid_t)atoi(de->d_name);
265 dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name);
266 if (chdir(dname) < 0) {
271 fuser_scan_link(opts, "cwd", pid, ilist, plist);
272 fuser_scan_link(opts, "exe", pid, ilist, plist);
273 fuser_scan_link(opts, "root", pid, ilist, plist);
274 fuser_scan_dir_links(opts, "fd", pid, ilist, plist);
275 fuser_scan_dir_links(opts, "lib", pid, ilist, plist);
276 fuser_scan_dir_links(opts, "mmap", pid, ilist, plist);
277 fuser_scan_pid_maps(opts, "maps", pid, ilist, plist);
284 static int fuser_print_pid_list(pid_list *plist)
286 pid_list *curr = plist;
290 while (curr != NULL) {
292 printf("%d ", curr->pid);
299 static int fuser_kill_pid_list(pid_list *plist, int sig)
301 pid_list *curr = plist;
302 pid_t mypid = getpid();
307 while (curr != NULL) {
308 if (curr->pid > 0 && curr->pid != mypid) {
309 if (kill(curr->pid, sig) != 0) {
310 bb_perror_msg("kill pid '%d'", curr->pid);
319 int fuser_main(int argc, char **argv);
320 int fuser_main(int argc, char **argv)
322 /*static -- huh???*/ int opt = 0; /* FUSER_OPT_ */
325 int* fni; /* file name indexes of argv */
326 int fnic = 0; /* file name index count */
332 int killsig = SIGTERM;
338 fni = xmalloc(sizeof(int));
339 for (i = 1; i < argc; i++) {
340 optn = fuser_option(argv[i]);
343 else if (argv[i][0] == '-') {
344 killsig = get_signum(argv[i]+1);
348 fni = xrealloc(fni, sizeof(int) * (fnic+2));
356 inodes = xmalloc(sizeof(inode_list));
357 for (i = 0; i < fnic; i++) {
358 if (fuser_parse_net_arg(argv[fni[i]], &proto, &port)) {
359 fuser_scan_proc_net(opt, proto, port, inodes);
361 if (!fuser_file_to_dev_inode(argv[fni[i]], &dev, &inode)) {
362 if (ENABLE_FEATURE_CLEAN_UP)
364 bb_perror_msg_and_die("cannot open '%s'", argv[fni[i]]);
366 fuser_add_inode(inodes, dev, inode);
369 pids = xmalloc(sizeof(pid_list));
370 success = fuser_scan_proc_pids(opt, inodes, pids);
371 /* if the first pid in the list is 0, none have been found */
375 if (opt & FUSER_OPT_KILL) {
376 success = fuser_kill_pid_list(pids, killsig);
377 } else if (!(opt & FUSER_OPT_SILENT)) {
378 success = fuser_print_pid_list(pids);
381 if (ENABLE_FEATURE_CLEAN_UP) {
385 /* return 0 on (success == 1) 1 otherwise */
386 return (success != 1);