2 * lsns(8) - list system namespaces
4 * Copyright (C) 2015 Karel Zak <kzak@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/types.h>
30 #include <libsmartcols.h>
32 #include "pathnames.h"
37 #include "closestream.h"
39 #include "procutils.h"
41 #include "namespace.h"
47 UL_DEBUG_DEFINE_MASK(lsns
);
48 UL_DEBUG_DEFINE_MASKNAMES(lsns
) = UL_DEBUG_EMPTY_MASKNAMES
;
50 #define LSNS_DEBUG_INIT (1 << 1)
51 #define LSNS_DEBUG_PROC (1 << 2)
52 #define LSNS_DEBUG_NS (1 << 3)
53 #define LSNS_DEBUG_ALL 0xFFFF
55 #define DBG(m, x) __UL_DBG(lsns, LSNS_DEBUG_, m, x)
56 #define ON_DBG(m, x) __UL_DBG_CALL(lsns, LSNS_DEBUG_, m, x)
58 struct idcache
*uid_cache
= NULL
;
75 const char *name
; /* header */
76 double whint
; /* width hint (N < 1 is in percent of termwidth) */
77 int flags
; /* SCOLS_FL_* */
81 /* columns descriptions */
82 static const struct colinfo infos
[] = {
83 [COL_NS
] = { "NS", 10, SCOLS_FL_RIGHT
, N_("command of the process holding the lock") },
84 [COL_TYPE
] = { "TYPE", 5, 0, N_("kind of namespace") },
85 [COL_PATH
] = { "PATH", 0, 0, N_("path to the namespace")},
86 [COL_NPROCS
] = { "NPROCS", 5, SCOLS_FL_RIGHT
, N_("number of processes in the namespace") },
87 [COL_PID
] = { "PID", 5, SCOLS_FL_RIGHT
, N_("lowers PID in the namespace") },
88 [COL_PPID
] = { "PPID", 5, SCOLS_FL_RIGHT
, N_("PPID of the PID") },
89 [COL_COMMAND
] = { "COMMAND", 0, SCOLS_FL_TRUNC
, N_("command line of the PID")},
90 [COL_UID
] = { "UID", 0, SCOLS_FL_RIGHT
, N_("user ID of the PID")},
91 [COL_USER
] = { "USER", 0, 0, N_("username of the PID")}
94 static int columns
[ARRAY_SIZE(infos
) * 2];
95 static size_t ncolumns
;
106 static char *ns_names
[] = {
107 [LSNS_ID_MNT
] = "mnt",
108 [LSNS_ID_NET
] = "net",
109 [LSNS_ID_PID
] = "pid",
110 [LSNS_ID_UTS
] = "uts",
111 [LSNS_ID_IPC
] = "ipc",
112 [LSNS_ID_USER
] = "user"
115 struct lsns_namespace
{
117 int type
; /* LSNS_* */
120 struct lsns_process
*proc
;
122 struct list_head namespaces
; /* lsns->processes member */
123 struct list_head processes
; /* head of lsns_process *siblings */
126 struct lsns_process
{
127 pid_t pid
; /* process PID */
128 pid_t ppid
; /* parent's PID */
129 pid_t tpid
; /* thread group */
133 ino_t ns_ids
[ARRAY_SIZE(ns_names
)];
134 struct list_head ns_siblings
[ARRAY_SIZE(ns_names
)];
136 struct list_head processes
; /* list of processes */
138 struct libscols_line
*outline
;
139 struct lsns_process
*parent
;
143 struct list_head processes
;
144 struct list_head namespaces
;
146 pid_t fltr_pid
; /* filter out by PID */
147 ino_t fltr_ns
; /* filter out by namespace */
148 int fltr_types
[ARRAY_SIZE(ns_names
)];
151 unsigned int raw
: 1,
159 static void lsns_init_debug(void)
161 __UL_INIT_DEBUG(lsns
, LSNS_DEBUG_
, 0, LSNS_DEBUG
);
164 static int ns_name2type(const char *name
)
168 for (i
= 0; i
< ARRAY_SIZE(ns_names
); i
++) {
169 if (strcmp(ns_names
[i
], name
) == 0)
175 static int column_name_to_id(const char *name
, size_t namesz
)
181 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
182 const char *cn
= infos
[i
].name
;
184 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
187 warnx(_("unknown column: %s"), name
);
191 static inline int get_column_id(int num
)
194 assert((size_t) num
< ncolumns
);
195 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
200 static inline const struct colinfo
*get_column_info(unsigned num
)
202 return &infos
[ get_column_id(num
) ];
205 static ino_t
get_ns_ino(int dir
, const char *nsname
, ino_t
*ino
)
210 snprintf(path
, sizeof(path
), "ns/%s", nsname
);
212 if (fstatat(dir
, path
, &st
, 0) != 0)
219 static int read_process(struct lsns
*ls
, pid_t pid
)
221 struct lsns_process
*p
= NULL
;
229 DBG(PROC
, ul_debug("reading %d", (int) pid
));
231 snprintf(buf
, sizeof(buf
), "/proc/%d", pid
);
236 p
= calloc(1, sizeof(*p
));
242 if (fstat(dirfd(dir
), &st
) == 0) {
244 add_uid(uid_cache
, st
.st_uid
);
247 fd
= openat(dirfd(dir
), "stat", O_RDONLY
);
252 if (!(f
= fdopen(fd
, "r"))) {
256 rc
= fscanf(f
, "%d %*s %c %d*[^\n]", &p
->pid
, &p
->state
, &p
->ppid
);
263 for (i
= 0; i
< ARRAY_SIZE(p
->ns_ids
); i
++) {
264 INIT_LIST_HEAD(&p
->ns_siblings
[i
]);
266 if (!ls
->fltr_types
[i
])
269 rc
= get_ns_ino(dirfd(dir
), ns_names
[i
], &p
->ns_ids
[i
]);
270 if (rc
&& rc
!= -EACCES
)
275 INIT_LIST_HEAD(&p
->processes
);
277 DBG(PROC
, ul_debugobj(p
, "new pid=%d", p
->pid
));
278 list_add_tail(&p
->processes
, &ls
->processes
);
288 static int read_processes(struct lsns
*ls
)
290 struct proc_processes
*proc
= NULL
;
294 DBG(PROC
, ul_debug("opening /proc"));
296 if (!(proc
= proc_open_processes())) {
301 while (proc_next_pid(proc
, &pid
) == 0) {
302 rc
= read_process(ls
, pid
);
303 if (rc
&& rc
!= -EACCES
)
308 DBG(PROC
, ul_debug("closing /proc"));
309 proc_close_processes(proc
);
313 static struct lsns_namespace
*get_namespace(struct lsns
*ls
, ino_t ino
)
317 list_for_each(p
, &ls
->namespaces
) {
318 struct lsns_namespace
*ns
= list_entry(p
, struct lsns_namespace
, namespaces
);
326 static int namespace_has_process(struct lsns_namespace
*ns
, pid_t pid
)
330 list_for_each(p
, &ns
->processes
) {
331 struct lsns_process
*proc
= list_entry(p
, struct lsns_process
, ns_siblings
[ns
->type
]);
333 if (proc
->pid
== pid
)
339 static struct lsns_namespace
*add_namespace(struct lsns
*ls
, int type
, ino_t ino
)
341 struct lsns_namespace
*ns
= calloc(1, sizeof(*ns
));
346 DBG(NS
, ul_debugobj(ns
, "new %s[%lu]", ns_names
[type
], ino
));
348 INIT_LIST_HEAD(&ns
->processes
);
349 INIT_LIST_HEAD(&ns
->namespaces
);
354 list_add_tail(&ns
->namespaces
, &ls
->namespaces
);
358 static int add_process_to_namespace(struct lsns
*ls
, struct lsns_namespace
*ns
, struct lsns_process
*proc
)
362 DBG(NS
, ul_debugobj(ns
, "add process [%p] pid=%d to %s[%lu]", proc
, proc
->pid
, ns_names
[ns
->type
], ns
->id
));
364 list_for_each(p
, &ls
->processes
) {
365 struct lsns_process
*xproc
= list_entry(p
, struct lsns_process
, processes
);
367 if (xproc
->pid
== proc
->ppid
) /* my parent */
368 proc
->parent
= xproc
;
369 else if (xproc
->ppid
== proc
->pid
) /* my child */
370 xproc
->parent
= proc
;
373 list_add_tail(&proc
->ns_siblings
[ns
->type
], &ns
->processes
);
376 if (!ns
->proc
|| ns
->proc
->pid
> proc
->pid
)
382 static int read_namespaces(struct lsns
*ls
)
386 DBG(NS
, ul_debug("reading namespace"));
388 list_for_each(p
, &ls
->processes
) {
390 struct lsns_namespace
*ns
;
391 struct lsns_process
*proc
= list_entry(p
, struct lsns_process
, processes
);
393 for (i
= 0; i
< ARRAY_SIZE(proc
->ns_ids
); i
++) {
394 if (proc
->ns_ids
[i
] == 0)
396 if (!(ns
= get_namespace(ls
, proc
->ns_ids
[i
]))) {
397 ns
= add_namespace(ls
, i
, proc
->ns_ids
[i
]);
401 add_process_to_namespace(ls
, ns
, proc
);
408 static void add_scols_line(struct lsns
*ls
, struct libscols_table
*table
,
409 struct lsns_namespace
*ns
, struct lsns_process
*proc
)
412 struct libscols_line
*line
;
417 line
= scols_table_new_line(table
,
418 ls
->tree
&& proc
->parent
? proc
->parent
->outline
: NULL
);
420 warn(_("failed to add line to output"));
424 for (i
= 0; i
< ncolumns
; i
++) {
427 switch (get_column_id(i
)) {
429 xasprintf(&str
, "%lu", ns
->id
);
432 xasprintf(&str
, "%d", (int) proc
->pid
);
435 xasprintf(&str
, "%d", (int) proc
->ppid
);
438 xasprintf(&str
, "%s", ns_names
[ns
->type
]);
441 xasprintf(&str
, "%d", ns
->nprocs
);
444 str
= proc_get_command(proc
->pid
);
446 str
= proc_get_command_name(proc
->pid
);
449 xasprintf(&str
, "/proc/%d/ns/%s", (int) proc
->pid
, ns_names
[ns
->type
]);
452 xasprintf(&str
, "%d", (int) proc
->uid
);
455 xasprintf(&str
, "%s", get_id(uid_cache
, proc
->uid
)->name
);
462 scols_line_set_data(line
, i
, str
);
465 proc
->outline
= line
;
468 static struct libscols_table
*init_scols_table(struct lsns
*ls
)
470 struct libscols_table
*tab
;
473 tab
= scols_new_table();
475 warn(_("failed to initialize output table"));
479 scols_table_enable_raw(tab
, ls
->raw
);
480 scols_table_enable_json(tab
, ls
->json
);
481 scols_table_enable_noheadings(tab
, ls
->no_headings
);
484 scols_table_set_name(tab
, "namespaces");
486 for (i
= 0; i
< ncolumns
; i
++) {
487 const struct colinfo
*col
= get_column_info(i
);
488 int flags
= col
->flags
;
491 flags
&= ~SCOLS_FL_TRUNC
;
492 if (ls
->tree
&& get_column_id(i
) == COL_COMMAND
)
493 flags
|= SCOLS_FL_TREE
;
495 if (!scols_table_new_column(tab
, col
->name
, col
->whint
, flags
)) {
496 warnx(_("failed to initialize output column"));
503 scols_unref_table(tab
);
507 static int show_namespaces(struct lsns
*ls
)
509 struct libscols_table
*tab
;
513 tab
= init_scols_table(ls
);
517 list_for_each(p
, &ls
->namespaces
) {
518 struct lsns_namespace
*ns
= list_entry(p
, struct lsns_namespace
, namespaces
);
520 if (ls
->fltr_pid
!= 0 && !namespace_has_process(ns
, ls
->fltr_pid
))
523 add_scols_line(ls
, tab
, ns
, ns
->proc
);
526 scols_print_table(tab
);
527 scols_unref_table(tab
);
531 static void show_process(struct lsns
*ls
, struct libscols_table
*tab
,
532 struct lsns_process
*proc
, struct lsns_namespace
*ns
)
535 * create a tree from parent->child relation, but only if the parent is
536 * within the same namespace
540 && !proc
->parent
->outline
541 && proc
->parent
->ns_ids
[ns
->type
] == proc
->ns_ids
[ns
->type
])
542 show_process(ls
, tab
, proc
->parent
, ns
);
544 add_scols_line(ls
, tab
, ns
, proc
);
548 static int show_namespace_processes(struct lsns
*ls
, struct lsns_namespace
*ns
)
550 struct libscols_table
*tab
;
553 tab
= init_scols_table(ls
);
557 list_for_each(p
, &ns
->processes
) {
558 struct lsns_process
*proc
= list_entry(p
, struct lsns_process
, ns_siblings
[ns
->type
]);
559 show_process(ls
, tab
, proc
, ns
);
563 scols_print_table(tab
);
564 scols_unref_table(tab
);
568 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
572 fputs(USAGE_HEADER
, out
);
575 _(" %s [options] [<namespace>]\n"), program_invocation_short_name
);
577 fputs(USAGE_SEPARATOR
, out
);
578 fputs(_("List system namespaces.\n"), out
);
580 fputs(USAGE_OPTIONS
, out
);
581 fputs(_(" -J, --json use JSON output format\n"), out
);
582 fputs(_(" -l, --list use list format output\n"), out
);
583 fputs(_(" -n, --noheadings don't print headings\n"), out
);
584 fputs(_(" -o, --output <list> define which output columns to use\n"), out
);
585 fputs(_(" -p, --task <pid> print process namespaces\n"), out
);
586 fputs(_(" -r, --raw use the raw output format\n"), out
);
587 fputs(_(" -u, --notruncate don't truncate text in columns\n"), out
);
588 fputs(_(" -t, --type <name> namespace type (mnt, net, ipc, user, pid, uts)\n"), out
);
590 fputs(USAGE_SEPARATOR
, out
);
591 fputs(USAGE_HELP
, out
);
592 fputs(USAGE_VERSION
, out
);
594 fputs(_("\nAvailable columns (for --output):\n"), out
);
596 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
597 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
599 fprintf(out
, USAGE_MAN_TAIL("lsns(8)"));
601 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
605 int main(int argc
, char *argv
[])
611 static const struct option long_opts
[] = {
612 { "json", no_argument
, NULL
, 'J' },
613 { "task", required_argument
, NULL
, 'p' },
614 { "help", no_argument
, NULL
, 'h' },
615 { "output", required_argument
, NULL
, 'o' },
616 { "notruncate", no_argument
, NULL
, 'u' },
617 { "version", no_argument
, NULL
, 'V' },
618 { "noheadings", no_argument
, NULL
, 'n' },
619 { "list", no_argument
, NULL
, 'l' },
620 { "raw", no_argument
, NULL
, 'r' },
621 { "type", required_argument
, NULL
, 't' },
625 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
629 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
631 setlocale(LC_ALL
, "");
632 bindtextdomain(PACKAGE
, LOCALEDIR
);
634 atexit(close_stdout
);
637 memset(&ls
, 0, sizeof(ls
));
639 INIT_LIST_HEAD(&ls
.processes
);
640 INIT_LIST_HEAD(&ls
.namespaces
);
642 while ((c
= getopt_long(argc
, argv
,
643 "Jlp:o:nruhVt:", long_opts
, NULL
)) != -1) {
645 err_exclusive_options(c
, long_opts
, excl
, excl_st
);
658 printf(UTIL_LINUX_VERSION
);
661 ls
.fltr_pid
= strtos32_or_err(optarg
, _("invalid PID argument"));
676 int type
= ns_name2type(optarg
);
678 errx(EXIT_FAILURE
, _("unknown namespace type: %s"), optarg
);
679 ls
.fltr_types
[type
] = 1;
689 if (!ls
.fltr_ntypes
) {
691 for (i
= 0; i
< ARRAY_SIZE(ns_names
); i
++)
692 ls
.fltr_types
[i
] = 1;
697 errx(EXIT_FAILURE
, _("--task is mutually exclusive with <namespace>"));
698 ls
.fltr_ns
= strtou64_or_err(argv
[optind
], _("invalid namespace argument"));
699 ls
.tree
= ls
.list
? 0 : 1;
702 columns
[ncolumns
++] = COL_PID
;
703 columns
[ncolumns
++] = COL_PPID
;
704 columns
[ncolumns
++] = COL_USER
;
705 columns
[ncolumns
++] = COL_COMMAND
;
710 columns
[ncolumns
++] = COL_NS
;
711 columns
[ncolumns
++] = COL_TYPE
;
712 columns
[ncolumns
++] = COL_NPROCS
;
713 columns
[ncolumns
++] = COL_PID
;
714 columns
[ncolumns
++] = COL_USER
;
715 columns
[ncolumns
++] = COL_COMMAND
;
718 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
719 &ncolumns
, column_name_to_id
) < 0)
724 uid_cache
= new_idcache();
726 err(EXIT_FAILURE
, _("failed to allocate UID cache"));
728 r
= read_processes(&ls
);
730 r
= read_namespaces(&ls
);
733 struct lsns_namespace
*ns
= get_namespace(&ls
, ls
.fltr_ns
);
736 errx(EXIT_FAILURE
, _("not found namespace: %ju"), (uintmax_t) ls
.fltr_ns
);
737 r
= show_namespace_processes(&ls
, ns
);
739 r
= show_namespaces(&ls
);
742 free_idcache(uid_cache
);
743 return r
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;