lsfd: introduce --source filter option
Reflecting the review comment: add filter by SOURCE (lsfd --source sda1) Example output: # ./lsfd --source proc | head COMMAND PID USER ASSOC MODE TYPE SOURCE MNTID INODE NAME systemd 1 root 10 r-- REG proc 23 18542 /proc/1/mountinfo systemd 1 root 14 r-- REG proc 23 4026532082 /proc/swaps systemd-resolve 856 root 7 r-- REG proc 254 24841 /proc/sys/kernel/hostname low-memory-moni 898 root 7 rw- REG proc 425 4026532073 /proc/pressure/memory low-memory-moni 898 root 8 rw- REG proc 425 4026532073 /proc/pressure/memory low-memory-moni 898 root 9 rw- REG proc 425 4026532073 /proc/pressure/memory packagekitd 25205 root 12 r-- REG proc 23 69645 /proc/25205/mountinfo systemd 26466 root 10 r-- REG proc 23 71813 /proc/26466/mountinfo systemd 26466 root 14 r-- REG proc 23 4026532082 /proc/swaps # ./lsfd --source proc -Q '(PID == 898)' COMMAND PID USER ASSOC MODE TYPE SOURCE MNTID INODE NAME low-memory-moni 898 root 7 rw- REG proc 425 4026532073 /proc/pressure/memory low-memory-moni 898 root 8 rw- REG proc 425 4026532073 /proc/pressure/memory low-memory-moni 898 root 9 rw- REG proc 425 4026532073 /proc/pressure/memory Signed-off-by: Masatake YAMATO <yamato@redhat.com>
This commit is contained in:
parent
52ab46daa2
commit
aed101f011
1 changed files with 37 additions and 1 deletions
|
@ -941,6 +941,7 @@ static void __attribute__((__noreturn__)) usage(void)
|
|||
fputs(_(" --sysroot <dir> use specified directory as system root\n"), out);
|
||||
fputs(_(" -u, --notruncate don't truncate text in columns\n"), out);
|
||||
fputs(_(" -Q, --filter <expr> apply display filter\n"), out);
|
||||
fputs(_(" --source <source> add filter by SOURCE\n"), out);
|
||||
|
||||
fputs(USAGE_SEPARATOR, out);
|
||||
printf(USAGE_HELP_OPTIONS(23));
|
||||
|
@ -965,6 +966,28 @@ static void xstrappend(char **a, const char *b)
|
|||
err(EXIT_FAILURE, _("failed to allocate memory for string"));
|
||||
}
|
||||
|
||||
static char * quote_filter_expr(char *expr)
|
||||
{
|
||||
char c[] = {'\0', '\0'};
|
||||
char *r = strdup("");
|
||||
while (*expr) {
|
||||
switch (*expr) {
|
||||
case '\'':
|
||||
xstrappend(&r, "\\'");
|
||||
break;
|
||||
case '"':
|
||||
xstrappend(&r, "\\\"");
|
||||
break;
|
||||
default:
|
||||
c[0] = *expr;
|
||||
xstrappend(&r, c);
|
||||
break;
|
||||
}
|
||||
expr++;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static void append_filter_expr(char **a, const char *b, bool and)
|
||||
{
|
||||
if (*a == NULL) {
|
||||
|
@ -995,7 +1018,8 @@ int main(int argc, char *argv[])
|
|||
char *filter_expr = NULL;
|
||||
|
||||
enum {
|
||||
OPT_SYSROOT = CHAR_MAX + 1
|
||||
OPT_SYSROOT = CHAR_MAX + 1,
|
||||
OPT_SOURCE,
|
||||
};
|
||||
static const struct option longopts[] = {
|
||||
{ "noheadings", no_argument, NULL, 'n' },
|
||||
|
@ -1008,6 +1032,7 @@ int main(int argc, char *argv[])
|
|||
{ "notruncate", no_argument, NULL, 'u' },
|
||||
{ "sysroot", required_argument, NULL, OPT_SYSROOT },
|
||||
{ "filter", required_argument, NULL, 'Q' },
|
||||
{ "source", required_argument, NULL, OPT_SOURCE },
|
||||
{ NULL, 0, NULL, 0 },
|
||||
};
|
||||
|
||||
|
@ -1042,6 +1067,17 @@ int main(int argc, char *argv[])
|
|||
case 'Q':
|
||||
append_filter_expr(&filter_expr, optarg, true);
|
||||
break;
|
||||
case OPT_SOURCE: {
|
||||
char * quoted_source = quote_filter_expr(optarg);
|
||||
char * source_expr = NULL;
|
||||
xstrappend(&source_expr, "(SOURCE == '");
|
||||
xstrappend(&source_expr, quoted_source);
|
||||
xstrappend(&source_expr, "')");
|
||||
append_filter_expr(&filter_expr, source_expr, true);
|
||||
free(source_expr);
|
||||
free(quoted_source);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'V':
|
||||
print_version(EXIT_SUCCESS);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue