[lxc-devel] [lxc/master] tools: add UNPRIVILEGED field in fancy output mode

brauner on Github lxc-bot at linuxcontainers.org
Sat Dec 16 12:11:38 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20171216/b1dc3680/attachment.bin>
-------------- next part --------------
From 72c94ff968f66081bd52b7bdf6758e87917925f0 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 16 Dec 2017 13:10:47 +0100
Subject: [PATCH] tools: add UNPRIVILEGED field in fancy output mode

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/tools/lxc_ls.c | 61 +++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 46 insertions(+), 15 deletions(-)

diff --git a/src/lxc/tools/lxc_ls.c b/src/lxc/tools/lxc_ls.c
index c7f25cbfe..6f9719a86 100644
--- a/src/lxc/tools/lxc_ls.c
+++ b/src/lxc/tools/lxc_ls.c
@@ -71,6 +71,7 @@ struct ls {
 	double swap;
 	bool autostart;
 	bool running;
+	bool unprivileged;
 };
 
 /* Keep track of field widths for printing. */
@@ -85,6 +86,7 @@ struct lengths {
 	unsigned int ram_length;
 	unsigned int swap_length;
 	unsigned int autostart_length;
+	unsigned int unprivileged_length;
 };
 
 static int ls_deserialize(int rpipefd, struct ls **m, size_t *len);
@@ -187,7 +189,7 @@ Options :\n\
   -f, --fancy        use a fancy, column-based output\n\
   -F, --fancy-format comma separated list of columns to show in the fancy output\n\
                      valid columns are: NAME, STATE, PID, RAM, SWAP, AUTOSTART,\n\
-                     GROUPS, INTERFACE, IPV4 and IPV6\n\
+                     GROUPS, INTERFACE, IPV4 and IPV6, UNPRIVILEGED\n\
   --active           list only active containers\n\
   --running          list only running containers\n\
   --frozen           list only frozen containers\n\
@@ -236,16 +238,17 @@ int main(int argc, char *argv[])
 
 	struct lengths max_len = {
 		/* default header length */
-		.name_length = 4,      /* NAME */
-		.state_length = 5,     /* STATE */
-		.groups_length = 6,    /* GROUPS */
-		.interface_length = 9, /* INTERFACE */
-		.ipv4_length = 4,      /* IPV4 */
-		.ipv6_length = 4,      /* IPV6 */
-		.init_length = 3,      /* PID */
-		.ram_length = 3,       /* RAM */
-		.swap_length = 4,      /* SWAP */
-		.autostart_length = 9, /* AUTOSTART */
+		.name_length = 4,          /* NAME */
+		.state_length = 5,         /* STATE */
+		.groups_length = 6,        /* GROUPS */
+		.interface_length = 9,     /* INTERFACE */
+		.ipv4_length = 4,          /* IPV4 */
+		.ipv6_length = 4,          /* IPV6 */
+		.init_length = 3,          /* PID */
+		.ram_length = 3,           /* RAM */
+		.swap_length = 4,          /* SWAP */
+		.autostart_length = 9,     /* AUTOSTART */
+		.unprivileged_length = 12, /* UNPRIVILEGED */
 	};
 
 	char **grps = NULL;
@@ -481,7 +484,11 @@ static int ls_get(struct ls **m, size_t *size, const struct lxc_arguments *args,
 			free(tmp);
 
 			if (running) {
+				char *val;
+
 				l->init = c->init_pid(c);
+				if (l->init <= 0)
+					goto put_and_next;
 
 				l->interface = ls_get_interface(c);
 
@@ -497,6 +504,15 @@ static int ls_get(struct ls **m, size_t *size, const struct lxc_arguments *args,
 				}
 
 				l->swap = ls_get_swap(c);
+
+				val = c->get_running_config_item(c, "lxc.idmap");
+				l->unprivileged = (val == NULL);
+				free(val);
+			} else {
+				int ret;
+
+				ret = c->get_config_item(c, "lxc.idmap", NULL, 0);
+				l->unprivileged = (ret == 0);
 			}
 		}
 
@@ -813,10 +829,11 @@ static void ls_print_fancy_format(struct ls *l, struct lengths *lht,
 	/* Check for invalid keys. */
 	for (s = tmp; s && *s; s++) {
 		if (strcasecmp(*s, "NAME") && strcasecmp(*s, "STATE") &&
-				strcasecmp(*s, "PID") && strcasecmp(*s, "RAM") &&
-				strcasecmp(*s, "SWAP") && strcasecmp(*s, "AUTOSTART") &&
-				strcasecmp(*s, "GROUPS") && strcasecmp(*s, "INTERFACE") &&
-				strcasecmp(*s, "IPV4") && strcasecmp(*s, "IPV6")) {
+		    strcasecmp(*s, "PID") && strcasecmp(*s, "RAM") &&
+		    strcasecmp(*s, "SWAP") && strcasecmp(*s, "AUTOSTART") &&
+		    strcasecmp(*s, "GROUPS") && strcasecmp(*s, "INTERFACE") &&
+		    strcasecmp(*s, "IPV4") && strcasecmp(*s, "IPV6") &&
+		    strcasecmp(*s, "UNPRIVILEGED")) {
 			fprintf(stderr, "Invalid key: %s\n", *s);
 			return;
 		}
@@ -844,6 +861,8 @@ static void ls_print_fancy_format(struct ls *l, struct lengths *lht,
 			printf("%-*s ", lht->ipv4_length, "IPV4");
 		else if (strcasecmp(*s, "IPV6") == 0)
 			printf("%-*s ", lht->ipv6_length, "IPV6");
+		else if (strcasecmp(*s, "UNPRIVILEGED") == 0)
+			printf("%-*s ", lht->unprivileged_length, "UNPRIVILEGED");
 	}
 	printf("\n");
 
@@ -885,6 +904,8 @@ static void ls_print_fancy_format(struct ls *l, struct lengths *lht,
 				printf("%-*s ", lht->ipv4_length, m->ipv4 ? m->ipv4 : "-");
 			} else if (strcasecmp(*s, "IPV6") == 0) {
 				printf("%-*s ", lht->ipv6_length, m->ipv6 ? m->ipv6 : "-");
+			} else if (strcasecmp(*s, "UNPRIVILEGED") == 0) {
+				printf("%-*s ", lht->unprivileged_length, m->unprivileged ? "true" : "false");
 			}
 		}
 		printf("\n");
@@ -907,6 +928,7 @@ static void ls_print_table(struct ls *l, struct lengths *lht,
 	printf("%-*s ", lht->groups_length, "GROUPS");
 	printf("%-*s ", lht->ipv4_length, "IPV4");
 	printf("%-*s ", lht->ipv6_length, "IPV6");
+	printf("%-*s ", lht->unprivileged_length, "UNPRIVILEGED");
 	printf("\n");
 
 	size_t i;
@@ -922,6 +944,7 @@ static void ls_print_table(struct ls *l, struct lengths *lht,
 		printf("%-*s ", lht->groups_length, m->groups ? m->groups : "-");
 		printf("%-*s ", lht->ipv4_length, m->ipv4 ? m->ipv4 : "-");
 		printf("%-*s ", lht->ipv6_length, m->ipv6 ? m->ipv6 : "-");
+		printf("%-*s ", lht->unprivileged_length, m->unprivileged ? "true" : "false");
 		printf("\n");
 	}
 }
@@ -1093,6 +1116,10 @@ static int ls_serialize(int wpipefd, struct ls *n)
 	if (lxc_write_nointr(wpipefd, &n->running, (size_t)nbytes) != nbytes)
 		return -1;
 
+	nbytes = sizeof(n->unprivileged);
+	if (lxc_write_nointr(wpipefd, &n->unprivileged, (size_t)nbytes) != nbytes)
+		return -1;
+
 	nbytes = sizeof(n->nestlvl);
 	if (lxc_write_nointr(wpipefd, &n->nestlvl, (size_t)nbytes) != nbytes)
 		return -1;
@@ -1176,6 +1203,10 @@ static int ls_deserialize(int rpipefd, struct ls **m, size_t *len)
 		if (lxc_read_nointr(rpipefd, &n->running, (size_t)nbytes) != nbytes)
 			return -1;
 
+		nbytes = sizeof(n->unprivileged);
+		if (lxc_read_nointr(rpipefd, &n->unprivileged, (size_t)nbytes) != nbytes)
+			return -1;
+
 		nbytes = sizeof(n->nestlvl);
 		if (lxc_read_nointr(rpipefd, &n->nestlvl, (size_t)nbytes) != nbytes)
 			return -1;


More information about the lxc-devel mailing list