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 tarball for details.
14 #define OPTION_STRING "mks64"
18 OPT_SILENT = (1 << 2),
23 typedef struct inode_list {
24 struct inode_list *next;
29 typedef struct pid_list {
30 struct pid_list *next;
36 pid_list *pid_list_head;
37 inode_list *inode_list_head;
39 #define G (*(struct globals*)&bb_common_bufsiz1)
40 #define INIT_G() do { } while (0)
43 static void add_pid(const pid_t pid)
45 pid_list **curr = &G.pid_list_head;
48 if ((*curr)->pid == pid)
50 curr = &(*curr)->next;
53 *curr = xzalloc(sizeof(pid_list));
57 static void add_inode(const struct stat *st)
59 inode_list **curr = &G.inode_list_head;
62 if ((*curr)->dev == st->st_dev
63 && (*curr)->inode == st->st_ino
67 curr = &(*curr)->next;
70 *curr = xzalloc(sizeof(inode_list));
71 (*curr)->dev = st->st_dev;
72 (*curr)->inode = st->st_ino;
75 static void scan_proc_net(const char *path, unsigned port)
77 char line[MAX_LINE + 1];
78 long long uint64_inode;
86 fd = socket(AF_INET, SOCK_DGRAM, 0);
92 f = fopen_for_read(path);
96 while (fgets(line, MAX_LINE, f)) {
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
102 int len = strlen(addr);
103 if (len == 8 && (option_mask32 & OPT_IP6))
105 if (len > 8 && (option_mask32 & OPT_IP4))
107 if (tmp_port == port) {
108 st.st_ino = uint64_inode;
116 static int search_dev_inode(const struct stat *st)
118 inode_list *ilist = G.inode_list_head;
121 if (ilist->dev == st->st_dev) {
122 if (option_mask32 & OPT_MOUNT)
124 if (ilist->inode == st->st_ino)
132 static void scan_pid_maps(const char *fname, pid_t pid)
135 char line[MAX_LINE + 1];
137 long long uint64_inode;
140 file = fopen_for_read(fname);
144 while (fgets(line, MAX_LINE, file)) {
145 if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3)
147 st.st_ino = uint64_inode;
148 if (major == 0 && minor == 0 && st.st_ino == 0)
150 st.st_dev = makedev(major, minor);
151 if (search_dev_inode(&st))
157 static void scan_link(const char *lname, pid_t pid)
161 if (stat(lname, &st) >= 0) {
162 if (search_dev_inode(&st))
167 static void scan_dir_links(const char *dname, pid_t pid)
177 while ((de = readdir(d)) != NULL) {
178 lname = concat_subpath_file(dname, de->d_name);
181 scan_link(lname, pid);
187 /* NB: does chdir internally */
188 static void scan_proc_pids(void)
195 d = opendir("/proc");
199 while ((de = readdir(d)) != NULL) {
200 pid = (pid_t)bb_strtou(de->d_name, NULL, 10);
203 if (chdir(de->d_name) < 0)
205 scan_link("cwd", pid);
206 scan_link("exe", pid);
207 scan_link("root", pid);
209 scan_dir_links("fd", pid);
210 scan_dir_links("lib", pid);
211 scan_dir_links("mmap", pid);
213 scan_pid_maps("maps", pid);
219 int fuser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
220 int fuser_main(int argc UNUSED_PARAM, char **argv)
231 fuser [OPTIONS] FILE or PORT/PROTO
232 Find 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)
240 /* Handle -SIGNAL. Oh my... */
241 killsig = SIGKILL; /* yes, the default is not SIGTERM */
247 if (arg[1] == '-' && arg[2] == '\0') /* "--" */
249 if ((arg[1] == '4' || arg[1] == '6') && arg[2] == '\0')
250 continue; /* it's "-4" or "-6" */
251 opt = get_signum(&arg[1]);
254 /* "-SIGNAL" option found. Remove it and bail out */
263 opt_complementary = "-1"; /* at least one param */
264 opt = getopt32(argv, OPTION_STRING);
270 char path[20], tproto[5];
271 if (sscanf(*pp, "%u/%4s", &port, tproto) != 2)
273 sprintf(path, "/proc/net/%s", tproto);
274 if (access(path, R_OK) != 0) { /* PORT/PROTO */
275 scan_proc_net(path, port);
284 scan_proc_pids(); /* changes dir to "/proc" */
287 plist = G.pid_list_head;
291 if (plist->pid != mypid)
296 exitcode = EXIT_SUCCESS;
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;
305 if (!(opt & OPT_SILENT)) {
306 printf("%u ", (unsigned)plist->pid);
312 if (!(opt & (OPT_SILENT))) {