]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: introduce -p/--pid option, pids filter working in the early stage
authorMasatake YAMATO <yamato@redhat.com>
Fri, 22 Oct 2021 03:48:47 +0000 (12:48 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Tue, 2 Nov 2021 02:34:15 +0000 (11:34 +0900)
    $ 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 <yamato@redhat.com>
misc-utils/lsfd.c

index 8ab57a42803dd7037a31f81df820b8bbe76fc0bf..ef77064d54da7011352b43b67b65c6d490dc3687 100644 (file)
@@ -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);