int fuser_main(int argc UNUSED_PARAM, char **argv) { pid_list *plist; inode_list *ilist; char **pp; dev_t dev; ino_t inode; unsigned port; int opt; int success; int killsig; /* fuser [options] FILEs or PORT/PROTOs Find processes which use FILEs or PORTs -m Find processes which use same fs as FILEs -4 Search only IPv4 space -6 Search only IPv6 space -s Silent: just exit with 0 if any processes are found -k Kill found processes (otherwise display PIDs) -SIGNAL Signal to send (default: TERM) */ /* Handle -SIGNAL. Oh my... */ killsig = SIGTERM; pp = argv; while (*++pp) { char *arg = *pp; if (arg[0] != '-') continue; if (arg[1] == '-' && arg[2] == '\0') /* "--" */ break; if ((arg[1] == '4' || arg[1] == '6') && arg[2] == '\0') continue; /* it's "-4" or "-6" */ opt = get_signum(&arg[1]); if (opt < 0) continue; /* "-SIGNAL" option found. Remove it and bail out */ killsig = opt; do { pp[0] = arg = pp[1]; pp++; } while (arg); break; } opt = getopt32(argv, OPTION_STRING); argv += optind; ilist = NULL; pp = argv; while (*pp) { char *proto = parse_net_arg(*pp, &port); if (proto) { /* PORT/PROTO */ ilist = scan_proc_net(proto, port, ilist); free(proto); } else { /* FILE */ if (!file_to_dev_inode(*pp, &dev, &inode)) bb_perror_msg_and_die("can't open %s", *pp); ilist = add_inode(ilist, dev, inode); } pp++; } plist = scan_proc_pids(ilist); /* changes dir to "/proc" */ if (!plist) return EXIT_FAILURE; success = 1; if (opt & OPT_KILL) { success = kill_pid_list(plist, killsig); } else if (!(opt & OPT_SILENT)) { success = print_pid_list(plist); } return (success != 1); /* 0 == success */ }
int fuser_main(int argc UNUSED_PARAM, char **argv) { pid_list *plist; pid_t mypid; char **pp; struct stat st; unsigned port; int opt; int exitcode; int killsig; /* fuser [OPTIONS] FILE or PORT/PROTO Find processes which use FILEs or PORTs -m Find processes which use same fs as FILEs -4 Search only IPv4 space -6 Search only IPv6 space -s Don't display PIDs -k Kill found processes -SIGNAL Signal to send (default: KILL) */ /* Handle -SIGNAL. Oh my... */ killsig = SIGKILL; /* yes, the default is not SIGTERM */ pp = argv; while (*++pp) { char *arg = *pp; if (arg[0] != '-') continue; if (arg[1] == '-' && arg[2] == '\0') /* "--" */ break; if ((arg[1] == '4' || arg[1] == '6') && arg[2] == '\0') continue; /* it's "-4" or "-6" */ opt = get_signum(&arg[1]); if (opt < 0) continue; /* "-SIGNAL" option found. Remove it and bail out */ killsig = opt; do { pp[0] = arg = pp[1]; pp++; } while (arg); break; } opt_complementary = "-1"; /* at least one param */ opt = getopt32(argv, OPTION_STRING); argv += optind; pp = argv; while (*pp) { /* parse net arg */ char path[20], tproto[5]; if (sscanf(*pp, "%u/%4s", &port, tproto) != 2) goto file; sprintf(path, "/proc/net/%s", tproto); if (access(path, R_OK) != 0) { /* PORT/PROTO */ scan_proc_net(path, port); } else { /* FILE */ file: xstat(*pp, &st); add_inode(&st); } pp++; } scan_proc_pids(); /* changes dir to "/proc" */ mypid = getpid(); plist = G.pid_list_head; while (1) { if (!plist) return EXIT_FAILURE; if (plist->pid != mypid) break; plist = plist->next; } exitcode = EXIT_SUCCESS; do { if (plist->pid != mypid) { if (opt & OPT_KILL) { if (kill(plist->pid, killsig) != 0) { bb_perror_msg("kill pid %u", (unsigned)plist->pid); exitcode = EXIT_FAILURE; } } if (!(opt & OPT_SILENT)) { printf("%u ", (unsigned)plist->pid); } } plist = plist->next; } while (plist); if (!(opt & (OPT_SILENT))) { bb_putchar('\n'); } return exitcode; }