diff options
| author | Maksym Kryzhanovskyy <xmaks@email.cz> | 2010-05-22 20:41:08 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-05-22 20:41:08 +0200 |
| commit | fef9ee70727452954d2c5d28cc65e8b0fffcd6f1 (patch) | |
| tree | 93083c2e5b970f9d280ed7d2dc4df1c4cef4c31e | |
| parent | 8391c4800cda0eda6e64c464cc0ea85d1785a768 (diff) | |
| download | busybox-w32-fef9ee70727452954d2c5d28cc65e8b0fffcd6f1.tar.gz busybox-w32-fef9ee70727452954d2c5d28cc65e8b0fffcd6f1.tar.bz2 busybox-w32-fef9ee70727452954d2c5d28cc65e8b0fffcd6f1.zip | |
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>
| -rw-r--r-- | include/usage.h | 6 | ||||
| -rw-r--r-- | procps/fuser.c | 258 |
2 files changed, 125 insertions, 139 deletions
diff --git a/include/usage.h b/include/usage.h index a295ab865..8f695f8b8 100644 --- a/include/usage.h +++ b/include/usage.h | |||
| @@ -1474,9 +1474,9 @@ | |||
| 1474 | "\n -m Find processes which use same fs as FILEs" \ | 1474 | "\n -m Find processes which use same fs as FILEs" \ |
| 1475 | "\n -4 Search only IPv4 space" \ | 1475 | "\n -4 Search only IPv4 space" \ |
| 1476 | "\n -6 Search only IPv6 space" \ | 1476 | "\n -6 Search only IPv6 space" \ |
| 1477 | "\n -s Silent: just exit with 0 if any processes are found" \ | 1477 | "\n -s Don't display PIDs" \ |
| 1478 | "\n -k Kill found processes (otherwise display PIDs)" \ | 1478 | "\n -k Kill found processes" \ |
| 1479 | "\n -SIGNAL Signal to send (default: TERM)" \ | 1479 | "\n -SIGNAL Signal to send (default: KILL)" \ |
| 1480 | 1480 | ||
| 1481 | #define getenforce_trivial_usage NOUSAGE_STR | 1481 | #define getenforce_trivial_usage NOUSAGE_STR |
| 1482 | #define getenforce_full_usage "" | 1482 | #define getenforce_full_usage "" |
diff --git a/procps/fuser.c b/procps/fuser.c index dc3d01bda..7465d4554 100644 --- a/procps/fuser.c +++ b/procps/fuser.c | |||
| @@ -31,6 +31,15 @@ typedef struct pid_list { | |||
| 31 | pid_t pid; | 31 | pid_t pid; |
| 32 | } pid_list; | 32 | } pid_list; |
| 33 | 33 | ||
| 34 | |||
| 35 | struct globals { | ||
| 36 | pid_list *pid_list_head; | ||
| 37 | inode_list *inode_list_head; | ||
| 38 | }; | ||
| 39 | #define G (*(struct globals*)&bb_common_bufsiz1) | ||
| 40 | #define INIT_G() do { } while (0) | ||
| 41 | |||
| 42 | |||
| 34 | static dev_t find_socket_dev(void) | 43 | static dev_t find_socket_dev(void) |
| 35 | { | 44 | { |
| 36 | int fd = socket(AF_INET, SOCK_DGRAM, 0); | 45 | int fd = socket(AF_INET, SOCK_DGRAM, 0); |
| @@ -44,16 +53,6 @@ static dev_t find_socket_dev(void) | |||
| 44 | return 0; | 53 | return 0; |
| 45 | } | 54 | } |
| 46 | 55 | ||
| 47 | static int file_to_dev_inode(const char *filename, dev_t *dev, ino_t *inode) | ||
| 48 | { | ||
| 49 | struct stat f_stat; | ||
| 50 | if (stat(filename, &f_stat)) | ||
| 51 | return 0; | ||
| 52 | *inode = f_stat.st_ino; | ||
| 53 | *dev = f_stat.st_dev; | ||
| 54 | return 1; | ||
| 55 | } | ||
| 56 | |||
| 57 | static char *parse_net_arg(const char *arg, unsigned *port) | 56 | static char *parse_net_arg(const char *arg, unsigned *port) |
| 58 | { | 57 | { |
| 59 | char path[20], tproto[5]; | 58 | char path[20], tproto[5]; |
| @@ -63,54 +62,54 @@ static char *parse_net_arg(const char *arg, unsigned *port) | |||
| 63 | sprintf(path, "/proc/net/%s", tproto); | 62 | sprintf(path, "/proc/net/%s", tproto); |
| 64 | if (access(path, R_OK) != 0) | 63 | if (access(path, R_OK) != 0) |
| 65 | return NULL; | 64 | return NULL; |
| 66 | return xstrdup(tproto); | 65 | return xstrdup(path); |
| 67 | } | 66 | } |
| 68 | 67 | ||
| 69 | static pid_list *add_pid(pid_list *plist, pid_t pid) | 68 | static void add_pid(const pid_t pid) |
| 70 | { | 69 | { |
| 71 | pid_list *curr = plist; | 70 | pid_list **curr = &G.pid_list_head; |
| 72 | while (curr != NULL) { | 71 | |
| 73 | if (curr->pid == pid) | 72 | while (*curr) { |
| 74 | return plist; | 73 | if ((*curr)->pid == pid) |
| 75 | curr = curr->next; | 74 | return; |
| 75 | curr = &(*curr)->next; | ||
| 76 | } | 76 | } |
| 77 | curr = xmalloc(sizeof(pid_list)); | 77 | |
| 78 | curr->pid = pid; | 78 | *curr = xzalloc(sizeof(pid_list)); |
| 79 | curr->next = plist; | 79 | (*curr)->pid = pid; |
| 80 | return curr; | ||
| 81 | } | 80 | } |
| 82 | 81 | ||
| 83 | static inode_list *add_inode(inode_list *ilist, dev_t dev, ino_t inode) | 82 | static void add_inode(const struct stat *st) |
| 84 | { | 83 | { |
| 85 | inode_list *curr = ilist; | 84 | inode_list **curr = &G.inode_list_head; |
| 86 | while (curr != NULL) { | 85 | |
| 87 | if (curr->inode == inode && curr->dev == dev) | 86 | while (*curr) { |
| 88 | return ilist; | 87 | if ((*curr)->dev == st->st_dev |
| 89 | curr = curr->next; | 88 | && (*curr)->inode == st->st_ino |
| 89 | ) { | ||
| 90 | return; | ||
| 91 | } | ||
| 92 | curr = &(*curr)->next; | ||
| 90 | } | 93 | } |
| 91 | curr = xmalloc(sizeof(inode_list)); | 94 | |
| 92 | curr->dev = dev; | 95 | *curr = xzalloc(sizeof(inode_list)); |
| 93 | curr->inode = inode; | 96 | (*curr)->dev = st->st_dev; |
| 94 | curr->next = ilist; | 97 | (*curr)->inode = st->st_ino; |
| 95 | return curr; | ||
| 96 | } | 98 | } |
| 97 | 99 | ||
| 98 | static inode_list *scan_proc_net(const char *proto, | 100 | static void scan_proc_net(const char *path, unsigned port) |
| 99 | unsigned port, inode_list *ilist) | ||
| 100 | { | 101 | { |
| 101 | char path[20], line[MAX_LINE + 1]; | 102 | char line[MAX_LINE + 1]; |
| 102 | ino_t tmp_inode; | ||
| 103 | dev_t tmp_dev; | ||
| 104 | long long uint64_inode; | 103 | long long uint64_inode; |
| 105 | unsigned tmp_port; | 104 | unsigned tmp_port; |
| 106 | FILE *f; | 105 | FILE *f; |
| 106 | struct stat st; | ||
| 107 | 107 | ||
| 108 | tmp_dev = find_socket_dev(); | 108 | st.st_dev = find_socket_dev(); |
| 109 | 109 | ||
| 110 | sprintf(path, "/proc/net/%s", proto); | ||
| 111 | f = fopen_for_read(path); | 110 | f = fopen_for_read(path); |
| 112 | if (!f) | 111 | if (!f) |
| 113 | return ilist; | 112 | return; |
| 114 | 113 | ||
| 115 | while (fgets(line, MAX_LINE, f)) { | 114 | while (fgets(line, MAX_LINE, f)) { |
| 116 | char addr[68]; | 115 | char addr[68]; |
| @@ -124,22 +123,23 @@ static inode_list *scan_proc_net(const char *proto, | |||
| 124 | if (len > 8 && (option_mask32 & OPT_IP4)) | 123 | if (len > 8 && (option_mask32 & OPT_IP4)) |
| 125 | continue; | 124 | continue; |
| 126 | if (tmp_port == port) { | 125 | if (tmp_port == port) { |
| 127 | tmp_inode = uint64_inode; | 126 | st.st_ino = uint64_inode; |
| 128 | ilist = add_inode(ilist, tmp_dev, tmp_inode); | 127 | add_inode(&st); |
| 129 | } | 128 | } |
| 130 | } | 129 | } |
| 131 | } | 130 | } |
| 132 | fclose(f); | 131 | fclose(f); |
| 133 | return ilist; | ||
| 134 | } | 132 | } |
| 135 | 133 | ||
| 136 | static int search_dev_inode(inode_list *ilist, dev_t dev, ino_t inode) | 134 | static int search_dev_inode(const struct stat *st) |
| 137 | { | 135 | { |
| 136 | inode_list *ilist = G.inode_list_head; | ||
| 137 | |||
| 138 | while (ilist) { | 138 | while (ilist) { |
| 139 | if (ilist->dev == dev) { | 139 | if (ilist->dev == st->st_dev) { |
| 140 | if (option_mask32 & OPT_MOUNT) | 140 | if (option_mask32 & OPT_MOUNT) |
| 141 | return 1; | 141 | return 1; |
| 142 | if (ilist->inode == inode) | 142 | if (ilist->inode == st->st_ino) |
| 143 | return 1; | 143 | return 1; |
| 144 | } | 144 | } |
| 145 | ilist = ilist->next; | 145 | ilist = ilist->next; |
| @@ -147,48 +147,42 @@ static int search_dev_inode(inode_list *ilist, dev_t dev, ino_t inode) | |||
| 147 | return 0; | 147 | return 0; |
| 148 | } | 148 | } |
| 149 | 149 | ||
| 150 | static pid_list *scan_pid_maps(const char *fname, pid_t pid, | 150 | static void scan_pid_maps(const char *fname, pid_t pid) |
| 151 | inode_list *ilist, pid_list *plist) | ||
| 152 | { | 151 | { |
| 153 | FILE *file; | 152 | FILE *file; |
| 154 | char line[MAX_LINE + 1]; | 153 | char line[MAX_LINE + 1]; |
| 155 | int major, minor; | 154 | int major, minor; |
| 156 | ino_t inode; | ||
| 157 | long long uint64_inode; | 155 | long long uint64_inode; |
| 158 | dev_t dev; | 156 | struct stat st; |
| 159 | 157 | ||
| 160 | file = fopen_for_read(fname); | 158 | file = fopen_for_read(fname); |
| 161 | if (!file) | 159 | if (!file) |
| 162 | return plist; | 160 | return; |
| 161 | |||
| 163 | while (fgets(line, MAX_LINE, file)) { | 162 | while (fgets(line, MAX_LINE, file)) { |
| 164 | if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3) | 163 | if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3) |
| 165 | continue; | 164 | continue; |
| 166 | inode = uint64_inode; | 165 | st.st_ino = uint64_inode; |
| 167 | if (major == 0 && minor == 0 && inode == 0) | 166 | if (major == 0 && minor == 0 && st.st_ino == 0) |
| 168 | continue; | 167 | continue; |
| 169 | dev = makedev(major, minor); | 168 | st.st_dev = makedev(major, minor); |
| 170 | if (search_dev_inode(ilist, dev, inode)) | 169 | if (search_dev_inode(&st)) |
| 171 | plist = add_pid(plist, pid); | 170 | add_pid(pid); |
| 172 | } | 171 | } |
| 173 | fclose(file); | 172 | fclose(file); |
| 174 | return plist; | ||
| 175 | } | 173 | } |
| 176 | 174 | ||
| 177 | static pid_list *scan_link(const char *lname, pid_t pid, | 175 | static void scan_link(const char *lname, pid_t pid) |
| 178 | inode_list *ilist, pid_list *plist) | ||
| 179 | { | 176 | { |
| 180 | ino_t inode; | 177 | struct stat st; |
| 181 | dev_t dev; | ||
| 182 | 178 | ||
| 183 | if (!file_to_dev_inode(lname, &dev, &inode)) | 179 | if (stat(lname, &st) >= 0) { |
| 184 | return plist; | 180 | if (search_dev_inode(&st)) |
| 185 | if (search_dev_inode(ilist, dev, inode)) | 181 | add_pid(pid); |
| 186 | plist = add_pid(plist, pid); | 182 | } |
| 187 | return plist; | ||
| 188 | } | 183 | } |
| 189 | 184 | ||
| 190 | static pid_list *scan_dir_links(const char *dname, pid_t pid, | 185 | static void scan_dir_links(const char *dname, pid_t pid) |
| 191 | inode_list *ilist, pid_list *plist) | ||
| 192 | { | 186 | { |
| 193 | DIR *d; | 187 | DIR *d; |
| 194 | struct dirent *de; | 188 | struct dirent *de; |
| @@ -196,102 +190,73 @@ static pid_list *scan_dir_links(const char *dname, pid_t pid, | |||
| 196 | 190 | ||
| 197 | d = opendir(dname); | 191 | d = opendir(dname); |
| 198 | if (!d) | 192 | if (!d) |
| 199 | return plist; | 193 | return; |
| 194 | |||
| 200 | while ((de = readdir(d)) != NULL) { | 195 | while ((de = readdir(d)) != NULL) { |
| 201 | lname = concat_subpath_file(dname, de->d_name); | 196 | lname = concat_subpath_file(dname, de->d_name); |
| 202 | if (lname == NULL) | 197 | if (lname == NULL) |
| 203 | continue; | 198 | continue; |
| 204 | plist = scan_link(lname, pid, ilist, plist); | 199 | scan_link(lname, pid); |
| 205 | free(lname); | 200 | free(lname); |
| 206 | } | 201 | } |
| 207 | closedir(d); | 202 | closedir(d); |
| 208 | return plist; | ||
| 209 | } | 203 | } |
| 210 | 204 | ||
| 211 | /* NB: does chdir internally */ | 205 | /* NB: does chdir internally */ |
| 212 | static pid_list *scan_proc_pids(inode_list *ilist) | 206 | static void scan_proc_pids(void) |
| 213 | { | 207 | { |
| 214 | DIR *d; | 208 | DIR *d; |
| 215 | struct dirent *de; | 209 | struct dirent *de; |
| 216 | pid_t pid; | 210 | pid_t pid; |
| 217 | pid_list *plist; | ||
| 218 | 211 | ||
| 219 | xchdir("/proc"); | 212 | xchdir("/proc"); |
| 220 | d = opendir("/proc"); | 213 | d = opendir("/proc"); |
| 221 | if (!d) | 214 | if (!d) |
| 222 | return NULL; | 215 | return; |
| 223 | 216 | ||
| 224 | plist = NULL; | ||
| 225 | while ((de = readdir(d)) != NULL) { | 217 | while ((de = readdir(d)) != NULL) { |
| 226 | pid = (pid_t)bb_strtou(de->d_name, NULL, 10); | 218 | pid = (pid_t)bb_strtou(de->d_name, NULL, 10); |
| 227 | if (errno) | 219 | if (errno) |
| 228 | continue; | 220 | continue; |
| 229 | if (chdir(de->d_name) < 0) | 221 | if (chdir(de->d_name) < 0) |
| 230 | continue; | 222 | continue; |
| 231 | plist = scan_link("cwd", pid, ilist, plist); | 223 | scan_link("cwd", pid); |
| 232 | plist = scan_link("exe", pid, ilist, plist); | 224 | scan_link("exe", pid); |
| 233 | plist = scan_link("root", pid, ilist, plist); | 225 | scan_link("root", pid); |
| 234 | plist = scan_dir_links("fd", pid, ilist, plist); | ||
| 235 | plist = scan_dir_links("lib", pid, ilist, plist); | ||
| 236 | plist = scan_dir_links("mmap", pid, ilist, plist); | ||
| 237 | plist = scan_pid_maps("maps", pid, ilist, plist); | ||
| 238 | xchdir("/proc"); | ||
| 239 | } | ||
| 240 | closedir(d); | ||
| 241 | return plist; | ||
| 242 | } | ||
| 243 | 226 | ||
| 244 | static int print_pid_list(pid_list *plist) | 227 | scan_dir_links("fd", pid); |
| 245 | { | 228 | scan_dir_links("lib", pid); |
| 246 | while (plist != NULL) { | 229 | scan_dir_links("mmap", pid); |
| 247 | printf("%u ", (unsigned)plist->pid); | ||
| 248 | plist = plist->next; | ||
| 249 | } | ||
| 250 | bb_putchar('\n'); | ||
| 251 | return 1; | ||
| 252 | } | ||
| 253 | |||
| 254 | static int kill_pid_list(pid_list *plist, int sig) | ||
| 255 | { | ||
| 256 | pid_t mypid = getpid(); | ||
| 257 | int success = 1; | ||
| 258 | 230 | ||
| 259 | while (plist != NULL) { | 231 | scan_pid_maps("maps", pid); |
| 260 | if (plist->pid != mypid) { | 232 | xchdir("/proc"); |
| 261 | if (kill(plist->pid, sig) != 0) { | ||
| 262 | bb_perror_msg("kill pid %u", (unsigned)plist->pid); | ||
| 263 | success = 0; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | plist = plist->next; | ||
| 267 | } | 233 | } |
| 268 | return success; | 234 | closedir(d); |
| 269 | } | 235 | } |
| 270 | 236 | ||
| 271 | int fuser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 237 | int fuser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 272 | int fuser_main(int argc UNUSED_PARAM, char **argv) | 238 | int fuser_main(int argc UNUSED_PARAM, char **argv) |
| 273 | { | 239 | { |
| 274 | pid_list *plist; | 240 | pid_list *plist; |
| 275 | inode_list *ilist; | 241 | pid_t mypid; |
| 276 | char **pp; | 242 | char **pp; |
| 277 | dev_t dev; | 243 | struct stat st; |
| 278 | ino_t inode; | ||
| 279 | unsigned port; | 244 | unsigned port; |
| 280 | int opt; | 245 | int opt; |
| 281 | int success; | 246 | int exitcode; |
| 282 | int killsig; | 247 | int killsig; |
| 283 | /* | 248 | /* |
| 284 | fuser [options] FILEs or PORT/PROTOs | 249 | fuser [OPTIONS] FILE or PORT/PROTO |
| 285 | Find processes which use FILEs or PORTs | 250 | Find processes which use FILEs or PORTs |
| 286 | -m Find processes which use same fs as FILEs | 251 | -m Find processes which use same fs as FILEs |
| 287 | -4 Search only IPv4 space | 252 | -4 Search only IPv4 space |
| 288 | -6 Search only IPv6 space | 253 | -6 Search only IPv6 space |
| 289 | -s Silent: just exit with 0 if any processes are found | 254 | -s Don't display PIDs |
| 290 | -k Kill found processes (otherwise display PIDs) | 255 | -k Kill found processes |
| 291 | -SIGNAL Signal to send (default: TERM) | 256 | -SIGNAL Signal to send (default: KILL) |
| 292 | */ | 257 | */ |
| 293 | /* Handle -SIGNAL. Oh my... */ | 258 | /* Handle -SIGNAL. Oh my... */ |
| 294 | killsig = SIGTERM; | 259 | killsig = SIGKILL; /* yes, the default is not SIGTERM */ |
| 295 | pp = argv; | 260 | pp = argv; |
| 296 | while (*++pp) { | 261 | while (*++pp) { |
| 297 | char *arg = *pp; | 262 | char *arg = *pp; |
| @@ -313,33 +278,54 @@ Find processes which use FILEs or PORTs | |||
| 313 | break; | 278 | break; |
| 314 | } | 279 | } |
| 315 | 280 | ||
| 281 | opt_complementary = "-1"; /* at least one param */ | ||
| 316 | opt = getopt32(argv, OPTION_STRING); | 282 | opt = getopt32(argv, OPTION_STRING); |
| 317 | argv += optind; | 283 | argv += optind; |
| 318 | 284 | ||
| 319 | ilist = NULL; | ||
| 320 | pp = argv; | 285 | pp = argv; |
| 321 | while (*pp) { | 286 | while (*pp) { |
| 322 | char *proto = parse_net_arg(*pp, &port); | 287 | char *path = parse_net_arg(*pp, &port); |
| 323 | if (proto) { /* PORT/PROTO */ | 288 | if (path) { /* PORT/PROTO */ |
| 324 | ilist = scan_proc_net(proto, port, ilist); | 289 | scan_proc_net(path, port); |
| 325 | free(proto); | 290 | free(path); |
| 326 | } else { /* FILE */ | 291 | } else { /* FILE */ |
| 327 | if (!file_to_dev_inode(*pp, &dev, &inode)) | 292 | xstat(*pp, &st); |
| 328 | bb_perror_msg_and_die("can't open '%s'", *pp); | 293 | add_inode(&st); |
| 329 | ilist = add_inode(ilist, dev, inode); | ||
| 330 | } | 294 | } |
| 331 | pp++; | 295 | pp++; |
| 332 | } | 296 | } |
| 333 | 297 | ||
| 334 | plist = scan_proc_pids(ilist); /* changes dir to "/proc" */ | 298 | scan_proc_pids(); /* changes dir to "/proc" */ |
| 335 | 299 | ||
| 336 | if (!plist) | 300 | mypid = getpid(); |
| 337 | return EXIT_FAILURE; | 301 | plist = G.pid_list_head; |
| 338 | success = 1; | 302 | while (1) { |
| 339 | if (opt & OPT_KILL) { | 303 | if (!plist) |
| 340 | success = kill_pid_list(plist, killsig); | 304 | return EXIT_FAILURE; |
| 341 | } else if (!(opt & OPT_SILENT)) { | 305 | if (plist->pid != mypid) |
| 342 | success = print_pid_list(plist); | 306 | break; |
| 307 | plist = plist->next; | ||
| 343 | } | 308 | } |
| 344 | return (success != 1); /* 0 == success */ | 309 | |
| 310 | exitcode = EXIT_SUCCESS; | ||
| 311 | do { | ||
| 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; | ||
| 317 | } | ||
| 318 | } | ||
| 319 | if (!(opt & OPT_SILENT)) { | ||
| 320 | printf("%u ", (unsigned)plist->pid); | ||
| 321 | } | ||
| 322 | } | ||
| 323 | plist = plist->next; | ||
| 324 | } while (plist); | ||
| 325 | |||
| 326 | if (!(opt & (OPT_SILENT))) { | ||
| 327 | bb_putchar('\n'); | ||
| 328 | } | ||
| 329 | |||
| 330 | return exitcode; | ||
| 345 | } | 331 | } |
