]> git.ipfire.org Git - thirdparty/lxc.git/commit
reimplement lxc-ls in C 748/head
authorChristian Brauner <christian.brauner@mailbox.org>
Mon, 14 Dec 2015 20:25:10 +0000 (21:25 +0100)
committerChristian Brauner <christian.brauner@mailbox.org>
Wed, 13 Jan 2016 16:06:23 +0000 (17:06 +0100)
commit15fd209a88dc1a93492c5b412eb615485a8cf78e
tree20d56182e1b6f69a5fceb9906d0c3209ef4c9017
parent449710f84a08acfb1f9492103cfdb811107b7967
reimplement lxc-ls in C

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>
src/lxc/Makefile.am
src/lxc/arguments.h
src/lxc/lxc_ls.c [new file with mode: 0644]