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>
33 #ifdef HAVE_LINUX_NET_NAMESPACE_H
35 #include <sys/socket.h>
36 #include <linux/netlink.h>
37 #include <linux/rtnetlink.h>
38 #include <linux/net_namespace.h>
41 #include "pathnames.h"
46 #include "closestream.h"
48 #include "procutils.h"
50 #include "namespace.h"
56 static UL_DEBUG_DEFINE_MASK(lsns
);
57 UL_DEBUG_DEFINE_MASKNAMES(lsns
) = UL_DEBUG_EMPTY_MASKNAMES
;
59 #define LSNS_DEBUG_INIT (1 << 1)
60 #define LSNS_DEBUG_PROC (1 << 2)
61 #define LSNS_DEBUG_NS (1 << 3)
62 #define LSNS_DEBUG_ALL 0xFFFF
64 #define LSNS_NETNS_UNUSABLE -2
66 #define DBG(m, x) __UL_DBG(lsns, LSNS_DEBUG_, m, x)
67 #define ON_DBG(m, x) __UL_DBG_CALL(lsns, LSNS_DEBUG_, m, x)
69 static struct idcache
*uid_cache
= NULL
;
88 const char *name
; /* header */
89 double whint
; /* width hint (N < 1 is in percent of termwidth) */
90 int flags
; /* SCOLS_FL_* */
94 /* columns descriptions */
95 static const struct colinfo infos
[] = {
96 [COL_NS
] = { "NS", 10, SCOLS_FL_RIGHT
, N_("namespace identifier (inode number)") },
97 [COL_TYPE
] = { "TYPE", 5, 0, N_("kind of namespace") },
98 [COL_PATH
] = { "PATH", 0, 0, N_("path to the namespace")},
99 [COL_NPROCS
] = { "NPROCS", 5, SCOLS_FL_RIGHT
, N_("number of processes in the namespace") },
100 [COL_PID
] = { "PID", 5, SCOLS_FL_RIGHT
, N_("lowest PID in the namespace") },
101 [COL_PPID
] = { "PPID", 5, SCOLS_FL_RIGHT
, N_("PPID of the PID") },
102 [COL_COMMAND
] = { "COMMAND", 0, SCOLS_FL_TRUNC
, N_("command line of the PID")},
103 [COL_UID
] = { "UID", 0, SCOLS_FL_RIGHT
, N_("UID of the PID")},
104 [COL_USER
] = { "USER", 0, 0, N_("username of the PID")},
105 [COL_NETNSID
] = { "NETNSID", 0, SCOLS_FL_RIGHT
, N_("namespace ID as used by network subsystem")},
106 [COL_NSFS
] = { "NSFS", 0, SCOLS_FL_WRAP
, N_("nsfs mountpoint (usually used network subsystem)")}
109 static int columns
[ARRAY_SIZE(infos
) * 2];
110 static size_t ncolumns
;
122 static char *ns_names
[] = {
123 [LSNS_ID_MNT
] = "mnt",
124 [LSNS_ID_NET
] = "net",
125 [LSNS_ID_PID
] = "pid",
126 [LSNS_ID_UTS
] = "uts",
127 [LSNS_ID_IPC
] = "ipc",
128 [LSNS_ID_USER
] = "user",
129 [LSNS_ID_CGROUP
] = "cgroup"
132 struct lsns_namespace
{
134 int type
; /* LSNS_* */
138 struct lsns_process
*proc
;
140 struct list_head namespaces
; /* lsns->processes member */
141 struct list_head processes
; /* head of lsns_process *siblings */
144 struct lsns_process
{
145 pid_t pid
; /* process PID */
146 pid_t ppid
; /* parent's PID */
147 pid_t tpid
; /* thread group */
151 ino_t ns_ids
[ARRAY_SIZE(ns_names
)];
152 struct list_head ns_siblings
[ARRAY_SIZE(ns_names
)];
154 struct list_head processes
; /* list of processes */
156 struct libscols_line
*outline
;
157 struct lsns_process
*parent
;
163 struct list_head processes
;
164 struct list_head namespaces
;
166 pid_t fltr_pid
; /* filter out by PID */
167 ino_t fltr_ns
; /* filter out by namespace */
168 int fltr_types
[ARRAY_SIZE(ns_names
)];
171 unsigned int raw
: 1,
179 struct libmnt_table
*tab
;
182 struct netnsid_cache
{
185 struct list_head netnsids
;
188 static struct list_head netnsids_cache
;
190 static int netlink_fd
= -1;
192 static void lsns_init_debug(void)
194 __UL_INIT_DEBUG(lsns
, LSNS_DEBUG_
, 0, LSNS_DEBUG
);
197 static int ns_name2type(const char *name
)
201 for (i
= 0; i
< ARRAY_SIZE(ns_names
); i
++) {
202 if (strcmp(ns_names
[i
], name
) == 0)
208 static int column_name_to_id(const char *name
, size_t namesz
)
214 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
215 const char *cn
= infos
[i
].name
;
217 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
220 warnx(_("unknown column: %s"), name
);
224 static int has_column(int id
)
228 for (i
= 0; i
< ncolumns
; i
++) {
229 if (columns
[i
] == id
)
235 static inline int get_column_id(int num
)
238 assert((size_t) num
< ncolumns
);
239 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
244 static inline const struct colinfo
*get_column_info(unsigned num
)
246 return &infos
[ get_column_id(num
) ];
249 static int get_ns_ino(int dir
, const char *nsname
, ino_t
*ino
)
254 snprintf(path
, sizeof(path
), "ns/%s", nsname
);
256 if (fstatat(dir
, path
, &st
, 0) != 0)
262 static int parse_proc_stat(FILE *fp
, pid_t
*pid
, char *state
, pid_t
*ppid
)
264 char *line
= NULL
, *p
;
268 if (getline(&line
, &len
, fp
) < 0) {
273 p
= strrchr(line
, ')');
275 sscanf(line
, "%d (", pid
) != 1 ||
276 sscanf(p
, ") %c %d*[^\n]", state
, ppid
) != 2) {
287 #ifdef HAVE_LINUX_NET_NAMESPACE_H
288 static int netnsid_cache_find(ino_t netino
, int *netnsid
)
292 list_for_each(p
, &netnsids_cache
) {
293 struct netnsid_cache
*e
= list_entry(p
,
294 struct netnsid_cache
,
296 if (e
->ino
== netino
) {
305 static void netnsid_cache_add(ino_t netino
, int netnsid
)
307 struct netnsid_cache
*e
;
309 e
= xcalloc(1, sizeof(*e
));
312 INIT_LIST_HEAD(&e
->netnsids
);
313 list_add(&e
->netnsids
, &netnsids_cache
);
316 static int get_netnsid_via_netlink_send_request(int target_fd
)
318 unsigned char req
[NLMSG_SPACE(sizeof(struct rtgenmsg
))
319 + RTA_SPACE(sizeof(int32_t))];
321 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)req
;
322 struct rtgenmsg
*rt
= NLMSG_DATA(req
);
323 struct rtattr
*rta
= (struct rtattr
*)
324 (req
+ NLMSG_SPACE(sizeof(struct rtgenmsg
)));
325 int32_t *fd
= RTA_DATA(rta
);
327 nlh
->nlmsg_len
= sizeof(req
);
328 nlh
->nlmsg_flags
= NLM_F_REQUEST
;
329 nlh
->nlmsg_type
= RTM_GETNSID
;
330 rt
->rtgen_family
= AF_UNSPEC
;
331 rta
->rta_type
= NETNSA_FD
;
332 rta
->rta_len
= RTA_SPACE(sizeof(int32_t));
335 if (send(netlink_fd
, req
, sizeof(req
), 0) < 0)
340 static int get_netnsid_via_netlink_recv_response(int *netnsid
)
342 unsigned char res
[NLMSG_SPACE(sizeof(struct rtgenmsg
))
343 + ((RTA_SPACE(sizeof(int32_t))
344 < RTA_SPACE(sizeof(struct nlmsgerr
)))
345 ? RTA_SPACE(sizeof(struct nlmsgerr
))
346 : RTA_SPACE(sizeof(int32_t)))];
349 struct nlmsghdr
*nlh
;
352 reslen
= recv(netlink_fd
, res
, sizeof(res
), 0);
356 nlh
= (struct nlmsghdr
*)res
;
357 if (!(NLMSG_OK(nlh
, reslen
)
358 && nlh
->nlmsg_type
== RTM_NEWNSID
))
361 rtalen
= NLMSG_PAYLOAD(nlh
, sizeof(struct rtgenmsg
));
362 rta
= (struct rtattr
*)(res
+ NLMSG_SPACE(sizeof(struct rtgenmsg
)));
363 if (!(RTA_OK(rta
, rtalen
)
364 && rta
->rta_type
== NETNSA_NSID
))
367 *netnsid
= *(int *)RTA_DATA(rta
);
372 static int get_netnsid_via_netlink(int dir
, const char *path
)
378 return LSNS_NETNS_UNUSABLE
;
380 target_fd
= openat(dir
, path
, O_RDONLY
);
382 return LSNS_NETNS_UNUSABLE
;
384 if (get_netnsid_via_netlink_send_request(target_fd
) < 0) {
385 netnsid
= LSNS_NETNS_UNUSABLE
;
389 if (get_netnsid_via_netlink_recv_response(&netnsid
) < 0) {
390 netnsid
= LSNS_NETNS_UNUSABLE
;
399 static int get_netnsid(int dir
, ino_t netino
)
403 if (!netnsid_cache_find(netino
, &netnsid
)) {
404 netnsid
= get_netnsid_via_netlink(dir
, "ns/net");
405 netnsid_cache_add(netino
, netnsid
);
411 static int get_netnsid(int dir
__attribute__((__unused__
)),
412 ino_t netino
__attribute__((__unused__
)))
414 return LSNS_NETNS_UNUSABLE
;
416 #endif /* HAVE_LINUX_NET_NAMESPACE_H */
418 static int read_process(struct lsns
*ls
, pid_t pid
)
420 struct lsns_process
*p
= NULL
;
428 DBG(PROC
, ul_debug("reading %d", (int) pid
));
430 snprintf(buf
, sizeof(buf
), "/proc/%d", pid
);
435 p
= xcalloc(1, sizeof(*p
));
440 p
->netnsid
= LSNS_NETNS_UNUSABLE
;
442 if (fstat(dirfd(dir
), &st
) == 0) {
444 add_uid(uid_cache
, st
.st_uid
);
447 fd
= openat(dirfd(dir
), "stat", O_RDONLY
);
452 if (!(f
= fdopen(fd
, "r"))) {
456 rc
= parse_proc_stat(f
, &p
->pid
, &p
->state
, &p
->ppid
);
461 for (i
= 0; i
< ARRAY_SIZE(p
->ns_ids
); i
++) {
462 INIT_LIST_HEAD(&p
->ns_siblings
[i
]);
464 if (!ls
->fltr_types
[i
])
467 rc
= get_ns_ino(dirfd(dir
), ns_names
[i
], &p
->ns_ids
[i
]);
468 if (rc
&& rc
!= -EACCES
&& rc
!= -ENOENT
)
470 if (i
== LSNS_ID_NET
)
471 p
->netnsid
= get_netnsid(dirfd(dir
), p
->ns_ids
[i
]);
475 INIT_LIST_HEAD(&p
->processes
);
477 DBG(PROC
, ul_debugobj(p
, "new pid=%d", p
->pid
));
478 list_add_tail(&p
->processes
, &ls
->processes
);
488 static int read_processes(struct lsns
*ls
)
490 struct proc_processes
*proc
= NULL
;
494 DBG(PROC
, ul_debug("opening /proc"));
496 if (!(proc
= proc_open_processes())) {
501 while (proc_next_pid(proc
, &pid
) == 0) {
502 rc
= read_process(ls
, pid
);
503 if (rc
&& rc
!= -EACCES
&& rc
!= -ENOENT
)
508 DBG(PROC
, ul_debug("closing /proc"));
509 proc_close_processes(proc
);
513 static struct lsns_namespace
*get_namespace(struct lsns
*ls
, ino_t ino
)
517 list_for_each(p
, &ls
->namespaces
) {
518 struct lsns_namespace
*ns
= list_entry(p
, struct lsns_namespace
, namespaces
);
526 static int namespace_has_process(struct lsns_namespace
*ns
, pid_t pid
)
530 list_for_each(p
, &ns
->processes
) {
531 struct lsns_process
*proc
= list_entry(p
, struct lsns_process
, ns_siblings
[ns
->type
]);
533 if (proc
->pid
== pid
)
539 static struct lsns_namespace
*add_namespace(struct lsns
*ls
, int type
, ino_t ino
)
541 struct lsns_namespace
*ns
= xcalloc(1, sizeof(*ns
));
546 DBG(NS
, ul_debugobj(ns
, "new %s[%ju]", ns_names
[type
], (uintmax_t)ino
));
548 INIT_LIST_HEAD(&ns
->processes
);
549 INIT_LIST_HEAD(&ns
->namespaces
);
554 list_add_tail(&ns
->namespaces
, &ls
->namespaces
);
558 static int add_process_to_namespace(struct lsns
*ls
, struct lsns_namespace
*ns
, struct lsns_process
*proc
)
562 DBG(NS
, ul_debugobj(ns
, "add process [%p] pid=%d to %s[%ju]",
563 proc
, proc
->pid
, ns_names
[ns
->type
], (uintmax_t)ns
->id
));
565 list_for_each(p
, &ls
->processes
) {
566 struct lsns_process
*xproc
= list_entry(p
, struct lsns_process
, processes
);
568 if (xproc
->pid
== proc
->ppid
) /* my parent */
569 proc
->parent
= xproc
;
570 else if (xproc
->ppid
== proc
->pid
) /* my child */
571 xproc
->parent
= proc
;
574 list_add_tail(&proc
->ns_siblings
[ns
->type
], &ns
->processes
);
577 if (!ns
->proc
|| ns
->proc
->pid
> proc
->pid
)
583 static int cmp_namespaces(struct list_head
*a
, struct list_head
*b
,
584 __attribute__((__unused__
)) void *data
)
586 struct lsns_namespace
*xa
= list_entry(a
, struct lsns_namespace
, namespaces
),
587 *xb
= list_entry(b
, struct lsns_namespace
, namespaces
);
589 return cmp_numbers(xa
->id
, xb
->id
);
592 static int netnsid_xasputs(char **str
, int netnsid
)
595 return xasprintf(str
, "%d", netnsid
);
596 #ifdef NETNSA_NSID_NOT_ASSIGNED
597 else if (netnsid
== NETNSA_NSID_NOT_ASSIGNED
)
598 return xasprintf(str
, "%s", "unassigned");
604 static int read_namespaces(struct lsns
*ls
)
608 DBG(NS
, ul_debug("reading namespace"));
610 list_for_each(p
, &ls
->processes
) {
612 struct lsns_namespace
*ns
;
613 struct lsns_process
*proc
= list_entry(p
, struct lsns_process
, processes
);
615 for (i
= 0; i
< ARRAY_SIZE(proc
->ns_ids
); i
++) {
616 if (proc
->ns_ids
[i
] == 0)
618 if (!(ns
= get_namespace(ls
, proc
->ns_ids
[i
]))) {
619 ns
= add_namespace(ls
, i
, proc
->ns_ids
[i
]);
623 add_process_to_namespace(ls
, ns
, proc
);
627 list_sort(&ls
->namespaces
, cmp_namespaces
, NULL
);
632 static int is_nsfs_root(struct libmnt_fs
*fs
, void *data
)
634 if (!mnt_fs_match_fstype(fs
, "nsfs") || !mnt_fs_get_root(fs
))
637 return (strcmp(mnt_fs_get_root(fs
), (char *)data
) == 0);
640 static int is_path_included(const char *path_set
, const char *elt
,
648 tmp
= strstr(path_set
, elt
);
652 elt_len
= strlen(elt
);
653 path_set_len
= strlen(path_set
);
655 /* path_set includes only elt or
656 * path_set includes elt as the first element.
659 && ((path_set_len
== elt_len
)
660 || (path_set
[elt_len
] == sep
)))
663 /* path_set includes elt at the middle
664 * or as the last element.
666 if ((*(tmp
- 1) == sep
)
667 && ((*(tmp
+ elt_len
) == sep
)
668 || (*(tmp
+ elt_len
) == '\0')))
674 static int nsfs_xasputs(char **str
,
675 struct lsns_namespace
*ns
,
676 struct libmnt_table
*tab
,
679 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_FORWARD
);
681 struct libmnt_fs
*fs
= NULL
;
683 xasprintf(&expected_root
, "%s:[%ju]", ns_names
[ns
->type
], (uintmax_t)ns
->id
);
686 while (mnt_table_find_next_fs(tab
, itr
, is_nsfs_root
,
687 expected_root
, &fs
) == 0) {
689 const char *tgt
= mnt_fs_get_target(fs
);
692 xasprintf(str
, "%s", tgt
);
694 else if (!is_path_included(*str
, tgt
, sep
)) {
697 xasprintf(&tmp
, "%s%c%s", *str
, sep
, tgt
);
707 static void add_scols_line(struct lsns
*ls
, struct libscols_table
*table
,
708 struct lsns_namespace
*ns
, struct lsns_process
*proc
)
711 struct libscols_line
*line
;
716 line
= scols_table_new_line(table
,
717 ls
->tree
&& proc
->parent
? proc
->parent
->outline
: NULL
);
719 warn(_("failed to add line to output"));
723 for (i
= 0; i
< ncolumns
; i
++) {
726 switch (get_column_id(i
)) {
728 xasprintf(&str
, "%ju", (uintmax_t)ns
->id
);
731 xasprintf(&str
, "%d", (int) proc
->pid
);
734 xasprintf(&str
, "%d", (int) proc
->ppid
);
737 xasprintf(&str
, "%s", ns_names
[ns
->type
]);
740 xasprintf(&str
, "%d", ns
->nprocs
);
743 str
= proc_get_command(proc
->pid
);
745 str
= proc_get_command_name(proc
->pid
);
748 xasprintf(&str
, "/proc/%d/ns/%s", (int) proc
->pid
, ns_names
[ns
->type
]);
751 xasprintf(&str
, "%d", (int) proc
->uid
);
754 xasprintf(&str
, "%s", get_id(uid_cache
, proc
->uid
)->name
);
757 if (ns
->type
== LSNS_ID_NET
)
758 netnsid_xasputs(&str
, proc
->netnsid
);
761 nsfs_xasputs(&str
, ns
, ls
->tab
, ls
->no_wrap
? ',' : '\n');
767 if (str
&& scols_line_refer_data(line
, i
, str
) != 0)
771 proc
->outline
= line
;
774 static struct libscols_table
*init_scols_table(struct lsns
*ls
)
776 struct libscols_table
*tab
;
779 tab
= scols_new_table();
781 warn(_("failed to initialize output table"));
785 scols_table_enable_raw(tab
, ls
->raw
);
786 scols_table_enable_json(tab
, ls
->json
);
787 scols_table_enable_noheadings(tab
, ls
->no_headings
);
790 scols_table_set_name(tab
, "namespaces");
792 for (i
= 0; i
< ncolumns
; i
++) {
793 const struct colinfo
*col
= get_column_info(i
);
794 int flags
= col
->flags
;
795 struct libscols_column
*cl
;
798 flags
&= ~SCOLS_FL_TRUNC
;
799 if (ls
->tree
&& get_column_id(i
) == COL_COMMAND
)
800 flags
|= SCOLS_FL_TREE
;
802 flags
&= ~SCOLS_FL_WRAP
;
804 cl
= scols_table_new_column(tab
, col
->name
, col
->whint
, flags
);
806 warnx(_("failed to initialize output column"));
809 if (!ls
->no_wrap
&& get_column_id(i
) == COL_NSFS
) {
810 scols_column_set_wrapfunc(cl
,
811 scols_wrapnl_chunksize
,
812 scols_wrapnl_nextchunk
,
814 scols_column_set_safechars(cl
, "\n");
820 scols_unref_table(tab
);
824 static int show_namespaces(struct lsns
*ls
)
826 struct libscols_table
*tab
;
830 tab
= init_scols_table(ls
);
834 list_for_each(p
, &ls
->namespaces
) {
835 struct lsns_namespace
*ns
= list_entry(p
, struct lsns_namespace
, namespaces
);
837 if (ls
->fltr_pid
!= 0 && !namespace_has_process(ns
, ls
->fltr_pid
))
840 add_scols_line(ls
, tab
, ns
, ns
->proc
);
843 scols_print_table(tab
);
844 scols_unref_table(tab
);
848 static void show_process(struct lsns
*ls
, struct libscols_table
*tab
,
849 struct lsns_process
*proc
, struct lsns_namespace
*ns
)
852 * create a tree from parent->child relation, but only if the parent is
853 * within the same namespace
857 && !proc
->parent
->outline
858 && proc
->parent
->ns_ids
[ns
->type
] == proc
->ns_ids
[ns
->type
])
859 show_process(ls
, tab
, proc
->parent
, ns
);
861 add_scols_line(ls
, tab
, ns
, proc
);
865 static int show_namespace_processes(struct lsns
*ls
, struct lsns_namespace
*ns
)
867 struct libscols_table
*tab
;
870 tab
= init_scols_table(ls
);
874 list_for_each(p
, &ns
->processes
) {
875 struct lsns_process
*proc
= list_entry(p
, struct lsns_process
, ns_siblings
[ns
->type
]);
878 show_process(ls
, tab
, proc
, ns
);
882 scols_print_table(tab
);
883 scols_unref_table(tab
);
887 static void __attribute__((__noreturn__
)) usage(void)
892 fputs(USAGE_HEADER
, out
);
895 _(" %s [options] [<namespace>]\n"), program_invocation_short_name
);
897 fputs(USAGE_SEPARATOR
, out
);
898 fputs(_("List system namespaces.\n"), out
);
900 fputs(USAGE_OPTIONS
, out
);
901 fputs(_(" -J, --json use JSON output format\n"), out
);
902 fputs(_(" -l, --list use list format output\n"), out
);
903 fputs(_(" -n, --noheadings don't print headings\n"), out
);
904 fputs(_(" -o, --output <list> define which output columns to use\n"), out
);
905 fputs(_(" -p, --task <pid> print process namespaces\n"), out
);
906 fputs(_(" -r, --raw use the raw output format\n"), out
);
907 fputs(_(" -u, --notruncate don't truncate text in columns\n"), out
);
908 fputs(_(" -W, --nowrap don't use multi-line representation\n"), out
);
909 fputs(_(" -t, --type <name> namespace type (mnt, net, ipc, user, pid, uts, cgroup)\n"), out
);
911 fputs(USAGE_SEPARATOR
, out
);
912 printf(USAGE_HELP_OPTIONS(24));
914 fputs(USAGE_COLUMNS
, out
);
915 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
916 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
918 printf(USAGE_MAN_TAIL("lsns(8)"));
924 int main(int argc
, char *argv
[])
930 static const struct option long_opts
[] = {
931 { "json", no_argument
, NULL
, 'J' },
932 { "task", required_argument
, NULL
, 'p' },
933 { "help", no_argument
, NULL
, 'h' },
934 { "output", required_argument
, NULL
, 'o' },
935 { "notruncate", no_argument
, NULL
, 'u' },
936 { "version", no_argument
, NULL
, 'V' },
937 { "noheadings", no_argument
, NULL
, 'n' },
938 { "nowrap", no_argument
, NULL
, 'W' },
939 { "list", no_argument
, NULL
, 'l' },
940 { "raw", no_argument
, NULL
, 'r' },
941 { "type", required_argument
, NULL
, 't' },
945 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
949 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
952 setlocale(LC_ALL
, "");
953 bindtextdomain(PACKAGE
, LOCALEDIR
);
955 atexit(close_stdout
);
958 memset(&ls
, 0, sizeof(ls
));
960 INIT_LIST_HEAD(&ls
.processes
);
961 INIT_LIST_HEAD(&ls
.namespaces
);
962 INIT_LIST_HEAD(&netnsids_cache
);
964 while ((c
= getopt_long(argc
, argv
,
965 "Jlp:o:nruhVt:W", long_opts
, NULL
)) != -1) {
967 err_exclusive_options(c
, long_opts
, excl
, excl_st
);
980 printf(UTIL_LINUX_VERSION
);
983 ls
.fltr_pid
= strtos32_or_err(optarg
, _("invalid PID argument"));
991 ls
.no_wrap
= ls
.raw
= 1;
998 int type
= ns_name2type(optarg
);
1000 errx(EXIT_FAILURE
, _("unknown namespace type: %s"), optarg
);
1001 ls
.fltr_types
[type
] = 1;
1003 if (type
== LSNS_ID_NET
)
1011 errtryhelp(EXIT_FAILURE
);
1015 if (!ls
.fltr_ntypes
) {
1018 for (i
= 0; i
< ARRAY_SIZE(ns_names
); i
++)
1019 ls
.fltr_types
[i
] = 1;
1022 if (optind
< argc
) {
1024 errx(EXIT_FAILURE
, _("--task is mutually exclusive with <namespace>"));
1025 ls
.fltr_ns
= strtou64_or_err(argv
[optind
], _("invalid namespace argument"));
1026 ls
.tree
= ls
.list
? 0 : 1;
1029 columns
[ncolumns
++] = COL_PID
;
1030 columns
[ncolumns
++] = COL_PPID
;
1031 columns
[ncolumns
++] = COL_USER
;
1032 columns
[ncolumns
++] = COL_COMMAND
;
1037 columns
[ncolumns
++] = COL_NS
;
1038 columns
[ncolumns
++] = COL_TYPE
;
1039 columns
[ncolumns
++] = COL_NPROCS
;
1040 columns
[ncolumns
++] = COL_PID
;
1041 columns
[ncolumns
++] = COL_USER
;
1043 columns
[ncolumns
++] = COL_NETNSID
;
1044 columns
[ncolumns
++] = COL_NSFS
;
1046 columns
[ncolumns
++] = COL_COMMAND
;
1049 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1050 &ncolumns
, column_name_to_id
) < 0)
1051 return EXIT_FAILURE
;
1053 scols_init_debug(0);
1055 uid_cache
= new_idcache();
1057 err(EXIT_FAILURE
, _("failed to allocate UID cache"));
1059 #ifdef HAVE_LINUX_NET_NAMESPACE_H
1060 if (has_column(COL_NETNSID
))
1061 netlink_fd
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
1063 if (has_column(COL_NSFS
)) {
1064 ls
.tab
= mnt_new_table_from_file(_PATH_PROC_MOUNTINFO
);
1066 err(MNT_EX_FAIL
, _("failed to parse %s"), _PATH_PROC_MOUNTINFO
);
1069 r
= read_processes(&ls
);
1071 r
= read_namespaces(&ls
);
1074 struct lsns_namespace
*ns
= get_namespace(&ls
, ls
.fltr_ns
);
1077 errx(EXIT_FAILURE
, _("not found namespace: %ju"), (uintmax_t) ls
.fltr_ns
);
1078 r
= show_namespace_processes(&ls
, ns
);
1080 r
= show_namespaces(&ls
);
1083 mnt_free_table(ls
.tab
);
1084 if (netlink_fd
>= 0)
1086 free_idcache(uid_cache
);
1087 return r
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;