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
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #include <sys/sysmacros.h>
25 #define FUSER_PROC_DIR "/proc"
26 #define FUSER_MAX_LINE 255
28 #define FUSER_OPT_MOUNT 1
29 #define FUSER_OPT_KILL 2
30 #define FUSER_OPT_SILENT 4
31 #define FUSER_OPT_IP6 8
32 #define FUSER_OPT_IP4 16
34 typedef struct inode_list {
37 struct inode_list *next;
40 typedef struct pid_list {
42 struct pid_list *next;
45 static int fuser_option(char *option)
49 if(!(strlen(option))) return 0;
50 if(option[0] != '-') return 0;
52 while(*option != '\0') {
53 if(*option == 'm') opt |= FUSER_OPT_MOUNT;
54 else if(*option == 'k') opt |= FUSER_OPT_KILL;
55 else if(*option == 's') opt |= FUSER_OPT_SILENT;
56 else if(*option == '6') opt |= FUSER_OPT_IP6;
57 else if(*option == '4') opt |= FUSER_OPT_IP4;
60 "Unsupported option '%c'", *option);
67 static int fuser_file_to_dev_inode(const char *filename,
68 dev_t *dev, ino_t *inode)
71 if((stat(filename, &f_stat)) < 0) return 0;
72 *inode = f_stat.st_ino;
77 static int fuser_find_socket_dev(dev_t *dev)
79 int fd = socket(PF_INET, SOCK_DGRAM,0);
82 if (fd >= 0 && (fstat(fd, &buf)) == 0) {
90 static int fuser_parse_net_arg(const char *filename,
91 const char **proto, int *port)
93 char path[sizeof(FUSER_PROC_DIR)+12], tproto[5];
95 if((sscanf(filename, "%d/%4s", port, tproto)) != 2) return 0;
96 sprintf(path, "%s/net/%s", FUSER_PROC_DIR, tproto);
97 if((access(path, R_OK)) != 0) return 0;
98 *proto = bb_xstrdup(tproto);
102 static int fuser_add_pid(pid_list *plist, pid_t pid)
104 pid_list *curr = NULL, *last = NULL;
106 if(plist->pid == 0) plist->pid = pid;
108 while(curr != NULL) {
109 if(curr->pid == pid) return 1;
113 curr = xmalloc(sizeof(pid_list));
120 static int fuser_add_inode(inode_list *ilist, dev_t dev, ino_t inode)
122 inode_list *curr = NULL, *last = NULL;
124 if(!ilist->inode && !ilist->dev) {
126 ilist->inode = inode;
129 while(curr != NULL) {
130 if(curr->inode == inode && curr->dev == dev) return 1;
134 curr = xmalloc(sizeof(inode_list));
142 static int fuser_scan_proc_net(int opts, const char *proto,
143 int port, inode_list *ilist)
145 char path[sizeof(FUSER_PROC_DIR)+12], line[FUSER_MAX_LINE+1];
149 long long uint64_inode;
153 if(!fuser_find_socket_dev(&tmp_dev)) tmp_dev = 0;
154 sprintf(path, "%s/net/%s", FUSER_PROC_DIR, proto);
156 if (!(f = fopen(path, "r"))) return 0;
157 while(fgets(line, FUSER_MAX_LINE, f)) {
159 "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
160 "%*x:%*x %*x %*d %*d %llu",
161 addr, &tmp_port, &uint64_inode) == 3) {
162 if((strlen(addr) == 8) &&
163 (opts & FUSER_OPT_IP6)) continue;
164 else if((strlen(addr) > 8) &&
165 (opts & FUSER_OPT_IP4)) continue;
166 if(tmp_port == port) {
167 tmp_inode = uint64_inode;
168 fuser_add_inode(ilist, tmp_dev, tmp_inode);
177 static int fuser_search_dev_inode(int opts, inode_list *ilist,
178 dev_t dev, ino_t inode)
184 if((opts & FUSER_OPT_MOUNT) && curr->dev == dev)
186 if(curr->inode == inode && curr->dev == dev)
193 static int fuser_scan_pid_maps(int opts, const char *fname, pid_t pid,
194 inode_list *ilist, pid_list *plist)
197 char line[FUSER_MAX_LINE + 1];
200 long long uint64_inode;
203 if (!(file = fopen(fname, "r"))) return 0;
204 while (fgets(line, FUSER_MAX_LINE, file)) {
205 if(sscanf(line, "%*s %*s %*s %x:%x %llu",
206 &major, &minor, &uint64_inode) != 3) continue;
207 inode = uint64_inode;
208 if(major == 0 && minor == 0 && inode == 0) continue;
209 dev = makedev(major, minor);
210 if(fuser_search_dev_inode(opts, ilist, dev, inode)) {
211 fuser_add_pid(plist, pid);
219 static int fuser_scan_link(int opts, const char *lname, pid_t pid,
220 inode_list *ilist, pid_list *plist)
225 if(!fuser_file_to_dev_inode(lname, &dev, &inode)) return 0;
226 if(fuser_search_dev_inode(opts, ilist, dev, inode))
227 fuser_add_pid(plist, pid);
231 static int fuser_scan_dir_links(int opts, const char *dname, pid_t pid,
232 inode_list *ilist, pid_list *plist)
238 if((d = opendir(dname))) {
239 while((de = readdir(d)) != NULL) {
240 lname = concat_subpath_file(dname, de->d_name);
243 fuser_scan_link(opts, lname, pid, ilist, plist);
253 static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist)
260 if(!(d = opendir(FUSER_PROC_DIR))) return 0;
261 while((de = readdir(d)) != NULL) {
262 pid = (pid_t)atoi(de->d_name);
264 dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name);
265 if(chdir(dname) < 0) {
270 fuser_scan_link(opts, "cwd", pid, ilist, plist);
271 fuser_scan_link(opts, "exe", pid, ilist, plist);
272 fuser_scan_link(opts, "root", pid, ilist, plist);
273 fuser_scan_dir_links(opts, "fd", pid, ilist, plist);
274 fuser_scan_dir_links(opts, "lib", pid, ilist, plist);
275 fuser_scan_dir_links(opts, "mmap", pid, ilist, plist);
276 fuser_scan_pid_maps(opts, "maps", pid, ilist, plist);
283 static int fuser_print_pid_list(pid_list *plist)
285 pid_list *curr = plist;
287 if(plist == NULL) return 0;
288 while(curr != NULL) {
289 if(curr->pid > 0) printf("%d ", curr->pid);
296 static int fuser_kill_pid_list(pid_list *plist, int sig)
298 pid_list *curr = plist;
299 pid_t mypid = getpid();
302 if(plist == NULL) return 0;
303 while(curr != NULL) {
304 if(curr->pid > 0 && curr->pid != mypid) {
305 if (kill(curr->pid, sig) != 0) {
307 "Could not kill pid '%d'", curr->pid);
316 int fuser_main(int argc, char **argv)
319 int* fni; /* file name indexes of argv */
320 int fnic = 0; /* file name index count */
322 static int opt = 0; /* FUSER_OPT_ */
327 int killsig = SIGTERM;
333 fni = xmalloc(sizeof(int));
334 for(i=1;i<argc;i++) {
335 optn = fuser_option(argv[i]);
336 if(optn) opt |= optn;
337 else if(argv[i][0] == '-') {
338 if(!(u_signal_names(argv[i]+1, &killsig, 0)))
342 fni = xrealloc(fni, sizeof(int) * (fnic+2));
348 pids = xmalloc(sizeof(pid_list));
349 inodes = xmalloc(sizeof(inode_list));
350 for(i=0;i<fnic;i++) {
351 if(fuser_parse_net_arg(argv[fni[i]], &proto, &port)) {
352 fuser_scan_proc_net(opt, proto, port, inodes);
355 if(!fuser_file_to_dev_inode(
356 argv[fni[i]], &dev, &inode)) {
359 bb_perror_msg_and_die(
360 "Could not open '%s'", argv[fni[i]]);
362 fuser_add_inode(inodes, dev, inode);
365 success = fuser_scan_proc_pids(opt, inodes, pids);
366 /* if the first pid in the list is 0, none have been found */
367 if(pids->pid == 0) success = 0;
369 if(opt & FUSER_OPT_KILL) {
370 success = fuser_kill_pid_list(pids, killsig);
372 else if(!(opt & FUSER_OPT_SILENT)) {
373 success = fuser_print_pid_list(pids);
378 /* return 0 on (success == 1) 1 otherwise */
379 return (success != 1);