source: MondoRescue/branches/2.2.9/mindi-busybox/procps/fuser.c@ 2887

Last change on this file since 2887 was 2859, checked in by Bruno Cornec, 13 years ago
  • Update to upstream busybox 1.18.5
File size: 5.9 KB
RevLine 
[821]1/* vi: set sw=4 ts=4: */
2/*
3 * tiny fuser implementation
4 *
5 * Copyright 2004 Tony J. White
6 *
[2725]7 * Licensed under GPLv2, see file LICENSE in this source tree.
[821]8 */
9
[1765]10#include "libbb.h"
[821]11
[2725]12#define MAX_LINE 255
[821]13
[2725]14#define OPTION_STRING "mks64"
15enum {
16 OPT_MOUNT = (1 << 0),
17 OPT_KILL = (1 << 1),
18 OPT_SILENT = (1 << 2),
19 OPT_IP6 = (1 << 3),
20 OPT_IP4 = (1 << 4),
21};
[821]22
23typedef struct inode_list {
[2725]24 struct inode_list *next;
[821]25 ino_t inode;
26 dev_t dev;
27} inode_list;
28
29typedef struct pid_list {
[2725]30 struct pid_list *next;
[821]31 pid_t pid;
32} pid_list;
33
34
[2725]35struct globals {
36 pid_list *pid_list_head;
37 inode_list *inode_list_head;
38};
39#define G (*(struct globals*)&bb_common_bufsiz1)
40#define INIT_G() do { } while (0)
[821]41
42
[2725]43static void add_pid(const pid_t pid)
[821]44{
[2725]45 pid_list **curr = &G.pid_list_head;
[821]46
[2725]47 while (*curr) {
48 if ((*curr)->pid == pid)
49 return;
50 curr = &(*curr)->next;
[821]51 }
52
[2725]53 *curr = xzalloc(sizeof(pid_list));
54 (*curr)->pid = pid;
[821]55}
56
[2725]57static void add_inode(const struct stat *st)
[821]58{
[2725]59 inode_list **curr = &G.inode_list_head;
[821]60
[2725]61 while (*curr) {
62 if ((*curr)->dev == st->st_dev
63 && (*curr)->inode == st->st_ino
64 ) {
65 return;
66 }
67 curr = &(*curr)->next;
[821]68 }
69
[2725]70 *curr = xzalloc(sizeof(inode_list));
71 (*curr)->dev = st->st_dev;
72 (*curr)->inode = st->st_ino;
[821]73}
74
[2725]75static void scan_proc_net(const char *path, unsigned port)
[821]76{
[2725]77 char line[MAX_LINE + 1];
[1765]78 long long uint64_inode;
[2725]79 unsigned tmp_port;
[821]80 FILE *f;
[2725]81 struct stat st;
82 int fd;
[821]83
[2725]84 /* find socket dev */
85 st.st_dev = 0;
86 fd = socket(AF_INET, SOCK_DGRAM, 0);
87 if (fd >= 0) {
88 fstat(fd, &st);
89 close(fd);
90 }
[821]91
[2725]92 f = fopen_for_read(path);
[1765]93 if (!f)
[2725]94 return;
95
96 while (fgets(line, MAX_LINE, f)) {
97 char addr[68];
[1765]98 if (sscanf(line, "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
99 "%*x:%*x %*x %*d %*d %llu",
100 addr, &tmp_port, &uint64_inode) == 3
101 ) {
[2725]102 int len = strlen(addr);
103 if (len == 8 && (option_mask32 & OPT_IP6))
[1765]104 continue;
[2725]105 if (len > 8 && (option_mask32 & OPT_IP4))
[1765]106 continue;
107 if (tmp_port == port) {
[2725]108 st.st_ino = uint64_inode;
109 add_inode(&st);
[821]110 }
111 }
112 }
113 fclose(f);
114}
115
[2725]116static int search_dev_inode(const struct stat *st)
[821]117{
[2725]118 inode_list *ilist = G.inode_list_head;
[821]119
[2725]120 while (ilist) {
121 if (ilist->dev == st->st_dev) {
122 if (option_mask32 & OPT_MOUNT)
123 return 1;
124 if (ilist->inode == st->st_ino)
125 return 1;
126 }
127 ilist = ilist->next;
[821]128 }
129 return 0;
130}
131
[2725]132static void scan_pid_maps(const char *fname, pid_t pid)
[821]133{
134 FILE *file;
[2725]135 char line[MAX_LINE + 1];
[821]136 int major, minor;
137 long long uint64_inode;
[2725]138 struct stat st;
[821]139
[2725]140 file = fopen_for_read(fname);
[1765]141 if (!file)
[2725]142 return;
143
144 while (fgets(line, MAX_LINE, file)) {
[1765]145 if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3)
146 continue;
[2725]147 st.st_ino = uint64_inode;
148 if (major == 0 && minor == 0 && st.st_ino == 0)
[1765]149 continue;
[2725]150 st.st_dev = makedev(major, minor);
151 if (search_dev_inode(&st))
152 add_pid(pid);
[821]153 }
154 fclose(file);
155}
156
[2725]157static void scan_link(const char *lname, pid_t pid)
[821]158{
[2725]159 struct stat st;
[821]160
[2725]161 if (stat(lname, &st) >= 0) {
162 if (search_dev_inode(&st))
163 add_pid(pid);
164 }
[821]165}
166
[2725]167static void scan_dir_links(const char *dname, pid_t pid)
[821]168{
169 DIR *d;
170 struct dirent *de;
171 char *lname;
172
[1765]173 d = opendir(dname);
174 if (!d)
[2725]175 return;
176
[1765]177 while ((de = readdir(d)) != NULL) {
178 lname = concat_subpath_file(dname, de->d_name);
179 if (lname == NULL)
180 continue;
[2725]181 scan_link(lname, pid);
[1765]182 free(lname);
[821]183 }
[1765]184 closedir(d);
[821]185}
186
[2725]187/* NB: does chdir internally */
188static void scan_proc_pids(void)
[821]189{
190 DIR *d;
191 struct dirent *de;
192 pid_t pid;
193
[2725]194 xchdir("/proc");
195 d = opendir("/proc");
[1765]196 if (!d)
[2725]197 return;
198
[1765]199 while ((de = readdir(d)) != NULL) {
[2725]200 pid = (pid_t)bb_strtou(de->d_name, NULL, 10);
201 if (errno)
[1765]202 continue;
[2725]203 if (chdir(de->d_name) < 0)
[821]204 continue;
[2725]205 scan_link("cwd", pid);
206 scan_link("exe", pid);
207 scan_link("root", pid);
208
209 scan_dir_links("fd", pid);
210 scan_dir_links("lib", pid);
211 scan_dir_links("mmap", pid);
212
213 scan_pid_maps("maps", pid);
214 xchdir("/proc");
[821]215 }
216 closedir(d);
217}
218
[2725]219int fuser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
220int fuser_main(int argc UNUSED_PARAM, char **argv)
[821]221{
[2725]222 pid_list *plist;
223 pid_t mypid;
224 char **pp;
225 struct stat st;
226 unsigned port;
227 int opt;
228 int exitcode;
229 int killsig;
230/*
231fuser [OPTIONS] FILE or PORT/PROTO
232Find processes which use FILEs or PORTs
233 -m Find processes which use same fs as FILEs
234 -4 Search only IPv4 space
235 -6 Search only IPv6 space
236 -s Don't display PIDs
237 -k Kill found processes
238 -SIGNAL Signal to send (default: KILL)
239*/
240 /* Handle -SIGNAL. Oh my... */
241 killsig = SIGKILL; /* yes, the default is not SIGTERM */
242 pp = argv;
243 while (*++pp) {
244 char *arg = *pp;
245 if (arg[0] != '-')
246 continue;
247 if (arg[1] == '-' && arg[2] == '\0') /* "--" */
248 break;
249 if ((arg[1] == '4' || arg[1] == '6') && arg[2] == '\0')
250 continue; /* it's "-4" or "-6" */
251 opt = get_signum(&arg[1]);
252 if (opt < 0)
253 continue;
254 /* "-SIGNAL" option found. Remove it and bail out */
255 killsig = opt;
256 do {
257 pp[0] = arg = pp[1];
258 pp++;
259 } while (arg);
260 break;
[821]261 }
262
[2725]263 opt_complementary = "-1"; /* at least one param */
264 opt = getopt32(argv, OPTION_STRING);
265 argv += optind;
[821]266
[2725]267 pp = argv;
268 while (*pp) {
269 /* parse net arg */
270 char path[20], tproto[5];
271 if (sscanf(*pp, "%u/%4s", &port, tproto) != 2)
272 goto file;
273 sprintf(path, "/proc/net/%s", tproto);
[2859]274 if (access(path, R_OK) == 0) { /* PORT/PROTO */
[2725]275 scan_proc_net(path, port);
276 } else { /* FILE */
277 file:
278 xstat(*pp, &st);
279 add_inode(&st);
[821]280 }
[2725]281 pp++;
[821]282 }
283
[2725]284 scan_proc_pids(); /* changes dir to "/proc" */
[1765]285
[2725]286 mypid = getpid();
287 plist = G.pid_list_head;
288 while (1) {
289 if (!plist)
290 return EXIT_FAILURE;
291 if (plist->pid != mypid)
292 break;
293 plist = plist->next;
[821]294 }
295
[2725]296 exitcode = EXIT_SUCCESS;
297 do {
298 if (plist->pid != mypid) {
299 if (opt & OPT_KILL) {
300 if (kill(plist->pid, killsig) != 0) {
301 bb_perror_msg("kill pid %u", (unsigned)plist->pid);
302 exitcode = EXIT_FAILURE;
303 }
[821]304 }
[2725]305 if (!(opt & OPT_SILENT)) {
306 printf("%u ", (unsigned)plist->pid);
307 }
[821]308 }
[2725]309 plist = plist->next;
310 } while (plist);
311
312 if (!(opt & (OPT_SILENT))) {
313 bb_putchar('\n');
[821]314 }
[2725]315
316 return exitcode;
[821]317}
Note: See TracBrowser for help on using the repository browser.