]>
Commit | Line | Data |
---|---|---|
5e97c3fc | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
9afe19d6 | 7 | * Daniel Lezcano <daniel.lezcano at free.fr> |
5e97c3fc | 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 | */ | |
b0a33c1e | 23 | |
24 | #define _GNU_SOURCE | |
5e97c3fc | 25 | #include <stdio.h> |
b0a33c1e | 26 | #undef _GNU_SOURCE |
27 | #include <stdlib.h> | |
28 | #include <errno.h> | |
29 | #include <string.h> | |
30 | #include <fcntl.h> | |
31 | #include <termios.h> | |
5e97c3fc | 32 | #include <unistd.h> |
b0a33c1e | 33 | #include <signal.h> |
34 | #include <libgen.h> | |
35 | #include <sys/param.h> | |
5e97c3fc | 36 | #include <sys/types.h> |
b0a33c1e | 37 | #include <sys/stat.h> |
38 | #include <sys/poll.h> | |
994f905e | 39 | #include <sys/ioctl.h> |
5e97c3fc | 40 | |
b5159817 | 41 | #include "../lxc/lxccontainer.h" |
7ee5bb55 DL |
42 | #include "error.h" |
43 | #include "lxc.h" | |
44 | #include "log.h" | |
45 | #include "mainloop.h" | |
41cfbac9 | 46 | #include "arguments.h" |
ef6e34ee | 47 | #include "commands.h" |
36eb9bde | 48 | |
77075659 | 49 | lxc_log_define(lxc_console_ui, lxc_console); |
5e97c3fc | 50 | |
84a24de2 TY |
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 | ||
41cfbac9 | 58 | static int my_parser(struct lxc_arguments* args, int c, char* arg) |
5e97c3fc | 59 | { |
41cfbac9 MN |
60 | switch (c) { |
61 | case 't': args->ttynum = atoi(arg); break; | |
84a24de2 | 62 | case 'e': args->escape = etoc(arg); break; |
41cfbac9 MN |
63 | } |
64 | return 0; | |
5e97c3fc | 65 | } |
66 | ||
41cfbac9 MN |
67 | static const struct option my_longopts[] = { |
68 | {"tty", required_argument, 0, 't'}, | |
84a24de2 | 69 | {"escape", required_argument, 0, 'e'}, |
41cfbac9 MN |
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\ | |
84a24de2 TY |
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", | |
41cfbac9 MN |
84 | .options = my_longopts, |
85 | .parser = my_parser, | |
86 | .checker = NULL, | |
87 | .ttynum = -1, | |
84a24de2 | 88 | .escape = 1, |
41cfbac9 MN |
89 | }; |
90 | ||
b0a33c1e | 91 | int main(int argc, char *argv[]) |
92 | { | |
b5159817 DE |
93 | int ret; |
94 | struct lxc_container *c; | |
b0a33c1e | 95 | |
b5159817 DE |
96 | ret = lxc_arguments_parse(&my_args, argc, argv); |
97 | if (ret) | |
98 | return EXIT_FAILURE; | |
b0a33c1e | 99 | |
b5159817 | 100 | ret = lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority, |
8d06bd13 | 101 | my_args.progname, my_args.quiet, my_args.lxcpath[0]); |
b5159817 DE |
102 | if (ret) |
103 | return EXIT_FAILURE; | |
7ee5bb55 | 104 | |
b5159817 DE |
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); | |
b0a33c1e | 109 | } |
7ee5bb55 | 110 | |
b5159817 DE |
111 | if (!c->is_running(c)) { |
112 | fprintf(stderr, "%s is not running\n", my_args.name); | |
113 | exit(EXIT_FAILURE); | |
7ee5bb55 DL |
114 | } |
115 | ||
b5159817 DE |
116 | ret = c->console(c, my_args.ttynum, 0, 1, 2, my_args.escape); |
117 | if (ret < 0) { | |
118 | exit(EXIT_FAILURE); | |
7ee5bb55 | 119 | } |
b5159817 | 120 | return EXIT_SUCCESS; |
b0a33c1e | 121 | } |