1 /* vi: set sw=4 ts=4: */
3 * tiny fuser implementation
5 * Copyright 2004 Tony J. White
7 * Licensed under GPLv2, see file LICENSE in this source tree.
10 //usage:#define fuser_trivial_usage
11 //usage: "[OPTIONS] FILE or PORT/PROTO"
12 //usage:#define fuser_full_usage "\n\n"
13 //usage: "Find processes which use FILEs or PORTs\n"
15 //usage: "\n -m Find processes which use same fs as FILEs"
16 //usage: "\n -4,-6 Search only IPv4/IPv6 space"
17 //usage: "\n -s Don't display PIDs"
18 //usage: "\n -k Kill found processes"
19 //usage: "\n -SIGNAL Signal to send (default: KILL)"
25 #define OPTION_STRING "mks64"
29 OPT_SILENT = (1 << 2),
34 typedef struct inode_list {
35 struct inode_list *next;
40 typedef struct pid_list {
41 struct pid_list *next;
47 pid_list *pid_list_head;
48 inode_list *inode_list_head;
50 #define G (*(struct globals*)&bb_common_bufsiz1)
51 #define INIT_G() do { } while (0)
54 static void add_pid(const pid_t pid)
56 pid_list **curr = &G.pid_list_head;
59 if ((*curr)->pid == pid)
61 curr = &(*curr)->next;
64 *curr = xzalloc(sizeof(pid_list));
68 static void add_inode(const struct stat *st)
70 inode_list **curr = &G.inode_list_head;
73 if ((*curr)->dev == st->st_dev
74 && (*curr)->inode == st->st_ino
78 curr = &(*curr)->next;
81 *curr = xzalloc(sizeof(inode_list));
82 (*curr)->dev = st->st_dev;
83 (*curr)->inode = st->st_ino;
86 static void scan_proc_net(const char *path, unsigned port)
88 char line[MAX_LINE + 1];
89 long long uint64_inode;
97 fd = socket(AF_INET, SOCK_DGRAM, 0);
103 f = fopen_for_read(path);
107 while (fgets(line, MAX_LINE, f)) {
109 if (sscanf(line, "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
110 "%*x:%*x %*x %*d %*d %llu",
111 addr, &tmp_port, &uint64_inode) == 3
113 int len = strlen(addr);
114 if (len == 8 && (option_mask32 & OPT_IP6))
116 if (len > 8 && (option_mask32 & OPT_IP4))
118 if (tmp_port == port) {
119 st.st_ino = uint64_inode;
127 static int search_dev_inode(const struct stat *st)
129 inode_list *ilist = G.inode_list_head;
132 if (ilist->dev == st->st_dev) {
133 if (option_mask32 & OPT_MOUNT)
135 if (ilist->inode == st->st_ino)
143 static void scan_pid_maps(const char *fname, pid_t pid)
146 char line[MAX_LINE + 1];
148 long long uint64_inode;
151 file = fopen_for_read(fname);
155 while (fgets(line, MAX_LINE, file)) {
156 if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3)
158 st.st_ino = uint64_inode;
159 if (major == 0 && minor == 0 && st.st_ino == 0)
161 st.st_dev = makedev(major, minor);
162 if (search_dev_inode(&st))
168 static void scan_link(const char *lname, pid_t pid)
172 if (stat(lname, &st) >= 0) {
173 if (search_dev_inode(&st))
178 static void scan_dir_links(const char *dname, pid_t pid)
188 while ((de = readdir(d)) != NULL) {
189 lname = concat_subpath_file(dname, de->d_name);
192 scan_link(lname, pid);
198 /* NB: does chdir internally */
199 static void scan_proc_pids(void)
206 d = opendir("/proc");
210 while ((de = readdir(d)) != NULL) {
211 pid = (pid_t)bb_strtou(de->d_name, NULL, 10);
214 if (chdir(de->d_name) < 0)
216 scan_link("cwd", pid);
217 scan_link("exe", pid);
218 scan_link("root", pid);
220 scan_dir_links("fd", pid);
221 scan_dir_links("lib", pid);
222 scan_dir_links("mmap", pid);
224 scan_pid_maps("maps", pid);
230 int fuser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
231 int fuser_main(int argc UNUSED_PARAM, char **argv)
242 fuser [OPTIONS] FILE or PORT/PROTO
243 Find processes which use FILEs or PORTs
244 -m Find processes which use same fs as FILEs
245 -4 Search only IPv4 space
246 -6 Search only IPv6 space
247 -s Don't display PIDs
248 -k Kill found processes
249 -SIGNAL Signal to send (default: KILL)
251 /* Handle -SIGNAL. Oh my... */
252 killsig = SIGKILL; /* yes, the default is not SIGTERM */
258 if (arg[1] == '-' && arg[2] == '\0') /* "--" */
260 if ((arg[1] == '4' || arg[1] == '6') && arg[2] == '\0')
261 continue; /* it's "-4" or "-6" */
262 opt = get_signum(&arg[1]);
265 /* "-SIGNAL" option found. Remove it and bail out */
274 opt_complementary = "-1"; /* at least one param */
275 opt = getopt32(argv, OPTION_STRING);
281 char path[20], tproto[5];
282 if (sscanf(*pp, "%u/%4s", &port, tproto) != 2)
284 sprintf(path, "/proc/net/%s", tproto);
285 if (access(path, R_OK) != 0) { /* PORT/PROTO */
286 scan_proc_net(path, port);
295 scan_proc_pids(); /* changes dir to "/proc" */
298 plist = G.pid_list_head;
302 if (plist->pid != mypid)
307 exitcode = EXIT_SUCCESS;
309 if (plist->pid != mypid) {
310 if (opt & OPT_KILL) {
311 if (kill(plist->pid, killsig) != 0) {
312 bb_perror_msg("kill pid %u", (unsigned)plist->pid);
313 exitcode = EXIT_FAILURE;
316 if (!(opt & OPT_SILENT)) {
317 printf("%u ", (unsigned)plist->pid);
323 if (!(opt & (OPT_SILENT))) {