[lxc-devel] [lxc/lxc] 15fd20: reimplement lxc-ls in C

GitHub noreply at github.com
Wed Jan 13 17:33:14 UTC 2016


  Branch: refs/heads/master
  Home:   https://github.com/lxc/lxc
  Commit: 15fd209a88dc1a93492c5b412eb615485a8cf78e
      https://github.com/lxc/lxc/commit/15fd209a88dc1a93492c5b412eb615485a8cf78e
  Author: Christian Brauner <christian.brauner at mailbox.org>
  Date:   2016-01-13 (Wed, 13 Jan 2016)

  Changed paths:
    M src/lxc/Makefile.am
    M src/lxc/arguments.h
    A src/lxc/lxc_ls.c

  Log Message:
  -----------
  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 at mailbox.org>


  Commit: a571193241b6162980df4d5e90aad453de4cac44
      https://github.com/lxc/lxc/commit/a571193241b6162980df4d5e90aad453de4cac44
  Author: Serge Hallyn <serge at hallyn.com>
  Date:   2016-01-13 (Wed, 13 Jan 2016)

  Changed paths:
    M src/lxc/Makefile.am
    M src/lxc/arguments.h
    A src/lxc/lxc_ls.c

  Log Message:
  -----------
  Merge pull request #748 from brauner/2015-01-01/lxc_ls

Reimplement lxc-ls in C


Compare: https://github.com/lxc/lxc/compare/5f4aafaccbd7...a571193241b6


More information about the lxc-devel mailing list