]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc_console.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include <sys/param.h>
36 #include <sys/types.h>
39 #include <sys/ioctl.h>
45 #include "arguments.h"
47 lxc_log_define(lxc_console_ui
, lxc_console
);
49 static char etoc(const char *expr
)
51 /* returns "control code" of given expression */
52 char c
= expr
[0] == '^' ? expr
[1] : expr
[0];
53 return 1 + ((c
> 'Z') ? (c
- 'a') : (c
- 'Z'));
56 static int my_parser(struct lxc_arguments
* args
, int c
, char* arg
)
59 case 't': args
->ttynum
= atoi(arg
); break;
60 case 'e': args
->escape
= etoc(arg
); break;
65 static const struct option my_longopts
[] = {
66 {"tty", required_argument
, 0, 't'},
67 {"escape", required_argument
, 0, 'e'},
71 static struct lxc_arguments my_args
= {
72 .progname
= "lxc-console",
74 --name=NAME [--tty NUMBER]\n\
76 lxc-console logs on the container with the identifier NAME\n\
79 -n, --name=NAME NAME for name of the container\n\
80 -t, --tty=NUMBER console tty number\n\
81 -e, --escape=PREFIX prefix for escape command\n",
82 .options
= my_longopts
,
89 static int master
= -1;
91 static void winsz(void)
94 if (ioctl(0, TIOCGWINSZ
, &wsz
) == 0)
95 ioctl(master
, TIOCSWINSZ
, &wsz
);
98 static void sigwinch(int sig
)
103 static int setup_tios(int fd
, struct termios
*newtios
, struct termios
*oldtios
)
106 ERROR("'%d' is not a tty", fd
);
110 /* Get current termios */
111 if (tcgetattr(0, oldtios
)) {
112 SYSERROR("failed to get current terminal settings");
118 /* Remove the echo characters and signal reception, the echo
119 * will be done below with master proxying */
120 newtios
->c_iflag
&= ~IGNBRK
;
121 newtios
->c_iflag
&= BRKINT
;
122 newtios
->c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
123 newtios
->c_cc
[VMIN
] = 1;
124 newtios
->c_cc
[VTIME
] = 0;
126 /* Set new attributes */
127 if (tcsetattr(0, TCSAFLUSH
, newtios
)) {
128 ERROR("failed to set new terminal settings");
135 static int stdin_handler(int fd
, void *data
, struct lxc_epoll_descr
*descr
)
137 static int wait4q
= 0;
138 int *peer
= (int *)data
;
141 if (read(0, &c
, 1) < 0) {
142 SYSERROR("failed to read");
146 /* we want to exit the console with Ctrl+a q */
147 if (c
== my_args
.escape
&& !wait4q
) {
152 if (c
== 'q' && wait4q
)
156 if (write(*peer
, &c
, 1) < 0) {
157 SYSERROR("failed to write");
164 static int master_handler(int fd
, void *data
, struct lxc_epoll_descr
*descr
)
167 int *peer
= (int *)data
;
170 r
= read(fd
, buf
, sizeof(buf
));
172 SYSERROR("failed to read");
175 r
= write(*peer
, buf
, r
);
180 int main(int argc
, char *argv
[])
183 struct lxc_epoll_descr descr
;
184 struct termios newtios
, oldtios
;
186 err
= lxc_arguments_parse(&my_args
, argc
, argv
);
190 err
= lxc_log_init(my_args
.name
, my_args
.log_file
, my_args
.log_priority
,
191 my_args
.progname
, my_args
.quiet
);
195 err
= setup_tios(0, &newtios
, &oldtios
);
197 ERROR("failed to setup tios");
201 err
= lxc_console(my_args
.name
, my_args
.ttynum
, &master
);
206 Type <Ctrl+%1$c q> to exit the console, \
207 <Ctrl+%1$c Ctrl+%1$c> to enter Ctrl+%1$c itself\n",
208 'a' + my_args
.escape
- 1);
212 INFO("already group leader");
214 if (signal(SIGWINCH
, sigwinch
) == SIG_ERR
) {
215 SYSERROR("failed to set SIGWINCH handler");
222 err
= lxc_mainloop_open(&descr
);
224 ERROR("failed to create mainloop");
228 err
= lxc_mainloop_add_handler(&descr
, 0, stdin_handler
, &master
);
230 ERROR("failed to add handler for the stdin");
231 goto out_mainloop_open
;
234 err
= lxc_mainloop_add_handler(&descr
, master
, master_handler
, &std_in
);
236 ERROR("failed to add handler for the master");
237 goto out_mainloop_open
;
240 err
= lxc_mainloop(&descr
);
242 ERROR("mainloop returned an error");
243 goto out_mainloop_open
;
249 lxc_mainloop_close(&descr
);
252 /* Restore previous terminal parameter */
253 tcsetattr(0, TCSAFLUSH
, &oldtios
);
255 /* Return to line it is */