2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/param.h>
33 #include <sys/prctl.h>
34 #include <sys/mount.h>
35 #include <sys/socket.h>
36 #include <sys/syscall.h>
38 #include <linux/unistd.h>
41 #if !HAVE_DECL_PR_CAPBSET_DROP
42 #define PR_CAPBSET_DROP 24
45 #include "namespace.h"
56 #if HAVE_SYS_PERSONALITY_H
57 #include <sys/personality.h>
61 # define SOCK_CLOEXEC 02000000
64 lxc_log_define(lxc_attach
, lxc
);
66 static struct lxc_proc_context_info
*lxc_proc_get_context_info(pid_t pid
)
68 struct lxc_proc_context_info
*info
= calloc(1, sizeof(*info
));
70 char proc_fn
[MAXPATHLEN
];
72 size_t line_bufsz
= 0;
76 SYSERROR("Could not allocate memory.");
80 /* read capabilities */
81 snprintf(proc_fn
, MAXPATHLEN
, "/proc/%d/status", pid
);
83 proc_file
= fopen(proc_fn
, "r");
85 SYSERROR("Could not open %s", proc_fn
);
90 while (getline(&line
, &line_bufsz
, proc_file
) != -1) {
91 ret
= sscanf(line
, "CapBnd: %llx", &info
->capability_mask
);
92 if (ret
!= EOF
&& ret
> 0) {
103 SYSERROR("Could not read capability bounding set from %s", proc_fn
);
108 /* read personality */
109 snprintf(proc_fn
, MAXPATHLEN
, "/proc/%d/personality", pid
);
111 proc_file
= fopen(proc_fn
, "r");
113 SYSERROR("Could not open %s", proc_fn
);
117 ret
= fscanf(proc_file
, "%lx", &info
->personality
);
120 if (ret
== EOF
|| ret
== 0) {
121 SYSERROR("Could not read personality from %s", proc_fn
);
125 info
->lsm_label
= lsm_process_label_get(pid
);
134 static void lxc_proc_put_context_info(struct lxc_proc_context_info
*ctx
)
137 free(ctx
->lsm_label
);
141 static int lxc_attach_to_ns(pid_t pid
, int which
)
143 char path
[MAXPATHLEN
];
144 /* according to <http://article.gmane.org/gmane.linux.kernel.containers.lxc.devel/1429>,
145 * the file for user namepsaces in /proc/$pid/ns will be called
146 * 'user' once the kernel supports it
148 static char *ns
[] = { "user", "mnt", "pid", "uts", "ipc", "net" };
149 static int flags
[] = {
150 CLONE_NEWUSER
, CLONE_NEWNS
, CLONE_NEWPID
, CLONE_NEWUTS
, CLONE_NEWIPC
,
153 static const int size
= sizeof(ns
) / sizeof(char *);
155 int i
, j
, saved_errno
;
158 snprintf(path
, MAXPATHLEN
, "/proc/%d/ns", pid
);
159 if (access(path
, X_OK
)) {
160 ERROR("Does this kernel version support 'attach' ?");
164 for (i
= 0; i
< size
; i
++) {
165 /* ignore if we are not supposed to attach to that
168 if (which
!= -1 && !(which
& flags
[i
])) {
173 snprintf(path
, MAXPATHLEN
, "/proc/%d/ns/%s", pid
, ns
[i
]);
174 fd
[i
] = open(path
, O_RDONLY
| O_CLOEXEC
);
178 /* close all already opened file descriptors before
179 * we return an error, so we don't leak them
181 for (j
= 0; j
< i
; j
++)
185 SYSERROR("failed to open '%s'", path
);
190 for (i
= 0; i
< size
; i
++) {
191 if (fd
[i
] >= 0 && setns(fd
[i
], 0) != 0) {
194 for (j
= i
; j
< size
; j
++)
198 SYSERROR("failed to set namespace '%s'", ns
[i
]);
208 static int lxc_attach_remount_sys_proc(void)
212 ret
= unshare(CLONE_NEWNS
);
214 SYSERROR("failed to unshare mount namespace");
218 /* assume /proc is always mounted, so remount it */
219 ret
= umount2("/proc", MNT_DETACH
);
221 SYSERROR("failed to unmount /proc");
225 ret
= mount("none", "/proc", "proc", 0, NULL
);
227 SYSERROR("failed to remount /proc");
231 /* try to umount /sys - if it's not a mount point,
232 * we'll get EINVAL, then we ignore it because it
233 * may not have been mounted in the first place
235 ret
= umount2("/sys", MNT_DETACH
);
236 if (ret
< 0 && errno
!= EINVAL
) {
237 SYSERROR("failed to unmount /sys");
239 } else if (ret
== 0) {
241 ret
= mount("none", "/sys", "sysfs", 0, NULL
);
243 SYSERROR("failed to remount /sys");
251 static int lxc_attach_drop_privs(struct lxc_proc_context_info
*ctx
)
253 int last_cap
= lxc_caps_last_cap();
256 for (cap
= 0; cap
<= last_cap
; cap
++) {
257 if (ctx
->capability_mask
& (1LL << cap
))
260 if (prctl(PR_CAPBSET_DROP
, cap
, 0, 0, 0)) {
261 SYSERROR("failed to remove capability id %d", cap
);
269 static int lxc_attach_set_environment(enum lxc_attach_env_policy_t policy
, char** extra_env
, char** extra_keep
)
271 if (policy
== LXC_ATTACH_CLEAR_ENV
) {
272 char **extra_keep_store
= NULL
;
278 for (count
= 0; extra_keep
[count
]; count
++);
280 extra_keep_store
= calloc(count
, sizeof(char *));
281 if (!extra_keep_store
) {
282 SYSERROR("failed to allocate memory for storing current "
283 "environment variable values that will be kept");
286 for (i
= 0; i
< count
; i
++) {
287 char *v
= getenv(extra_keep
[i
]);
289 extra_keep_store
[i
] = strdup(v
);
290 if (!extra_keep_store
[i
]) {
291 SYSERROR("failed to allocate memory for storing current "
292 "environment variable values that will be kept");
294 free(extra_keep_store
[--i
]);
295 free(extra_keep_store
);
298 if (strcmp(extra_keep
[i
], "PATH") == 0)
301 /* calloc sets entire array to zero, so we don't
308 SYSERROR("failed to clear environment");
309 if (extra_keep_store
) {
310 for (p
= extra_keep_store
; *p
; p
++)
312 free(extra_keep_store
);
317 if (extra_keep_store
) {
319 for (i
= 0; extra_keep
[i
]; i
++) {
320 if (extra_keep_store
[i
]) {
321 if (setenv(extra_keep
[i
], extra_keep_store
[i
], 1) < 0)
322 SYSERROR("Unable to set environment variable");
324 free(extra_keep_store
[i
]);
326 free(extra_keep_store
);
329 /* always set a default path; shells and execlp tend
330 * to be fine without it, but there is a disturbing
331 * number of C programs out there that just assume
332 * that getenv("PATH") is never NULL and then die a
333 * painful segfault death. */
339 n
= confstr(_CS_PATH
, NULL
, 0);
340 path_env
= malloc(n
);
342 confstr(_CS_PATH
, path_env
, n
);
343 setenv("PATH", path_env
, 1);
346 /* don't error out, this is just an extra service */
348 setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 1);
353 if (putenv("container=lxc")) {
354 SYSERROR("failed to set environment variable");
358 /* set extra environment variables */
360 for (; *extra_env
; extra_env
++) {
361 /* duplicate the string, just to be on
362 * the safe side, because putenv does not
364 char *p
= strdup(*extra_env
);
365 /* we just assume the user knows what they
366 * are doing, so we don't do any checks */
368 SYSERROR("failed to allocate memory for additional environment "
379 static char *lxc_attach_getpwshell(uid_t uid
)
381 /* local variables */
388 /* we need to fork off a process that runs the
389 * getent program, and we need to capture its
390 * output, so we use a pipe for that purpose
407 size_t line_bufsz
= 0;
413 pipe_f
= fdopen(pipes
[0], "r");
414 while (getline(&line
, &line_bufsz
, pipe_f
) != -1) {
416 char *saveptr
= NULL
;
421 /* if we already found something, just continue
422 * to read until the pipe doesn't deliver any more
423 * data, but don't modify the existing data
429 /* trim line on the right hand side */
430 for (i
= strlen(line
); i
> 0 && (line
[i
- 1] == '\n' || line
[i
- 1] == '\r'); --i
)
433 /* split into tokens: first user name */
434 token
= strtok_r(line
, ":", &saveptr
);
437 /* next: dummy password field */
438 token
= strtok_r(NULL
, ":", &saveptr
);
442 token
= strtok_r(NULL
, ":", &saveptr
);
443 value
= token
? strtol(token
, &endptr
, 10) : 0;
444 if (!token
|| !endptr
|| *endptr
|| value
== LONG_MIN
|| value
== LONG_MAX
)
446 /* dummy sanity check: user id matches */
447 if ((uid_t
) value
!= uid
)
449 /* skip fields: gid, gecos, dir, go to next field 'shell' */
450 for (i
= 0; i
< 4; i
++) {
451 token
= strtok_r(NULL
, ":", &saveptr
);
459 result
= strdup(token
);
461 /* sanity check that there are no fields after that */
462 token
= strtok_r(NULL
, ":", &saveptr
);
472 if (waitpid(pid
, &status
, 0) < 0) {
478 /* some sanity checks: if anything even hinted at going
479 * wrong: we can't be sure we have a valid result, so
483 if (!WIFEXITED(status
))
486 if (WEXITSTATUS(status
) != 0)
496 char *arguments
[] = {
505 /* we want to capture stdout */
509 /* get rid of stdin/stderr, so we try to associate it
512 fd
= open("/dev/null", O_RDWR
);
522 /* finish argument list */
523 ret
= snprintf(uid_buf
, sizeof(uid_buf
), "%ld", (long) uid
);
527 /* try to run getent program */
528 (void) execvp("getent", arguments
);
533 static void lxc_attach_get_init_uidgid(uid_t
* init_uid
, gid_t
* init_gid
)
536 char proc_fn
[MAXPATHLEN
];
538 size_t line_bufsz
= 0;
541 uid_t uid
= (uid_t
)-1;
542 gid_t gid
= (gid_t
)-1;
544 /* read capabilities */
545 snprintf(proc_fn
, MAXPATHLEN
, "/proc/%d/status", 1);
547 proc_file
= fopen(proc_fn
, "r");
551 while (getline(&line
, &line_bufsz
, proc_file
) != -1) {
552 /* format is: real, effective, saved set user, fs
553 * we only care about real uid
555 ret
= sscanf(line
, "Uid: %ld", &value
);
556 if (ret
!= EOF
&& ret
> 0) {
559 ret
= sscanf(line
, "Gid: %ld", &value
);
560 if (ret
!= EOF
&& ret
> 0)
563 if (uid
!= (uid_t
)-1 && gid
!= (gid_t
)-1)
570 /* only override arguments if we found something */
571 if (uid
!= (uid_t
)-1)
573 if (gid
!= (gid_t
)-1)
576 /* TODO: we should also parse supplementary groups and use
577 * setgroups() to set them */
580 struct attach_clone_payload
{
582 lxc_attach_options_t
* options
;
583 struct lxc_proc_context_info
* init_ctx
;
584 lxc_attach_exec_t exec_function
;
588 static int attach_child_main(void* data
);
590 /* help the optimizer along if it doesn't know that exit always exits */
591 #define rexit(c) do { int __c = (c); exit(__c); return __c; } while(0)
593 /* define default options if no options are supplied by the user */
594 static lxc_attach_options_t attach_static_default_options
= LXC_ATTACH_OPTIONS_DEFAULT
;
596 int lxc_attach(const char* name
, const char* lxcpath
, lxc_attach_exec_t exec_function
, void* exec_payload
, lxc_attach_options_t
* options
, pid_t
* attached_process
)
599 pid_t init_pid
, pid
, attached_pid
, expected
;
600 struct lxc_proc_context_info
*init_ctx
;
606 options
= &attach_static_default_options
;
608 init_pid
= lxc_cmd_get_init_pid(name
, lxcpath
);
610 ERROR("failed to get the init pid");
614 init_ctx
= lxc_proc_get_context_info(init_pid
);
616 ERROR("failed to get context of the init process, pid = %ld", (long)init_pid
);
620 cwd
= getcwd(NULL
, 0);
622 /* determine which namespaces the container was created with
623 * by asking lxc-start, if necessary
625 if (options
->namespaces
== -1) {
626 options
->namespaces
= lxc_cmd_get_clone_flags(name
, lxcpath
);
628 if (options
->namespaces
== -1) {
629 ERROR("failed to automatically determine the "
630 "namespaces which the container unshared");
632 lxc_proc_put_context_info(init_ctx
);
637 /* create a socket pair for IPC communication; set SOCK_CLOEXEC in order
638 * to make sure we don't irritate other threads that want to fork+exec away
640 * IMPORTANT: if the initial process is multithreaded and another call
641 * just fork()s away without exec'ing directly after, the socket fd will
642 * exist in the forked process from the other thread and any close() in
643 * our own child process will not really cause the socket to close properly,
644 * potentiall causing the parent to hang.
646 * For this reason, while IPC is still active, we have to use shutdown()
647 * if the child exits prematurely in order to signal that the socket
648 * is closed and cannot assume that the child exiting will automatically
651 * IPC mechanism: (X is receiver)
652 * initial process intermediate attached
656 * send 0 ------------------------------------> X
657 * [do initialization]
658 * X <------------------------------------ send 1
659 * [add to cgroup, ...]
660 * send 2 ------------------------------------> X
661 * close socket close socket
664 ret
= socketpair(PF_LOCAL
, SOCK_STREAM
| SOCK_CLOEXEC
, 0, ipc_sockets
);
666 SYSERROR("could not set up required IPC mechanism for attaching");
668 lxc_proc_put_context_info(init_ctx
);
672 /* create intermediate subprocess, three reasons:
673 * 1. runs all pthread_atfork handlers and the
674 * child will no longer be threaded
675 * (we can't properly setns() in a threaded process)
676 * 2. we can't setns() in the child itself, since
677 * we want to make sure we are properly attached to
679 * 3. also, the initial thread has to put the attached
680 * process into the cgroup, which we can only do if
681 * we didn't already setns() (otherwise, user
682 * namespaces will hate us)
687 SYSERROR("failed to create first subprocess");
689 lxc_proc_put_context_info(init_ctx
);
694 pid_t to_cleanup_pid
= pid
;
696 /* inital thread, we close the socket that is for the
699 close(ipc_sockets
[1]);
702 /* attach to cgroup, if requested */
703 if (options
->attach_flags
& LXC_ATTACH_MOVE_TO_CGROUP
) {
704 if (!cgroup_attach(name
, lxcpath
, pid
))
708 /* Let the child process know to go ahead */
710 ret
= lxc_write_nointr(ipc_sockets
[0], &status
, sizeof(status
));
712 ERROR("error using IPC to notify attached process for initialization (0)");
716 /* get pid from intermediate process */
717 ret
= lxc_read_nointr_expect(ipc_sockets
[0], &attached_pid
, sizeof(attached_pid
), NULL
);
720 ERROR("error using IPC to receive pid of attached process");
724 /* reap intermediate process */
725 ret
= wait_for_pid(pid
);
729 /* we will always have to reap the grandchild now */
730 to_cleanup_pid
= attached_pid
;
732 /* tell attached process it may start initializing */
734 ret
= lxc_write_nointr(ipc_sockets
[0], &status
, sizeof(status
));
736 ERROR("error using IPC to notify attached process for initialization (0)");
740 /* wait for the attached process to finish initializing */
742 ret
= lxc_read_nointr_expect(ipc_sockets
[0], &status
, sizeof(status
), &expected
);
745 ERROR("error using IPC to receive notification from attached process (1)");
749 /* tell attached process we're done */
751 ret
= lxc_write_nointr(ipc_sockets
[0], &status
, sizeof(status
));
753 ERROR("error using IPC to notify attached process for initialization (2)");
757 /* now shut down communication with child, we're done */
758 shutdown(ipc_sockets
[0], SHUT_RDWR
);
759 close(ipc_sockets
[0]);
760 lxc_proc_put_context_info(init_ctx
);
762 /* we're done, the child process should now execute whatever
763 * it is that the user requested. The parent can now track it
764 * with waitpid() or similar.
767 *attached_process
= attached_pid
;
771 /* first shut down the socket, then wait for the pid,
772 * otherwise the pid we're waiting for may never exit
774 shutdown(ipc_sockets
[0], SHUT_RDWR
);
775 close(ipc_sockets
[0]);
777 (void) wait_for_pid(to_cleanup_pid
);
778 lxc_proc_put_context_info(init_ctx
);
782 /* first subprocess begins here, we close the socket that is for the
785 close(ipc_sockets
[0]);
787 /* Wait for the parent to have setup cgroups */
790 ret
= lxc_read_nointr_expect(ipc_sockets
[1], &status
, sizeof(status
), &expected
);
792 ERROR("error communicating with child process");
793 shutdown(ipc_sockets
[1], SHUT_RDWR
);
797 /* attach now, create another subprocess later, since pid namespaces
798 * only really affect the children of the current process
800 ret
= lxc_attach_to_ns(init_pid
, options
->namespaces
);
802 ERROR("failed to enter the namespace");
803 shutdown(ipc_sockets
[1], SHUT_RDWR
);
807 /* attach succeeded, try to cwd */
808 if (options
->initial_cwd
)
809 new_cwd
= options
->initial_cwd
;
812 ret
= chdir(new_cwd
);
814 WARN("could not change directory to '%s'", new_cwd
);
817 /* now create the real child process */
819 struct attach_clone_payload payload
= {
820 .ipc_socket
= ipc_sockets
[1],
822 .init_ctx
= init_ctx
,
823 .exec_function
= exec_function
,
824 .exec_payload
= exec_payload
826 /* We use clone_parent here to make this subprocess a direct child of
827 * the initial process. Then this intermediate process can exit and
828 * the parent can directly track the attached process.
830 pid
= lxc_clone(attach_child_main
, &payload
, CLONE_PARENT
);
833 /* shouldn't happen, clone() should always return positive pid */
835 SYSERROR("failed to create subprocess");
836 shutdown(ipc_sockets
[1], SHUT_RDWR
);
840 /* tell grandparent the pid of the pid of the newly created child */
841 ret
= lxc_write_nointr(ipc_sockets
[1], &pid
, sizeof(pid
));
842 if (ret
!= sizeof(pid
)) {
843 /* if this really happens here, this is very unfortunate, since the
844 * parent will not know the pid of the attached process and will
845 * not be able to wait for it (and we won't either due to CLONE_PARENT)
846 * so the parent won't be able to reap it and the attached process
847 * will remain a zombie
849 ERROR("error using IPC to notify main process of pid of the attached process");
850 shutdown(ipc_sockets
[1], SHUT_RDWR
);
854 /* the rest is in the hands of the initial and the attached process */
858 static int attach_child_main(void* data
)
860 struct attach_clone_payload
* payload
= (struct attach_clone_payload
*)data
;
861 int ipc_socket
= payload
->ipc_socket
;
862 lxc_attach_options_t
* options
= payload
->options
;
863 struct lxc_proc_context_info
* init_ctx
= payload
->init_ctx
;
864 #if HAVE_SYS_PERSONALITY_H
865 long new_personality
;
875 /* wait for the initial thread to signal us that it's ready
876 * for us to start initializing
880 ret
= lxc_read_nointr_expect(ipc_socket
, &status
, sizeof(status
), &expected
);
882 ERROR("error using IPC to receive notification from initial process (0)");
883 shutdown(ipc_socket
, SHUT_RDWR
);
887 /* A description of the purpose of this functionality is
888 * provided in the lxc-attach(1) manual page. We have to
889 * remount here and not in the parent process, otherwise
890 * /proc may not properly reflect the new pid namespace.
892 if (!(options
->namespaces
& CLONE_NEWNS
) && (options
->attach_flags
& LXC_ATTACH_REMOUNT_PROC_SYS
)) {
893 ret
= lxc_attach_remount_sys_proc();
895 shutdown(ipc_socket
, SHUT_RDWR
);
900 /* now perform additional attachments*/
901 #if HAVE_SYS_PERSONALITY_H
902 if (options
->personality
< 0)
903 new_personality
= init_ctx
->personality
;
905 new_personality
= options
->personality
;
907 if (options
->attach_flags
& LXC_ATTACH_SET_PERSONALITY
) {
908 ret
= personality(new_personality
);
910 SYSERROR("could not ensure correct architecture");
911 shutdown(ipc_socket
, SHUT_RDWR
);
917 if (options
->attach_flags
& LXC_ATTACH_DROP_CAPABILITIES
) {
918 ret
= lxc_attach_drop_privs(init_ctx
);
920 ERROR("could not drop privileges");
921 shutdown(ipc_socket
, SHUT_RDWR
);
926 /* always set the environment (specify (LXC_ATTACH_KEEP_ENV, NULL, NULL) if you want this to be a no-op) */
927 ret
= lxc_attach_set_environment(options
->env_policy
, options
->extra_env_vars
, options
->extra_keep_env
);
929 ERROR("could not set initial environment for attached process");
930 shutdown(ipc_socket
, SHUT_RDWR
);
934 /* set user / group id */
937 /* ignore errors, we will fall back to root in that case
938 * (/proc was not mounted etc.)
940 if (options
->namespaces
& CLONE_NEWUSER
)
941 lxc_attach_get_init_uidgid(&new_uid
, &new_gid
);
943 if (options
->uid
!= (uid_t
)-1)
944 new_uid
= options
->uid
;
945 if (options
->gid
!= (gid_t
)-1)
946 new_gid
= options
->gid
;
948 /* try to set the uid/gid combination */
949 if ((new_gid
!= 0 || options
->namespaces
& CLONE_NEWUSER
)) {
950 if (setgid(new_gid
) || setgroups(0, NULL
)) {
951 SYSERROR("switching to container gid");
952 shutdown(ipc_socket
, SHUT_RDWR
);
956 if ((new_uid
!= 0 || options
->namespaces
& CLONE_NEWUSER
) && setuid(new_uid
)) {
957 SYSERROR("switching to container uid");
958 shutdown(ipc_socket
, SHUT_RDWR
);
962 /* tell initial process it may now put us into the cgroups */
964 ret
= lxc_write_nointr(ipc_socket
, &status
, sizeof(status
));
965 if (ret
!= sizeof(status
)) {
966 ERROR("error using IPC to notify initial process for initialization (1)");
967 shutdown(ipc_socket
, SHUT_RDWR
);
971 /* wait for the initial thread to signal us that it has done
972 * everything for us when it comes to cgroups etc.
976 ret
= lxc_read_nointr_expect(ipc_socket
, &status
, sizeof(status
), &expected
);
978 ERROR("error using IPC to receive final notification from initial process (2)");
979 shutdown(ipc_socket
, SHUT_RDWR
);
983 shutdown(ipc_socket
, SHUT_RDWR
);
986 /* set new apparmor profile/selinux context */
987 if ((options
->namespaces
& CLONE_NEWNS
) && (options
->attach_flags
& LXC_ATTACH_LSM
)) {
990 on_exec
= options
->attach_flags
& LXC_ATTACH_LSM_EXEC
? 1 : 0;
991 ret
= lsm_process_label_set(init_ctx
->lsm_label
, 0, on_exec
);
996 lxc_proc_put_context_info(init_ctx
);
998 /* The following is done after the communication socket is
999 * shut down. That way, all errors that might (though
1000 * unlikely) occur up until this point will have their messages
1001 * printed to the original stderr (if logging is so configured)
1002 * and not the fd the user supplied, if any.
1005 /* fd handling for stdin, stdout and stderr;
1006 * ignore errors here, user may want to make sure
1007 * the fds are closed, for example */
1008 if (options
->stdin_fd
>= 0 && options
->stdin_fd
!= 0)
1009 dup2(options
->stdin_fd
, 0);
1010 if (options
->stdout_fd
>= 0 && options
->stdout_fd
!= 1)
1011 dup2(options
->stdout_fd
, 1);
1012 if (options
->stderr_fd
>= 0 && options
->stderr_fd
!= 2)
1013 dup2(options
->stderr_fd
, 2);
1015 /* close the old fds */
1016 if (options
->stdin_fd
> 2)
1017 close(options
->stdin_fd
);
1018 if (options
->stdout_fd
> 2)
1019 close(options
->stdout_fd
);
1020 if (options
->stderr_fd
> 2)
1021 close(options
->stderr_fd
);
1023 /* try to remove CLOEXEC flag from stdin/stdout/stderr,
1024 * but also here, ignore errors */
1025 for (fd
= 0; fd
<= 2; fd
++) {
1026 flags
= fcntl(fd
, F_GETFL
);
1029 if (flags
& FD_CLOEXEC
) {
1030 if (fcntl(fd
, F_SETFL
, flags
& ~FD_CLOEXEC
) < 0) {
1031 SYSERROR("Unable to clear CLOEXEC from fd");
1036 /* we're done, so we can now do whatever the user intended us to do */
1037 rexit(payload
->exec_function(payload
->exec_payload
));
1040 int lxc_attach_run_command(void* payload
)
1042 lxc_attach_command_t
* cmd
= (lxc_attach_command_t
*)payload
;
1044 execvp(cmd
->program
, cmd
->argv
);
1045 SYSERROR("failed to exec '%s'", cmd
->program
);
1049 int lxc_attach_run_shell(void* payload
)
1052 struct passwd
*passwd
;
1055 /* ignore payload parameter */
1059 passwd
= getpwuid(uid
);
1061 /* this probably happens because of incompatible nss
1062 * implementations in host and container (remember, this
1063 * code is still using the host's glibc but our mount
1064 * namespace is in the container)
1065 * we may try to get the information by spawning a
1066 * [getent passwd uid] process and parsing the result
1069 user_shell
= lxc_attach_getpwshell(uid
);
1071 user_shell
= passwd
->pw_shell
;
1074 execlp(user_shell
, user_shell
, NULL
);
1076 /* executed if either no passwd entry or execvp fails,
1077 * we will fall back on /bin/sh as a default shell
1079 execlp("/bin/sh", "/bin/sh", NULL
);
1080 SYSERROR("failed to exec shell");