#include <lib/vserver.h>
#include <lib/internal.h>
-#include <lib_internal/sys_unshare.h>
+#include <lib_internal/sys_clone.h>
#include <sys/socket.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
-#include <sched.h>
+
// from selinux.h
// FIXME: add configure autodetection and include <selinux.h> directly
return res;
}
+struct ExecvParams
+{
+ char const * path;
+ char * const * argv;
+ char * const * envp;
+ char const * mnts;
+};
+
static int
-removeNamespaceMounts(char const *mnts)
+removeNamespaceMountsChild(struct ExecvParams const *params)
{
- char buf[strlen(mnts)+1], *ptr;
+ char buf[strlen(params->mnts)+1], *ptr;
- strcpy(buf, mnts);
+ strcpy(buf, params->mnts);
ptr = strtok(buf, ":");
while (ptr) {
if (umount2(ptr, 0)==-1) {
ptr = strtok(0, ":");
}
- return 0;
+ return execvWorker(params->path, params->argv, params->envp);
}
static int
-execv_main(char const *path, char * const argv[], char * const envp[])
+removeNamespaceMounts(char const *path,
+ char * const argv[], char * const envp[])
{
- int rc = 0;
+ if (mnts==0) return execvWorker(path, argv, envp);
- if (sys_unshare(CLONE_NEWNS)==-1) {
- perror("unshare()");
- return -1;
- }
+ {
+ int status;
+ pid_t p, pid;
+ struct ExecvParams params;
- if (mnts)
- rc = removeNamespaceMounts(mnts);
+ params.path = path;
+ params.argv = argv;
+ params.envp = envp;
+ params.mnts = mnts;
- if (rc!=0)
- return rc;
-
- return execvWorker(path, argv, envp);
+ // the rpmlib signal-handler is still active; use the default one to
+ // make wait4() working...
+ signal(SIGCHLD, SIG_DFL);
+
+#ifdef NDEBUG
+ pid = sys_clone(CLONE_NEWNS|SIGCHLD|CLONE_VFORK, 0);
+#else
+ pid = sys_clone(CLONE_NEWNS|SIGCHLD, 0);
+#endif
+
+ switch (pid) {
+ case -1 : return -1;
+ case 0 : _exit(removeNamespaceMountsChild(¶ms));
+ default : break;
+ }
+
+ while ((p=wait4(pid, &status, 0,0))==-1 &&
+ (errno==EINTR || errno==EAGAIN)) ;
+
+ if (p==-1) return -1;
+
+ if (WIFEXITED(status)) _exit(WEXITSTATUS(status));
+ if (WIFSIGNALED(status)) kill(getpid(), WTERMSIG(status));
+
+ return -1;
+ }
}
+
int
execv(char const *path, char * const argv[])
{
WRITE_MSG(2, "', ...)\n");
}
- return execv_main(path, argv, environ);
+ return removeNamespaceMounts(path, argv, environ);
}
int
WRITE_MSG(2, "', ...)\n");
}
- return execv_main(filename, argv, envp);
+ return removeNamespaceMounts(filename, argv, envp);
}
int
#endif
#include "util.h"
-#include <lib_internal/sys_unshare.h>
+#include <lib_internal/sys_clone.h>
#include <vserver.h>
}
static void
-newNamespace(void)
+newNamespace(char const *cmd)
{
- int rc;
+ pid_t pid;
- rc = sys_unshare(CLONE_NEWNS);
- if (rc!=0) {
- perror("vnamespace: unshare()");
- exit(wrapper_exit_code);
+ signal(SIGCHLD, SIG_DFL);
+
+#ifdef NDEBUG
+ pid = sys_clone(CLONE_NEWNS|CLONE_VFORK|SIGCHLD, 0);
+#else
+ pid = sys_clone(CLONE_NEWNS|SIGCHLD, 0);
+#endif
+
+ switch (pid) {
+ case -1 :
+ perror("vnamespace: clone()");
+ exit(wrapper_exit_code);
+ case 0 :
+ break;
+ default :
+ exitLikeProcess(pid, cmd, wrapper_exit_code);
}
}
else if (optind==argc && (do_new || do_enter))
WRITE_MSG(2, "No command specified; try '--help' for more information\n");
else {
- if (do_new) newNamespace();
+ if (do_new) newNamespace(argv[optind]);
else if (do_set) setNamespace(VC_SAMECTX, CLONE_NEWNS|CLONE_FS);
else if (do_cleanup) cleanupNamespace();
else if (do_enter) enterNamespace(xid, CLONE_NEWNS|CLONE_FS);
#endif
#include "util.h"
-#include <lib_internal/sys_unshare.h>
+#include <lib_internal/sys_clone.h>
#include <vserver.h>
}
static void
-newSpaces(uint_least64_t mask)
+newSpaces(uint_least64_t mask, const char *cmd)
{
- int rc;
+ pid_t pid;
- rc = sys_unshare(mask);
- if (rc) {
- perror(ENSC_WRAPPERS_PREFIX "unshare()");
- exit(wrapper_exit_code);
+ /* optimize default case */
+ if (mask == 0)
+ return;
+
+ signal(SIGCHLD, SIG_DFL);
+
+#ifdef NDEBUG
+ pid = sys_clone((int) mask | CLONE_VFORK|SIGCHLD, 0);
+#else
+ pid = sys_clone((int) mask | SIGCHLD, 0);
+#endif
+
+ switch (pid) {
+ case -1 :
+ perror(ENSC_WRAPPERS_PREFIX "clone()");
+ exit(wrapper_exit_code);
+ case 0 :
+ break;
+ default :
+ exitLikeProcess(pid, cmd, wrapper_exit_code);
}
}
else if (optind==argc && (do_new || do_enter))
WRITE_MSG(2, "No command specified; try '--help' for more information\n");
else {
- if (do_new) newSpaces(mask);
+ if (do_new) newSpaces(mask, argv[optind]);
else if (do_set) setSpaces(VC_SAMECTX, mask);
else if (do_enter) enterSpaces(xid, mask);