]>
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>
45 fprintf(stderr
, "%s <command>\n", basename(cmd
));
46 fprintf(stderr
, "\t -n <name> : name of the container\n");
47 fprintf(stderr
, "\t -t <tty#> : tty number\n");
51 int main(int argc
, char *argv
[])
59 int err
= LXC_ERROR_INTERNAL
;
60 struct termios tios
, oldtios
;
62 while ((opt
= getopt(argc
, argv
, "t:n:")) != -1) {
68 ttynum
= atoi(optarg
);
78 /* Get current termios */
79 if (tcgetattr(0, &tios
)) {
80 lxc_log_error("failed to get current terminal settings");
81 fprintf(stderr
, "%s\n", lxc_strerror(err
));
87 /* Remove the echo characters and signal reception, the echo
88 * will be done below with master proxying */
89 tios
.c_iflag
&= ~IGNBRK
;
90 tios
.c_iflag
&= BRKINT
;
91 tios
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
95 /* Set new attributes */
96 if (tcsetattr(0, TCSAFLUSH
, &tios
)) {
97 lxc_log_syserror("failed to set new terminal settings");
98 fprintf(stderr
, "%s\n", lxc_strerror(err
));
102 err
= lxc_console(name
, ttynum
, &master
);
104 fprintf(stderr
, "%s\n", lxc_strerror(err
));
108 fprintf(stderr
, "\nType <Ctrl+a q> to exit the console\n");
114 /* let's proxy the tty */
117 struct pollfd pfd
[2] = {
119 .events
= POLLIN
|POLLPRI
,
122 .events
= POLLIN
|POLLPRI
,
126 if (poll(pfd
, 2, -1) < 0) {
129 lxc_log_syserror("failed to poll");
133 /* read the "stdin" and write that to the master
135 if (pfd
[0].revents
& POLLIN
) {
138 /* we want to exit the console with Ctrl+a q */
144 if (c
== 'q' && wait4q
)
148 write(master
, &c
, 1);
151 /* other side has closed the connection */
152 if (pfd
[1].revents
& POLLHUP
)
155 /* read the master and write to "stdout" */
156 if (pfd
[1].revents
& POLLIN
) {
163 /* Restore previous terminal parameter */
164 tcsetattr(0, TCSAFLUSH
, &oldtios
);
166 /* Return to line it is */
174 fprintf(stderr
, "%s\n", lxc_strerror(-LXC_ERROR_INTERNAL
));