]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc_console.c
console API improvements
[mirror_lxc.git] / src / lxc / lxc_console.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
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.
13 *
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.
18 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #define _GNU_SOURCE
25 #include <stdio.h>
26 #undef _GNU_SOURCE
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <termios.h>
32 #include <unistd.h>
33 #include <signal.h>
34 #include <libgen.h>
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/poll.h>
39 #include <sys/ioctl.h>
40
41 #include "../lxc/lxccontainer.h"
42 #include "error.h"
43 #include "lxc.h"
44 #include "log.h"
45 #include "mainloop.h"
46 #include "arguments.h"
47 #include "commands.h"
48
49 lxc_log_define(lxc_console_ui, lxc_console);
50
51 static char etoc(const char *expr)
52 {
53 /* returns "control code" of given expression */
54 char c = expr[0] == '^' ? expr[1] : expr[0];
55 return 1 + ((c > 'Z') ? (c - 'a') : (c - 'Z'));
56 }
57
58 static int my_parser(struct lxc_arguments* args, int c, char* arg)
59 {
60 switch (c) {
61 case 't': args->ttynum = atoi(arg); break;
62 case 'e': args->escape = etoc(arg); break;
63 }
64 return 0;
65 }
66
67 static const struct option my_longopts[] = {
68 {"tty", required_argument, 0, 't'},
69 {"escape", required_argument, 0, 'e'},
70 LXC_COMMON_OPTIONS
71 };
72
73 static struct lxc_arguments my_args = {
74 .progname = "lxc-console",
75 .help = "\
76 --name=NAME [--tty NUMBER]\n\
77 \n\
78 lxc-console logs on the container with the identifier NAME\n\
79 \n\
80 Options :\n\
81 -n, --name=NAME NAME for name of the container\n\
82 -t, --tty=NUMBER console tty number\n\
83 -e, --escape=PREFIX prefix for escape command\n",
84 .options = my_longopts,
85 .parser = my_parser,
86 .checker = NULL,
87 .ttynum = -1,
88 .escape = 1,
89 };
90
91 int main(int argc, char *argv[])
92 {
93 int ret;
94 struct lxc_container *c;
95
96 ret = lxc_arguments_parse(&my_args, argc, argv);
97 if (ret)
98 return EXIT_FAILURE;
99
100 ret = lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
101 my_args.progname, my_args.quiet, my_args.lxcpath[0]);
102 if (ret)
103 return EXIT_FAILURE;
104
105 c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
106 if (!c) {
107 fprintf(stderr, "System error loading container\n");
108 exit(EXIT_FAILURE);
109 }
110
111 if (!c->is_running(c)) {
112 fprintf(stderr, "%s is not running\n", my_args.name);
113 exit(EXIT_FAILURE);
114 }
115
116 ret = c->console(c, my_args.ttynum, 0, 1, 2, my_args.escape);
117 if (ret < 0) {
118 exit(EXIT_FAILURE);
119 }
120 return EXIT_SUCCESS;
121 }