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
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;
}
close(fd[j]);
errno = saved_errno;
- SYSERROR("failed to open '%s'", path);
+ SYSERROR("failed to open namespace: '%s'.", ns[i]);
return -1;
}
}
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);