Skip to content

Commit

Permalink
reimplement lxc-ls in C
Browse files Browse the repository at this point in the history
This is a reimplementation of lxc-ls in C. It supports all features previously
supported by lxc-ls.

- All flags and parameters have the same name as before except when the user
  specifies a regex to filter container names by. In the previous Python
  implementation the regex was passed without paramter flag. The new
  C-implementation has the parameter flag -r/--regex for this.

- Since we fork in lxc_attach() we need some form of IPC. Opening shared memory
  in the parent (mmap()) seems to be impractical since we don't know the size
  of the mapping beforehand. The other option is to open shared memory in the
  child and then to attach the parent to it but then we would need to resort to
  shm_open() or shmget(). Instead we go for a socketpair() here and wait for
  the child.
- Note that we call lxc_attach() and pass ls_get() as exec function to it (To
  be even more specific: We do not pass ls_get() directly but rather a wrapper
  function for ls_get() which receives a few arguments to enable the
  communication between child and parent.). This implementation has the
  advantage that we do not depend on any lxc executables being present in the
  container. The gist in code:

	ls_get()
	{
		/* Gather all relevant information */

		/* get nested containers */
		if (args->ls_nested && running) {
			/* set up some more stuff */

			/*
			 * execute ls_get() in namespace of the container to
 			 * get nested containers
			 */
  			c->attach(c, ls_get_wrapper, &wrapargs, &aopt, &out)

			/* do some cleaning up */
		}
	}

- When the user requests listing of nested containers without fancy-format
  enabled we want him to easily recognize which container is nested in which.
  So in this case we do not simply record the name but rather the name
  prepended with all the parents of the container:

	grand-grand-parent/grand-parent/parent/child

- Pretty-printing nested containers: Any call to list_*_containers() will
  return a sorted array of container names.  Furthermore, the recursive
  implementation of lxc_ls() will automatically put the containers in the
  correct order regarding their nesting. That is if we have the following
  nesting:

	A
	A --> S
	A --> T --> O
	A --> T --> O --> L
	A --> T --> O --> M
	A --> U
	A --> U --> P
	A --> U --> Q
	B

  The array ls_get() will set up looks like this:
	A S T O L M U P Q B

  Hence, we only need to keep an additional variable nestlvl to indicate the
  nesting level a container is at and use that to compute (a) the maximum field
  width we need to print out the container names and (b) to correctly indent
  each container according to its nesting level when printing it.

- add comments to make the ls_get() function more accessible

Signed-off-by: Christian Brauner <christian.brauner@mailbox.org>
  • Loading branch information
Christian Brauner committed Jan 13, 2016
1 parent 449710f commit 15fd209
Show file tree
Hide file tree
Showing 3 changed files with 1,176 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lxc/Makefile.am
Expand Up @@ -215,6 +215,7 @@ bin_PROGRAMS = \
lxc-execute \
lxc-freeze \
lxc-info \
lxc-ls \
lxc-monitor \
lxc-snapshot \
lxc-start \
Expand Down Expand Up @@ -250,6 +251,7 @@ init_lxc_SOURCES = lxc_init.c
lxc_monitor_SOURCES = lxc_monitor.c
lxc_monitord_SOURCES = lxc_monitord.c
lxc_clone_SOURCES = lxc_clone.c
lxc_ls_SOURCES = lxc_ls.c
lxc_copy_SOURCES = lxc_copy.c
lxc_start_SOURCES = lxc_start.c
lxc_stop_SOURCES = lxc_stop.c
Expand Down
15 changes: 15 additions & 0 deletions src/lxc/arguments.h
Expand Up @@ -21,11 +21,14 @@
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef __LXC_ARGUMENTS_H
#define __LXC_ARGUMENTS_H

#include <getopt.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>

struct lxc_arguments;

Expand Down Expand Up @@ -117,6 +120,18 @@ struct lxc_arguments {
int keepname;
int keepmac;

/* lxc-ls */
char *ls_fancy_format;
char *ls_groups;
char *ls_regex;
bool ls_active;
bool ls_fancy;
bool ls_frozen;
bool ls_line;
bool ls_nesting;
bool ls_running;
bool ls_stopped;

/* remaining arguments */
char *const *argv;
int argc;
Expand Down

0 comments on commit 15fd209

Please sign in to comment.