lsns: reorganize members specifying other namespaces in lsns_namespace

This is a preparation change for planed "tree interpolation" patch.

parentid, ownerid, parentns, and ownerns were members of
lsns_namespace struct. When interpolating missing namespaces
for building a unified name space tree, duplicated functions
are needed; one is for parentid and parentns members, and
another is for ownerid and ownerns members. To avoid the
duplication, this change unifies the members as following:

 parentid and ownerid => related_id [2]
 parentns and ownerns => related_ns [2]

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
This commit is contained in:
Masatake YAMATO 2021-04-29 04:39:13 +09:00 committed by Karel Zak
parent e749d51a9c
commit c67b83c10d

View file

@ -142,18 +142,22 @@ static char *ns_names[] = {
[LSNS_ID_TIME] = "time" [LSNS_ID_TIME] = "time"
}; };
enum {
RELA_PARENT,
RELA_OWNER,
MAX_RELA
};
struct lsns_namespace { struct lsns_namespace {
ino_t id; ino_t id;
int type; /* LSNS_* */ int type; /* LSNS_* */
int nprocs; int nprocs;
int netnsid; int netnsid;
ino_t parentid; ino_t related_id[MAX_RELA];
ino_t ownerid;
struct lsns_process *proc; struct lsns_process *proc;
struct lsns_namespace *parentns; struct lsns_namespace *related_ns[MAX_RELA];
struct lsns_namespace *ownerns;
struct libscols_line *ns_outline; struct libscols_line *ns_outline;
struct list_head namespaces; /* lsns->processes member */ struct list_head namespaces; /* lsns->processes member */
@ -620,8 +624,8 @@ static struct lsns_namespace *add_namespace(struct lsns *ls, int type, ino_t ino
ns->type = type; ns->type = type;
ns->id = ino; ns->id = ino;
ns->parentid = parent_ino; ns->related_id[RELA_PARENT] = parent_ino;
ns->ownerid = owner_ino; ns->related_id[RELA_OWNER] = owner_ino;
list_add_tail(&ns->namespaces, &ls->namespaces); list_add_tail(&ns->namespaces, &ls->namespaces);
return ns; return ns;
@ -704,15 +708,15 @@ static int read_namespaces(struct lsns *ls)
struct lsns_namespace *pns = list_entry(pp, struct lsns_namespace, namespaces); struct lsns_namespace *pns = list_entry(pp, struct lsns_namespace, namespaces);
if (ns->type == LSNS_ID_USER if (ns->type == LSNS_ID_USER
|| ns->type == LSNS_ID_PID) { || ns->type == LSNS_ID_PID) {
if (ns->parentid == pns->id) if (ns->related_id[RELA_PARENT] == pns->id)
ns->parentns = pns; ns->related_ns[RELA_PARENT] = pns;
if (ns->ownerid == pns->id) if (ns->related_id[RELA_OWNER] == pns->id)
ns->ownerns = pns; ns->related_ns[RELA_OWNER] = pns;
if (ns->parentns && ns->ownerns) if (ns->related_ns[RELA_PARENT] && ns->related_ns[RELA_OWNER])
break; break;
} else { } else {
if (ns->ownerid == pns->id) { if (ns->related_id[RELA_OWNER] == pns->id) {
ns->ownerns = pns; ns->related_ns[RELA_OWNER] = pns;
break; break;
} }
} }
@ -811,8 +815,8 @@ static void add_scols_line(struct lsns *ls, struct libscols_table *table,
line = scols_table_new_line(table, line = scols_table_new_line(table,
(ls->tree == LSNS_TREE_PROCESS && proc) && proc->parent ? proc->parent->outline: (ls->tree == LSNS_TREE_PROCESS && proc) && proc->parent ? proc->parent->outline:
(ls->tree == LSNS_TREE_PARENT) && ns->parentns ? ns->parentns->ns_outline: (ls->tree == LSNS_TREE_PARENT) && ns->related_ns[RELA_PARENT] ? ns->related_ns[RELA_PARENT]->ns_outline:
(ls->tree == LSNS_TREE_OWNER) && ns->ownerns ? ns->ownerns->ns_outline: (ls->tree == LSNS_TREE_OWNER) && ns->related_ns[RELA_OWNER] ? ns->related_ns[RELA_OWNER]->ns_outline:
NULL); NULL);
if (!line) { if (!line) {
warn(_("failed to add line to output")); warn(_("failed to add line to output"));
@ -872,10 +876,10 @@ static void add_scols_line(struct lsns *ls, struct libscols_table *table,
nsfs_xasputs(&str, ns, ls->tab, ls->no_wrap ? ',' : '\n'); nsfs_xasputs(&str, ns, ls->tab, ls->no_wrap ? ',' : '\n');
break; break;
case COL_PNS: case COL_PNS:
xasprintf(&str, "%ju", (uintmax_t)ns->parentid); xasprintf(&str, "%ju", (uintmax_t)ns->related_id[RELA_PARENT]);
break; break;
case COL_ONS: case COL_ONS:
xasprintf(&str, "%ju", (uintmax_t)ns->ownerid); xasprintf(&str, "%ju", (uintmax_t)ns->related_id[RELA_OWNER]);
break; break;
default: default:
break; break;
@ -956,16 +960,16 @@ static void show_namespace(struct lsns *ls, struct libscols_table *tab,
* create a tree from owner->owned and/or parent->child relation * create a tree from owner->owned and/or parent->child relation
*/ */
if (ls->tree == LSNS_TREE_OWNER if (ls->tree == LSNS_TREE_OWNER
&& ns->ownerns && ns->related_ns[RELA_OWNER]
&& !ns->ownerns->ns_outline) && !ns->related_ns[RELA_OWNER]->ns_outline)
show_namespace(ls, tab, ns->ownerns, ns->ownerns->proc); show_namespace(ls, tab, ns->related_ns[RELA_OWNER], ns->related_ns[RELA_OWNER]->proc);
else if (ls->tree == LSNS_TREE_PARENT) { else if (ls->tree == LSNS_TREE_PARENT) {
if (ns->parentns) { if (ns->related_ns[RELA_PARENT]) {
if (!ns->parentns->ns_outline) if (!ns->related_ns[RELA_PARENT]->ns_outline)
show_namespace(ls, tab, ns->parentns, ns->parentns->proc); show_namespace(ls, tab, ns->related_ns[RELA_PARENT], ns->related_ns[RELA_PARENT]->proc);
} }
else if (ns->ownerns && !ns->ownerns->ns_outline) else if (ns->related_ns[RELA_OWNER] && !ns->related_ns[RELA_OWNER]->ns_outline)
show_namespace(ls, tab, ns->ownerns, ns->ownerns->proc); show_namespace(ls, tab, ns->related_ns[RELA_OWNER], ns->related_ns[RELA_OWNER]->proc);
} }
add_scols_line(ls, tab, ns, proc); add_scols_line(ls, tab, ns, proc);