]>
git.proxmox.com Git - mirror_lxc.git/blob - src/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
30 #include <sys/types.h>
35 #include "start.h" /* for struct lxc_handler */
41 lxc_log_define(lxc_console
, lxc
);
43 extern int lxc_console(const char *name
, int ttynum
, int *fd
)
46 struct lxc_command command
= {
47 .request
= { .type
= LXC_COMMAND_TTY
, .data
= ttynum
},
50 ret
= lxc_command(name
, &command
, &stopped
);
51 if (ret
< 0 && stopped
) {
52 ERROR("'%s' is stopped", name
);
57 ERROR("failed to send command");
62 ERROR("console denied by '%s'", name
);
66 if (command
.answer
.ret
) {
67 ERROR("console access denied: %s",
68 strerror(-command
.answer
.ret
));
72 *fd
= command
.answer
.fd
;
74 ERROR("unable to allocate fd for tty %d", ttynum
);
78 INFO("tty %d allocated", ttynum
);
82 /*----------------------------------------------------------------------------
83 * functions used by lxc-start mainloop
84 * to handle above command request.
85 *--------------------------------------------------------------------------*/
86 extern void lxc_console_remove_fd(int fd
, struct lxc_tty_info
*tty_info
)
90 for (i
= 0; i
< tty_info
->nbtty
; i
++) {
92 if (tty_info
->pty_info
[i
].busy
!= fd
)
95 tty_info
->pty_info
[i
].busy
= 0;
101 extern int lxc_console_callback(int fd
, struct lxc_request
*request
,
102 struct lxc_handler
*handler
)
104 int ttynum
= request
->data
;
105 struct lxc_tty_info
*tty_info
= &handler
->conf
->tty_info
;
108 if (ttynum
> tty_info
->nbtty
)
111 if (tty_info
->pty_info
[ttynum
- 1].busy
)
117 /* fixup index tty1 => [0] */
119 ttynum
<= tty_info
->nbtty
&& tty_info
->pty_info
[ttynum
- 1].busy
;
122 /* we didn't find any available slot for tty */
123 if (ttynum
> tty_info
->nbtty
)
127 if (lxc_af_unix_send_fd(fd
, tty_info
->pty_info
[ttynum
- 1].master
,
128 &ttynum
, sizeof(ttynum
)) < 0) {
129 ERROR("failed to send tty to client");
133 tty_info
->pty_info
[ttynum
- 1].busy
= fd
;
138 /* the close fd and related cleanup will be done by caller */
142 static int get_default_console(char **console
)
146 if (!access("/dev/tty", F_OK
)) {
147 fd
= open("/dev/tty", O_RDWR
);
150 *console
= strdup("/dev/tty");
155 if (!access("/dev/null", F_OK
)) {
156 *console
= strdup("/dev/null");
160 ERROR("No suitable default console");
162 return *console
? 0 : -1;
165 int lxc_create_console(struct lxc_conf
*conf
)
168 struct lxc_console
*console
= &conf
->console
;
171 if (!conf
->rootfs
.path
)
174 if (!console
->path
&& get_default_console(&console
->path
)) {
175 ERROR("failed to get default console");
179 if (openpty(&console
->master
, &console
->slave
,
180 console
->name
, NULL
, NULL
)) {
181 SYSERROR("failed to allocate a pty");
185 if (fcntl(console
->master
, F_SETFD
, FD_CLOEXEC
)) {
186 SYSERROR("failed to set console master to close-on-exec");
190 if (fcntl(console
->slave
, F_SETFD
, FD_CLOEXEC
)) {
191 SYSERROR("failed to set console slave to close-on-exec");
195 fd
= lxc_unpriv(open(console
->path
, O_CLOEXEC
| O_RDWR
| O_CREAT
|
198 SYSERROR("failed to open '%s'", console
->path
);
202 DEBUG("using '%s' as console", console
->path
);
206 if (!isatty(console
->peer
))
209 console
->tios
= malloc(sizeof(tios
));
210 if (!console
->tios
) {
211 SYSERROR("failed to allocate memory");
216 if (tcgetattr(console
->peer
, console
->tios
)) {
217 SYSERROR("failed to get current terminal settings");
221 tios
= *console
->tios
;
223 /* Remove the echo characters and signal reception, the echo
224 * will be done below with master proxying */
225 tios
.c_iflag
&= ~IGNBRK
;
226 tios
.c_iflag
&= BRKINT
;
227 tios
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
);
229 tios
.c_cc
[VTIME
] = 0;
231 /* Set new attributes */
232 if (tcsetattr(console
->peer
, TCSAFLUSH
, &tios
)) {
233 ERROR("failed to set new terminal settings");
242 close(console
->master
);
243 close(console
->slave
);
247 void lxc_delete_console(const struct lxc_console
*console
)
250 tcsetattr(console
->peer
, TCSAFLUSH
, console
->tios
))
251 WARN("failed to set old terminal settings");
252 close(console
->master
);
253 close(console
->slave
);
256 static int console_handler(int fd
, void *data
, struct lxc_epoll_descr
*descr
)
258 struct lxc_console
*console
= (struct lxc_console
*)data
;
262 r
= read(fd
, buf
, sizeof(buf
));
264 SYSERROR("failed to read");
269 INFO("console client has exited");
270 lxc_mainloop_del_handler(descr
, fd
);
275 /* no output for the console, do nothing */
276 if (console
->peer
== -1)
279 if (console
->peer
== fd
)
280 r
= write(console
->master
, buf
, r
);
282 r
= write(console
->peer
, buf
, r
);
287 int lxc_console_mainloop_add(struct lxc_epoll_descr
*descr
,
288 struct lxc_handler
*handler
)
290 struct lxc_conf
*conf
= handler
->conf
;
291 struct lxc_console
*console
= &conf
->console
;
293 if (!conf
->rootfs
.path
) {
294 INFO("no rootfs, no console.");
298 if (!console
->path
) {
299 INFO("no console specified");
303 if (lxc_mainloop_add_handler(descr
, console
->master
,
304 console_handler
, console
)) {
305 ERROR("failed to add to mainloop console handler for '%d'",
310 if (console
->peer
!= -1 &&
311 lxc_mainloop_add_handler(descr
, console
->peer
,
312 console_handler
, console
))
313 WARN("console input disabled");