Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lxc-ls: check for ENOMEM and tweaking
- If lxc_container_new() fails we check for ENOMEM and if so goto out. If ENOMEM is not set we will simply continue. The same goes for the call to regcomp() but instead of checking for ENOMEM we need to check for REG_ESPACE. - Tweaking: Since lxc-ls might have to gather a lot of containers and I don't know if compilers will always optimize this let's move *some* variable declarations outside of the loop when it does not hinder readability - Set ls_nesting to 0 initially. Otherwise users will always see nested containers printed. - ls_get() gains an argument char **lockpath which is a string pointing us to the lock we put under /run/lxc/lock/.../... so that we can remove the lock when we no longer need it. To avoid pointless memory allocation in each new recursion level we share lockpath amongst all non-fork()ing recursive call to ls_get(). As it is not guaranteed that realloc() does not do any memory moving when newlen == len_lockpath, we give ls_get() an additional argument size_t len_lockpath). Every time we have a non-fork()ing recursive call to ls_get() we check if newlen > len_lockpath and only then do we realloc(*lockpath, newlen * 2) a reasonable chunk of memory (as the path will keep growing) and set len_lockpath = newlen * 2 to pass to the next non-fork()ing recursive call to ls_get(). To avoid keeping a variable char *lockpath in main() which serves no purpose whatsoever and might be abused later we use a compound literal &(char *){NULL} which gives us an anonymous pointer which we can use for memory allocation in ls_get() for lockpath. We can conveniently free() it in ls_get() when the nesting level parameter lvl == 0 after exiting the loop. The advantage is that the variable is only accessible within ls_get() and not in main() while at the same time giving us an easy way to share lockpath amongst all non-fork()ing recursive calls to ls_get(). Signed-off-by: Christian Brauner <christian.brauner@mailbox.org> Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
- Loading branch information
Showing
1 changed file
with
72 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters