rfelker writes in Bug 738: sys/sysmacros.h is needed for makedev
[oweals/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 <sys/sysmacros.h>
22 #include "busybox.h"
23
24 #define FUSER_PROC_DIR "/proc"
25 #define FUSER_MAX_LINE 255
26
27 #define FUSER_OPT_MOUNT  1
28 #define FUSER_OPT_KILL   2
29 #define FUSER_OPT_SILENT 4
30 #define FUSER_OPT_IP6    8
31 #define FUSER_OPT_IP4    16
32
33 typedef struct inode_list {
34         ino_t inode;
35         dev_t dev;
36         struct inode_list *next;
37 } inode_list;
38
39 typedef struct pid_list {
40         pid_t pid;
41         struct pid_list *next;
42 } pid_list;
43
44 static int fuser_option(char *option)
45 {
46         int opt = 0;
47
48         if(!(strlen(option))) return 0;
49         if(option[0] != '-') return 0;
50         ++option;
51         while(*option != '\0') {
52                 if(*option == 'm') opt |= FUSER_OPT_MOUNT;
53                 else if(*option == 'k') opt |= FUSER_OPT_KILL;
54                 else if(*option == 's') opt |= FUSER_OPT_SILENT;
55                 else if(*option == '6') opt |= FUSER_OPT_IP6;
56                 else if(*option == '4') opt |= FUSER_OPT_IP4;
57                 else {
58                         bb_error_msg_and_die(
59                                 "Unsupported option '%c'", *option);
60                 }
61                 ++option;
62         }
63         return opt;
64 }
65
66 static int fuser_file_to_dev_inode(const char *filename,
67          dev_t *dev, ino_t *inode)
68 {
69         struct stat f_stat;
70         if((stat(filename, &f_stat)) < 0) return 0;
71         *inode = f_stat.st_ino;
72         *dev = f_stat.st_dev;
73         return 1;
74 }
75
76 static int fuser_find_socket_dev(dev_t *dev) {
77         int fd = socket(PF_INET, SOCK_DGRAM,0);
78         struct stat buf;
79
80         if (fd >= 0 && (fstat(fd, &buf)) == 0) {
81                 *dev =  buf.st_dev;
82                 close(fd);
83                 return 1;
84         }
85         return 0;
86 }
87
88 static int fuser_parse_net_arg(const char *filename,
89         const char **proto, int *port)
90 {
91         char path[sizeof(FUSER_PROC_DIR)+12], tproto[5];
92
93         if((sscanf(filename, "%d/%4s", port, tproto)) != 2) return 0;
94         sprintf(path, "%s/net/%s", FUSER_PROC_DIR, tproto);
95         if((access(path, R_OK)) != 0) return 0;
96         *proto = bb_xstrdup(tproto);
97         return 1;
98 }
99
100 static int fuser_add_pid(pid_list *plist, pid_t pid)
101 {
102         pid_list *curr = NULL, *last = NULL;
103
104         if(plist->pid == 0) plist->pid = pid;
105         curr = plist;
106         while(curr != NULL) {
107                 if(curr->pid == pid) return 1;
108                 last = curr;
109                 curr = curr->next;
110         }
111         curr = xmalloc(sizeof(pid_list));
112         last->next = curr;
113         curr->pid = pid;
114         curr->next = NULL;
115         return 1;
116 }
117
118 static int fuser_add_inode(inode_list *ilist, dev_t dev, ino_t inode)
119 {
120         inode_list *curr = NULL, *last = NULL;
121
122         if(!ilist->inode && !ilist->dev) {
123                 ilist->dev = dev;
124                 ilist->inode = inode;
125         }
126         curr = ilist;
127         while(curr != NULL) {
128                 if(curr->inode == inode && curr->dev == dev) return 1;
129                 last = curr;
130                 curr = curr->next;
131         }
132         curr = xmalloc(sizeof(inode_list));
133         last->next = curr;
134         curr->dev = dev;
135         curr->inode = inode;
136         curr->next = NULL;
137         return 1;
138 }
139
140 static int fuser_scan_proc_net(int opts, const char *proto,
141         int port, inode_list *ilist)
142 {
143         char path[sizeof(FUSER_PROC_DIR)+12], line[FUSER_MAX_LINE+1];
144         char addr[128];
145         ino_t tmp_inode;
146         dev_t tmp_dev;
147         long long  uint64_inode;
148         int tmp_port;
149         FILE *f;
150
151         if(!fuser_find_socket_dev(&tmp_dev)) tmp_dev = 0;
152         sprintf(path, "%s/net/%s", FUSER_PROC_DIR, proto);
153
154         if (!(f = fopen(path, "r"))) return 0;
155         while(fgets(line, FUSER_MAX_LINE, f)) {
156                 if(sscanf(line,
157                         "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
158                         "%*x:%*x %*x %*d %*d %llu",
159                         addr, &tmp_port, &uint64_inode) == 3) {
160                         if((strlen(addr) == 8) &&
161                                 (opts & FUSER_OPT_IP6)) continue;
162                         else if((strlen(addr) > 8) &&
163                                 (opts & FUSER_OPT_IP4)) continue;
164                         if(tmp_port == port) {
165                                 tmp_inode = uint64_inode;
166                                 fuser_add_inode(ilist, tmp_dev, tmp_inode);
167                         }
168                 }
169
170         }
171         fclose(f);
172         return 1;
173 }
174
175 static int fuser_search_dev_inode(int opts, inode_list *ilist,
176         dev_t dev, ino_t inode)
177 {
178         inode_list *curr;
179         curr = ilist;
180
181         while(curr) {
182                 if((opts & FUSER_OPT_MOUNT) &&  curr->dev == dev)
183                         return 1;
184                 if(curr->inode == inode && curr->dev == dev)
185                         return 1;
186                 curr = curr->next;
187         }
188         return 0;
189 }
190
191 static int fuser_scan_pid_maps(int opts, const char *fname, pid_t pid,
192         inode_list *ilist, pid_list *plist)
193 {
194         FILE *file;
195         char line[FUSER_MAX_LINE + 1];
196         int major, minor;
197         ino_t inode;
198         long long uint64_inode;
199         dev_t dev;
200
201         if (!(file = fopen(fname, "r"))) return 0;
202         while (fgets(line, FUSER_MAX_LINE, file)) {
203                 if(sscanf(line, "%*s %*s %*s %x:%x %llu",
204                         &major, &minor, &uint64_inode) != 3) continue;
205                 inode = uint64_inode;
206                 if(major == 0 && minor == 0 && inode == 0) continue;
207                 dev = makedev(major, minor);
208                 if(fuser_search_dev_inode(opts, ilist, dev, inode)) {
209                         fuser_add_pid(plist, pid);
210                 }
211
212         }
213         fclose(file);
214         return 1;
215 }
216
217 static int fuser_scan_link(int opts, const char *lname, pid_t pid,
218         inode_list *ilist, pid_list *plist)
219 {
220         ino_t inode;
221         dev_t dev;
222
223         if(!fuser_file_to_dev_inode(lname, &dev, &inode)) return 0;
224         if(fuser_search_dev_inode(opts, ilist, dev, inode))
225                 fuser_add_pid(plist, pid);
226         return 1;
227 }
228
229 static int fuser_scan_dir_links(int opts, const char *dname, pid_t pid,
230         inode_list *ilist, pid_list *plist)
231 {
232         DIR *d;
233         struct dirent *de;
234         char *lname;
235
236         if((d = opendir(dname))) {
237                 while((de = readdir(d)) != NULL) {
238                         lname = concat_subpath_file(dname, de->d_name);
239                         if(lname == NULL)
240                                 continue;
241                         fuser_scan_link(opts, lname, pid, ilist, plist);
242                         free(lname);
243                 }
244                 closedir(d);
245         }
246         else return 0;
247         return 1;
248
249 }
250
251 static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist)
252 {
253         DIR *d;
254         struct dirent *de;
255         pid_t pid;
256         char *dname;
257
258         if(!(d = opendir(FUSER_PROC_DIR))) return 0;
259         while((de = readdir(d)) != NULL) {
260                 pid = (pid_t)atoi(de->d_name);
261                 if(!pid) continue;
262                 dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name);
263                 if(chdir(dname) < 0) {
264                         free(dname);
265                         continue;
266                 }
267                 free(dname);
268                 fuser_scan_link(opts, "cwd", pid, ilist, plist);
269                 fuser_scan_link(opts, "exe", pid, ilist, plist);
270                 fuser_scan_link(opts, "root", pid, ilist, plist);
271                 fuser_scan_dir_links(opts, "fd", pid, ilist, plist);
272                 fuser_scan_dir_links(opts, "lib", pid, ilist, plist);
273                 fuser_scan_dir_links(opts, "mmap", pid, ilist, plist);
274                 fuser_scan_pid_maps(opts, "maps", pid, ilist, plist);
275                 chdir("..");
276         }
277         closedir(d);
278         return 1;
279 }
280
281 static int fuser_print_pid_list(pid_list *plist) {
282         pid_list *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 = plist;
295         pid_t mypid = getpid();
296         int success = 1;
297
298         if(plist == NULL) return 0;
299         while(curr != NULL) {
300                 if(curr->pid > 0 && curr->pid != mypid) {
301                         if (kill(curr->pid, sig) != 0) {
302                                 bb_perror_msg(
303                                         "Could not kill pid '%d'", curr->pid);
304                                 success = 0;
305                         }
306                 }
307                 curr = curr->next;
308         }
309         return success;
310 }
311
312 extern int fuser_main(int argc, char **argv) {
313         int port, i, optn;
314         int* fni; /* file name indexes of argv */
315         int fnic = 0;  /* file name index count */
316         const char *proto;
317         static int opt = 0; /* FUSER_OPT_ */
318         dev_t dev;
319         ino_t inode;
320         pid_list *pids;
321         inode_list *inodes;
322         int killsig = SIGTERM;
323         int success = 1;
324
325         fni = xmalloc(sizeof(int));
326         for(i=1;i<argc;i++) {
327                 optn = fuser_option(argv[i]);
328                 if(optn) opt |= optn;
329                 else if(argv[i][0] == '-') {
330                         if(!(u_signal_names(argv[i]+1, &killsig, 0)))
331                                 killsig = SIGTERM;
332                 }
333                 else {
334                         fni = xrealloc(fni, sizeof(int) * (fnic+2));
335                         fni[fnic++] = i;
336                 }
337         }
338         if(!fnic) return 1;
339
340         pids = xmalloc(sizeof(pid_list));
341         inodes = xmalloc(sizeof(inode_list));
342         for(i=0;i<fnic;i++) {
343                 if(fuser_parse_net_arg(argv[fni[i]], &proto, &port)) {
344                         fuser_scan_proc_net(opt, proto, port, inodes);
345                 }
346                 else {
347                         if(!fuser_file_to_dev_inode(
348                                 argv[fni[i]], &dev, &inode)) {
349                                 free(pids);
350                                 free(inodes);
351                                 bb_perror_msg_and_die(
352                                         "Could not open '%s'", argv[fni[i]]);
353                         }
354                         fuser_add_inode(inodes, dev, inode);
355                 }
356         }
357         success = fuser_scan_proc_pids(opt, inodes, pids);
358         /* if the first pid in the list is 0, none have been found */
359         if(pids->pid == 0) success = 0;
360         if(success) {
361                 if(opt & FUSER_OPT_KILL) {
362                         success = fuser_kill_pid_list(pids, killsig);
363                 }
364                 else if(!(opt & FUSER_OPT_SILENT)) {
365                         success = fuser_print_pid_list(pids);
366                 }
367         }
368         free(pids);
369         free(inodes);
370         /* return 0 on (success == 1) 1 otherwise */
371         return (success != 1);
372 }