2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/epoll.h>
33 #include <sys/types.h>
35 #include <lxc/lxccontainer.h>
46 #include "start.h" /* for struct lxc_handler */
52 #include <../include/openpty.h>
55 lxc_log_define(lxc_console
, lxc
);
57 static struct lxc_list lxc_ttys
;
59 typedef void (*sighandler_t
)(int);
61 __attribute__((constructor
))
62 void lxc_console_init(void)
64 lxc_list_init(&lxc_ttys
);
67 void lxc_console_winsz(int srcfd
, int dstfd
)
70 if (isatty(srcfd
) && ioctl(srcfd
, TIOCGWINSZ
, &wsz
) == 0) {
71 DEBUG("set winsz dstfd:%d cols:%d rows:%d", dstfd
,
72 wsz
.ws_col
, wsz
.ws_row
);
73 ioctl(dstfd
, TIOCSWINSZ
, &wsz
);
77 static void lxc_console_winch(struct lxc_tty_state
*ts
)
79 lxc_console_winsz(ts
->stdinfd
, ts
->masterfd
);
81 lxc_cmd_console_winch(ts
->winch_proxy
, ts
->winch_proxy_lxcpath
);
84 void lxc_console_sigwinch(int sig
)
87 struct lxc_tty_state
*ts
;
89 lxc_list_for_each(it
, &lxc_ttys
) {
91 lxc_console_winch(ts
);
95 int lxc_console_cb_sigwinch_fd(int fd
, uint32_t events
, void *cbdata
,
96 struct lxc_epoll_descr
*descr
)
98 struct signalfd_siginfo siginfo
;
99 struct lxc_tty_state
*ts
= cbdata
;
101 ssize_t ret
= read(fd
, &siginfo
, sizeof(siginfo
));
102 if (ret
< 0 || (size_t)ret
< sizeof(siginfo
)) {
103 ERROR("failed to read signal info");
107 lxc_console_winch(ts
);
111 struct lxc_tty_state
*lxc_console_sigwinch_init(int srcfd
, int dstfd
)
114 struct lxc_tty_state
*ts
;
116 ts
= malloc(sizeof(*ts
));
120 memset(ts
, 0, sizeof(*ts
));
122 ts
->masterfd
= dstfd
;
125 /* add tty to list to be scanned at SIGWINCH time */
126 lxc_list_add_elem(&ts
->node
, ts
);
127 lxc_list_add_tail(&lxc_ttys
, &ts
->node
);
130 sigaddset(&mask
, SIGWINCH
);
131 if (sigprocmask(SIG_BLOCK
, &mask
, &ts
->oldmask
)) {
132 SYSERROR("failed to block SIGWINCH.");
137 ts
->sigfd
= signalfd(-1, &mask
, 0);
139 SYSERROR("failed to get signalfd.");
140 sigprocmask(SIG_SETMASK
, &ts
->oldmask
, NULL
);
145 DEBUG("%d got SIGWINCH fd %d", getpid(), ts
->sigfd
);
149 void lxc_console_sigwinch_fini(struct lxc_tty_state
*ts
)
154 lxc_list_del(&ts
->node
);
155 sigprocmask(SIG_SETMASK
, &ts
->oldmask
, NULL
);
159 static int lxc_console_cb_con(int fd
, uint32_t events
, void *data
,
160 struct lxc_epoll_descr
*descr
)
162 struct lxc_console
*console
= (struct lxc_console
*)data
;
166 w
= r
= lxc_read_nointr(fd
, buf
, sizeof(buf
));
168 INFO("console client on fd %d has exited", fd
);
169 lxc_mainloop_del_handler(descr
, fd
);
174 if (fd
== console
->peer
)
175 w
= lxc_write_nointr(console
->master
, buf
, r
);
177 if (fd
== console
->master
) {
178 if (console
->log_fd
>= 0)
179 w
= lxc_write_nointr(console
->log_fd
, buf
, r
);
181 if (console
->peer
>= 0)
182 w
= lxc_write_nointr(console
->peer
, buf
, r
);
186 WARN("console short write r:%d w:%d", r
, w
);
191 static void lxc_console_mainloop_add_peer(struct lxc_console
*console
)
193 if (console
->peer
>= 0) {
194 if (lxc_mainloop_add_handler(console
->descr
, console
->peer
,
195 lxc_console_cb_con
, console
))
196 WARN("console peer not added to mainloop");
199 if (console
->tty_state
&& console
->tty_state
->sigfd
!= -1) {
200 if (lxc_mainloop_add_handler(console
->descr
,
201 console
->tty_state
->sigfd
,
202 lxc_console_cb_sigwinch_fd
,
203 console
->tty_state
)) {
204 WARN("failed to add to mainloop SIGWINCH handler for '%d'",
205 console
->tty_state
->sigfd
);
210 extern int lxc_console_mainloop_add(struct lxc_epoll_descr
*descr
,
211 struct lxc_conf
*conf
)
213 struct lxc_console
*console
= &conf
->console
;
215 if (conf
->is_execute
) {
216 INFO("no console for lxc-execute.");
220 if (!conf
->rootfs
.path
) {
221 INFO("no rootfs, no console.");
225 if (console
->master
< 0) {
230 if (lxc_mainloop_add_handler(descr
, console
->master
,
231 lxc_console_cb_con
, console
)) {
232 ERROR("failed to add to mainloop console handler for '%d'",
237 /* we cache the descr so that we can add an fd to it when someone
238 * does attach to it in lxc_console_allocate()
240 console
->descr
= descr
;
241 lxc_console_mainloop_add_peer(console
);
246 int lxc_setup_tios(int fd
, struct termios
*oldtios
)
248 struct termios newtios
;
251 ERROR("'%d' is not a tty", fd
);
255 /* Get current termios */
256 if (tcgetattr(fd
, oldtios
)) {
257 SYSERROR("failed to get current terminal settings");
263 /* Remove the echo characters and signal reception, the echo
264 * will be done with master proxying */
265 newtios
.c_iflag
&= ~(IGNBRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
|IXANY
|IXOFF
);
266 newtios
.c_iflag
&= BRKINT
;
267 newtios
.c_lflag
&= ~(ECHO
|ICANON
|ISIG
|IEXTEN
|ECHOE
|ECHOK
|ECHONL
);
268 newtios
.c_oflag
&= ~OPOST
;
269 newtios
.c_cc
[VMIN
] = 1;
270 newtios
.c_cc
[VTIME
] = 0;
272 /* Set new attributes */
273 if (tcsetattr(fd
, TCSAFLUSH
, &newtios
)) {
274 ERROR("failed to set new terminal settings");
281 static void lxc_console_peer_proxy_free(struct lxc_console
*console
)
283 if (console
->tty_state
&& console
->tty_state
->sigfd
!= -1) {
284 lxc_console_sigwinch_fini(console
->tty_state
);
285 console
->tty_state
= NULL
;
287 close(console
->peerpty
.master
);
288 close(console
->peerpty
.slave
);
289 console
->peerpty
.master
= -1;
290 console
->peerpty
.slave
= -1;
291 console
->peerpty
.busy
= -1;
292 console
->peerpty
.name
[0] = '\0';
296 static int lxc_console_peer_proxy_alloc(struct lxc_console
*console
, int sockfd
)
298 struct termios oldtermio
;
299 struct lxc_tty_state
*ts
;
302 if (console
->master
< 0) {
303 ERROR("console not set up");
306 if (console
->peerpty
.busy
!= -1 || console
->peer
!= -1) {
307 NOTICE("console already in use");
310 if (console
->tty_state
) {
311 ERROR("console already has tty_state");
315 /* this is the proxy pty that will be given to the client, and that
316 * the real pty master will send to / recv from
319 ret
= openpty(&console
->peerpty
.master
, &console
->peerpty
.slave
,
320 console
->peerpty
.name
, NULL
, NULL
);
323 SYSERROR("failed to create proxy pty");
327 if (lxc_setup_tios(console
->peerpty
.slave
, &oldtermio
) < 0)
330 ts
= lxc_console_sigwinch_init(console
->peerpty
.master
, console
->master
);
334 console
->tty_state
= ts
;
335 console
->peer
= console
->peerpty
.slave
;
336 console
->peerpty
.busy
= sockfd
;
337 lxc_console_mainloop_add_peer(console
);
339 DEBUG("%d %s peermaster:%d sockfd:%d", getpid(), __FUNCTION__
, console
->peerpty
.master
, sockfd
);
343 lxc_console_peer_proxy_free(console
);
347 int lxc_console_allocate(struct lxc_conf
*conf
, int sockfd
, int *ttyreq
)
349 int masterfd
= -1, ttynum
;
350 struct lxc_tty_info
*tty_info
= &conf
->tty_info
;
351 struct lxc_console
*console
= &conf
->console
;
354 if (lxc_console_peer_proxy_alloc(console
, sockfd
) < 0)
356 masterfd
= console
->peerpty
.master
;
361 if (*ttyreq
> tty_info
->nbtty
)
364 if (tty_info
->pty_info
[*ttyreq
- 1].busy
)
367 /* the requested tty is available */
372 /* search for next available tty, fixup index tty1 => [0] */
373 for (ttynum
= 1; ttynum
<= tty_info
->nbtty
&& tty_info
->pty_info
[ttynum
- 1].busy
; ttynum
++)
376 /* we didn't find any available slot for tty */
377 if (ttynum
> tty_info
->nbtty
)
383 tty_info
->pty_info
[ttynum
- 1].busy
= sockfd
;
384 masterfd
= tty_info
->pty_info
[ttynum
- 1].master
;
389 void lxc_console_free(struct lxc_conf
*conf
, int fd
)
392 struct lxc_tty_info
*tty_info
= &conf
->tty_info
;
393 struct lxc_console
*console
= &conf
->console
;
395 for (i
= 0; i
< tty_info
->nbtty
; i
++) {
396 if (tty_info
->pty_info
[i
].busy
== fd
)
397 tty_info
->pty_info
[i
].busy
= 0;
400 if (console
->peerpty
.busy
== fd
) {
401 lxc_mainloop_del_handler(console
->descr
, console
->peerpty
.slave
);
402 lxc_console_peer_proxy_free(console
);
406 static void lxc_console_peer_default(struct lxc_console
*console
)
408 struct lxc_tty_state
*ts
;
409 const char *path
= console
->path
;
411 /* if no console was given, try current controlling terminal, there
412 * won't be one if we were started as a daemon (-d)
414 if (!path
&& !access("/dev/tty", F_OK
)) {
416 fd
= open("/dev/tty", O_RDWR
);
426 DEBUG("opening %s for console peer", path
);
427 console
->peer
= lxc_unpriv(open(path
, O_CLOEXEC
| O_RDWR
| O_CREAT
|
429 if (console
->peer
< 0)
432 DEBUG("using '%s' as console", path
);
434 if (!isatty(console
->peer
))
437 ts
= lxc_console_sigwinch_init(console
->peer
, console
->master
);
438 console
->tty_state
= ts
;
440 WARN("Unable to install SIGWINCH");
444 lxc_console_winsz(console
->peer
, console
->master
);
446 console
->tios
= malloc(sizeof(*console
->tios
));
447 if (!console
->tios
) {
448 SYSERROR("failed to allocate memory");
452 if (lxc_setup_tios(console
->peer
, console
->tios
) < 0)
459 console
->tios
= NULL
;
461 close(console
->peer
);
464 DEBUG("no console peer");
468 void lxc_console_delete(struct lxc_console
*console
)
470 if (console
->tios
&& console
->peer
>= 0 &&
471 tcsetattr(console
->peer
, TCSAFLUSH
, console
->tios
))
472 WARN("failed to set old terminal settings");
474 console
->tios
= NULL
;
476 close(console
->peer
);
477 close(console
->master
);
478 close(console
->slave
);
479 if (console
->log_fd
>= 0)
480 close(console
->log_fd
);
483 console
->master
= -1;
485 console
->log_fd
= -1;
488 int lxc_console_create(struct lxc_conf
*conf
)
490 struct lxc_console
*console
= &conf
->console
;
493 if (conf
->is_execute
) {
494 INFO("no console for lxc-execute.");
498 if (!conf
->rootfs
.path
)
501 if (console
->path
&& !strcmp(console
->path
, "none"))
505 ret
= openpty(&console
->master
, &console
->slave
,
506 console
->name
, NULL
, NULL
);
509 SYSERROR("failed to allocate a pty");
513 if (fcntl(console
->master
, F_SETFD
, FD_CLOEXEC
)) {
514 SYSERROR("failed to set console master to close-on-exec");
518 if (fcntl(console
->slave
, F_SETFD
, FD_CLOEXEC
)) {
519 SYSERROR("failed to set console slave to close-on-exec");
523 lxc_console_peer_default(console
);
525 if (console
->log_path
) {
526 console
->log_fd
= lxc_unpriv(open(console
->log_path
,
528 O_CREAT
| O_APPEND
, 0600));
529 if (console
->log_fd
< 0) {
530 SYSERROR("failed to open '%s'", console
->log_path
);
533 DEBUG("using '%s' as console log", console
->log_path
);
539 lxc_console_delete(console
);
543 int lxc_console_set_stdfds(int fd
)
548 if (isatty(STDIN_FILENO
))
549 if (dup2(fd
, STDIN_FILENO
) < 0) {
550 SYSERROR("failed to duplicate stdin.");
554 if (isatty(STDOUT_FILENO
))
555 if (dup2(fd
, STDOUT_FILENO
) < 0) {
556 SYSERROR("failed to duplicate stdout.");
560 if (isatty(STDERR_FILENO
))
561 if (dup2(fd
, STDERR_FILENO
) < 0) {
562 SYSERROR("failed to duplicate stderr.");
569 int lxc_console_cb_tty_stdin(int fd
, uint32_t events
, void *cbdata
,
570 struct lxc_epoll_descr
*descr
)
572 struct lxc_tty_state
*ts
= cbdata
;
575 assert(fd
== ts
->stdinfd
);
576 if (lxc_read_nointr(ts
->stdinfd
, &c
, 1) <= 0)
579 if (ts
->escape
!= -1) {
580 /* we want to exit the console with Ctrl+a q */
581 if (c
== ts
->escape
&& !ts
->saw_escape
) {
586 if (c
== 'q' && ts
->saw_escape
)
592 if (lxc_write_nointr(ts
->masterfd
, &c
, 1) <= 0)
598 int lxc_console_cb_tty_master(int fd
, uint32_t events
, void *cbdata
,
599 struct lxc_epoll_descr
*descr
)
601 struct lxc_tty_state
*ts
= cbdata
;
605 assert(fd
== ts
->masterfd
);
606 r
= lxc_read_nointr(fd
, buf
, sizeof(buf
));
610 w
= lxc_write_nointr(ts
->stdoutfd
, buf
, r
);
614 SYSERROR("failed to write");
621 int lxc_console_getfd(struct lxc_container
*c
, int *ttynum
, int *masterfd
)
623 return lxc_cmd_console(c
->name
, ttynum
, masterfd
, c
->config_path
);
626 int lxc_console(struct lxc_container
*c
, int ttynum
,
627 int stdinfd
, int stdoutfd
, int stderrfd
,
630 int ret
, ttyfd
, masterfd
;
631 struct lxc_epoll_descr descr
;
632 struct termios oldtios
;
633 struct lxc_tty_state
*ts
;
635 if (!isatty(stdinfd
)) {
636 ERROR("stdin is not a tty");
640 ret
= lxc_setup_tios(stdinfd
, &oldtios
);
642 ERROR("failed to setup tios");
646 ttyfd
= lxc_cmd_console(c
->name
, &ttynum
, &masterfd
, c
->config_path
);
653 "Connected to tty %1$d\n"
654 "Type <Ctrl+%2$c q> to exit the console, "
655 "<Ctrl+%2$c Ctrl+%2$c> to enter Ctrl+%2$c itself\n",
656 ttynum
, 'a' + escape
- 1);
660 INFO("already group leader");
662 ts
= lxc_console_sigwinch_init(stdinfd
, masterfd
);
668 ts
->winch_proxy
= c
->name
;
669 ts
->winch_proxy_lxcpath
= c
->config_path
;
671 lxc_console_winsz(stdinfd
, masterfd
);
672 lxc_cmd_console_winch(ts
->winch_proxy
, ts
->winch_proxy_lxcpath
);
674 ret
= lxc_mainloop_open(&descr
);
676 ERROR("failed to create mainloop");
680 if (ts
->sigfd
!= -1) {
681 ret
= lxc_mainloop_add_handler(&descr
, ts
->sigfd
,
682 lxc_console_cb_sigwinch_fd
, ts
);
684 ERROR("failed to add handler for SIGWINCH fd");
689 ret
= lxc_mainloop_add_handler(&descr
, ts
->stdinfd
,
690 lxc_console_cb_tty_stdin
, ts
);
692 ERROR("failed to add handler for stdinfd");
696 ret
= lxc_mainloop_add_handler(&descr
, ts
->masterfd
,
697 lxc_console_cb_tty_master
, ts
);
699 ERROR("failed to add handler for masterfd");
703 ret
= lxc_mainloop(&descr
, -1);
705 ERROR("mainloop returned an error");
712 lxc_mainloop_close(&descr
);
715 lxc_console_sigwinch_fini(ts
);
720 tcsetattr(stdinfd
, TCSAFLUSH
, &oldtios
);