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
31 #include <sys/param.h>
32 #include <sys/prctl.h>
33 #include <sys/mount.h>
34 #include <sys/socket.h>
35 #include <sys/syscall.h>
37 #include <linux/unistd.h>
40 #if !HAVE_DECL_PR_CAPBSET_DROP
41 #define PR_CAPBSET_DROP 24
44 #include "namespace.h"
55 #if HAVE_SYS_PERSONALITY_H
56 #include <sys/personality.h>
60 # define SOCK_CLOEXEC 02000000
63 lxc_log_define(lxc_attach
, lxc
);
65 struct lxc_proc_context_info
*lxc_proc_get_context_info(pid_t pid
)
67 struct lxc_proc_context_info
*info
= calloc(1, sizeof(*info
));
69 char proc_fn
[MAXPATHLEN
];
71 size_t line_bufsz
= 0;
75 SYSERROR("Could not allocate memory.");
79 /* read capabilities */
80 snprintf(proc_fn
, MAXPATHLEN
, "/proc/%d/status", pid
);
83 proc_file
= fopen(proc_fn
, "r");
86 SYSERROR("Could not open %s", proc_fn
);
91 while (getline(&line
, &line_bufsz
, proc_file
) != -1) {
92 ret
= sscanf(line
, "CapBnd: %llx", &info
->capability_mask
);
93 if (ret
!= EOF
&& ret
> 0) {
106 SYSERROR("Could not read capability bounding set from %s", proc_fn
);
111 /* read personality */
112 snprintf(proc_fn
, MAXPATHLEN
, "/proc/%d/personality", pid
);
115 proc_file
= fopen(proc_fn
, "r");
118 SYSERROR("Could not open %s", proc_fn
);
122 ret
= fscanf(proc_file
, "%lx", &info
->personality
);
127 if (ret
== EOF
|| ret
== 0) {
128 SYSERROR("Could not read personality from %s", proc_fn
);
132 info
->lsm_label
= lsm_process_label_get(pid
);
141 static void lxc_proc_put_context_info(struct lxc_proc_context_info
*ctx
)
144 free(ctx
->lsm_label
);
148 int lxc_attach_to_ns(pid_t pid
, int which
)
150 char path
[MAXPATHLEN
];
151 /* according to <http://article.gmane.org/gmane.linux.kernel.containers.lxc.devel/1429>,
152 * the file for user namepsaces in /proc/$pid/ns will be called
153 * 'user' once the kernel supports it
155 static char *ns
[] = { "mnt", "pid", "uts", "ipc", "user", "net" };
156 static int flags
[] = {
157 CLONE_NEWNS
, CLONE_NEWPID
, CLONE_NEWUTS
, CLONE_NEWIPC
,
158 CLONE_NEWUSER
, CLONE_NEWNET
160 static const int size
= sizeof(ns
) / sizeof(char *);
162 int i
, j
, saved_errno
;
165 snprintf(path
, MAXPATHLEN
, "/proc/%d/ns", pid
);
166 if (access(path
, X_OK
)) {
167 ERROR("Does this kernel version support 'attach' ?");
171 for (i
= 0; i
< size
; i
++) {
172 /* ignore if we are not supposed to attach to that
175 if (which
!= -1 && !(which
& flags
[i
])) {
180 snprintf(path
, MAXPATHLEN
, "/proc/%d/ns/%s", pid
, ns
[i
]);
182 fd
[i
] = open(path
, O_RDONLY
| O_CLOEXEC
);
187 /* close all already opened file descriptors before
188 * we return an error, so we don't leak them
191 for (j
= 0; j
< i
; j
++)
196 SYSERROR("failed to open '%s'", path
);
201 for (i
= 0; i
< size
; i
++) {
202 if (fd
[i
] >= 0 && setns(fd
[i
], 0) != 0) {
205 for (j
= i
; j
< size
; j
++)
209 SYSERROR("failed to set namespace '%s'", ns
[i
]);
221 int lxc_attach_remount_sys_proc()
225 ret
= unshare(CLONE_NEWNS
);
227 SYSERROR("failed to unshare mount namespace");
231 /* assume /proc is always mounted, so remount it */
232 ret
= umount2("/proc", MNT_DETACH
);
234 SYSERROR("failed to unmount /proc");
238 ret
= mount("none", "/proc", "proc", 0, NULL
);
240 SYSERROR("failed to remount /proc");
244 /* try to umount /sys - if it's not a mount point,
245 * we'll get EINVAL, then we ignore it because it
246 * may not have been mounted in the first place
248 ret
= umount2("/sys", MNT_DETACH
);
249 if (ret
< 0 && errno
!= EINVAL
) {
250 SYSERROR("failed to unmount /sys");
252 } else if (ret
== 0) {
254 ret
= mount("none", "/sys", "sysfs", 0, NULL
);
256 SYSERROR("failed to remount /sys");
264 int lxc_attach_drop_privs(struct lxc_proc_context_info
*ctx
)
266 int last_cap
= lxc_caps_last_cap();
269 for (cap
= 0; cap
<= last_cap
; cap
++) {
270 if (ctx
->capability_mask
& (1LL << cap
))
273 if (prctl(PR_CAPBSET_DROP
, cap
, 0, 0, 0)) {
274 SYSERROR("failed to remove capability id %d", cap
);
282 int lxc_attach_set_environment(enum lxc_attach_env_policy_t policy
, char** extra_env
, char** extra_keep
)
284 if (policy
== LXC_ATTACH_CLEAR_ENV
) {
285 char **extra_keep_store
= NULL
;
291 for (count
= 0; extra_keep
[count
]; count
++);
293 extra_keep_store
= calloc(count
, sizeof(char *));
294 if (!extra_keep_store
) {
295 SYSERROR("failed to allocate memory for storing current "
296 "environment variable values that will be kept");
299 for (i
= 0; i
< count
; i
++) {
300 char *v
= getenv(extra_keep
[i
]);
302 extra_keep_store
[i
] = strdup(v
);
303 if (!extra_keep_store
[i
]) {
304 SYSERROR("failed to allocate memory for storing current "
305 "environment variable values that will be kept");
307 free(extra_keep_store
[--i
]);
308 free(extra_keep_store
);
311 if (strcmp(extra_keep
[i
], "PATH") == 0)
314 /* calloc sets entire array to zero, so we don't
321 SYSERROR("failed to clear environment");
322 if (extra_keep_store
) {
323 for (p
= extra_keep_store
; *p
; p
++)
325 free(extra_keep_store
);
330 if (extra_keep_store
) {
332 for (i
= 0; extra_keep
[i
]; i
++) {
333 if (extra_keep_store
[i
])
334 setenv(extra_keep
[i
], extra_keep_store
[i
], 1);
335 free(extra_keep_store
[i
]);
337 free(extra_keep_store
);
340 /* always set a default path; shells and execlp tend
341 * to be fine without it, but there is a disturbing
342 * number of C programs out there that just assume
343 * that getenv("PATH") is never NULL and then die a
344 * painful segfault death. */
350 n
= confstr(_CS_PATH
, NULL
, 0);
351 path_env
= malloc(n
);
353 confstr(_CS_PATH
, path_env
, n
);
354 setenv("PATH", path_env
, 1);
357 /* don't error out, this is just an extra service */
359 setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 1);
364 if (putenv("container=lxc")) {
365 SYSERROR("failed to set environment variable");
369 /* set extra environment variables */
371 for (; *extra_env
; extra_env
++) {
372 /* duplicate the string, just to be on
373 * the safe side, because putenv does not
375 char *p
= strdup(*extra_env
);
376 /* we just assume the user knows what they
377 * are doing, so we don't do any checks */
379 SYSERROR("failed to allocate memory for additional environment "
390 char *lxc_attach_getpwshell(uid_t uid
)
392 /* local variables */
399 /* we need to fork off a process that runs the
400 * getent program, and we need to capture its
401 * output, so we use a pipe for that purpose
422 size_t line_bufsz
= 0;
431 pipe_f
= fdopen(pipes
[0], "r");
433 while (getline(&line
, &line_bufsz
, pipe_f
) != -1) {
435 char *saveptr
= NULL
;
440 /* if we already found something, just continue
441 * to read until the pipe doesn't deliver any more
442 * data, but don't modify the existing data
448 /* trim line on the right hand side */
449 for (i
= strlen(line
); i
> 0 && (line
[i
- 1] == '\n' || line
[i
- 1] == '\r'); --i
)
452 /* split into tokens: first user name */
453 token
= strtok_r(line
, ":", &saveptr
);
456 /* next: dummy password field */
457 token
= strtok_r(NULL
, ":", &saveptr
);
461 token
= strtok_r(NULL
, ":", &saveptr
);
462 value
= token
? strtol(token
, &endptr
, 10) : 0;
463 if (!token
|| !endptr
|| *endptr
|| value
== LONG_MIN
|| value
== LONG_MAX
)
465 /* dummy sanity check: user id matches */
466 if ((uid_t
) value
!= uid
)
468 /* skip fields: gid, gecos, dir, go to next field 'shell' */
469 for (i
= 0; i
< 4; i
++) {
470 token
= strtok_r(NULL
, ":", &saveptr
);
478 result
= strdup(token
);
480 /* sanity check that there are no fields after that */
481 token
= strtok_r(NULL
, ":", &saveptr
);
493 if (waitpid(pid
, &status
, 0) < 0) {
499 /* some sanity checks: if anything even hinted at going
500 * wrong: we can't be sure we have a valid result, so
504 if (!WIFEXITED(status
))
507 if (WEXITSTATUS(status
) != 0)
517 char *arguments
[] = {
524 process_unlock(); // we're no longer sharing
527 /* we want to capture stdout */
531 /* get rid of stdin/stderr, so we try to associate it
534 fd
= open("/dev/null", O_RDWR
);
544 /* finish argument list */
545 ret
= snprintf(uid_buf
, sizeof(uid_buf
), "%ld", (long) uid
);
549 /* try to run getent program */
550 (void) execvp("getent", arguments
);
555 void lxc_attach_get_init_uidgid(uid_t
* init_uid
, gid_t
* init_gid
)
558 char proc_fn
[MAXPATHLEN
];
560 size_t line_bufsz
= 0;
563 uid_t uid
= (uid_t
)-1;
564 gid_t gid
= (gid_t
)-1;
566 /* read capabilities */
567 snprintf(proc_fn
, MAXPATHLEN
, "/proc/%d/status", 1);
569 proc_file
= fopen(proc_fn
, "r");
573 while (getline(&line
, &line_bufsz
, proc_file
) != -1) {
574 /* format is: real, effective, saved set user, fs
575 * we only care about real uid
577 ret
= sscanf(line
, "Uid: %ld", &value
);
578 if (ret
!= EOF
&& ret
> 0) {
581 ret
= sscanf(line
, "Gid: %ld", &value
);
582 if (ret
!= EOF
&& ret
> 0)
585 if (uid
!= (uid_t
)-1 && gid
!= (gid_t
)-1)
592 /* only override arguments if we found something */
593 if (uid
!= (uid_t
)-1)
595 if (gid
!= (gid_t
)-1)
598 /* TODO: we should also parse supplementary groups and use
599 * setgroups() to set them */
602 struct attach_clone_payload
{
604 lxc_attach_options_t
* options
;
605 struct lxc_proc_context_info
* init_ctx
;
606 lxc_attach_exec_t exec_function
;
610 static int attach_child_main(void* data
);
612 /* help the optimizer along if it doesn't know that exit always exits */
613 #define rexit(c) do { int __c = (c); exit(__c); return __c; } while(0)
615 /* define default options if no options are supplied by the user */
616 static lxc_attach_options_t attach_static_default_options
= LXC_ATTACH_OPTIONS_DEFAULT
;
618 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
)
621 pid_t init_pid
, pid
, attached_pid
;
622 struct lxc_proc_context_info
*init_ctx
;
628 options
= &attach_static_default_options
;
630 init_pid
= lxc_cmd_get_init_pid(name
, lxcpath
);
632 ERROR("failed to get the init pid");
636 init_ctx
= lxc_proc_get_context_info(init_pid
);
638 ERROR("failed to get context of the init process, pid = %ld", (long)init_pid
);
642 cwd
= getcwd(NULL
, 0);
644 /* determine which namespaces the container was created with
645 * by asking lxc-start, if necessary
647 if (options
->namespaces
== -1) {
648 options
->namespaces
= lxc_cmd_get_clone_flags(name
, lxcpath
);
650 if (options
->namespaces
== -1) {
651 ERROR("failed to automatically determine the "
652 "namespaces which the container unshared");
654 lxc_proc_put_context_info(init_ctx
);
659 /* create a socket pair for IPC communication; set SOCK_CLOEXEC in order
660 * to make sure we don't irritate other threads that want to fork+exec away
662 * IMPORTANT: if the initial process is multithreaded and another call
663 * just fork()s away without exec'ing directly after, the socket fd will
664 * exist in the forked process from the other thread and any close() in
665 * our own child process will not really cause the socket to close properly,
666 * potentiall causing the parent to hang.
668 * For this reason, while IPC is still active, we have to use shutdown()
669 * if the child exits prematurely in order to signal that the socket
670 * is closed and cannot assume that the child exiting will automatically
673 * IPC mechanism: (X is receiver)
674 * initial process intermediate attached
678 * send 0 ------------------------------------> X
679 * [do initialization]
680 * X <------------------------------------ send 1
681 * [add to cgroup, ...]
682 * send 2 ------------------------------------> X
683 * close socket close socket
687 ret
= socketpair(PF_LOCAL
, SOCK_STREAM
| SOCK_CLOEXEC
, 0, ipc_sockets
);
690 SYSERROR("could not set up required IPC mechanism for attaching");
692 lxc_proc_put_context_info(init_ctx
);
696 /* create intermediate subprocess, three reasons:
697 * 1. runs all pthread_atfork handlers and the
698 * child will no longer be threaded
699 * (we can't properly setns() in a threaded process)
700 * 2. we can't setns() in the child itself, since
701 * we want to make sure we are properly attached to
703 * 3. also, the initial thread has to put the attached
704 * process into the cgroup, which we can only do if
705 * we didn't already setns() (otherwise, user
706 * namespaces will hate us)
711 SYSERROR("failed to create first subprocess");
713 lxc_proc_put_context_info(init_ctx
);
718 pid_t to_cleanup_pid
= pid
;
721 /* inital thread, we close the socket that is for the
725 close(ipc_sockets
[1]);
729 /* get pid from intermediate process */
730 ret
= lxc_read_nointr_expect(ipc_sockets
[0], &attached_pid
, sizeof(attached_pid
), NULL
);
733 ERROR("error using IPC to receive pid of attached process");
737 /* reap intermediate process */
738 ret
= wait_for_pid(pid
);
742 /* we will always have to reap the grandchild now */
743 to_cleanup_pid
= attached_pid
;
745 /* tell attached process it may start initializing */
747 ret
= lxc_write_nointr(ipc_sockets
[0], &status
, sizeof(status
));
749 ERROR("error using IPC to notify attached process for initialization (0)");
753 /* wait for the attached process to finish initializing */
755 ret
= lxc_read_nointr_expect(ipc_sockets
[0], &status
, sizeof(status
), &expected
);
758 ERROR("error using IPC to receive notification from attached process (1)");
762 /* attach to cgroup, if requested */
763 if (options
->attach_flags
& LXC_ATTACH_MOVE_TO_CGROUP
) {
764 struct cgroup_meta_data
*meta_data
;
765 struct cgroup_process_info
*container_info
;
767 meta_data
= lxc_cgroup_load_meta();
769 ERROR("could not move attached process %ld to cgroup of container", (long)attached_pid
);
773 container_info
= lxc_cgroup_get_container_info(name
, lxcpath
, meta_data
);
774 lxc_cgroup_put_meta(meta_data
);
775 if (!container_info
) {
776 ERROR("could not move attached process %ld to cgroup of container", (long)attached_pid
);
780 ret
= lxc_cgroup_enter(container_info
, attached_pid
, false);
781 lxc_cgroup_process_info_free(container_info
);
783 ERROR("could not move attached process %ld to cgroup of container", (long)attached_pid
);
788 /* tell attached process we're done */
790 ret
= lxc_write_nointr(ipc_sockets
[0], &status
, sizeof(status
));
792 ERROR("error using IPC to notify attached process for initialization (2)");
796 /* now shut down communication with child, we're done */
797 shutdown(ipc_sockets
[0], SHUT_RDWR
);
799 close(ipc_sockets
[0]);
801 lxc_proc_put_context_info(init_ctx
);
803 /* we're done, the child process should now execute whatever
804 * it is that the user requested. The parent can now track it
805 * with waitpid() or similar.
808 *attached_process
= attached_pid
;
812 /* first shut down the socket, then wait for the pid,
813 * otherwise the pid we're waiting for may never exit
815 shutdown(ipc_sockets
[0], SHUT_RDWR
);
817 close(ipc_sockets
[0]);
820 (void) wait_for_pid(to_cleanup_pid
);
821 lxc_proc_put_context_info(init_ctx
);
825 process_unlock(); // we're no longer sharing
826 /* first subprocess begins here, we close the socket that is for the
829 close(ipc_sockets
[0]);
831 /* attach now, create another subprocess later, since pid namespaces
832 * only really affect the children of the current process
834 ret
= lxc_attach_to_ns(init_pid
, options
->namespaces
);
836 ERROR("failed to enter the namespace");
837 shutdown(ipc_sockets
[1], SHUT_RDWR
);
841 /* attach succeeded, try to cwd */
842 if (options
->initial_cwd
)
843 new_cwd
= options
->initial_cwd
;
846 ret
= chdir(new_cwd
);
848 WARN("could not change directory to '%s'", new_cwd
);
851 /* now create the real child process */
853 struct attach_clone_payload payload
= {
854 .ipc_socket
= ipc_sockets
[1],
856 .init_ctx
= init_ctx
,
857 .exec_function
= exec_function
,
858 .exec_payload
= exec_payload
860 /* We use clone_parent here to make this subprocess a direct child of
861 * the initial process. Then this intermediate process can exit and
862 * the parent can directly track the attached process.
864 pid
= lxc_clone(attach_child_main
, &payload
, CLONE_PARENT
);
867 /* shouldn't happen, clone() should always return positive pid */
869 SYSERROR("failed to create subprocess");
870 shutdown(ipc_sockets
[1], SHUT_RDWR
);
874 /* tell grandparent the pid of the pid of the newly created child */
875 ret
= lxc_write_nointr(ipc_sockets
[1], &pid
, sizeof(pid
));
876 if (ret
!= sizeof(pid
)) {
877 /* if this really happens here, this is very unfortunate, since the
878 * parent will not know the pid of the attached process and will
879 * not be able to wait for it (and we won't either due to CLONE_PARENT)
880 * so the parent won't be able to reap it and the attached process
881 * will remain a zombie
883 ERROR("error using IPC to notify main process of pid of the attached process");
884 shutdown(ipc_sockets
[1], SHUT_RDWR
);
888 /* the rest is in the hands of the initial and the attached process */
892 int attach_child_main(void* data
)
894 struct attach_clone_payload
* payload
= (struct attach_clone_payload
*)data
;
895 int ipc_socket
= payload
->ipc_socket
;
896 lxc_attach_options_t
* options
= payload
->options
;
897 struct lxc_proc_context_info
* init_ctx
= payload
->init_ctx
;
898 #if HAVE_SYS_PERSONALITY_H
899 long new_personality
;
909 /* wait for the initial thread to signal us that it's ready
910 * for us to start initializing
914 ret
= lxc_read_nointr_expect(ipc_socket
, &status
, sizeof(status
), &expected
);
916 ERROR("error using IPC to receive notification from initial process (0)");
917 shutdown(ipc_socket
, SHUT_RDWR
);
921 /* load apparmor profile */
922 if ((options
->namespaces
& CLONE_NEWNS
) && (options
->attach_flags
& LXC_ATTACH_APPARMOR
)) {
923 ret
= lsm_process_label_set(init_ctx
->lsm_label
, 0);
925 shutdown(ipc_socket
, SHUT_RDWR
);
930 /* A description of the purpose of this functionality is
931 * provided in the lxc-attach(1) manual page. We have to
932 * remount here and not in the parent process, otherwise
933 * /proc may not properly reflect the new pid namespace.
935 if (!(options
->namespaces
& CLONE_NEWNS
) && (options
->attach_flags
& LXC_ATTACH_REMOUNT_PROC_SYS
)) {
936 ret
= lxc_attach_remount_sys_proc();
938 shutdown(ipc_socket
, SHUT_RDWR
);
943 /* now perform additional attachments*/
944 #if HAVE_SYS_PERSONALITY_H
945 if (options
->personality
< 0)
946 new_personality
= init_ctx
->personality
;
948 new_personality
= options
->personality
;
950 if (options
->attach_flags
& LXC_ATTACH_SET_PERSONALITY
) {
951 ret
= personality(new_personality
);
953 SYSERROR("could not ensure correct architecture");
954 shutdown(ipc_socket
, SHUT_RDWR
);
960 if (options
->attach_flags
& LXC_ATTACH_DROP_CAPABILITIES
) {
961 ret
= lxc_attach_drop_privs(init_ctx
);
963 ERROR("could not drop privileges");
964 shutdown(ipc_socket
, SHUT_RDWR
);
969 /* always set the environment (specify (LXC_ATTACH_KEEP_ENV, NULL, NULL) if you want this to be a no-op) */
970 ret
= lxc_attach_set_environment(options
->env_policy
, options
->extra_env_vars
, options
->extra_keep_env
);
972 ERROR("could not set initial environment for attached process");
973 shutdown(ipc_socket
, SHUT_RDWR
);
977 /* set user / group id */
980 /* ignore errors, we will fall back to root in that case
981 * (/proc was not mounted etc.)
983 if (options
->namespaces
& CLONE_NEWUSER
)
984 lxc_attach_get_init_uidgid(&new_uid
, &new_gid
);
986 if (options
->uid
!= (uid_t
)-1)
987 new_uid
= options
->uid
;
988 if (options
->gid
!= (gid_t
)-1)
989 new_gid
= options
->gid
;
991 /* try to set the uid/gid combination */
992 if ((new_gid
!= 0 || options
->namespaces
& CLONE_NEWUSER
) && setgid(new_gid
)) {
993 SYSERROR("switching to container gid");
994 shutdown(ipc_socket
, SHUT_RDWR
);
997 if ((new_uid
!= 0 || options
->namespaces
& CLONE_NEWUSER
) && setuid(new_uid
)) {
998 SYSERROR("switching to container uid");
999 shutdown(ipc_socket
, SHUT_RDWR
);
1003 /* tell initial process it may now put us into the cgroups */
1005 ret
= lxc_write_nointr(ipc_socket
, &status
, sizeof(status
));
1006 if (ret
!= sizeof(status
)) {
1007 ERROR("error using IPC to notify initial process for initialization (1)");
1008 shutdown(ipc_socket
, SHUT_RDWR
);
1012 /* wait for the initial thread to signal us that it has done
1013 * everything for us when it comes to cgroups etc.
1017 ret
= lxc_read_nointr_expect(ipc_socket
, &status
, sizeof(status
), &expected
);
1019 ERROR("error using IPC to receive final notification from initial process (2)");
1020 shutdown(ipc_socket
, SHUT_RDWR
);
1024 shutdown(ipc_socket
, SHUT_RDWR
);
1026 lxc_proc_put_context_info(init_ctx
);
1028 /* The following is done after the communication socket is
1029 * shut down. That way, all errors that might (though
1030 * unlikely) occur up until this point will have their messages
1031 * printed to the original stderr (if logging is so configured)
1032 * and not the fd the user supplied, if any.
1035 /* fd handling for stdin, stdout and stderr;
1036 * ignore errors here, user may want to make sure
1037 * the fds are closed, for example */
1038 if (options
->stdin_fd
>= 0 && options
->stdin_fd
!= 0)
1039 dup2(options
->stdin_fd
, 0);
1040 if (options
->stdout_fd
>= 0 && options
->stdout_fd
!= 1)
1041 dup2(options
->stdout_fd
, 1);
1042 if (options
->stderr_fd
>= 0 && options
->stderr_fd
!= 2)
1043 dup2(options
->stderr_fd
, 2);
1045 /* close the old fds */
1046 if (options
->stdin_fd
> 2)
1047 close(options
->stdin_fd
);
1048 if (options
->stdout_fd
> 2)
1049 close(options
->stdout_fd
);
1050 if (options
->stderr_fd
> 2)
1051 close(options
->stderr_fd
);
1053 /* try to remove CLOEXEC flag from stdin/stdout/stderr,
1054 * but also here, ignore errors */
1055 for (fd
= 0; fd
<= 2; fd
++) {
1056 flags
= fcntl(fd
, F_GETFL
);
1059 if (flags
& FD_CLOEXEC
)
1060 fcntl(fd
, F_SETFL
, flags
& ~FD_CLOEXEC
);
1063 /* we're done, so we can now do whatever the user intended us to do */
1064 rexit(payload
->exec_function(payload
->exec_payload
));
1067 int lxc_attach_run_command(void* payload
)
1069 lxc_attach_command_t
* cmd
= (lxc_attach_command_t
*)payload
;
1071 execvp(cmd
->program
, cmd
->argv
);
1072 SYSERROR("failed to exec '%s'", cmd
->program
);
1076 int lxc_attach_run_shell(void* payload
)
1079 struct passwd
*passwd
;
1082 /* ignore payload parameter */
1086 passwd
= getpwuid(uid
);
1088 /* this probably happens because of incompatible nss
1089 * implementations in host and container (remember, this
1090 * code is still using the host's glibc but our mount
1091 * namespace is in the container)
1092 * we may try to get the information by spawning a
1093 * [getent passwd uid] process and parsing the result
1096 user_shell
= lxc_attach_getpwshell(uid
);
1098 user_shell
= passwd
->pw_shell
;
1101 execlp(user_shell
, user_shell
, NULL
);
1103 /* executed if either no passwd entry or execvp fails,
1104 * we will fall back on /bin/sh as a default shell
1106 execlp("/bin/sh", "/bin/sh", NULL
);
1107 SYSERROR("failed to exec shell");