From 6d61eb13635ac1121762f55376989af3d6c6ebad Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 24 Jun 2021 12:02:17 +0200 Subject: [PATCH] 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 --- util-linux/taskset.c | 35 +++++++++++++++++------------------ 1 file 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 { OPT_c = (1 << 2) * ENABLE_FEATURE_TASKSET_CPULIST, }; -static void process_pid_str(const char *pid_str, unsigned opts, char *aff) +static int process_pid_str(const char *pid_str, unsigned opts, char *aff) { ul *mask; unsigned mask_size_in_bytes; @@ -237,7 +237,7 @@ static void process_pid_str(const char *pid_str, unsigned opts, char *aff) /* Either it was just "-p ", * or it was "-p " and we came here * for the second time (see goto below) */ - return; + return 0; } current_new = "new"; } @@ -312,6 +312,14 @@ static void process_pid_str(const char *pid_str, unsigned opts, char *aff) aff = NULL; goto print_aff; /* print new affinity and exit */ } + return 0; +} + +static int FAST_FUNC iter(const char *dn UNUSED_PARAM, struct dirent *ent, void *aff) +{ + if (isdigit(ent->d_name[0])) + return process_pid_str(ent->d_name, option_mask32, aff); + return 0; } int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; @@ -341,29 +349,20 @@ int taskset_main(int argc UNUSED_PARAM, char **argv) pid_str = aff; if (*argv) /* "-p ...rest.is.ignored..." */ - pid_str = *argv; /* NB: *argv != NULL in this case */ + pid_str = *argv; else aff = NULL; if (opts & OPT_a) { char *dn; - DIR *dir; - struct dirent *ent; + int r; dn = xasprintf("/proc/%s/task", pid_str); - dir = opendir(dn); + r = iterate_on_dir(dn, iter, aff); IF_FEATURE_CLEAN_UP(free(dn);) - if (!dir) { - goto no_threads; - } - while ((ent = readdir(dir)) != NULL) { - if (isdigit(ent->d_name[0])) - process_pid_str(ent->d_name, opts, aff); - } - IF_FEATURE_CLEAN_UP(closedir(dir);) - } else { - no_threads: - process_pid_str(pid_str, opts, aff); + if (r == 0) + return r; /* EXIT_SUCCESS */ + /* else: no /proc/PID/task, act as if no -a was given */ } - return EXIT_SUCCESS; + return process_pid_str(pid_str, opts, aff); }