2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2010
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/ioctl.h>
34 #include <sys/types.h>
39 #include <lxc/lxccontainer.h>
41 #include "arguments.h"
49 lxc_log_define(lxc_attach
, lxc
);
51 static int my_parser(struct lxc_arguments
*args
, int c
, char *arg
);
52 static int add_to_simple_array(char ***array
, ssize_t
*capacity
, char *value
);
53 static bool stdfd_is_pty(void);
54 static int lxc_attach_create_log_file(const char *log_file
);
56 static int elevated_privileges
;
57 static signed long new_personality
= -1;
58 static int namespace_flags
= -1;
59 static int remount_sys_proc
;
60 static lxc_attach_env_policy_t env_policy
= LXC_ATTACH_KEEP_ENV
;
61 static char **extra_env
;
62 static ssize_t extra_env_size
;
63 static char **extra_keep
;
64 static ssize_t extra_keep_size
;
66 static const struct option my_longopts
[] = {
67 {"elevated-privileges", optional_argument
, 0, 'e'},
68 {"arch", required_argument
, 0, 'a'},
69 {"namespaces", required_argument
, 0, 's'},
70 {"remount-sys-proc", no_argument
, 0, 'R'},
71 /* TODO: decide upon short option names */
72 {"clear-env", no_argument
, 0, 500},
73 {"keep-env", no_argument
, 0, 501},
74 {"keep-var", required_argument
, 0, 502},
75 {"set-var", required_argument
, 0, 'v'},
76 {"pty-log", required_argument
, 0, 'L'},
77 {"rcfile", required_argument
, 0, 'f'},
78 {"uid", required_argument
, 0, 'u'},
79 {"gid", required_argument
, 0, 'g'},
83 static struct lxc_arguments my_args
= {
84 .progname
= "lxc-attach",
86 --name=NAME [-- COMMAND]\n\
88 Execute the specified COMMAND - enter the container NAME\n\
91 -n, --name=NAME NAME of the container\n\
92 -e, --elevated-privileges=PRIVILEGES\n\
93 Use elevated privileges instead of those of the\n\
94 container. If you don't specify privileges to be\n\
95 elevated as OR'd list: CAP, CGROUP and LSM (capabilities,\n\
96 cgroup and restrictions, respectively) then all of them\n\
98 WARNING: This may leak privileges into the container.\n\
100 -a, --arch=ARCH Use ARCH for program instead of container's own\n\
102 -s, --namespaces=FLAGS\n\
103 Don't attach to all the namespaces of the container\n\
104 but just to the following OR'd list of flags:\n\
105 MOUNT, PID, UTSNAME, IPC, USER or NETWORK.\n\
106 WARNING: Using -s implies -e with all privileges\n\
107 elevated, it may therefore leak privileges into the\n\
108 container. Use with care.\n\
109 -R, --remount-sys-proc\n\
110 Remount /sys and /proc if not attaching to the\n\
111 mount namespace when using -s in order to properly\n\
112 reflect the correct namespace context. See the\n\
113 lxc-attach(1) manual page for details.\n\
114 --clear-env Clear all environment variables before attaching.\n\
115 The attached shell/program will start with only\n\
116 container=lxc set.\n\
117 --keep-env Keep all current environment variables. This\n\
118 is the current default behaviour, but is likely to\n\
119 change in the future.\n\
120 -L, --pty-log=FILE\n\
121 Log pty output to FILE\n\
122 -v, --set-var Set an additional variable that is seen by the\n\
123 attached program in the container. May be specified\n\
125 --keep-var Keep an additional environment variable. Only\n\
126 applicable if --clear-env is specified. May be used\n\
129 Load configuration file FILE\n\
130 -u, --uid=UID Execute COMMAND with UID inside the container\n\
131 -g, --gid=GID Execute COMMAND with GID inside the container\n\
133 .options
= my_longopts
,
136 .log_priority
= "ERROR",
140 static int my_parser(struct lxc_arguments
*args
, int c
, char *arg
)
146 ret
= lxc_fill_elevated_privileges(arg
, &elevated_privileges
);
150 case 'R': remount_sys_proc
= 1; break;
152 new_personality
= lxc_config_parse_arch(arg
);
153 if (new_personality
< 0) {
154 ERROR("Invalid architecture specified: %s", arg
);
161 if (lxc_namespace_2_std_identifiers(arg
) < 0)
164 ret
= lxc_fill_namespace_flags(arg
, &namespace_flags
);
169 lxc_fill_elevated_privileges(NULL
, &elevated_privileges
);
171 case 500: /* clear-env */
172 env_policy
= LXC_ATTACH_CLEAR_ENV
;
174 case 501: /* keep-env */
175 env_policy
= LXC_ATTACH_KEEP_ENV
;
177 case 502: /* keep-var */
178 ret
= add_to_simple_array(&extra_keep
, &extra_keep_size
, arg
);
180 ERROR("Failed to alloc memory");
185 ret
= add_to_simple_array(&extra_env
, &extra_env_size
, arg
);
187 ERROR("Failed to alloc memory");
192 args
->console_log
= arg
;
198 if (lxc_safe_uint(arg
, &args
->uid
) < 0)
202 if (lxc_safe_uint(arg
, &args
->gid
) < 0)
210 static int add_to_simple_array(char ***array
, ssize_t
*capacity
, char *value
)
218 for (; (*array
)[count
]; count
++);
220 /* we have to reallocate */
221 if (count
>= *capacity
- 1) {
222 ssize_t new_capacity
= ((count
+ 1) / 32 + 1) * 32;
224 char **new_array
= realloc((void*)*array
, sizeof(char *) * new_capacity
);
228 memset(&new_array
[count
], 0, sizeof(char*)*(new_capacity
- count
));
231 *capacity
= new_capacity
;
237 (*array
)[count
] = value
;
241 static bool stdfd_is_pty(void)
243 if (isatty(STDIN_FILENO
))
246 if (isatty(STDOUT_FILENO
))
249 if (isatty(STDERR_FILENO
))
255 static int lxc_attach_create_log_file(const char *log_file
)
259 fd
= open(log_file
, O_CLOEXEC
| O_RDWR
| O_CREAT
| O_APPEND
, 0600);
261 ERROR("Failed to open log file \"%s\"", log_file
);
268 int main(int argc
, char *argv
[])
274 lxc_attach_options_t attach_options
= LXC_ATTACH_OPTIONS_DEFAULT
;
275 lxc_attach_command_t command
= (lxc_attach_command_t
){.program
= NULL
};
280 if (lxc_arguments_parse(&my_args
, argc
, argv
))
283 log
.name
= my_args
.name
;
284 log
.file
= my_args
.log_file
;
285 log
.level
= my_args
.log_priority
;
286 log
.prefix
= my_args
.progname
;
287 log
.quiet
= my_args
.quiet
;
288 log
.lxcpath
= my_args
.lxcpath
[0];
290 if (lxc_log_init(&log
))
294 if (access(my_args
.lxcpath
[0], O_RDONLY
) < 0) {
295 ERROR("You lack access to %s", my_args
.lxcpath
[0]);
299 struct lxc_container
*c
= lxc_container_new(my_args
.name
, my_args
.lxcpath
[0]);
303 if (my_args
.rcfile
) {
305 if (!c
->load_config(c
, my_args
.rcfile
)) {
306 ERROR("Failed to load rcfile");
307 lxc_container_put(c
);
311 c
->configfile
= strdup(my_args
.rcfile
);
312 if (!c
->configfile
) {
313 ERROR("Out of memory setting new config filename");
314 lxc_container_put(c
);
319 if (!c
->may_control(c
)) {
320 ERROR("Insufficent privileges to control %s", c
->name
);
321 lxc_container_put(c
);
325 if (remount_sys_proc
)
326 attach_options
.attach_flags
|= LXC_ATTACH_REMOUNT_PROC_SYS
;
328 if (elevated_privileges
)
329 attach_options
.attach_flags
&= ~(elevated_privileges
);
332 attach_options
.attach_flags
|= LXC_ATTACH_TERMINAL
;
334 attach_options
.namespaces
= namespace_flags
;
335 attach_options
.personality
= new_personality
;
336 attach_options
.env_policy
= env_policy
;
337 attach_options
.extra_env_vars
= extra_env
;
338 attach_options
.extra_keep_env
= extra_keep
;
340 if (my_args
.argc
> 0) {
341 command
.program
= my_args
.argv
[0];
342 command
.argv
= (char**)my_args
.argv
;
345 if (my_args
.console_log
) {
346 attach_options
.log_fd
= lxc_attach_create_log_file(my_args
.console_log
);
347 if (attach_options
.log_fd
< 0)
352 attach_options
.uid
= my_args
.uid
;
355 attach_options
.gid
= my_args
.gid
;
358 ret
= c
->attach(c
, lxc_attach_run_command
, &command
, &attach_options
, &pid
);
360 ret
= c
->attach(c
, lxc_attach_run_shell
, NULL
, &attach_options
, &pid
);
364 ret
= lxc_wait_for_pid_status(pid
);
369 wexit
= WEXITSTATUS(ret
);
372 lxc_container_put(c
);