66e0988aed2686ae3d4499bbb2d0006f5897ff75
[platform/upstream/busybox.git] / procps / fuser.c
1 /*
2  * tiny fuser implementation 
3  * 
4  * Copyright 2004 Tony J. White
5  *
6  * May be distributed under the conditions of the
7  * GNU Library General Public License
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <limits.h>
15 #include <dirent.h>
16 #include <signal.h>
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
19 #include <sys/stat.h>
20 #include <sys/socket.h>
21 #include "busybox.h"
22
23 #define FUSER_PROC_DIR "/proc"
24 #define FUSER_MAX_LINE 255
25
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 
31
32 typedef struct inode_list {
33         ino_t inode;
34         dev_t dev;
35         struct inode_list *next;
36 } inode_list;
37
38 typedef struct pid_list {
39         pid_t pid;
40         struct pid_list *next;
41 } pid_list;
42
43 static int fuser_option(char *option) 
44 {
45         int opt = 0;
46
47         if(!(strlen(option))) return 0;
48         if(option[0] != '-') return 0;
49         *++option;
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;
56                 else {
57                         bb_error_msg_and_die(
58                                 "Unsupported option '%c'", *option); 
59                 }
60                 *++option;
61         }
62         return opt;
63 }
64
65 static int fuser_file_to_dev_inode(const char *filename,
66          dev_t *dev, ino_t *inode)
67 {
68         struct stat f_stat;
69         if((stat(filename, &f_stat)) < 0) return 0;
70         *inode = f_stat.st_ino;
71         *dev = f_stat.st_dev;
72         return 1;
73 }
74
75 static int fuser_find_socket_dev(dev_t *dev) {
76         int fd = socket(PF_INET, SOCK_DGRAM,0);
77         struct stat buf;
78         
79         if (fd >= 0 && (fstat(fd, &buf)) == 0) {
80                 *dev =  buf.st_dev;
81                 close(fd);
82                 return 1;
83         }
84         return 0;
85 }
86
87 static int fuser_parse_net_arg(const char *filename, 
88         const char **proto, int *port) 
89 {
90         char path[sizeof(FUSER_PROC_DIR)+12], tproto[5];
91
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);
96         return 1;       
97 }
98
99 static int fuser_add_pid(pid_list *plist, pid_t pid)
100 {
101         pid_list *curr = NULL, *last = NULL;
102         
103         if(plist->pid == 0) plist->pid = pid;
104         curr = plist;
105         while(curr != NULL) {
106                 if(curr->pid == pid) return 1;
107                 last = curr;
108                 curr = curr->next;
109         }
110         curr = xmalloc(sizeof(pid_list));
111         last->next = curr;
112         curr->pid = pid;
113         curr->next = NULL;
114         return 1;
115 }
116
117 static int fuser_add_inode(inode_list *ilist, dev_t dev, ino_t inode)
118 {
119         inode_list *curr = NULL, *last = NULL;
120                                                                                 
121         if(!ilist->inode && !ilist->dev) {
122                 ilist->dev = dev;
123                 ilist->inode = inode;
124         }
125         curr = ilist;
126         while(curr != NULL) {
127                 if(curr->inode == inode && curr->dev == dev) return 1;
128                 last = curr;
129                 curr = curr->next;
130         }
131         curr = xmalloc(sizeof(inode_list));
132         last->next = curr;
133         curr->dev = dev;
134         curr->inode = inode;
135         curr->next = NULL;
136         return 1;
137 }
138
139 static int fuser_scan_proc_net(int opts, const char *proto, 
140         int port, inode_list *ilist) 
141 {
142         char path[sizeof(FUSER_PROC_DIR)+12], line[FUSER_MAX_LINE+1];
143         char addr[128];
144         ino_t tmp_inode;
145         dev_t tmp_dev;
146         uint64_t  uint64_inode;
147         int tmp_port;
148         FILE *f;
149
150         if(!fuser_find_socket_dev(&tmp_dev)) tmp_dev = 0;
151         sprintf(path, "%s/net/%s", FUSER_PROC_DIR, proto);
152
153         if (!(f = fopen(path, "r"))) return 0;
154         while(fgets(line, FUSER_MAX_LINE, f)) {
155                 if(sscanf(line,
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);
166                         }
167                 }
168
169         }
170         fclose(f);
171         return 1;
172 }
173
174 static int fuser_search_dev_inode(int opts, inode_list *ilist, 
175         dev_t dev, ino_t inode) 
176 {
177         inode_list *curr;
178         curr = ilist;
179
180         while(curr) {
181                 if((opts & FUSER_OPT_MOUNT) &&  curr->dev == dev)
182                         return 1;
183                 if(curr->inode == inode && curr->dev == dev)
184                         return 1;
185                 curr = curr->next;
186         }
187         return 0;
188 }
189
190 static int fuser_scan_pid_maps(int opts, const char *fname, pid_t pid,
191         inode_list *ilist, pid_list *plist) 
192 {
193         FILE *file;
194         char line[FUSER_MAX_LINE + 1];
195         int major, minor;
196         ino_t inode;
197         uint64_t uint64_inode;
198         dev_t dev;
199
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);
209                 }
210                 
211         }
212         fclose(file);
213         return 1;
214 }
215
216 static int fuser_scan_link(int opts, const char *lname, pid_t pid, 
217         inode_list *ilist, pid_list *plist) 
218 {
219         ino_t inode;
220         dev_t dev;      
221
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);
225         return 1;
226 }
227
228 static int fuser_scan_dir_links(int opts, const char *dname, pid_t pid,
229         inode_list *ilist, pid_list *plist)
230 {
231         DIR *d;
232         struct dirent *de;
233         char *lname;
234
235         if((d = opendir(dname))) {
236                 while((de = readdir(d)) != NULL) {
237                         lname = concat_subpath_file(dname, de->d_name);
238                         if(lname == NULL)
239                                 continue;
240                         fuser_scan_link(opts, lname, pid, ilist, plist);
241                         free(lname);
242                 }
243                 closedir(d);
244         }
245         else return 0;
246         return 1;
247
248
249
250 static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist) 
251 {
252         DIR *d;
253         struct dirent *de;
254         pid_t pid;
255         char *dname;
256
257         if(!(d = opendir(FUSER_PROC_DIR))) return 0;
258         while((de = readdir(d)) != NULL) {
259                 pid = (pid_t)atoi(de->d_name);
260                 if(!pid) continue;
261                 dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name);
262                 if(chdir(dname) < 0) {
263                         free(dname);
264                         continue; 
265                 }
266                 free(dname);
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);
274                 chdir("..");
275         } 
276         closedir(d);    
277         return 1;
278 }
279
280 static int fuser_print_pid_list(pid_list *plist) {
281         pid_list *curr;
282         curr = plist;
283
284         if(plist == NULL) return 0;
285         while(curr != NULL) {
286                 if(curr->pid > 0) printf("%d ", curr->pid);
287                 curr = curr->next;
288         }
289         printf("\n");
290         return 1;
291 }
292
293 static int fuser_kill_pid_list(pid_list *plist, int sig) {
294         pid_list *curr;
295         curr = plist;
296         pid_t mypid = getpid();
297         int success = 1;
298
299         if(plist == NULL) return 0;
300         while(curr != NULL) {
301                 if(curr->pid > 0 && curr->pid != mypid) {
302                         if (kill(curr->pid, sig) != 0) { 
303                                 bb_perror_msg(
304                                         "Could not kill pid '%d'", curr->pid);
305                                 success = 0;            
306                         }
307                 }
308                 curr = curr->next;
309         }
310         return success;
311 }
312
313 extern int fuser_main(int argc, char **argv) {
314         int port, i, optn;
315         int* fni; /* file name indexes of argv */
316         int fnic = 0;  /* file name index count */
317         const char *proto;
318         static int opt = 0; /* FUSER_OPT_ */
319         dev_t dev;
320         ino_t inode;
321         pid_list *pids;
322         inode_list *inodes;
323         int killsig = SIGTERM;
324         int success = 1;
325
326         fni = xmalloc(sizeof(int));
327         for(i=1;i<argc;i++) {
328                 optn = fuser_option(argv[i]);
329                 if(optn) opt |= optn; 
330                 else if(argv[i][0] == '-') {
331                         if(!(u_signal_names(argv[i]+1, &killsig, 0)))
332                                 killsig = SIGTERM;
333                 }
334                 else {
335                         fni = xrealloc(fni, sizeof(int) * (fnic+2));
336                         fni[fnic++] = i;
337                 }
338         }
339         if(!fnic) return 1;
340
341         pids = xmalloc(sizeof(pid_list));
342         inodes = xmalloc(sizeof(inode_list));
343         for(i=0;i<fnic;i++) {
344                 if(fuser_parse_net_arg(argv[fni[i]], &proto, &port)) {
345                         fuser_scan_proc_net(opt, proto, port, inodes);
346                 }
347                 else {
348                         if(!fuser_file_to_dev_inode(
349                                 argv[fni[i]], &dev, &inode)) {
350                                 free(pids);
351                                 free(inodes);
352                                 bb_perror_msg_and_die(
353                                         "Could not open '%s'", argv[fni[i]]);
354                         }
355                         fuser_add_inode(inodes, dev, inode);
356                 }
357         }
358         success = fuser_scan_proc_pids(opt, inodes, pids);
359         /* if the first pid in the list is 0, none have been found */
360         if(pids->pid == 0) success = 0;
361         if(success) {
362                 if(opt & FUSER_OPT_KILL) {
363                         success = fuser_kill_pid_list(pids, killsig);
364                 }
365                 else if(!(opt & FUSER_OPT_SILENT)) {
366                         success = fuser_print_pid_list(pids);
367                 }
368         }
369         free(pids);
370         free(inodes);
371         /* return 0 on (success == 1) 1 otherwise */
372         return (success != 1);
373