Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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