fuser: code shrink, fixing default signal, exitcode and PID display

Defult signal is KILL, not TERM.
We used to not display PIDs with -k but without -s, but
upstream (fuser from PSmisc 22.7) still shows PIDs.
Filtering out of our own PID was buggy.

function                                             old     new   delta
fuser_main                                           669     918    +249
search_dev_inode                                      67      74      +7
add_pid                                               38      39      +1
scan_pid_maps                                        225     222      -3
add_inode                                             91      88      -3
packed_usage                                       27047   27039      -8
scan_dir_links                                       102      76     -26
scan_link                                             78      46     -32
file_to_dev_inode                                     64       -     -64
scan_proc_net                                        307       -    -307
------------------------------------------------------------------------------
(add/remove: 0/2 grow/shrink: 3/5 up/down: 257/-443)         Total: -186 bytes

Signed-off-by: Maksym Kryzhanovskyy <xmaks@email.cz>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Maksym Kryzhanovskyy 2010-05-22 20:41:08 +02:00 committed by Denys Vlasenko
parent 8391c4800c
commit fef9ee7072
2 changed files with 125 additions and 139 deletions

View File

@ -1474,9 +1474,9 @@
"\n -m Find processes which use same fs as FILEs" \ "\n -m Find processes which use same fs as FILEs" \
"\n -4 Search only IPv4 space" \ "\n -4 Search only IPv4 space" \
"\n -6 Search only IPv6 space" \ "\n -6 Search only IPv6 space" \
"\n -s Silent: just exit with 0 if any processes are found" \ "\n -s Don't display PIDs" \
"\n -k Kill found processes (otherwise display PIDs)" \ "\n -k Kill found processes" \
"\n -SIGNAL Signal to send (default: TERM)" \ "\n -SIGNAL Signal to send (default: KILL)" \
#define getenforce_trivial_usage NOUSAGE_STR #define getenforce_trivial_usage NOUSAGE_STR
#define getenforce_full_usage "" #define getenforce_full_usage ""

View File

@ -31,6 +31,15 @@ typedef struct pid_list {
pid_t pid; pid_t pid;
} pid_list; } pid_list;
struct globals {
pid_list *pid_list_head;
inode_list *inode_list_head;
};
#define G (*(struct globals*)&bb_common_bufsiz1)
#define INIT_G() do { } while (0)
static dev_t find_socket_dev(void) static dev_t find_socket_dev(void)
{ {
int fd = socket(AF_INET, SOCK_DGRAM, 0); int fd = socket(AF_INET, SOCK_DGRAM, 0);
@ -44,16 +53,6 @@ static dev_t find_socket_dev(void)
return 0; return 0;
} }
static int file_to_dev_inode(const char *filename, dev_t *dev, ino_t *inode)
{
struct stat f_stat;
if (stat(filename, &f_stat))
return 0;
*inode = f_stat.st_ino;
*dev = f_stat.st_dev;
return 1;
}
static char *parse_net_arg(const char *arg, unsigned *port) static char *parse_net_arg(const char *arg, unsigned *port)
{ {
char path[20], tproto[5]; char path[20], tproto[5];
@ -63,54 +62,54 @@ static char *parse_net_arg(const char *arg, unsigned *port)
sprintf(path, "/proc/net/%s", tproto); sprintf(path, "/proc/net/%s", tproto);
if (access(path, R_OK) != 0) if (access(path, R_OK) != 0)
return NULL; return NULL;
return xstrdup(tproto); return xstrdup(path);
} }
static pid_list *add_pid(pid_list *plist, pid_t pid) static void add_pid(const pid_t pid)
{ {
pid_list *curr = plist; pid_list **curr = &G.pid_list_head;
while (curr != NULL) {
if (curr->pid == pid) while (*curr) {
return plist; if ((*curr)->pid == pid)
curr = curr->next; return;
curr = &(*curr)->next;
} }
curr = xmalloc(sizeof(pid_list));
curr->pid = pid; *curr = xzalloc(sizeof(pid_list));
curr->next = plist; (*curr)->pid = pid;
return curr;
} }
static inode_list *add_inode(inode_list *ilist, dev_t dev, ino_t inode) static void add_inode(const struct stat *st)
{ {
inode_list *curr = ilist; inode_list **curr = &G.inode_list_head;
while (curr != NULL) {
if (curr->inode == inode && curr->dev == dev) while (*curr) {
return ilist; if ((*curr)->dev == st->st_dev
curr = curr->next; && (*curr)->inode == st->st_ino
) {
return;
}
curr = &(*curr)->next;
} }
curr = xmalloc(sizeof(inode_list));
curr->dev = dev; *curr = xzalloc(sizeof(inode_list));
curr->inode = inode; (*curr)->dev = st->st_dev;
curr->next = ilist; (*curr)->inode = st->st_ino;
return curr;
} }
static inode_list *scan_proc_net(const char *proto, static void scan_proc_net(const char *path, unsigned port)
unsigned port, inode_list *ilist)
{ {
char path[20], line[MAX_LINE + 1]; char line[MAX_LINE + 1];
ino_t tmp_inode;
dev_t tmp_dev;
long long uint64_inode; long long uint64_inode;
unsigned tmp_port; unsigned tmp_port;
FILE *f; FILE *f;
struct stat st;
tmp_dev = find_socket_dev(); st.st_dev = find_socket_dev();
sprintf(path, "/proc/net/%s", proto);
f = fopen_for_read(path); f = fopen_for_read(path);
if (!f) if (!f)
return ilist; return;
while (fgets(line, MAX_LINE, f)) { while (fgets(line, MAX_LINE, f)) {
char addr[68]; char addr[68];
@ -124,22 +123,23 @@ static inode_list *scan_proc_net(const char *proto,
if (len > 8 && (option_mask32 & OPT_IP4)) if (len > 8 && (option_mask32 & OPT_IP4))
continue; continue;
if (tmp_port == port) { if (tmp_port == port) {
tmp_inode = uint64_inode; st.st_ino = uint64_inode;
ilist = add_inode(ilist, tmp_dev, tmp_inode); add_inode(&st);
} }
} }
} }
fclose(f); fclose(f);
return ilist;
} }
static int search_dev_inode(inode_list *ilist, dev_t dev, ino_t inode) static int search_dev_inode(const struct stat *st)
{ {
inode_list *ilist = G.inode_list_head;
while (ilist) { while (ilist) {
if (ilist->dev == dev) { if (ilist->dev == st->st_dev) {
if (option_mask32 & OPT_MOUNT) if (option_mask32 & OPT_MOUNT)
return 1; return 1;
if (ilist->inode == inode) if (ilist->inode == st->st_ino)
return 1; return 1;
} }
ilist = ilist->next; ilist = ilist->next;
@ -147,48 +147,42 @@ static int search_dev_inode(inode_list *ilist, dev_t dev, ino_t inode)
return 0; return 0;
} }
static pid_list *scan_pid_maps(const char *fname, pid_t pid, static void scan_pid_maps(const char *fname, pid_t pid)
inode_list *ilist, pid_list *plist)
{ {
FILE *file; FILE *file;
char line[MAX_LINE + 1]; char line[MAX_LINE + 1];
int major, minor; int major, minor;
ino_t inode;
long long uint64_inode; long long uint64_inode;
dev_t dev; struct stat st;
file = fopen_for_read(fname); file = fopen_for_read(fname);
if (!file) if (!file)
return plist; return;
while (fgets(line, MAX_LINE, file)) { while (fgets(line, MAX_LINE, file)) {
if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3) if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3)
continue; continue;
inode = uint64_inode; st.st_ino = uint64_inode;
if (major == 0 && minor == 0 && inode == 0) if (major == 0 && minor == 0 && st.st_ino == 0)
continue; continue;
dev = makedev(major, minor); st.st_dev = makedev(major, minor);
if (search_dev_inode(ilist, dev, inode)) if (search_dev_inode(&st))
plist = add_pid(plist, pid); add_pid(pid);
} }
fclose(file); fclose(file);
return plist;
} }
static pid_list *scan_link(const char *lname, pid_t pid, static void scan_link(const char *lname, pid_t pid)
inode_list *ilist, pid_list *plist)
{ {
ino_t inode; struct stat st;
dev_t dev;
if (!file_to_dev_inode(lname, &dev, &inode)) if (stat(lname, &st) >= 0) {
return plist; if (search_dev_inode(&st))
if (search_dev_inode(ilist, dev, inode)) add_pid(pid);
plist = add_pid(plist, pid); }
return plist;
} }
static pid_list *scan_dir_links(const char *dname, pid_t pid, static void scan_dir_links(const char *dname, pid_t pid)
inode_list *ilist, pid_list *plist)
{ {
DIR *d; DIR *d;
struct dirent *de; struct dirent *de;
@ -196,102 +190,73 @@ static pid_list *scan_dir_links(const char *dname, pid_t pid,
d = opendir(dname); d = opendir(dname);
if (!d) if (!d)
return plist; return;
while ((de = readdir(d)) != NULL) { while ((de = readdir(d)) != NULL) {
lname = concat_subpath_file(dname, de->d_name); lname = concat_subpath_file(dname, de->d_name);
if (lname == NULL) if (lname == NULL)
continue; continue;
plist = scan_link(lname, pid, ilist, plist); scan_link(lname, pid);
free(lname); free(lname);
} }
closedir(d); closedir(d);
return plist;
} }
/* NB: does chdir internally */ /* NB: does chdir internally */
static pid_list *scan_proc_pids(inode_list *ilist) static void scan_proc_pids(void)
{ {
DIR *d; DIR *d;
struct dirent *de; struct dirent *de;
pid_t pid; pid_t pid;
pid_list *plist;
xchdir("/proc"); xchdir("/proc");
d = opendir("/proc"); d = opendir("/proc");
if (!d) if (!d)
return NULL; return;
plist = NULL;
while ((de = readdir(d)) != NULL) { while ((de = readdir(d)) != NULL) {
pid = (pid_t)bb_strtou(de->d_name, NULL, 10); pid = (pid_t)bb_strtou(de->d_name, NULL, 10);
if (errno) if (errno)
continue; continue;
if (chdir(de->d_name) < 0) if (chdir(de->d_name) < 0)
continue; continue;
plist = scan_link("cwd", pid, ilist, plist); scan_link("cwd", pid);
plist = scan_link("exe", pid, ilist, plist); scan_link("exe", pid);
plist = scan_link("root", pid, ilist, plist); scan_link("root", pid);
plist = scan_dir_links("fd", pid, ilist, plist);
plist = scan_dir_links("lib", pid, ilist, plist); scan_dir_links("fd", pid);
plist = scan_dir_links("mmap", pid, ilist, plist); scan_dir_links("lib", pid);
plist = scan_pid_maps("maps", pid, ilist, plist); scan_dir_links("mmap", pid);
scan_pid_maps("maps", pid);
xchdir("/proc"); xchdir("/proc");
} }
closedir(d); closedir(d);
return plist;
}
static int print_pid_list(pid_list *plist)
{
while (plist != NULL) {
printf("%u ", (unsigned)plist->pid);
plist = plist->next;
}
bb_putchar('\n');
return 1;
}
static int kill_pid_list(pid_list *plist, int sig)
{
pid_t mypid = getpid();
int success = 1;
while (plist != NULL) {
if (plist->pid != mypid) {
if (kill(plist->pid, sig) != 0) {
bb_perror_msg("kill pid %u", (unsigned)plist->pid);
success = 0;
}
}
plist = plist->next;
}
return success;
} }
int fuser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int fuser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int fuser_main(int argc UNUSED_PARAM, char **argv) int fuser_main(int argc UNUSED_PARAM, char **argv)
{ {
pid_list *plist; pid_list *plist;
inode_list *ilist; pid_t mypid;
char **pp; char **pp;
dev_t dev; struct stat st;
ino_t inode;
unsigned port; unsigned port;
int opt; int opt;
int success; int exitcode;
int killsig; int killsig;
/* /*
fuser [options] FILEs or PORT/PROTOs fuser [OPTIONS] FILE or PORT/PROTO
Find processes which use FILEs or PORTs Find processes which use FILEs or PORTs
-m Find processes which use same fs as FILEs -m Find processes which use same fs as FILEs
-4 Search only IPv4 space -4 Search only IPv4 space
-6 Search only IPv6 space -6 Search only IPv6 space
-s Silent: just exit with 0 if any processes are found -s Don't display PIDs
-k Kill found processes (otherwise display PIDs) -k Kill found processes
-SIGNAL Signal to send (default: TERM) -SIGNAL Signal to send (default: KILL)
*/ */
/* Handle -SIGNAL. Oh my... */ /* Handle -SIGNAL. Oh my... */
killsig = SIGTERM; killsig = SIGKILL; /* yes, the default is not SIGTERM */
pp = argv; pp = argv;
while (*++pp) { while (*++pp) {
char *arg = *pp; char *arg = *pp;
@ -313,33 +278,54 @@ Find processes which use FILEs or PORTs
break; break;
} }
opt_complementary = "-1"; /* at least one param */
opt = getopt32(argv, OPTION_STRING); opt = getopt32(argv, OPTION_STRING);
argv += optind; argv += optind;
ilist = NULL;
pp = argv; pp = argv;
while (*pp) { while (*pp) {
char *proto = parse_net_arg(*pp, &port); char *path = parse_net_arg(*pp, &port);
if (proto) { /* PORT/PROTO */ if (path) { /* PORT/PROTO */
ilist = scan_proc_net(proto, port, ilist); scan_proc_net(path, port);
free(proto); free(path);
} else { /* FILE */ } else { /* FILE */
if (!file_to_dev_inode(*pp, &dev, &inode)) xstat(*pp, &st);
bb_perror_msg_and_die("can't open '%s'", *pp); add_inode(&st);
ilist = add_inode(ilist, dev, inode);
} }
pp++; pp++;
} }
plist = scan_proc_pids(ilist); /* changes dir to "/proc" */ scan_proc_pids(); /* changes dir to "/proc" */
if (!plist) mypid = getpid();
return EXIT_FAILURE; plist = G.pid_list_head;
success = 1; while (1) {
if (opt & OPT_KILL) { if (!plist)
success = kill_pid_list(plist, killsig); return EXIT_FAILURE;
} else if (!(opt & OPT_SILENT)) { if (plist->pid != mypid)
success = print_pid_list(plist); break;
plist = plist->next;
} }
return (success != 1); /* 0 == success */
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;
} }