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>
This commit is contained in:
Denys Vlasenko 2021-06-24 12:02:17 +02:00
parent 1746218bee
commit 6d61eb1363

View File

@ -213,7 +213,7 @@ enum {
OPT_c = (1 << 2) * ENABLE_FEATURE_TASKSET_CPULIST, 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; ul *mask;
unsigned mask_size_in_bytes; 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 <pid>", /* Either it was just "-p <pid>",
* or it was "-p <aff> <pid>" and we came here * or it was "-p <aff> <pid>" and we came here
* for the second time (see goto below) */ * for the second time (see goto below) */
return; return 0;
} }
current_new = "new"; current_new = "new";
} }
@ -312,6 +312,14 @@ static void process_pid_str(const char *pid_str, unsigned opts, char *aff)
aff = NULL; aff = NULL;
goto print_aff; /* print new affinity and exit */ 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; 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; pid_str = aff;
if (*argv) /* "-p <aff> <pid> ...rest.is.ignored..." */ if (*argv) /* "-p <aff> <pid> ...rest.is.ignored..." */
pid_str = *argv; /* NB: *argv != NULL in this case */ pid_str = *argv;
else else
aff = NULL; aff = NULL;
if (opts & OPT_a) { if (opts & OPT_a) {
char *dn; char *dn;
DIR *dir; int r;
struct dirent *ent;
dn = xasprintf("/proc/%s/task", pid_str); dn = xasprintf("/proc/%s/task", pid_str);
dir = opendir(dn); r = iterate_on_dir(dn, iter, aff);
IF_FEATURE_CLEAN_UP(free(dn);) IF_FEATURE_CLEAN_UP(free(dn);)
if (!dir) { if (r == 0)
goto no_threads; return r; /* EXIT_SUCCESS */
} /* else: no /proc/PID/task, act as if no -a was given */
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);
} }
return EXIT_SUCCESS; return process_pid_str(pid_str, opts, aff);
} }