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
30 #include <sys/ioctl.h>
31 #include <sys/types.h>
36 #include <lxc/lxccontainer.h>
39 #include "arguments.h"
51 #include <../include/openpty.h>
54 lxc_log_define(lxc_attach_ui
, lxc
);
56 static const struct option my_longopts
[] = {
57 {"elevated-privileges", optional_argument
, 0, 'e'},
58 {"arch", required_argument
, 0, 'a'},
59 {"namespaces", required_argument
, 0, 's'},
60 {"remount-sys-proc", no_argument
, 0, 'R'},
61 /* TODO: decide upon short option names */
62 {"clear-env", no_argument
, 0, 500},
63 {"keep-env", no_argument
, 0, 501},
64 {"keep-var", required_argument
, 0, 502},
65 {"set-var", required_argument
, 0, 'v'},
66 {"pty-log", required_argument
, 0, 'L'},
67 {"rcfile", required_argument
, 0, 'f'},
71 static int elevated_privileges
= 0;
72 static signed long new_personality
= -1;
73 static int namespace_flags
= -1;
74 static int remount_sys_proc
= 0;
75 static lxc_attach_env_policy_t env_policy
= LXC_ATTACH_KEEP_ENV
;
76 static char **extra_env
= NULL
;
77 static ssize_t extra_env_size
= 0;
78 static char **extra_keep
= NULL
;
79 static ssize_t extra_keep_size
= 0;
81 static int add_to_simple_array(char ***array
, ssize_t
*capacity
, char *value
)
89 for (; (*array
)[count
]; count
++);
91 /* we have to reallocate */
92 if (count
>= *capacity
- 1) {
93 ssize_t new_capacity
= ((count
+ 1) / 32 + 1) * 32;
94 char **new_array
= realloc((void*)*array
, sizeof(char *) * new_capacity
);
97 memset(&new_array
[count
], 0, sizeof(char*)*(new_capacity
- count
));
99 *capacity
= new_capacity
;
105 (*array
)[count
] = value
;
109 static int my_parser(struct lxc_arguments
* args
, int c
, char* arg
)
117 ret
= lxc_fill_elevated_privileges(arg
, &elevated_privileges
);
121 case 'R': remount_sys_proc
= 1; break;
123 new_personality
= lxc_config_parse_arch(arg
);
124 if (new_personality
< 0) {
125 lxc_error(args
, "invalid architecture specified: %s", arg
);
132 /* The identifiers for namespaces used with lxc-attach as given
133 * on the manpage do not align with the standard identifiers.
134 * This affects network, mount, and uts namespaces. The standard
135 * identifiers are: "mnt", "uts", and "net" whereas lxc-attach
136 * uses "MOUNT", "UTSNAME", and "NETWORK". So let's use some
137 * cheap memmove()s to replace them by their standard
138 * identifiers. Let's illustrate this with an example:
145 * dest: del + 1 == OUNT|PID
146 * src: del + 3 == NT|PID
148 while ((del
= strstr(arg
, "MOUNT")))
149 memmove(del
+ 1, del
+ 3, strlen(del
) - 2);
151 for (it
= (char *[]){"NETWORK", "UTSNAME", NULL
}; it
&& *it
; it
++)
152 while ((del
= strstr(arg
, *it
)))
153 memmove(del
+ 3, del
+ 7, strlen(del
) - 6);
155 ret
= lxc_fill_namespace_flags(arg
, &namespace_flags
);
159 lxc_fill_elevated_privileges(NULL
, &elevated_privileges
);
161 case 500: /* clear-env */
162 env_policy
= LXC_ATTACH_CLEAR_ENV
;
164 case 501: /* keep-env */
165 env_policy
= LXC_ATTACH_KEEP_ENV
;
167 case 502: /* keep-var */
168 ret
= add_to_simple_array(&extra_keep
, &extra_keep_size
, arg
);
170 lxc_error(args
, "memory allocation error");
175 ret
= add_to_simple_array(&extra_env
, &extra_env_size
, arg
);
177 lxc_error(args
, "memory allocation error");
182 args
->console_log
= arg
;
192 static struct lxc_arguments my_args
= {
193 .progname
= "lxc-attach",
195 --name=NAME [-- COMMAND]\n\
197 Execute the specified COMMAND - enter the container NAME\n\
200 -n, --name=NAME NAME of the container\n\
201 -e, --elevated-privileges=PRIVILEGES\n\
202 Use elevated privileges instead of those of the\n\
203 container. If you don't specify privileges to be\n\
204 elevated as OR'd list: CAP, CGROUP and LSM (capabilities,\n\
205 cgroup and restrictions, respectively) then all of them\n\
207 WARNING: This may leak privileges into the container.\n\
209 -a, --arch=ARCH Use ARCH for program instead of container's own\n\
211 -s, --namespaces=FLAGS\n\
212 Don't attach to all the namespaces of the container\n\
213 but just to the following OR'd list of flags:\n\
214 MOUNT, PID, UTSNAME, IPC, USER or NETWORK.\n\
215 WARNING: Using -s implies -e with all privileges\n\
216 elevated, it may therefore leak privileges into the\n\
217 container. Use with care.\n\
218 -R, --remount-sys-proc\n\
219 Remount /sys and /proc if not attaching to the\n\
220 mount namespace when using -s in order to properly\n\
221 reflect the correct namespace context. See the\n\
222 lxc-attach(1) manual page for details.\n\
223 --clear-env Clear all environment variables before attaching.\n\
224 The attached shell/program will start with only\n\
225 container=lxc set.\n\
226 --keep-env Keep all current environment variables. This\n\
227 is the current default behaviour, but is likely to\n\
228 change in the future.\n\
229 -L, --pty-log=FILE\n\
230 Log pty output to FILE\n\
231 -v, --set-var Set an additional variable that is seen by the\n\
232 attached program in the container. May be specified\n\
234 --keep-var Keep an additional environment variable. Only\n\
235 applicable if --clear-env is specified. May be used\n\
238 Load configuration file FILE\n\
240 .options
= my_longopts
,
246 lxc_attach_options_t
*options
;
247 lxc_attach_command_t
*command
;
248 struct lxc_console
*console
;
252 /* Minimalistic login_tty() implementation. */
253 static int login_pty(int fd
)
256 if (ioctl(fd
, TIOCSCTTY
, NULL
) < 0)
258 if (lxc_console_set_stdfds(fd
) < 0)
260 if (fd
> STDERR_FILENO
)
265 static int get_pty_on_host_callback(void *p
)
267 struct wrapargs
*wrap
= p
;
269 close(wrap
->console
->master
);
270 if (login_pty(wrap
->console
->slave
) < 0)
273 if (wrap
->command
->program
)
274 lxc_attach_run_command(wrap
->command
);
276 lxc_attach_run_shell(NULL
);
280 static int get_pty_on_host(struct lxc_container
*c
, struct wrapargs
*wrap
, int *pid
)
283 struct wrapargs
*args
= wrap
;
284 struct lxc_epoll_descr descr
;
285 struct lxc_conf
*conf
;
286 struct lxc_tty_state
*ts
;
288 INFO("Trying to allocate a pty on the host");
290 if (!isatty(args
->ptyfd
)) {
291 ERROR("Standard file descriptor does not refer to a pty\n.");
296 free(conf
->console
.log_path
);
297 if (my_args
.console_log
)
298 conf
->console
.log_path
= strdup(my_args
.console_log
);
300 conf
->console
.log_path
= NULL
;
302 /* In the case of lxc-attach our peer pty will always be the current
303 * controlling terminal. We clear whatever was set by the user for
304 * lxc.console.path here and set it NULL. lxc_console_peer_default()
305 * will then try to open /dev/tty. If the process doesn't have a
306 * controlling terminal we should still proceed.
308 free(conf
->console
.path
);
309 conf
->console
.path
= NULL
;
311 /* Create pty on the host. */
312 if (lxc_console_create(conf
) < 0)
314 ts
= conf
->console
.tty_state
;
315 conf
->console
.descr
= &descr
;
317 /* Shift ttys to container. */
318 if (lxc_ttys_shift_ids(conf
) < 0) {
319 ERROR("Failed to shift tty into container");
323 /* Send wrapper function on its way. */
324 wrap
->console
= &conf
->console
;
325 if (c
->attach(c
, get_pty_on_host_callback
, wrap
, wrap
->options
, pid
) < 0)
327 close(conf
->console
.slave
); /* Close slave side. */
329 ret
= lxc_mainloop_open(&descr
);
331 ERROR("failed to create mainloop");
335 if (lxc_console_mainloop_add(&descr
, conf
) < 0) {
336 ERROR("Failed to add handlers to lxc mainloop.");
340 ret
= lxc_mainloop(&descr
, -1);
342 ERROR("mainloop returned an error");
348 lxc_mainloop_close(&descr
);
350 if (ts
&& ts
->sigfd
!= -1)
351 lxc_console_sigwinch_fini(ts
);
353 lxc_console_delete(&conf
->console
);
358 static int stdfd_is_pty(void)
360 if (isatty(STDIN_FILENO
))
362 if (isatty(STDOUT_FILENO
))
363 return STDOUT_FILENO
;
364 if (isatty(STDERR_FILENO
))
365 return STDERR_FILENO
;
370 int main(int argc
, char *argv
[])
375 lxc_attach_options_t attach_options
= LXC_ATTACH_OPTIONS_DEFAULT
;
376 lxc_attach_command_t command
= (lxc_attach_command_t
){.program
= NULL
};
382 r
= lxc_arguments_parse(&my_args
, argc
, argv
);
386 if (!my_args
.log_file
)
387 my_args
.log_file
= "none";
389 r
= lxc_log_init(my_args
.name
, my_args
.log_file
, my_args
.log_priority
,
390 my_args
.progname
, my_args
.quiet
, my_args
.lxcpath
[0]);
393 lxc_log_options_no_override();
396 if (access(my_args
.lxcpath
[0], O_RDONLY
) < 0) {
398 fprintf(stderr
, "You lack access to %s\n", my_args
.lxcpath
[0]);
403 struct lxc_container
*c
= lxc_container_new(my_args
.name
, my_args
.lxcpath
[0]);
407 if (my_args
.rcfile
) {
409 if (!c
->load_config(c
, my_args
.rcfile
)) {
410 ERROR("Failed to load rcfile");
411 lxc_container_put(c
);
414 c
->configfile
= strdup(my_args
.rcfile
);
415 if (!c
->configfile
) {
416 ERROR("Out of memory setting new config filename");
417 lxc_container_put(c
);
422 if (!c
->may_control(c
)) {
423 fprintf(stderr
, "Insufficent privileges to control %s\n", c
->name
);
424 lxc_container_put(c
);
428 if (!c
->is_defined(c
)) {
429 fprintf(stderr
, "Error: container %s is not defined\n", c
->name
);
430 lxc_container_put(c
);
434 if (remount_sys_proc
)
435 attach_options
.attach_flags
|= LXC_ATTACH_REMOUNT_PROC_SYS
;
436 if (elevated_privileges
)
437 attach_options
.attach_flags
&= ~(elevated_privileges
);
438 attach_options
.namespaces
= namespace_flags
;
439 attach_options
.personality
= new_personality
;
440 attach_options
.env_policy
= env_policy
;
441 attach_options
.extra_env_vars
= extra_env
;
442 attach_options
.extra_keep_env
= extra_keep
;
444 if (my_args
.argc
> 0) {
445 command
.program
= my_args
.argv
[0];
446 command
.argv
= (char**)my_args
.argv
;
449 struct wrapargs wrap
= (struct wrapargs
){
451 .options
= &attach_options
454 wrap
.ptyfd
= stdfd_is_pty();
455 if (wrap
.ptyfd
>= 0) {
456 if ((!isatty(STDOUT_FILENO
) || !isatty(STDERR_FILENO
)) && my_args
.console_log
) {
457 fprintf(stderr
, "-L/--pty-log can only be used when stdout and stderr refer to a pty.\n");
460 ret
= get_pty_on_host(c
, &wrap
, &pid
);
462 if (my_args
.console_log
) {
463 fprintf(stderr
, "-L/--pty-log can only be used when stdout and stderr refer to a pty.\n");
467 ret
= c
->attach(c
, lxc_attach_run_command
, &command
, &attach_options
, &pid
);
469 ret
= c
->attach(c
, lxc_attach_run_shell
, NULL
, &attach_options
, &pid
);
475 ret
= lxc_wait_for_pid_status(pid
);
480 wexit
= WEXITSTATUS(ret
);
482 lxc_container_put(c
);