From: Daniel Lezcano Date: Thu, 8 Apr 2010 07:44:23 +0000 (+0200) Subject: fork/exec after attach X-Git-Tag: lxc-0.7.0~108 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7adff31cbb521d52c31a3e6e2447db8bcd8e3784;p=thirdparty%2Flxc.git fork/exec after attach The command to attach has to be fork/exec. Signed-off-by: Daniel Lezcano --- diff --git a/src/lxc/lxc_attach.c b/src/lxc/lxc_attach.c index a012c2c6e..bae76ed4e 100644 --- a/src/lxc/lxc_attach.c +++ b/src/lxc/lxc_attach.c @@ -25,6 +25,9 @@ #include #include #include +#include +#include + #include "commands.h" #include "arguments.h" #include "namespace.h" @@ -106,29 +109,58 @@ int main(int argc, char *argv[], char *envp[]) return -1; } - if (my_args.argc) { - execve(my_args.argv[0], my_args.argv, envp); - SYSERROR("failed to exec '%s'", my_args.argv[0]); + pid = fork(); + + if (pid < 0) { + SYSERROR("failed to fork"); return -1; } - uid = getuid(); + if (pid) { + int status; + + again: + if (waitpid(pid, &status, 0) < 0) { + if (errno == EINTR) + goto again; + SYSERROR("failed to wait '%d'", pid); + return -1; + } + + if (WIFEXITED(status)) + return WEXITSTATUS(status); - passwd = getpwuid(uid); - if (!passwd) { - SYSERROR("failed to get passwd entry for uid '%d'", uid); return -1; } - { - char *const args[] = { - passwd->pw_shell, - NULL, - }; + if (!pid) { + + if (my_args.argc) { + execve(my_args.argv[0], my_args.argv, envp); + SYSERROR("failed to exec '%s'", my_args.argv[0]); + return -1; + } + + uid = getuid(); + + passwd = getpwuid(uid); + if (!passwd) { + SYSERROR("failed to get passwd " \ + "entry for uid '%d'", uid); + return -1; + } + + { + char *const args[] = { + passwd->pw_shell, + NULL, + }; + + execve(args[0], args, envp); + SYSERROR("failed to exec '%s'", args[0]); + return -1; + } - execve(args[0], args, envp); - SYSERROR("failed to exec '%s'", args[0]); - return -1; } return 0;