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 dev_t find_socket_dev(void)
45 int fd = socket(AF_INET, SOCK_DGRAM, 0);
48 int r = fstat(fd, &buf);
56 static char *parse_net_arg(const char *arg, unsigned *port)
58 char path[20], tproto[5];
60 if (sscanf(arg, "%u/%4s", port, tproto) != 2)
62 sprintf(path, "/proc/net/%s", tproto);
63 if (access(path, R_OK) != 0)
68 static void add_pid(const pid_t pid)
70 pid_list **curr = &G.pid_list_head;
73 if ((*curr)->pid == pid)
75 curr = &(*curr)->next;
78 *curr = xzalloc(sizeof(pid_list));
82 static void add_inode(const struct stat *st)
84 inode_list **curr = &G.inode_list_head;
87 if ((*curr)->dev == st->st_dev
88 && (*curr)->inode == st->st_ino
92 curr = &(*curr)->next;
95 *curr = xzalloc(sizeof(inode_list));
96 (*curr)->dev = st->st_dev;
97 (*curr)->inode = st->st_ino;
100 static void scan_proc_net(const char *path, unsigned port)
102 char line[MAX_LINE + 1];
103 long long uint64_inode;
108 st.st_dev = find_socket_dev();
110 f = fopen_for_read(path);
114 while (fgets(line, MAX_LINE, f)) {
116 if (sscanf(line, "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
117 "%*x:%*x %*x %*d %*d %llu",
118 addr, &tmp_port, &uint64_inode) == 3
120 int len = strlen(addr);
121 if (len == 8 && (option_mask32 & OPT_IP6))
123 if (len > 8 && (option_mask32 & OPT_IP4))
125 if (tmp_port == port) {
126 st.st_ino = uint64_inode;
134 static int search_dev_inode(const struct stat *st)
136 inode_list *ilist = G.inode_list_head;
139 if (ilist->dev == st->st_dev) {
140 if (option_mask32 & OPT_MOUNT)
142 if (ilist->inode == st->st_ino)
150 static void scan_pid_maps(const char *fname, pid_t pid)
153 char line[MAX_LINE + 1];
155 long long uint64_inode;
158 file = fopen_for_read(fname);
162 while (fgets(line, MAX_LINE, file)) {
163 if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3)
165 st.st_ino = uint64_inode;
166 if (major == 0 && minor == 0 && st.st_ino == 0)
168 st.st_dev = makedev(major, minor);
169 if (search_dev_inode(&st))
175 static void scan_link(const char *lname, pid_t pid)
179 if (stat(lname, &st) >= 0) {
180 if (search_dev_inode(&st))
185 static void scan_dir_links(const char *dname, pid_t pid)
195 while ((de = readdir(d)) != NULL) {
196 lname = concat_subpath_file(dname, de->d_name);
199 scan_link(lname, pid);
205 /* NB: does chdir internally */
206 static void scan_proc_pids(void)
213 d = opendir("/proc");
217 while ((de = readdir(d)) != NULL) {
218 pid = (pid_t)bb_strtou(de->d_name, NULL, 10);
221 if (chdir(de->d_name) < 0)
223 scan_link("cwd", pid);
224 scan_link("exe", pid);
225 scan_link("root", pid);
227 scan_dir_links("fd", pid);
228 scan_dir_links("lib", pid);
229 scan_dir_links("mmap", pid);
231 scan_pid_maps("maps", pid);
237 int fuser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
238 int fuser_main(int argc UNUSED_PARAM, char **argv)
249 fuser [OPTIONS] FILE or PORT/PROTO
250 Find processes which use FILEs or PORTs
251 -m Find processes which use same fs as FILEs
252 -4 Search only IPv4 space
253 -6 Search only IPv6 space
254 -s Don't display PIDs
255 -k Kill found processes
256 -SIGNAL Signal to send (default: KILL)
258 /* Handle -SIGNAL. Oh my... */
259 killsig = SIGKILL; /* yes, the default is not SIGTERM */
265 if (arg[1] == '-' && arg[2] == '\0') /* "--" */
267 if ((arg[1] == '4' || arg[1] == '6') && arg[2] == '\0')
268 continue; /* it's "-4" or "-6" */
269 opt = get_signum(&arg[1]);
272 /* "-SIGNAL" option found. Remove it and bail out */
281 opt_complementary = "-1"; /* at least one param */
282 opt = getopt32(argv, OPTION_STRING);
287 char *path = parse_net_arg(*pp, &port);
288 if (path) { /* PORT/PROTO */
289 scan_proc_net(path, port);
298 scan_proc_pids(); /* changes dir to "/proc" */
301 plist = G.pid_list_head;
305 if (plist->pid != mypid)
310 exitcode = EXIT_SUCCESS;
312 if (plist->pid != mypid) {
313 if (opt & OPT_KILL) {
314 if (kill(plist->pid, killsig) != 0) {
315 bb_perror_msg("kill pid %u", (unsigned)plist->pid);
316 exitcode = EXIT_FAILURE;
319 if (!(opt & OPT_SILENT)) {
320 printf("%u ", (unsigned)plist->pid);
326 if (!(opt & (OPT_SILENT))) {