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 #ifdef HAVE_LINUX_NSFS_H
42 #include <linux/nsfs.h>
45 #include "pathnames.h"
50 #include "closestream.h"
52 #include "procutils.h"
54 #include "namespace.h"
59 static UL_DEBUG_DEFINE_MASK(lsns
);
60 UL_DEBUG_DEFINE_MASKNAMES(lsns
) = UL_DEBUG_EMPTY_MASKNAMES
;
62 #define LSNS_DEBUG_INIT (1 << 1)
63 #define LSNS_DEBUG_PROC (1 << 2)
64 #define LSNS_DEBUG_NS (1 << 3)
65 #define LSNS_DEBUG_ALL 0xFFFF
67 #define LSNS_NETNS_UNUSABLE -2
69 #define DBG(m, x) __UL_DBG(lsns, LSNS_DEBUG_, m, x)
70 #define ON_DBG(m, x) __UL_DBG_CALL(lsns, LSNS_DEBUG_, m, x)
72 #define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(lsns)
75 static struct idcache
*uid_cache
= NULL
;
90 COL_PNS
, /* parent namespace */
91 COL_ONS
, /* owner namespace */
96 const char *name
; /* header */
97 double whint
; /* width hint (N < 1 is in percent of termwidth) */
98 int flags
; /* SCOLS_FL_* */
103 /* columns descriptions */
104 static const struct colinfo infos
[] = {
105 [COL_NS
] = { "NS", 10, SCOLS_FL_RIGHT
, N_("namespace identifier (inode number)"), SCOLS_JSON_NUMBER
},
106 [COL_TYPE
] = { "TYPE", 5, 0, N_("kind of namespace") },
107 [COL_PATH
] = { "PATH", 0, 0, N_("path to the namespace")},
108 [COL_NPROCS
] = { "NPROCS", 5, SCOLS_FL_RIGHT
, N_("number of processes in the namespace"), SCOLS_JSON_NUMBER
},
109 [COL_PID
] = { "PID", 5, SCOLS_FL_RIGHT
, N_("lowest PID in the namespace"), SCOLS_JSON_NUMBER
},
110 [COL_PPID
] = { "PPID", 5, SCOLS_FL_RIGHT
, N_("PPID of the PID"), SCOLS_JSON_NUMBER
},
111 [COL_COMMAND
] = { "COMMAND", 0, SCOLS_FL_TRUNC
, N_("command line of the PID")},
112 [COL_UID
] = { "UID", 0, SCOLS_FL_RIGHT
, N_("UID of the PID"), SCOLS_JSON_NUMBER
},
113 [COL_USER
] = { "USER", 0, 0, N_("username of the PID")},
114 [COL_NETNSID
] = { "NETNSID", 0, SCOLS_FL_RIGHT
, N_("namespace ID as used by network subsystem")},
115 [COL_NSFS
] = { "NSFS", 0, SCOLS_FL_WRAP
, N_("nsfs mountpoint (usually used network subsystem)")},
116 [COL_PNS
] = { "PNS", 10, SCOLS_FL_RIGHT
, N_("parent namespace identifier (inode number)"), SCOLS_JSON_NUMBER
},
117 [COL_ONS
] = { "ONS", 10, SCOLS_FL_RIGHT
, N_("owner namespace identifier (inode number)"), SCOLS_JSON_NUMBER
},
120 static int columns
[ARRAY_SIZE(infos
) * 2];
121 static size_t ncolumns
;
134 static char *ns_names
[] = {
135 [LSNS_ID_MNT
] = "mnt",
136 [LSNS_ID_NET
] = "net",
137 [LSNS_ID_PID
] = "pid",
138 [LSNS_ID_UTS
] = "uts",
139 [LSNS_ID_IPC
] = "ipc",
140 [LSNS_ID_USER
] = "user",
141 [LSNS_ID_CGROUP
] = "cgroup",
142 [LSNS_ID_TIME
] = "time"
145 struct lsns_namespace
{
147 int type
; /* LSNS_* */
153 struct lsns_process
*proc
;
155 struct lsns_namespace
*parentns
;
156 struct lsns_namespace
*ownerns
;
157 struct libscols_line
*ns_outline
;
159 struct list_head namespaces
; /* lsns->processes member */
160 struct list_head processes
; /* head of lsns_process *siblings */
163 struct lsns_process
{
164 pid_t pid
; /* process PID */
165 pid_t ppid
; /* parent's PID */
166 pid_t tpid
; /* thread group */
170 ino_t ns_ids
[ARRAY_SIZE(ns_names
)];
171 ino_t ns_pids
[ARRAY_SIZE(ns_names
)];
172 ino_t ns_oids
[ARRAY_SIZE(ns_names
)];
174 struct list_head ns_siblings
[ARRAY_SIZE(ns_names
)];
176 struct list_head processes
; /* list of processes */
178 struct libscols_line
*outline
;
179 struct lsns_process
*parent
;
193 struct list_head processes
;
194 struct list_head namespaces
;
196 pid_t fltr_pid
; /* filter out by PID */
197 ino_t fltr_ns
; /* filter out by namespace */
198 int fltr_types
[ARRAY_SIZE(ns_names
)];
201 unsigned int raw
: 1,
209 struct libmnt_table
*tab
;
212 struct netnsid_cache
{
215 struct list_head netnsids
;
218 static struct list_head netnsids_cache
;
220 static int netlink_fd
= -1;
222 static void lsns_init_debug(void)
224 __UL_INIT_DEBUG_FROM_ENV(lsns
, LSNS_DEBUG_
, 0, LSNS_DEBUG
);
227 static int ns_name2type(const char *name
)
231 for (i
= 0; i
< ARRAY_SIZE(ns_names
); i
++) {
232 if (strcmp(ns_names
[i
], name
) == 0)
238 static int column_name_to_id(const char *name
, size_t namesz
)
244 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
245 const char *cn
= infos
[i
].name
;
247 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
250 warnx(_("unknown column: %s"), name
);
254 static int has_column(int id
)
258 for (i
= 0; i
< ncolumns
; i
++) {
259 if (columns
[i
] == id
)
265 static inline int get_column_id(int num
)
268 assert((size_t) num
< ncolumns
);
269 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
274 static inline const struct colinfo
*get_column_info(unsigned num
)
276 return &infos
[ get_column_id(num
) ];
279 static int get_ns_ino(int dir
, const char *nsname
, ino_t
*ino
, ino_t
*pino
, ino_t
*oino
)
284 snprintf(path
, sizeof(path
), "ns/%s", nsname
);
286 if (fstatat(dir
, path
, &st
, 0) != 0)
293 #ifdef HAVE_LINUX_NSFS_H
295 fd
= openat(dir
, path
, 0);
298 if (strcmp(nsname
, "pid") == 0 || strcmp(nsname
, "user") == 0) {
299 if ((pfd
= ioctl(fd
, NS_GET_PARENT
)) < 0) {
305 if (fstat(pfd
, &st
) < 0) {
314 if ((ofd
= ioctl(fd
, NS_GET_USERNS
)) < 0) {
320 if (fstat(ofd
, &st
) < 0) {
333 static int parse_proc_stat(FILE *fp
, pid_t
*pid
, char *state
, pid_t
*ppid
)
335 char *line
= NULL
, *p
;
339 if (getline(&line
, &len
, fp
) < 0) {
344 p
= strrchr(line
, ')');
346 sscanf(line
, "%d (", pid
) != 1 ||
347 sscanf(p
, ") %c %d*[^\n]", state
, ppid
) != 2) {
358 #ifdef HAVE_LINUX_NET_NAMESPACE_H
359 static int netnsid_cache_find(ino_t netino
, int *netnsid
)
363 list_for_each(p
, &netnsids_cache
) {
364 struct netnsid_cache
*e
= list_entry(p
,
365 struct netnsid_cache
,
367 if (e
->ino
== netino
) {
376 static void netnsid_cache_add(ino_t netino
, int netnsid
)
378 struct netnsid_cache
*e
;
380 e
= xcalloc(1, sizeof(*e
));
383 INIT_LIST_HEAD(&e
->netnsids
);
384 list_add(&e
->netnsids
, &netnsids_cache
);
387 static int get_netnsid_via_netlink_send_request(int target_fd
)
389 unsigned char req
[NLMSG_SPACE(sizeof(struct rtgenmsg
))
390 + RTA_SPACE(sizeof(int32_t))];
392 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)req
;
393 struct rtgenmsg
*rt
= NLMSG_DATA(req
);
394 struct rtattr
*rta
= (struct rtattr
*)
395 (req
+ NLMSG_SPACE(sizeof(struct rtgenmsg
)));
396 int32_t *fd
= RTA_DATA(rta
);
398 nlh
->nlmsg_len
= sizeof(req
);
399 nlh
->nlmsg_flags
= NLM_F_REQUEST
;
400 nlh
->nlmsg_type
= RTM_GETNSID
;
401 rt
->rtgen_family
= AF_UNSPEC
;
402 rta
->rta_type
= NETNSA_FD
;
403 rta
->rta_len
= RTA_SPACE(sizeof(int32_t));
406 if (send(netlink_fd
, req
, sizeof(req
), 0) < 0)
411 static int get_netnsid_via_netlink_recv_response(int *netnsid
)
413 unsigned char res
[NLMSG_SPACE(sizeof(struct rtgenmsg
))
414 + ((RTA_SPACE(sizeof(int32_t))
415 < RTA_SPACE(sizeof(struct nlmsgerr
)))
416 ? RTA_SPACE(sizeof(struct nlmsgerr
))
417 : RTA_SPACE(sizeof(int32_t)))];
421 struct nlmsghdr
*nlh
;
424 reslen
= recv(netlink_fd
, res
, sizeof(res
), 0);
428 nlh
= (struct nlmsghdr
*)res
;
429 if (!(NLMSG_OK(nlh
, (size_t)reslen
)
430 && nlh
->nlmsg_type
== RTM_NEWNSID
))
433 rtalen
= NLMSG_PAYLOAD(nlh
, sizeof(struct rtgenmsg
));
434 rta
= (struct rtattr
*)(res
+ NLMSG_SPACE(sizeof(struct rtgenmsg
)));
435 if (!(RTA_OK(rta
, rtalen
)
436 && rta
->rta_type
== NETNSA_NSID
))
439 *netnsid
= *(int *)RTA_DATA(rta
);
444 static int get_netnsid_via_netlink(int dir
, const char *path
)
450 return LSNS_NETNS_UNUSABLE
;
452 target_fd
= openat(dir
, path
, O_RDONLY
);
454 return LSNS_NETNS_UNUSABLE
;
456 if (get_netnsid_via_netlink_send_request(target_fd
) < 0) {
457 netnsid
= LSNS_NETNS_UNUSABLE
;
461 if (get_netnsid_via_netlink_recv_response(&netnsid
) < 0) {
462 netnsid
= LSNS_NETNS_UNUSABLE
;
471 static int get_netnsid(int dir
, ino_t netino
)
475 if (!netnsid_cache_find(netino
, &netnsid
)) {
476 netnsid
= get_netnsid_via_netlink(dir
, "ns/net");
477 netnsid_cache_add(netino
, netnsid
);
483 static int get_netnsid(int dir
__attribute__((__unused__
)),
484 ino_t netino
__attribute__((__unused__
)))
486 return LSNS_NETNS_UNUSABLE
;
488 #endif /* HAVE_LINUX_NET_NAMESPACE_H */
490 static int read_process(struct lsns
*ls
, pid_t pid
)
492 struct lsns_process
*p
= NULL
;
500 DBG(PROC
, ul_debug("reading %d", (int) pid
));
502 snprintf(buf
, sizeof(buf
), "/proc/%d", pid
);
507 p
= xcalloc(1, sizeof(*p
));
508 p
->netnsid
= LSNS_NETNS_UNUSABLE
;
510 if (fstat(dirfd(dir
), &st
) == 0) {
512 add_uid(uid_cache
, st
.st_uid
);
515 fd
= openat(dirfd(dir
), "stat", O_RDONLY
);
520 if (!(f
= fdopen(fd
, "r"))) {
524 rc
= parse_proc_stat(f
, &p
->pid
, &p
->state
, &p
->ppid
);
529 for (i
= 0; i
< ARRAY_SIZE(p
->ns_ids
); i
++) {
530 INIT_LIST_HEAD(&p
->ns_siblings
[i
]);
532 if (!ls
->fltr_types
[i
])
535 rc
= get_ns_ino(dirfd(dir
), ns_names
[i
], &p
->ns_ids
[i
],
536 &p
->ns_pids
[i
], &p
->ns_oids
[i
]);
537 if (rc
&& rc
!= -EACCES
&& rc
!= -ENOENT
)
539 if (i
== LSNS_ID_NET
)
540 p
->netnsid
= get_netnsid(dirfd(dir
), p
->ns_ids
[i
]);
544 INIT_LIST_HEAD(&p
->processes
);
546 DBG(PROC
, ul_debugobj(p
, "new pid=%d", p
->pid
));
547 list_add_tail(&p
->processes
, &ls
->processes
);
557 static int read_processes(struct lsns
*ls
)
559 struct proc_processes
*proc
= NULL
;
563 DBG(PROC
, ul_debug("opening /proc"));
565 if (!(proc
= proc_open_processes())) {
570 while (proc_next_pid(proc
, &pid
) == 0) {
571 rc
= read_process(ls
, pid
);
572 if (rc
&& rc
!= -EACCES
&& rc
!= -ENOENT
)
577 DBG(PROC
, ul_debug("closing /proc"));
578 proc_close_processes(proc
);
582 static struct lsns_namespace
*get_namespace(struct lsns
*ls
, ino_t ino
)
586 list_for_each(p
, &ls
->namespaces
) {
587 struct lsns_namespace
*ns
= list_entry(p
, struct lsns_namespace
, namespaces
);
595 static int namespace_has_process(struct lsns_namespace
*ns
, pid_t pid
)
599 list_for_each(p
, &ns
->processes
) {
600 struct lsns_process
*proc
= list_entry(p
, struct lsns_process
, ns_siblings
[ns
->type
]);
602 if (proc
->pid
== pid
)
608 static struct lsns_namespace
*add_namespace(struct lsns
*ls
, int type
, ino_t ino
,
609 ino_t parent_ino
, ino_t owner_ino
)
611 struct lsns_namespace
*ns
= xcalloc(1, sizeof(*ns
));
616 DBG(NS
, ul_debugobj(ns
, "new %s[%ju]", ns_names
[type
], (uintmax_t)ino
));
618 INIT_LIST_HEAD(&ns
->processes
);
619 INIT_LIST_HEAD(&ns
->namespaces
);
623 ns
->parentid
= parent_ino
;
624 ns
->ownerid
= owner_ino
;
626 list_add_tail(&ns
->namespaces
, &ls
->namespaces
);
630 static int add_process_to_namespace(struct lsns
*ls
, struct lsns_namespace
*ns
, struct lsns_process
*proc
)
634 DBG(NS
, ul_debugobj(ns
, "add process [%p] pid=%d to %s[%ju]",
635 proc
, proc
->pid
, ns_names
[ns
->type
], (uintmax_t)ns
->id
));
637 list_for_each(p
, &ls
->processes
) {
638 struct lsns_process
*xproc
= list_entry(p
, struct lsns_process
, processes
);
640 if (xproc
->pid
== proc
->ppid
) /* my parent */
641 proc
->parent
= xproc
;
642 else if (xproc
->ppid
== proc
->pid
) /* my child */
643 xproc
->parent
= proc
;
646 list_add_tail(&proc
->ns_siblings
[ns
->type
], &ns
->processes
);
649 if (!ns
->proc
|| ns
->proc
->pid
> proc
->pid
)
655 static int cmp_namespaces(struct list_head
*a
, struct list_head
*b
,
656 __attribute__((__unused__
)) void *data
)
658 struct lsns_namespace
*xa
= list_entry(a
, struct lsns_namespace
, namespaces
),
659 *xb
= list_entry(b
, struct lsns_namespace
, namespaces
);
661 return cmp_numbers(xa
->id
, xb
->id
);
664 static int netnsid_xasputs(char **str
, int netnsid
)
667 return xasprintf(str
, "%d", netnsid
);
668 #ifdef NETNSA_NSID_NOT_ASSIGNED
669 if (netnsid
== NETNSA_NSID_NOT_ASSIGNED
)
670 return xasprintf(str
, "%s", "unassigned");
675 static int read_namespaces(struct lsns
*ls
)
679 DBG(NS
, ul_debug("reading namespace"));
681 list_for_each(p
, &ls
->processes
) {
683 struct lsns_namespace
*ns
;
684 struct lsns_process
*proc
= list_entry(p
, struct lsns_process
, processes
);
686 for (i
= 0; i
< ARRAY_SIZE(proc
->ns_ids
); i
++) {
687 if (proc
->ns_ids
[i
] == 0)
689 if (!(ns
= get_namespace(ls
, proc
->ns_ids
[i
]))) {
690 ns
= add_namespace(ls
, i
, proc
->ns_ids
[i
],
691 proc
->ns_pids
[i
], proc
->ns_oids
[i
]);
695 add_process_to_namespace(ls
, ns
, proc
);
699 if (ls
->tree
== LSNS_TREE_OWNER
|| ls
->tree
== LSNS_TREE_PARENT
) {
700 list_for_each(p
, &ls
->namespaces
) {
701 struct lsns_namespace
*ns
= list_entry(p
, struct lsns_namespace
, namespaces
);
702 struct list_head
*pp
;
703 list_for_each(pp
, &ls
->namespaces
) {
704 struct lsns_namespace
*pns
= list_entry(pp
, struct lsns_namespace
, namespaces
);
705 if (ns
->type
== LSNS_ID_USER
706 || ns
->type
== LSNS_ID_PID
) {
707 if (ns
->parentid
== pns
->id
)
709 if (ns
->ownerid
== pns
->id
)
711 if (ns
->parentns
&& ns
->ownerns
)
714 if (ns
->ownerid
== pns
->id
) {
723 list_sort(&ls
->namespaces
, cmp_namespaces
, NULL
);
728 static int is_nsfs_root(struct libmnt_fs
*fs
, void *data
)
730 if (!mnt_fs_match_fstype(fs
, "nsfs") || !mnt_fs_get_root(fs
))
733 return (strcmp(mnt_fs_get_root(fs
), (char *)data
) == 0);
736 static int is_path_included(const char *path_set
, const char *elt
,
744 tmp
= strstr(path_set
, elt
);
748 elt_len
= strlen(elt
);
749 path_set_len
= strlen(path_set
);
751 /* path_set includes only elt or
752 * path_set includes elt as the first element.
755 && ((path_set_len
== elt_len
)
756 || (path_set
[elt_len
] == sep
)))
759 /* path_set includes elt at the middle
760 * or as the last element.
762 if ((*(tmp
- 1) == sep
)
763 && ((*(tmp
+ elt_len
) == sep
)
764 || (*(tmp
+ elt_len
) == '\0')))
770 static int nsfs_xasputs(char **str
,
771 struct lsns_namespace
*ns
,
772 struct libmnt_table
*tab
,
775 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_FORWARD
);
777 struct libmnt_fs
*fs
= NULL
;
779 xasprintf(&expected_root
, "%s:[%ju]", ns_names
[ns
->type
], (uintmax_t)ns
->id
);
782 while (mnt_table_find_next_fs(tab
, itr
, is_nsfs_root
,
783 expected_root
, &fs
) == 0) {
785 const char *tgt
= mnt_fs_get_target(fs
);
788 xasprintf(str
, "%s", tgt
);
790 else if (!is_path_included(*str
, tgt
, sep
)) {
793 xasprintf(&tmp
, "%s%c%s", *str
, sep
, tgt
);
803 static void add_scols_line(struct lsns
*ls
, struct libscols_table
*table
,
804 struct lsns_namespace
*ns
, struct lsns_process
*proc
)
807 struct libscols_line
*line
;
812 line
= scols_table_new_line(table
,
813 (ls
->tree
== LSNS_TREE_PROCESS
) && proc
->parent
? proc
->parent
->outline
:
814 (ls
->tree
== LSNS_TREE_PARENT
) && ns
->parentns
? ns
->parentns
->ns_outline
:
815 (ls
->tree
== LSNS_TREE_OWNER
) && ns
->ownerns
? ns
->ownerns
->ns_outline
:
818 warn(_("failed to add line to output"));
822 for (i
= 0; i
< ncolumns
; i
++) {
825 switch (get_column_id(i
)) {
827 xasprintf(&str
, "%ju", (uintmax_t)ns
->id
);
830 xasprintf(&str
, "%d", (int) proc
->pid
);
833 xasprintf(&str
, "%d", (int) proc
->ppid
);
836 xasprintf(&str
, "%s", ns_names
[ns
->type
]);
839 xasprintf(&str
, "%d", ns
->nprocs
);
842 str
= proc_get_command(proc
->pid
);
844 str
= proc_get_command_name(proc
->pid
);
847 xasprintf(&str
, "/proc/%d/ns/%s", (int) proc
->pid
, ns_names
[ns
->type
]);
850 xasprintf(&str
, "%d", (int) proc
->uid
);
853 xasprintf(&str
, "%s", get_id(uid_cache
, proc
->uid
)->name
);
856 if (ns
->type
== LSNS_ID_NET
)
857 netnsid_xasputs(&str
, proc
->netnsid
);
860 nsfs_xasputs(&str
, ns
, ls
->tab
, ls
->no_wrap
? ',' : '\n');
863 xasprintf(&str
, "%ju", (uintmax_t)ns
->parentid
);
866 xasprintf(&str
, "%ju", (uintmax_t)ns
->ownerid
);
872 if (str
&& scols_line_refer_data(line
, i
, str
) != 0)
876 if (ls
->tree
== LSNS_TREE_OWNER
|| ls
->tree
== LSNS_TREE_PARENT
)
877 ns
->ns_outline
= line
;
879 proc
->outline
= line
;
882 static struct libscols_table
*init_scols_table(struct lsns
*ls
)
884 struct libscols_table
*tab
;
887 tab
= scols_new_table();
889 warn(_("failed to initialize output table"));
893 scols_table_enable_raw(tab
, ls
->raw
);
894 scols_table_enable_json(tab
, ls
->json
);
895 scols_table_enable_noheadings(tab
, ls
->no_headings
);
898 scols_table_set_name(tab
, "namespaces");
900 for (i
= 0; i
< ncolumns
; i
++) {
901 const struct colinfo
*col
= get_column_info(i
);
902 int flags
= col
->flags
;
903 struct libscols_column
*cl
;
906 flags
&= ~SCOLS_FL_TRUNC
;
907 if (ls
->tree
== LSNS_TREE_PROCESS
&& get_column_id(i
) == COL_COMMAND
)
908 flags
|= SCOLS_FL_TREE
;
910 flags
&= ~SCOLS_FL_WRAP
;
911 if ((ls
->tree
== LSNS_TREE_OWNER
|| ls
->tree
== LSNS_TREE_PARENT
)
912 && get_column_id(i
) == COL_NS
) {
913 flags
|= SCOLS_FL_TREE
;
914 flags
&= ~SCOLS_FL_RIGHT
;
917 cl
= scols_table_new_column(tab
, col
->name
, col
->whint
, flags
);
919 warnx(_("failed to initialize output column"));
923 scols_column_set_json_type(cl
, col
->json_type
);
925 if (!ls
->no_wrap
&& get_column_id(i
) == COL_NSFS
) {
926 scols_column_set_wrapfunc(cl
,
927 scols_wrapnl_chunksize
,
928 scols_wrapnl_nextchunk
,
930 scols_column_set_safechars(cl
, "\n");
936 scols_unref_table(tab
);
940 static void show_namespace(struct lsns
*ls
, struct libscols_table
*tab
,
941 struct lsns_namespace
*ns
, struct lsns_process
*proc
)
944 * create a tree from owner->owned and/or parent->child relation
946 if (ls
->tree
== LSNS_TREE_OWNER
948 && !ns
->ownerns
->ns_outline
)
949 show_namespace(ls
, tab
, ns
->ownerns
, ns
->ownerns
->proc
);
950 else if (ls
->tree
== LSNS_TREE_PARENT
) {
952 if (!ns
->parentns
->ns_outline
)
953 show_namespace(ls
, tab
, ns
->parentns
, ns
->parentns
->proc
);
955 else if (ns
->ownerns
&& !ns
->ownerns
->ns_outline
)
956 show_namespace(ls
, tab
, ns
->ownerns
, ns
->ownerns
->proc
);
959 add_scols_line(ls
, tab
, ns
, proc
);
962 static int show_namespaces(struct lsns
*ls
)
964 struct libscols_table
*tab
;
968 tab
= init_scols_table(ls
);
972 list_for_each(p
, &ls
->namespaces
) {
973 struct lsns_namespace
*ns
= list_entry(p
, struct lsns_namespace
, namespaces
);
975 if (ls
->fltr_pid
!= 0 && !namespace_has_process(ns
, ls
->fltr_pid
))
979 show_namespace(ls
, tab
, ns
, ns
->proc
);
982 scols_print_table(tab
);
983 scols_unref_table(tab
);
987 static void show_process(struct lsns
*ls
, struct libscols_table
*tab
,
988 struct lsns_process
*proc
, struct lsns_namespace
*ns
)
991 * create a tree from parent->child relation, but only if the parent is
992 * within the same namespace
994 if (ls
->tree
== LSNS_TREE_PROCESS
996 && !proc
->parent
->outline
997 && proc
->parent
->ns_ids
[ns
->type
] == proc
->ns_ids
[ns
->type
])
998 show_process(ls
, tab
, proc
->parent
, ns
);
1000 add_scols_line(ls
, tab
, ns
, proc
);
1004 static int show_namespace_processes(struct lsns
*ls
, struct lsns_namespace
*ns
)
1006 struct libscols_table
*tab
;
1007 struct list_head
*p
;
1009 tab
= init_scols_table(ls
);
1013 list_for_each(p
, &ns
->processes
) {
1014 struct lsns_process
*proc
= list_entry(p
, struct lsns_process
, ns_siblings
[ns
->type
]);
1017 show_process(ls
, tab
, proc
, ns
);
1021 scols_print_table(tab
);
1022 scols_unref_table(tab
);
1026 static void __attribute__((__noreturn__
)) usage(void)
1031 fputs(USAGE_HEADER
, out
);
1034 _(" %s [options] [<namespace>]\n"), program_invocation_short_name
);
1036 fputs(USAGE_SEPARATOR
, out
);
1037 fputs(_("List system namespaces.\n"), out
);
1039 fputs(USAGE_OPTIONS
, out
);
1040 fputs(_(" -J, --json use JSON output format\n"), out
);
1041 fputs(_(" -l, --list use list format output\n"), out
);
1042 fputs(_(" -n, --noheadings don't print headings\n"), out
);
1043 fputs(_(" -o, --output <list> define which output columns to use\n"), out
);
1044 fputs(_(" --output-all output all columns\n"), out
);
1045 fputs(_(" -p, --task <pid> print process namespaces\n"), out
);
1046 fputs(_(" -r, --raw use the raw output format\n"), out
);
1047 fputs(_(" -u, --notruncate don't truncate text in columns\n"), out
);
1048 fputs(_(" -W, --nowrap don't use multi-line representation\n"), out
);
1049 fputs(_(" -t, --type <name> namespace type (mnt, net, ipc, user, pid, uts, cgroup, time)\n"), out
);
1050 fputs(_(" -T, --tree <rel> use tree format (parent, owner, or process)\n"), out
);
1052 fputs(USAGE_SEPARATOR
, out
);
1053 printf(USAGE_HELP_OPTIONS(24));
1055 fputs(USAGE_COLUMNS
, out
);
1056 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
1057 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1059 printf(USAGE_MAN_TAIL("lsns(8)"));
1065 int main(int argc
, char *argv
[])
1068 int c
, force_list
= 0;
1070 char *outarg
= NULL
;
1072 OPT_OUTPUT_ALL
= CHAR_MAX
+ 1
1074 static const struct option long_opts
[] = {
1075 { "json", no_argument
, NULL
, 'J' },
1076 { "task", required_argument
, NULL
, 'p' },
1077 { "help", no_argument
, NULL
, 'h' },
1078 { "output", required_argument
, NULL
, 'o' },
1079 { "output-all", no_argument
, NULL
, OPT_OUTPUT_ALL
},
1080 { "notruncate", no_argument
, NULL
, 'u' },
1081 { "version", no_argument
, NULL
, 'V' },
1082 { "noheadings", no_argument
, NULL
, 'n' },
1083 { "nowrap", no_argument
, NULL
, 'W' },
1084 { "list", no_argument
, NULL
, 'l' },
1085 { "raw", no_argument
, NULL
, 'r' },
1086 { "type", required_argument
, NULL
, 't' },
1087 { "tree", optional_argument
, NULL
, 'T' },
1088 { NULL
, 0, NULL
, 0 }
1091 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1096 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1099 setlocale(LC_ALL
, "");
1100 bindtextdomain(PACKAGE
, LOCALEDIR
);
1101 textdomain(PACKAGE
);
1102 close_stdout_atexit();
1105 memset(&ls
, 0, sizeof(ls
));
1107 INIT_LIST_HEAD(&ls
.processes
);
1108 INIT_LIST_HEAD(&ls
.namespaces
);
1109 INIT_LIST_HEAD(&netnsids_cache
);
1111 while ((c
= getopt_long(argc
, argv
,
1112 "Jlp:o:nruhVt:T::W", long_opts
, NULL
)) != -1) {
1114 err_exclusive_options(c
, long_opts
, excl
, excl_st
);
1126 case OPT_OUTPUT_ALL
:
1127 for (ncolumns
= 0; ncolumns
< ARRAY_SIZE(infos
); ncolumns
++)
1128 columns
[ncolumns
] = ncolumns
;
1131 ls
.fltr_pid
= strtos32_or_err(optarg
, _("invalid PID argument"));
1137 ls
.no_wrap
= ls
.raw
= 1;
1144 int type
= ns_name2type(optarg
);
1146 errx(EXIT_FAILURE
, _("unknown namespace type: %s"), optarg
);
1147 ls
.fltr_types
[type
] = 1;
1149 if (type
== LSNS_ID_NET
)
1157 ls
.tree
= LSNS_TREE_OWNER
;
1161 if (strcmp (optarg
, "parent") == 0)
1162 ls
.tree
= LSNS_TREE_PARENT
;
1163 else if (strcmp (optarg
, "process") == 0)
1164 ls
.tree
= LSNS_TREE_PROCESS
;
1165 else if (strcmp (optarg
, "owner") != 0)
1166 errx(EXIT_FAILURE
, _("unknown nstree type: %s"), optarg
);
1173 print_version(EXIT_SUCCESS
);
1175 errtryhelp(EXIT_FAILURE
);
1179 if (!ls
.fltr_ntypes
) {
1182 for (i
= 0; i
< ARRAY_SIZE(ns_names
); i
++)
1183 ls
.fltr_types
[i
] = 1;
1186 if (optind
< argc
) {
1188 errx(EXIT_FAILURE
, _("--task is mutually exclusive with <namespace>"));
1189 ls
.fltr_ns
= strtou64_or_err(argv
[optind
], _("invalid namespace argument"));
1190 if (!ls
.tree
&& !force_list
)
1191 ls
.tree
= LSNS_TREE_PROCESS
;
1194 columns
[ncolumns
++] = COL_PID
;
1195 columns
[ncolumns
++] = COL_PPID
;
1196 columns
[ncolumns
++] = COL_USER
;
1197 columns
[ncolumns
++] = COL_COMMAND
;
1202 columns
[ncolumns
++] = COL_NS
;
1203 columns
[ncolumns
++] = COL_TYPE
;
1204 columns
[ncolumns
++] = COL_NPROCS
;
1205 columns
[ncolumns
++] = COL_PID
;
1206 columns
[ncolumns
++] = COL_USER
;
1208 columns
[ncolumns
++] = COL_NETNSID
;
1209 columns
[ncolumns
++] = COL_NSFS
;
1211 columns
[ncolumns
++] = COL_COMMAND
;
1213 if (!ls
.tree
&& !force_list
)
1214 ls
.tree
= LSNS_TREE_PROCESS
;
1217 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1218 &ncolumns
, column_name_to_id
) < 0)
1219 return EXIT_FAILURE
;
1221 scols_init_debug(0);
1223 uid_cache
= new_idcache();
1225 err(EXIT_FAILURE
, _("failed to allocate UID cache"));
1227 #ifdef HAVE_LINUX_NET_NAMESPACE_H
1228 if (has_column(COL_NETNSID
))
1229 netlink_fd
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
1231 if (has_column(COL_NSFS
)) {
1232 ls
.tab
= mnt_new_table_from_file(_PATH_PROC_MOUNTINFO
);
1234 err(MNT_EX_FAIL
, _("failed to parse %s"), _PATH_PROC_MOUNTINFO
);
1237 r
= read_processes(&ls
);
1239 r
= read_namespaces(&ls
);
1242 struct lsns_namespace
*ns
= get_namespace(&ls
, ls
.fltr_ns
);
1245 errx(EXIT_FAILURE
, _("not found namespace: %ju"), (uintmax_t) ls
.fltr_ns
);
1246 r
= show_namespace_processes(&ls
, ns
);
1248 r
= show_namespaces(&ls
);
1251 mnt_free_table(ls
.tab
);
1252 if (netlink_fd
>= 0)
1254 free_idcache(uid_cache
);
1255 return r
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;