From: Masatake YAMATO Date: Fri, 22 Oct 2021 03:48:47 +0000 (+0900) Subject: lsfd: introduce -p/--pid option, pids filter working in the early stage X-Git-Tag: v2.38-rc1~144^2~14 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c647ebc2b68f152d202dd8abedbe5161245aa795;p=thirdparty%2Futil-linux.git lsfd: introduce -p/--pid option, pids filter working in the early stage $ time sudo ./lsfd -Q '(PID == 1) or (PID == 2)' > /dev/null real 0m0.508s user 0m0.230s sys 0m0.267s $ time sudo ./lsfd -p 1,2 > /dev/null real 0m0.088s user 0m0.036s sys 0m0.033s $ [ $(./lsfd -p 1,2) = $(./lsfd -Q '(PID == 1) or (PID == 2)') ] $ echo $? 0 Signed-off-by: Masatake YAMATO --- diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index 8ab57a4280..ef77064d54 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -949,13 +949,11 @@ static void read_process(struct lsfd_control *ctl, struct path_cxt *pc, ul_path_close_dirfd(pc); } -static void parse_pids(const char *str, pid_t pids[], int *count, const int pids_size) +static void parse_pids(const char *str, pid_t **pids, int *count) { long v; char *next = NULL; - if (!(*count < pids_size)) - err(EXIT_FAILURE, _("too many pids (more than %d) are specified"), pids_size); if (*str == '\0') return; @@ -967,13 +965,16 @@ static void parse_pids(const char *str, pid_t pids[], int *count, const int pids errx(EXIT_FAILURE, _("garbage at the end of pid specification: %s"), str); if (v < 0) errx(EXIT_FAILURE, _("out of range value for pid specification: %ld"), v); - pids[(*count)++] = (pid_t)v; + + (*count)++; + *pids = xrealloc(*pids, (*count) * sizeof(**pids)); + (*pids)[*count - 1] = (pid_t)v; while (next && *next != '\0' && (isspace((unsigned char)*next) || *next == ',')) next++; if (*next != '\0') - parse_pids(next, pids, count, pids_size); + parse_pids(next, pids, count); } static int pidcmp(const void *a, const void *b) @@ -1104,8 +1105,7 @@ int main(int argc, char *argv[]) struct lsfd_control ctl = {}; char *filter_expr = NULL; bool debug_filter = false; -#define MAX_PIDS 128 - pid_t pids[MAX_PIDS] = {}; + pid_t *pids = NULL; int n_pids = 0; enum { @@ -1157,7 +1157,7 @@ int main(int argc, char *argv[]) ctl.sysroot = optarg; break; case 'p': - parse_pids(optarg, pids, &n_pids, MAX_PIDS); + parse_pids(optarg, &pids, &n_pids); break; case 'Q': append_filter_expr(&filter_expr, optarg, true); @@ -1242,6 +1242,7 @@ int main(int argc, char *argv[]) initialize_classes(); collect_processes(&ctl, pids, n_pids); + free(pids); convert(&ctl.procs, &ctl); emit(&ctl);