Added CONFIG_CLEAR and CONFIG_RESET to config.maemo
[busybox4maemo] / procps / fuser.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * tiny fuser implementation
4  *
5  * Copyright 2004 Tony J. White
6  *
7  * May be distributed under the conditions of the
8  * GNU Library General Public License
9  */
10
11 #include "libbb.h"
12
13 #define MAX_LINE 255
14
15 #define OPTION_STRING "mks64"
16 enum {
17         OPT_MOUNT  = (1 << 0),
18         OPT_KILL   = (1 << 1),
19         OPT_SILENT = (1 << 2),
20         OPT_IP6    = (1 << 3),
21         OPT_IP4    = (1 << 4),
22 };
23
24 typedef struct inode_list {
25         struct inode_list *next;
26         ino_t inode;
27         dev_t dev;
28 } inode_list;
29
30 typedef struct pid_list {
31         struct pid_list *next;
32         pid_t pid;
33 } pid_list;
34
35 static dev_t find_socket_dev(void)
36 {
37         int fd = socket(AF_INET, SOCK_DGRAM, 0);
38         if (fd >= 0) {
39                 struct stat buf;
40                 int r = fstat(fd, &buf);
41                 close(fd);
42                 if (r == 0)
43                         return buf.st_dev;
44         }
45         return 0;
46 }
47
48 static int file_to_dev_inode(const char *filename, dev_t *dev, ino_t *inode)
49 {
50         struct stat f_stat;
51         if (stat(filename, &f_stat))
52                 return 0;
53         *inode = f_stat.st_ino;
54         *dev = f_stat.st_dev;
55         return 1;
56 }
57
58 static char *parse_net_arg(const char *arg, unsigned *port)
59 {
60         char path[20], tproto[5];
61
62         if (sscanf(arg, "%u/%4s", port, tproto) != 2)
63                 return NULL;
64         sprintf(path, "/proc/net/%s", tproto);
65         if (access(path, R_OK) != 0)
66                 return NULL;
67         return xstrdup(tproto);
68 }
69
70 static pid_list *add_pid(pid_list *plist, pid_t pid)
71 {
72         pid_list *curr = plist;
73         while (curr != NULL) {
74                 if (curr->pid == pid)
75                         return plist;
76                 curr = curr->next;
77         }
78         curr = xmalloc(sizeof(pid_list));
79         curr->pid = pid;
80         curr->next = plist;
81         return curr;
82 }
83
84 static inode_list *add_inode(inode_list *ilist, dev_t dev, ino_t inode)
85 {
86         inode_list *curr = ilist;
87         while (curr != NULL) {
88                 if (curr->inode == inode && curr->dev == dev)
89                         return ilist;
90                 curr = curr->next;
91         }
92         curr = xmalloc(sizeof(inode_list));
93         curr->dev = dev;
94         curr->inode = inode;
95         curr->next = ilist;
96         return curr;
97 }
98
99 static inode_list *scan_proc_net(const char *proto,
100                                 unsigned port, inode_list *ilist)
101 {
102         char path[20], line[MAX_LINE + 1];
103         char addr[128];
104         ino_t tmp_inode;
105         dev_t tmp_dev;
106         long long uint64_inode;
107         unsigned tmp_port;
108         FILE *f;
109
110         tmp_dev = find_socket_dev();
111
112         sprintf(path, "/proc/net/%s", proto);
113         f = fopen(path, "r");
114         if (!f)
115                 return ilist;
116
117         while (fgets(line, MAX_LINE, f)) {
118                 if (sscanf(line, "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
119                                 "%*x:%*x %*x %*d %*d %llu",
120                                 addr, &tmp_port, &uint64_inode) == 3
121                 ) {
122                         if (strlen(addr) == 8 && (option_mask32 & OPT_IP6))
123                                 continue;
124                         if (strlen(addr) > 8 && (option_mask32 & OPT_IP4))
125                                 continue;
126                         if (tmp_port == port) {
127                                 tmp_inode = uint64_inode;
128                                 ilist = add_inode(ilist, tmp_dev, tmp_inode);
129                         }
130                 }
131         }
132         fclose(f);
133         return ilist;
134 }
135
136 static int search_dev_inode(inode_list *ilist, dev_t dev, ino_t inode)
137 {
138         while (ilist) {
139                 if (ilist->dev == dev) {
140                         if (option_mask32 & OPT_MOUNT)
141                                 return 1;
142                         if (ilist->inode == inode)
143                                 return 1;
144                 }
145                 ilist = ilist->next;
146         }
147         return 0;
148 }
149
150 static pid_list *scan_pid_maps(const char *fname, pid_t pid,
151                                 inode_list *ilist, pid_list *plist)
152 {
153         FILE *file;
154         char line[MAX_LINE + 1];
155         int major, minor;
156         ino_t inode;
157         long long uint64_inode;
158         dev_t dev;
159
160         file = fopen(fname, "r");
161         if (!file)
162                 return plist;
163         while (fgets(line, MAX_LINE, file)) {
164                 if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3)
165                         continue;
166                 inode = uint64_inode;
167                 if (major == 0 && minor == 0 && inode == 0)
168                         continue;
169                 dev = makedev(major, minor);
170                 if (search_dev_inode(ilist, dev, inode))
171                         plist = add_pid(plist, pid);
172         }
173         fclose(file);
174         return plist;
175 }
176
177 static pid_list *scan_link(const char *lname, pid_t pid,
178                                 inode_list *ilist, pid_list *plist)
179 {
180         ino_t inode;
181         dev_t dev;
182
183         if (!file_to_dev_inode(lname, &dev, &inode))
184                 return plist;
185         if (search_dev_inode(ilist, dev, inode))
186                 plist = add_pid(plist, pid);
187         return plist;
188 }
189
190 static pid_list *scan_dir_links(const char *dname, pid_t pid,
191                                 inode_list *ilist, pid_list *plist)
192 {
193         DIR *d;
194         struct dirent *de;
195         char *lname;
196
197         d = opendir(dname);
198         if (!d)
199                 return plist;
200         while ((de = readdir(d)) != NULL) {
201                 lname = concat_subpath_file(dname, de->d_name);
202                 if (lname == NULL)
203                         continue;
204                 plist = scan_link(lname, pid, ilist, plist);
205                 free(lname);
206         }
207         closedir(d);
208         return plist;
209 }
210
211 static pid_list *scan_proc_pids(inode_list *ilist)
212 {
213         DIR *d;
214         struct dirent *de;
215         pid_t pid;
216         pid_list *plist;
217
218         d = opendir(".");
219         if (!d)
220                 return NULL;
221
222         plist = NULL;
223         while ((de = readdir(d)) != NULL) {
224                 pid = (pid_t)bb_strtou(de->d_name, NULL, 10);
225                 if (errno)
226                         continue;
227                 if (chdir(de->d_name) < 0)
228                         continue;
229                 plist = scan_link("cwd", pid, ilist, plist);
230                 plist = scan_link("exe", pid, ilist, plist);
231                 plist = scan_link("root", pid, ilist, plist);
232                 plist = scan_dir_links("fd", pid, ilist, plist);
233                 plist = scan_dir_links("lib", pid, ilist, plist);
234                 plist = scan_dir_links("mmap", pid, ilist, plist);
235                 plist = scan_pid_maps("maps", pid, ilist, plist);
236                 xchdir("/proc");
237         }
238         closedir(d);
239         return plist;
240 }
241
242 static int print_pid_list(pid_list *plist)
243 {
244         while (plist != NULL) {
245                 printf("%u ", (unsigned)plist->pid);
246                 plist = plist->next;
247         }
248         bb_putchar('\n');
249         return 1;
250 }
251
252 static int kill_pid_list(pid_list *plist, int sig)
253 {
254         pid_t mypid = getpid();
255         int success = 1;
256
257         while (plist != NULL) {
258                 if (plist->pid != mypid) {
259                         if (kill(plist->pid, sig) != 0) {
260                                 bb_perror_msg("kill pid %u", (unsigned)plist->pid);
261                                 success = 0;
262                         }
263                 }
264                 plist = plist->next;
265         }
266         return success;
267 }
268
269 int fuser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
270 int fuser_main(int argc ATTRIBUTE_UNUSED, char **argv)
271 {
272         pid_list *plist;
273         inode_list *ilist;
274         char **pp;
275         dev_t dev;
276         ino_t inode;
277         unsigned port;
278         int opt;
279         int success;
280         int killsig;
281 /*
282 fuser [options] FILEs or PORT/PROTOs
283 Find processes which use FILEs or PORTs
284         -m      Find processes which use same fs as FILEs
285         -4      Search only IPv4 space
286         -6      Search only IPv6 space
287         -s      Silent: just exit with 0 if any processes are found
288         -k      Kill found processes (otherwise display PIDs)
289         -SIGNAL Signal to send (default: TERM)
290 */
291         /* Handle -SIGNAL. Oh my... */
292         killsig = SIGTERM;
293         pp = argv;
294         while (*++pp) {
295                 char *arg = *pp;
296                 if (arg[0] != '-')
297                         continue;
298                 if (arg[1] == '-' && arg[2] == '\0') /* "--" */
299                         break;
300                 if ((arg[1] == '4' || arg[1] == '6') && arg[2] == '\0')
301                         continue; /* it's "-4" or "-6" */
302                 opt = get_signum(&arg[1]);
303                 if (opt < 0)
304                         continue;
305                 /* "-SIGNAL" option found. Remove it and bail out */
306                 killsig = opt;
307                 do {
308                         pp[0] = arg = pp[1];
309                         pp++;
310                 } while (arg);
311                 break;
312         }
313
314         opt = getopt32(argv, OPTION_STRING);
315         argv += optind;
316
317         ilist = NULL;
318         pp = argv;
319         while (*pp) {
320                 char *proto = parse_net_arg(*pp, &port);
321                 if (proto) { /* PORT/PROTO */
322                         ilist = scan_proc_net(proto, port, ilist);
323                         free(proto);
324                 } else { /* FILE */
325                         if (!file_to_dev_inode(*pp, &dev, &inode))
326                                 bb_perror_msg_and_die("can't open %s", *pp);
327                         ilist = add_inode(ilist, dev, inode);
328                 }
329                 pp++;
330         }
331
332         plist = scan_proc_pids(ilist);
333
334         if (!plist)
335                 return EXIT_FAILURE;
336         success = 1;
337         if (opt & OPT_KILL) {
338                 success = kill_pid_list(plist, killsig);
339         } else if (!(opt & OPT_SILENT)) {
340                 success = print_pid_list(plist);
341         }
342         return (success != 1); /* 0 == success */
343 }