]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/tools/lxc_console.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
14 #include <sys/ioctl.h>
15 #include <sys/param.h>
17 #include <sys/types.h>
22 #include "arguments.h"
27 lxc_log_define(lxc_console
, lxc
);
29 static int my_parser(struct lxc_arguments
*args
, int c
, char *arg
);
30 static char etoc(const char *expr
);
32 static const struct option my_longopts
[] = {
33 {"tty", required_argument
, 0, 't'},
34 {"escape", required_argument
, 0, 'e'},
38 static struct lxc_arguments my_args
= {
39 .progname
= "lxc-console",
41 --name=NAME [--tty NUMBER]\n\
43 lxc-console logs on the container with the identifier NAME\n\
46 -n, --name=NAME NAME of the container\n\
47 -t, --tty=NUMBER console tty number\n\
48 -e, --escape=PREFIX prefix for escape command\n\
49 --rcfile=FILE Load configuration file FILE\n",
50 .options
= my_longopts
,
53 .log_priority
= "ERROR",
59 static int my_parser(struct lxc_arguments
*args
, int c
, char *arg
)
63 if (lxc_safe_uint(arg
, &args
->ttynum
) < 0)
67 args
->escape
= etoc(arg
);
74 static char etoc(const char *expr
)
76 /* returns "control code" of given expression */
77 char c
= expr
[0] == '^' ? expr
[1] : expr
[0];
79 return 1 + ((c
> 'Z') ? (c
- 'a') : (c
- 'Z'));
82 int main(int argc
, char *argv
[])
85 struct lxc_container
*c
;
88 if (lxc_arguments_parse(&my_args
, argc
, argv
))
91 log
.name
= my_args
.name
;
92 log
.file
= my_args
.log_file
;
93 log
.level
= my_args
.log_priority
;
94 log
.prefix
= my_args
.progname
;
95 log
.quiet
= my_args
.quiet
;
96 log
.lxcpath
= my_args
.lxcpath
[0];
98 if (lxc_log_init(&log
))
101 c
= lxc_container_new(my_args
.name
, my_args
.lxcpath
[0]);
103 ERROR("System error loading container");
107 if (my_args
.rcfile
) {
110 if (!c
->load_config(c
, my_args
.rcfile
)) {
111 ERROR("Failed to load rcfile");
112 lxc_container_put(c
);
116 c
->configfile
= strdup(my_args
.rcfile
);
117 if (!c
->configfile
) {
118 ERROR("Out of memory setting new config filename");
119 lxc_container_put(c
);
124 if (!c
->may_control(c
)) {
125 ERROR("Insufficent privileges to control %s", my_args
.name
);
126 lxc_container_put(c
);
130 if (!c
->is_running(c
)) {
131 ERROR("%s is not running", my_args
.name
);
132 lxc_container_put(c
);
136 ret
= c
->console(c
, my_args
.ttynum
, 0, 1, 2, my_args
.escape
);
138 lxc_container_put(c
);
142 lxc_container_put(c
);