[lxc-devel] [lxc/lxc] 7f3c1c: lxc-ls: check for ENOMEM and tweaking

GitHub noreply at github.com
Thu Jan 28 11:33:33 UTC 2016


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

  Changed paths:
    M src/lxc/lxc_ls.c

  Log Message:
  -----------
  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 calls
  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. This pointer 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 at mailbox.org>
Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>


  Commit: e4434e0fc99631036b1fdabe7216e0a1218f4f3f
      https://github.com/lxc/lxc/commit/e4434e0fc99631036b1fdabe7216e0a1218f4f3f
  Author: Christian Brauner <christianvanbrauner at gmail.com>
  Date:   2016-01-28 (Thu, 28 Jan 2016)

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

  Log Message:
  -----------
  lxc-ls: Restore old [filter] behaviour

In the Python implementation users could pass a regex without a parameter flag
as additional argument on the command line. The C implementation gained the
flag -r/--regex for this. To not irritate users we restore the old behaviour
and additionally rename -r/--regex to --filter to allow eplicitly passing the
regex.

Signed-off-by: Christian Brauner <christian.brauner at mailbox.org>
Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>


  Commit: adbc2d3389d2b5c6d43938a40fb7e7e1285ec6b3
      https://github.com/lxc/lxc/commit/adbc2d3389d2b5c6d43938a40fb7e7e1285ec6b3
  Author: Stéphane Graber <stgraber at ubuntu.com>
  Date:   2016-01-28 (Thu, 28 Jan 2016)

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

  Log Message:
  -----------
  Revert "lxc-ls: Restore old [filter] behaviour"

This reverts commit e4434e0fc99631036b1fdabe7216e0a1218f4f3f.


  Commit: a8459b91f62ead915bf5926b2e902557c34f82f0
      https://github.com/lxc/lxc/commit/a8459b91f62ead915bf5926b2e902557c34f82f0
  Author: Stéphane Graber <stgraber at ubuntu.com>
  Date:   2016-01-28 (Thu, 28 Jan 2016)

  Changed paths:
    M src/lxc/lxc_ls.c

  Log Message:
  -----------
  Revert "lxc-ls: check for ENOMEM and tweaking"

This reverts commit 7f3c1cf27f1fcd29b5e7f0b11e4aadfadd5f18ec.


  Commit: 07385df53e918d31faf029de41d4b5b2d7766b5c
      https://github.com/lxc/lxc/commit/07385df53e918d31faf029de41d4b5b2d7766b5c
  Author: Christian Brauner <christian.brauner at mailbox.org>
  Date:   2016-01-28 (Thu, 28 Jan 2016)

  Changed paths:
    M src/lxc/lxc_ls.c

  Log Message:
  -----------
  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 at mailbox.org>
Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>


  Commit: 4763f6cade7ac1d1d1433276bbaf41aaf57ff9b3
      https://github.com/lxc/lxc/commit/4763f6cade7ac1d1d1433276bbaf41aaf57ff9b3
  Author: Christian Brauner <christian.brauner at mailbox.org>
  Date:   2016-01-28 (Thu, 28 Jan 2016)

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

  Log Message:
  -----------
  Restore old behaviour [filter] behaviour

In the Python implementation users could pass a regex without a parameter flag
as additional argument on the command line. The C implementation gained the
flag -r/--regex for this. To not irritate users we restore the old behaviour
and additionally rename -r/--regex to --filter to allow eplicitly passing the
regex.

Signed-off-by: Christian Brauner <christian.brauner at mailbox.org>
Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>


  Commit: fa659172a1270ecb98896ae00f630bf864e883e4
      https://github.com/lxc/lxc/commit/fa659172a1270ecb98896ae00f630bf864e883e4
  Author: Christian Brauner <christian.brauner at mailbox.org>
  Date:   2016-01-28 (Thu, 28 Jan 2016)

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

  Log Message:
  -----------
  lxc-ls: tweak algorithm for ls_has_all_grps()

- With the -g/--groups argument the user can give a comma-separated list of
  groups MUST a container must have in order to be displayed. We receive
  this list as a single string. ls_has_all_grps() is called to check if a
  container has all the groups of MUST in its current list of groups HAS. I.e.
  we determine whether MUST ⊆ HAS and only then do we record the container.
  The original implementation was dumb in that it split the string MUST
  everytime it needed to check whether MUST ⊆ HAS for a given container. That's
  pointless work. Instead we split the string MUST only once in main() and pass
  it to ls_get() which passes it along to ls_has_all_grps().
- Before doing any costly checking make sure that #MUST <= #HAS. If not bail
  immediately.
- The linear search algorithm ls_has_all_grps() currently uses stays for now.
  Binary search et al. do not seem to make sense since sorting the array HAS
  for each container is probably too costly. Especially, since it seems
  unlikely that a users specifies 50+ or so groups on the command line a
  container must have to be displayed. If however there are a lot of use-cases
  where users have a lot of containers each with 50-100 groups and regularly use
  lxc-ls with -g/--groups to only show containers that have 50 specified groups
  among their 50-100 groups we can revisit this issue and implement e.g. binary
  search or a ternary search tree.

Signed-off-by: Christian Brauner <christian.brauner at mailbox.org>
Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>


Compare: https://github.com/lxc/lxc/compare/280d23796c0a...fa659172a127


More information about the lxc-devel mailing list