diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2021-06-24 12:02:17 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-06-24 12:02:17 +0200 |
commit | 6d61eb13635ac1121762f55376989af3d6c6ebad (patch) | |
tree | 613a092df0c92f00c04a90e2b88d50b95e66367d | |
parent | 1746218beebc7e180f3eaed905277f9f46983ac4 (diff) | |
download | busybox-w32-6d61eb13635ac1121762f55376989af3d6c6ebad.tar.gz busybox-w32-6d61eb13635ac1121762f55376989af3d6c6ebad.tar.bz2 busybox-w32-6d61eb13635ac1121762f55376989af3d6c6ebad.zip |
taskset: use iterate_on_dir()
function old new delta
iter - 27 +27
process_pid_str 854 856 +2
taskset_main 181 135 -46
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/1 up/down: 29/-46) Total: -17 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | util-linux/taskset.c | 35 |
1 files changed, 17 insertions, 18 deletions
diff --git a/util-linux/taskset.c b/util-linux/taskset.c index 47e989792..afe2f04d2 100644 --- a/util-linux/taskset.c +++ b/util-linux/taskset.c | |||
@@ -213,7 +213,7 @@ enum { | |||
213 | OPT_c = (1 << 2) * ENABLE_FEATURE_TASKSET_CPULIST, | 213 | OPT_c = (1 << 2) * ENABLE_FEATURE_TASKSET_CPULIST, |
214 | }; | 214 | }; |
215 | 215 | ||
216 | static void process_pid_str(const char *pid_str, unsigned opts, char *aff) | 216 | static int process_pid_str(const char *pid_str, unsigned opts, char *aff) |
217 | { | 217 | { |
218 | ul *mask; | 218 | ul *mask; |
219 | unsigned mask_size_in_bytes; | 219 | unsigned mask_size_in_bytes; |
@@ -237,7 +237,7 @@ static void process_pid_str(const char *pid_str, unsigned opts, char *aff) | |||
237 | /* Either it was just "-p <pid>", | 237 | /* Either it was just "-p <pid>", |
238 | * or it was "-p <aff> <pid>" and we came here | 238 | * or it was "-p <aff> <pid>" and we came here |
239 | * for the second time (see goto below) */ | 239 | * for the second time (see goto below) */ |
240 | return; | 240 | return 0; |
241 | } | 241 | } |
242 | current_new = "new"; | 242 | current_new = "new"; |
243 | } | 243 | } |
@@ -312,6 +312,14 @@ static void process_pid_str(const char *pid_str, unsigned opts, char *aff) | |||
312 | aff = NULL; | 312 | aff = NULL; |
313 | goto print_aff; /* print new affinity and exit */ | 313 | goto print_aff; /* print new affinity and exit */ |
314 | } | 314 | } |
315 | return 0; | ||
316 | } | ||
317 | |||
318 | static int FAST_FUNC iter(const char *dn UNUSED_PARAM, struct dirent *ent, void *aff) | ||
319 | { | ||
320 | if (isdigit(ent->d_name[0])) | ||
321 | return process_pid_str(ent->d_name, option_mask32, aff); | ||
322 | return 0; | ||
315 | } | 323 | } |
316 | 324 | ||
317 | int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 325 | int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
@@ -341,29 +349,20 @@ int taskset_main(int argc UNUSED_PARAM, char **argv) | |||
341 | 349 | ||
342 | pid_str = aff; | 350 | pid_str = aff; |
343 | if (*argv) /* "-p <aff> <pid> ...rest.is.ignored..." */ | 351 | if (*argv) /* "-p <aff> <pid> ...rest.is.ignored..." */ |
344 | pid_str = *argv; /* NB: *argv != NULL in this case */ | 352 | pid_str = *argv; |
345 | else | 353 | else |
346 | aff = NULL; | 354 | aff = NULL; |
347 | 355 | ||
348 | if (opts & OPT_a) { | 356 | if (opts & OPT_a) { |
349 | char *dn; | 357 | char *dn; |
350 | DIR *dir; | 358 | int r; |
351 | struct dirent *ent; | ||
352 | 359 | ||
353 | dn = xasprintf("/proc/%s/task", pid_str); | 360 | dn = xasprintf("/proc/%s/task", pid_str); |
354 | dir = opendir(dn); | 361 | r = iterate_on_dir(dn, iter, aff); |
355 | IF_FEATURE_CLEAN_UP(free(dn);) | 362 | IF_FEATURE_CLEAN_UP(free(dn);) |
356 | if (!dir) { | 363 | if (r == 0) |
357 | goto no_threads; | 364 | return r; /* EXIT_SUCCESS */ |
358 | } | 365 | /* else: no /proc/PID/task, act as if no -a was given */ |
359 | while ((ent = readdir(dir)) != NULL) { | ||
360 | if (isdigit(ent->d_name[0])) | ||
361 | process_pid_str(ent->d_name, opts, aff); | ||
362 | } | ||
363 | IF_FEATURE_CLEAN_UP(closedir(dir);) | ||
364 | } else { | ||
365 | no_threads: | ||
366 | process_pid_str(pid_str, opts, aff); | ||
367 | } | 366 | } |
368 | return EXIT_SUCCESS; | 367 | return process_pid_str(pid_str, opts, aff); |
369 | } | 368 | } |