From: Masatake YAMATO Date: Fri, 3 Mar 2023 15:28:37 +0000 (+0900) Subject: lsfd: choose anon_ops declarative way X-Git-Tag: v2.40-rc1~458^2~15 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=007e405b1e75e1dfbb69c43df8cefb9d3c2f9272;p=thirdparty%2Futil-linux.git lsfd: choose anon_ops declarative way Signed-off-by: Masatake YAMATO --- diff --git a/misc-utils/lsfd-unkn.c b/misc-utils/lsfd-unkn.c index 087e31dad2..da616c6ccd 100644 --- a/misc-utils/lsfd-unkn.c +++ b/misc-utils/lsfd-unkn.c @@ -33,6 +33,7 @@ struct unkn { struct anon_ops { const char *class; + bool (*probe)(const char *); char * (*get_name)(struct unkn *); /* Return true is handled the column. */ bool (*fill_column)(struct proc *, @@ -46,8 +47,7 @@ struct anon_ops { int (*handle_fdinfo)(struct unkn *, const char *, const char *); }; -static const struct anon_ops anon_generic_ops; -static const struct anon_ops anon_pidfd_ops; +static const struct anon_ops *anon_probe(const char *); static char * anon_get_class(struct unkn *unkn) { @@ -129,10 +129,7 @@ static void unkn_init_content(struct file *file) && strncmp(file->name, "anon_inode:", 11) == 0) { const char *rest = file->name + 11; - if (strncmp(rest, "[pidfd]", 7) == 0) - unkn->anon_ops = &anon_pidfd_ops; - else - unkn->anon_ops = &anon_generic_ops; + unkn->anon_ops = anon_probe(rest); if (unkn->anon_ops->init) unkn->anon_ops->init(unkn); @@ -166,6 +163,11 @@ struct anon_pidfd_data { char *nspid; }; +static bool anon_pidfd_probe(const char *str) +{ + return (strncmp(str, "[pidfd]", 7) == 0); +} + static char *anon_pidfd_get_name(struct unkn *unkn) { char *str = NULL; @@ -253,6 +255,7 @@ static bool anon_pidfd_fill_column(struct proc *proc __attribute__((__unused__) static const struct anon_ops anon_pidfd_ops = { .class = "pidfd", + .probe = anon_pidfd_probe, .get_name = anon_pidfd_get_name, .fill_column = anon_pidfd_fill_column, .init = anon_pidfd_init, @@ -272,6 +275,18 @@ static const struct anon_ops anon_generic_ops = { .handle_fdinfo = NULL, }; +static const struct anon_ops *anon_ops[] = { + &anon_pidfd_ops, +}; + +static const struct anon_ops *anon_probe(const char *str) +{ + for (size_t i = 0; i < ARRAY_SIZE(anon_ops); i++) + if (anon_ops[i]->probe(str)) + return anon_ops[i]; + return &anon_generic_ops; +} + const struct file_class unkn_class = { .super = &file_class, .size = sizeof(struct unkn),