Skip to content

Commit

Permalink
attach, utils: bugfixes
Browse files Browse the repository at this point in the history
- simply check /proc/self/ns
- improve SYSERROR() report
- use #define to prevent gcc & clang to use a VLA

Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
  • Loading branch information
Christian Brauner authored and stgraber committed Nov 22, 2016
1 parent a2f2695 commit f23504a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/lxc/attach.c
Expand Up @@ -212,7 +212,6 @@ static void lxc_proc_put_context_info(struct lxc_proc_context_info *ctx)

static int lxc_attach_to_ns(pid_t pid, int which)
{
char path[MAXPATHLEN];
/* according to <http://article.gmane.org/gmane.linux.kernel.containers.lxc.devel/1429>,
* the file for user namespaces in /proc/$pid/ns will be called
* 'user' once the kernel supports it
Expand All @@ -227,8 +226,7 @@ static int lxc_attach_to_ns(pid_t pid, int which)
int i, j, saved_errno;


snprintf(path, MAXPATHLEN, "/proc/%d/ns", pid);
if (access(path, X_OK)) {
if (access("/proc/self/ns", X_OK)) {
ERROR("Does this kernel version support 'attach' ?");
return -1;
}
Expand All @@ -253,7 +251,7 @@ static int lxc_attach_to_ns(pid_t pid, int which)
close(fd[j]);

errno = saved_errno;
SYSERROR("failed to open '%s'", path);
SYSERROR("failed to open namespace: '%s'.", ns[i]);
return -1;
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/lxc/utils.c
Expand Up @@ -1972,17 +1972,18 @@ int lxc_append_string(char ***list, char *entry)
int lxc_preserve_ns(const int pid, const char *ns)
{
int ret;
size_t len = 5 /* /proc */ + 21 /* /int_as_str */ + 3 /* /ns */ + 20 /* /NS_NAME */ + 1 /* \0 */;
char path[len];
/* 5 /proc + 21 /int_as_str + 3 /ns + 20 /NS_NAME + 1 \0 */
#define __NS_PATH_LEN 50
char path[__NS_PATH_LEN];

/* This way we can use this function to also check whether namespaces
* are supported by the kernel by passing in the NULL or the empty
* string.
*/
ret = snprintf(path, len, "/proc/%d/ns%s%s", pid,
ret = snprintf(path, __NS_PATH_LEN, "/proc/%d/ns%s%s", pid,
!ns || strcmp(ns, "") == 0 ? "" : "/",
!ns || strcmp(ns, "") == 0 ? "" : ns);
if (ret < 0 || (size_t)ret >= len)
if (ret < 0 || (size_t)ret >= __NS_PATH_LEN)
return -1;

return open(path, O_RDONLY | O_CLOEXEC);
Expand Down

0 comments on commit f23504a

Please sign in to comment.