]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/terminal.c
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
29 #include <lxc/lxccontainer.h>
34 #include <sys/epoll.h>
35 #include <sys/types.h>
48 #include "syscall_wrappers.h"
55 #include <../include/openpty.h>
58 #define LXC_TERMINAL_BUFFER_SIZE 1024
60 lxc_log_define(terminal
, lxc
);
62 void lxc_terminal_winsz(int srcfd
, int dstfd
)
70 ret
= ioctl(srcfd
, TIOCGWINSZ
, &wsz
);
72 WARN("Failed to get window size");
76 ret
= ioctl(dstfd
, TIOCSWINSZ
, &wsz
);
78 WARN("Failed to set window size");
80 DEBUG("Set window size to %d columns and %d rows", wsz
.ws_col
,
86 static void lxc_terminal_winch(struct lxc_terminal_state
*ts
)
88 lxc_terminal_winsz(ts
->stdinfd
, ts
->masterfd
);
91 int lxc_terminal_signalfd_cb(int fd
, uint32_t events
, void *cbdata
,
92 struct lxc_epoll_descr
*descr
)
95 struct signalfd_siginfo siginfo
;
96 struct lxc_terminal_state
*ts
= cbdata
;
98 ret
= lxc_read_nointr(fd
, &siginfo
, sizeof(siginfo
));
99 if (ret
< 0 || (size_t)ret
< sizeof(siginfo
)) {
100 ERROR("Failed to read signal info");
101 return LXC_MAINLOOP_ERROR
;
104 if (siginfo
.ssi_signo
== SIGTERM
) {
105 DEBUG("Received SIGTERM. Detaching from the terminal");
106 return LXC_MAINLOOP_CLOSE
;
109 if (siginfo
.ssi_signo
== SIGWINCH
)
110 lxc_terminal_winch(ts
);
112 return LXC_MAINLOOP_CONTINUE
;
115 struct lxc_terminal_state
*lxc_terminal_signal_init(int srcfd
, int dstfd
)
120 struct lxc_terminal_state
*ts
;
122 ts
= malloc(sizeof(*ts
));
126 memset(ts
, 0, sizeof(*ts
));
128 ts
->masterfd
= dstfd
;
131 ret
= sigemptyset(&mask
);
133 SYSERROR("Failed to initialize an empty signal set");
137 istty
= (isatty(srcfd
) == 1);
139 INFO("fd %d does not refer to a tty device", srcfd
);
141 ret
= sigaddset(&mask
, SIGWINCH
);
143 SYSNOTICE("Failed to add SIGWINCH to signal set");
146 /* Exit the mainloop cleanly on SIGTERM. */
147 ret
= sigaddset(&mask
, SIGTERM
);
149 SYSERROR("Failed to add SIGWINCH to signal set");
153 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, &ts
->oldmask
);
155 WARN("Failed to block signals");
159 ts
->sigfd
= signalfd(-1, &mask
, SFD_CLOEXEC
);
161 WARN("Failed to create signal fd");
162 (void)pthread_sigmask(SIG_SETMASK
, &ts
->oldmask
, NULL
);
166 DEBUG("Created signal fd %d", ts
->sigfd
);
170 ERROR("Failed to create signal fd");
171 if (ts
->sigfd
>= 0) {
179 void lxc_terminal_signal_fini(struct lxc_terminal_state
*ts
)
181 if (ts
->sigfd
>= 0) {
184 if (pthread_sigmask(SIG_SETMASK
, &ts
->oldmask
, NULL
) < 0)
185 SYSWARN("Failed to restore signal mask");
191 static int lxc_terminal_truncate_log_file(struct lxc_terminal
*terminal
)
193 /* be very certain things are kosher */
194 if (!terminal
->log_path
|| terminal
->log_fd
< 0)
197 return lxc_unpriv(ftruncate(terminal
->log_fd
, 0));
200 static int lxc_terminal_rotate_log_file(struct lxc_terminal
*terminal
)
206 if (!terminal
->log_path
|| terminal
->log_rotate
== 0)
209 /* be very certain things are kosher */
210 if (terminal
->log_fd
< 0)
213 len
= strlen(terminal
->log_path
) + sizeof(".1");
216 ret
= snprintf(tmp
, len
, "%s.1", terminal
->log_path
);
217 if (ret
< 0 || (size_t)ret
>= len
)
220 close(terminal
->log_fd
);
221 terminal
->log_fd
= -1;
222 ret
= lxc_unpriv(rename(terminal
->log_path
, tmp
));
226 return lxc_terminal_create_log_file(terminal
);
229 static int lxc_terminal_write_log_file(struct lxc_terminal
*terminal
, char *buf
,
234 int64_t space_left
= -1;
236 if (terminal
->log_fd
< 0)
239 /* A log size <= 0 means that there's no limit on the size of the log
240 * file at which point we simply ignore whether the log is supposed to
243 if (terminal
->log_size
<= 0)
244 return lxc_write_nointr(terminal
->log_fd
, buf
, bytes_read
);
246 /* Get current size of the log file. */
247 ret
= fstat(terminal
->log_fd
, &st
);
249 SYSERROR("Failed to stat the terminal log file descriptor");
253 /* handle non-regular files */
254 if ((st
.st_mode
& S_IFMT
) != S_IFREG
) {
255 /* This isn't a regular file. so rotating the file seems a
256 * dangerous thing to do, size limits are also very
257 * questionable. Let's not risk anything and tell the user that
258 * he's requesting us to do weird stuff.
260 if (terminal
->log_rotate
> 0 || terminal
->log_size
> 0)
263 /* I mean, sure log wherever you want to. */
264 return lxc_write_nointr(terminal
->log_fd
, buf
, bytes_read
);
267 space_left
= terminal
->log_size
- st
.st_size
;
269 /* User doesn't want to rotate the log file and there's no more space
270 * left so simply truncate it.
272 if (space_left
<= 0 && terminal
->log_rotate
<= 0) {
273 ret
= lxc_terminal_truncate_log_file(terminal
);
277 if (bytes_read
<= terminal
->log_size
)
278 return lxc_write_nointr(terminal
->log_fd
, buf
, bytes_read
);
280 /* Write as much as we can into the buffer and loose the rest. */
281 return lxc_write_nointr(terminal
->log_fd
, buf
, terminal
->log_size
);
284 /* There's enough space left. */
285 if (bytes_read
<= space_left
)
286 return lxc_write_nointr(terminal
->log_fd
, buf
, bytes_read
);
288 /* There's not enough space left but at least write as much as we can
289 * into the old log file.
291 ret
= lxc_write_nointr(terminal
->log_fd
, buf
, space_left
);
295 /* Calculate how many bytes we still need to write. */
296 bytes_read
-= space_left
;
298 /* There'd be more to write but we aren't instructed to rotate the log
299 * file so simply return. There's no error on our side here.
301 if (terminal
->log_rotate
> 0)
302 ret
= lxc_terminal_rotate_log_file(terminal
);
304 ret
= lxc_terminal_truncate_log_file(terminal
);
308 if (terminal
->log_size
< bytes_read
) {
309 /* Well, this is unfortunate because it means that there is more
310 * to write than the user has granted us space. There are
311 * multiple ways to handle this but let's use the simplest one:
312 * write as much as we can, tell the user that there was more
313 * stuff to write and move on.
314 * Note that this scenario shouldn't actually happen with the
315 * standard pty-based terminal that LXC allocates since it will
316 * be switched into raw mode. In raw mode only 1 byte at a time
317 * should be read and written.
319 WARN("Size of terminal log file is smaller than the bytes to write");
320 ret
= lxc_write_nointr(terminal
->log_fd
, buf
, terminal
->log_size
);
327 /* Yay, we made it. */
328 ret
= lxc_write_nointr(terminal
->log_fd
, buf
, bytes_read
);
335 int lxc_terminal_io_cb(int fd
, uint32_t events
, void *data
,
336 struct lxc_epoll_descr
*descr
)
338 struct lxc_terminal
*terminal
= data
;
339 char buf
[LXC_TERMINAL_BUFFER_SIZE
];
340 int r
, w
, w_log
, w_rbuf
;
342 w
= r
= lxc_read_nointr(fd
, buf
, sizeof(buf
));
344 INFO("Terminal client on fd %d has exited", fd
);
345 lxc_mainloop_del_handler(descr
, fd
);
347 if (fd
== terminal
->master
) {
348 terminal
->master
= -EBADF
;
349 } else if (fd
== terminal
->peer
) {
350 if (terminal
->tty_state
) {
351 lxc_terminal_signal_fini(terminal
->tty_state
);
352 terminal
->tty_state
= NULL
;
354 terminal
->peer
= -EBADF
;
356 ERROR("Handler received unexpected file descriptor");
360 return LXC_MAINLOOP_CLOSE
;
363 if (fd
== terminal
->peer
)
364 w
= lxc_write_nointr(terminal
->master
, buf
, r
);
367 if (fd
== terminal
->master
) {
368 /* write to peer first */
369 if (terminal
->peer
>= 0)
370 w
= lxc_write_nointr(terminal
->peer
, buf
, r
);
372 /* write to terminal ringbuffer */
373 if (terminal
->buffer_size
> 0)
374 w_rbuf
= lxc_ringbuf_write(&terminal
->ringbuf
, buf
, r
);
376 /* write to terminal log */
377 if (terminal
->log_fd
>= 0)
378 w_log
= lxc_terminal_write_log_file(terminal
, buf
, r
);
382 WARN("Short write on terminal r:%d != w:%d", r
, w
);
386 SYSTRACE("Failed to write %d bytes to terminal ringbuffer", r
);
390 TRACE("Failed to write %d bytes to terminal log", r
);
392 return LXC_MAINLOOP_CONTINUE
;
395 static int lxc_terminal_mainloop_add_peer(struct lxc_terminal
*terminal
)
399 if (terminal
->peer
>= 0) {
400 ret
= lxc_mainloop_add_handler(terminal
->descr
, terminal
->peer
,
401 lxc_terminal_io_cb
, terminal
);
403 WARN("Failed to add terminal peer handler to mainloop");
408 if (!terminal
->tty_state
|| terminal
->tty_state
->sigfd
< 0)
411 ret
= lxc_mainloop_add_handler(terminal
->descr
, terminal
->tty_state
->sigfd
,
412 lxc_terminal_signalfd_cb
, terminal
->tty_state
);
414 WARN("Failed to add signal handler to mainloop");
421 int lxc_terminal_mainloop_add(struct lxc_epoll_descr
*descr
,
422 struct lxc_terminal
*terminal
)
426 if (terminal
->master
< 0) {
427 INFO("Terminal is not initialized");
431 ret
= lxc_mainloop_add_handler(descr
, terminal
->master
,
432 lxc_terminal_io_cb
, terminal
);
434 ERROR("Failed to add handler for terminal master fd %d to "
435 "mainloop", terminal
->master
);
439 /* We cache the descr so that we can add an fd to it when someone
440 * does attach to it in lxc_terminal_allocate().
442 terminal
->descr
= descr
;
444 return lxc_terminal_mainloop_add_peer(terminal
);
447 int lxc_setup_tios(int fd
, struct termios
*oldtios
)
450 struct termios newtios
;
453 ERROR("File descriptor %d does not refert to a terminal", fd
);
457 /* Get current termios. */
458 ret
= tcgetattr(fd
, oldtios
);
460 SYSERROR("Failed to get current terminal settings");
464 /* ensure we don't end up in an endless loop:
465 * The kernel might fire SIGTTOU while an
466 * ioctl() in tcsetattr() is executed. When the ioctl()
467 * is resumed and retries, the signal handler interrupts it again.
469 signal (SIGTTIN
, SIG_IGN
);
470 signal (SIGTTOU
, SIG_IGN
);
474 /* We use the same settings that ssh does. */
475 newtios
.c_iflag
|= IGNPAR
;
476 newtios
.c_iflag
&= ~(ISTRIP
| INLCR
| IGNCR
| ICRNL
| IXON
| IXANY
| IXOFF
);
478 newtios
.c_iflag
&= ~IUCLC
;
480 newtios
.c_lflag
&= ~(TOSTOP
| ISIG
| ICANON
| ECHO
| ECHOE
| ECHOK
| ECHONL
);
482 newtios
.c_lflag
&= ~IEXTEN
;
484 newtios
.c_oflag
&= ~ONLCR
;
485 newtios
.c_oflag
|= OPOST
;
486 newtios
.c_cc
[VMIN
] = 1;
487 newtios
.c_cc
[VTIME
] = 0;
489 /* Set new attributes. */
490 ret
= tcsetattr(fd
, TCSAFLUSH
, &newtios
);
492 ERROR("Failed to set new terminal settings");
499 static void lxc_terminal_peer_proxy_free(struct lxc_terminal
*terminal
)
501 if (terminal
->tty_state
) {
502 lxc_terminal_signal_fini(terminal
->tty_state
);
503 terminal
->tty_state
= NULL
;
506 close(terminal
->proxy
.master
);
507 terminal
->proxy
.master
= -1;
509 close(terminal
->proxy
.slave
);
510 terminal
->proxy
.slave
= -1;
512 terminal
->proxy
.busy
= -1;
514 terminal
->proxy
.name
[0] = '\0';
519 static int lxc_terminal_peer_proxy_alloc(struct lxc_terminal
*terminal
,
523 struct termios oldtermio
;
524 struct lxc_terminal_state
*ts
;
526 if (terminal
->master
< 0) {
527 ERROR("Terminal not set up");
531 if (terminal
->proxy
.busy
!= -1 || terminal
->peer
!= -1) {
532 NOTICE("Terminal already in use");
536 if (terminal
->tty_state
) {
537 ERROR("Terminal has already been initialized");
541 /* This is the proxy terminal that will be given to the client, and
542 * that the real terminal master will send to / recv from.
544 ret
= openpty(&terminal
->proxy
.master
, &terminal
->proxy
.slave
, NULL
,
547 SYSERROR("Failed to open proxy terminal");
551 ret
= ttyname_r(terminal
->proxy
.slave
, terminal
->proxy
.name
,
552 sizeof(terminal
->proxy
.name
));
554 SYSERROR("Failed to retrieve name of proxy terminal slave");
558 ret
= fd_cloexec(terminal
->proxy
.master
, true);
560 SYSERROR("Failed to set FD_CLOEXEC flag on proxy terminal master");
564 ret
= fd_cloexec(terminal
->proxy
.slave
, true);
566 SYSERROR("Failed to set FD_CLOEXEC flag on proxy terminal slave");
570 ret
= lxc_setup_tios(terminal
->proxy
.slave
, &oldtermio
);
574 ts
= lxc_terminal_signal_init(terminal
->proxy
.master
, terminal
->master
);
578 terminal
->tty_state
= ts
;
579 terminal
->peer
= terminal
->proxy
.slave
;
580 terminal
->proxy
.busy
= sockfd
;
581 ret
= lxc_terminal_mainloop_add_peer(terminal
);
585 NOTICE("Opened proxy terminal with master fd %d and slave fd %d",
586 terminal
->proxy
.master
, terminal
->proxy
.slave
);
590 lxc_terminal_peer_proxy_free(terminal
);
594 int lxc_terminal_allocate(struct lxc_conf
*conf
, int sockfd
, int *ttyreq
)
598 struct lxc_tty_info
*ttys
= &conf
->ttys
;
599 struct lxc_terminal
*terminal
= &conf
->console
;
604 ret
= lxc_terminal_peer_proxy_alloc(terminal
, sockfd
);
608 masterfd
= terminal
->proxy
.master
;
613 if (*ttyreq
> ttys
->max
)
616 if (ttys
->tty
[*ttyreq
- 1].busy
)
619 /* The requested tty is available. */
624 /* Search for next available tty, fixup index tty1 => [0]. */
625 for (ttynum
= 1; ttynum
<= ttys
->max
&& ttys
->tty
[ttynum
- 1].busy
; ttynum
++) {
629 /* We didn't find any available slot for tty. */
630 if (ttynum
> ttys
->max
)
636 ttys
->tty
[ttynum
- 1].busy
= sockfd
;
637 masterfd
= ttys
->tty
[ttynum
- 1].master
;
643 void lxc_terminal_free(struct lxc_conf
*conf
, int fd
)
646 struct lxc_tty_info
*ttys
= &conf
->ttys
;
647 struct lxc_terminal
*terminal
= &conf
->console
;
649 for (i
= 0; i
< ttys
->max
; i
++)
650 if (ttys
->tty
[i
].busy
== fd
)
651 ttys
->tty
[i
].busy
= 0;
653 if (terminal
->proxy
.busy
!= fd
)
656 lxc_mainloop_del_handler(terminal
->descr
, terminal
->proxy
.slave
);
657 lxc_terminal_peer_proxy_free(terminal
);
660 static int lxc_terminal_peer_default(struct lxc_terminal
*terminal
)
662 struct lxc_terminal_state
*ts
;
667 path
= terminal
->path
;
671 terminal
->peer
= lxc_unpriv(open(path
, O_RDWR
| O_CLOEXEC
));
672 if (terminal
->peer
< 0) {
673 if (!terminal
->path
) {
675 SYSDEBUG("The process does not have a controlling terminal");
679 SYSERROR("Failed to open proxy terminal \"%s\"", path
);
682 DEBUG("Using terminal \"%s\" as proxy", path
);
684 if (!isatty(terminal
->peer
)) {
685 ERROR("File descriptor for \"%s\" does not refer to a terminal", path
);
686 goto on_error_free_tios
;
689 ts
= lxc_terminal_signal_init(terminal
->peer
, terminal
->master
);
690 terminal
->tty_state
= ts
;
692 WARN("Failed to install signal handler");
693 goto on_error_free_tios
;
696 lxc_terminal_winsz(terminal
->peer
, terminal
->master
);
698 terminal
->tios
= malloc(sizeof(*terminal
->tios
));
700 goto on_error_free_tios
;
702 ret
= lxc_setup_tios(terminal
->peer
, terminal
->tios
);
704 goto on_error_close_peer
;
709 free(terminal
->tios
);
710 terminal
->tios
= NULL
;
713 close(terminal
->peer
);
721 int lxc_terminal_write_ringbuffer(struct lxc_terminal
*terminal
)
726 struct lxc_ringbuf
*buf
= &terminal
->ringbuf
;
728 /* There's not log file where we can dump the ringbuffer to. */
729 if (terminal
->log_fd
< 0)
732 used
= lxc_ringbuf_used(buf
);
736 ret
= lxc_terminal_truncate_log_file(terminal
);
740 /* Write as much as we can without exceeding the limit. */
741 if (terminal
->log_size
< used
)
742 used
= terminal
->log_size
;
744 r_addr
= lxc_ringbuf_get_read_addr(buf
);
745 ret
= lxc_write_nointr(terminal
->log_fd
, r_addr
, used
);
752 void lxc_terminal_delete(struct lxc_terminal
*terminal
)
756 ret
= lxc_terminal_write_ringbuffer(terminal
);
758 WARN("Failed to write terminal log to disk");
760 if (terminal
->tios
&& terminal
->peer
>= 0) {
761 ret
= tcsetattr(terminal
->peer
, TCSAFLUSH
, terminal
->tios
);
763 SYSWARN("Failed to set old terminal settings");
765 free(terminal
->tios
);
766 terminal
->tios
= NULL
;
768 if (terminal
->peer
>= 0)
769 close(terminal
->peer
);
772 if (terminal
->master
>= 0)
773 close(terminal
->master
);
774 terminal
->master
= -1;
776 if (terminal
->slave
>= 0)
777 close(terminal
->slave
);
778 terminal
->slave
= -1;
780 if (terminal
->log_fd
>= 0)
781 close(terminal
->log_fd
);
782 terminal
->log_fd
= -1;
786 * Note that this function needs to run before the mainloop starts. Since we
787 * register a handler for the terminal's masterfd when we create the mainloop
788 * the terminal handler needs to see an allocated ringbuffer.
790 static int lxc_terminal_create_ringbuf(struct lxc_terminal
*terminal
)
793 struct lxc_ringbuf
*buf
= &terminal
->ringbuf
;
794 uint64_t size
= terminal
->buffer_size
;
796 /* no ringbuffer previously allocated and no ringbuffer requested */
797 if (!buf
->addr
&& size
<= 0)
800 /* ringbuffer allocated but no new ringbuffer requested */
801 if (buf
->addr
&& size
<= 0) {
802 lxc_ringbuf_release(buf
);
807 TRACE("Deallocated terminal ringbuffer");
814 /* check wether the requested size for the ringbuffer has changed */
815 if (buf
->addr
&& buf
->size
!= size
) {
816 TRACE("Terminal ringbuffer size changed from %" PRIu64
817 " to %" PRIu64
" bytes. Deallocating terminal ringbuffer",
819 lxc_ringbuf_release(buf
);
822 ret
= lxc_ringbuf_create(buf
, size
);
824 ERROR("Failed to setup %" PRIu64
" byte terminal ringbuffer", size
);
828 TRACE("Allocated %" PRIu64
" byte terminal ringbuffer", size
);
833 * This is the terminal log file. Please note that the terminal log file is
834 * (implementation wise not content wise) independent of the terminal ringbuffer.
836 int lxc_terminal_create_log_file(struct lxc_terminal
*terminal
)
838 if (!terminal
->log_path
)
841 terminal
->log_fd
= lxc_unpriv(open(terminal
->log_path
, O_CLOEXEC
| O_RDWR
| O_CREAT
| O_APPEND
, 0600));
842 if (terminal
->log_fd
< 0) {
843 SYSERROR("Failed to open terminal log file \"%s\"", terminal
->log_path
);
847 DEBUG("Using \"%s\" as terminal log file", terminal
->log_path
);
851 int lxc_terminal_create(struct lxc_terminal
*terminal
)
855 ret
= openpty(&terminal
->master
, &terminal
->slave
, NULL
, NULL
, NULL
);
857 SYSERROR("Failed to open terminal");
861 ret
= ttyname_r(terminal
->slave
, terminal
->name
, sizeof(terminal
->name
));
863 SYSERROR("Failed to retrieve name of terminal slave");
867 ret
= fd_cloexec(terminal
->master
, true);
869 SYSERROR("Failed to set FD_CLOEXEC flag on terminal master");
873 ret
= fd_cloexec(terminal
->slave
, true);
875 SYSERROR("Failed to set FD_CLOEXEC flag on terminal slave");
879 ret
= lxc_terminal_peer_default(terminal
);
881 ERROR("Failed to allocate proxy terminal");
888 lxc_terminal_delete(terminal
);
892 int lxc_terminal_setup(struct lxc_conf
*conf
)
895 struct lxc_terminal
*terminal
= &conf
->console
;
897 if (terminal
->path
&& strcmp(terminal
->path
, "none") == 0) {
898 INFO("No terminal requested");
902 ret
= lxc_terminal_create(terminal
);
906 ret
= lxc_terminal_create_log_file(terminal
);
910 ret
= lxc_terminal_create_ringbuf(terminal
);
917 lxc_terminal_delete(terminal
);
921 static bool __terminal_dup2(int duplicate
, int original
)
925 if (!isatty(original
))
928 ret
= dup2(duplicate
, original
);
930 SYSERROR("Failed to dup2(%d, %d)", duplicate
, original
);
937 int lxc_terminal_set_stdfds(int fd
)
944 for (i
= 0; i
< 3; i
++)
945 if (!__terminal_dup2(fd
, (int[]){STDIN_FILENO
, STDOUT_FILENO
,
952 int lxc_terminal_stdin_cb(int fd
, uint32_t events
, void *cbdata
,
953 struct lxc_epoll_descr
*descr
)
957 struct lxc_terminal_state
*ts
= cbdata
;
959 if (fd
!= ts
->stdinfd
)
960 return LXC_MAINLOOP_CLOSE
;
962 ret
= lxc_read_nointr(ts
->stdinfd
, &c
, 1);
964 return LXC_MAINLOOP_CLOSE
;
966 if (ts
->escape
>= 1) {
967 /* we want to exit the terminal with Ctrl+a q */
968 if (c
== ts
->escape
&& !ts
->saw_escape
) {
970 return LXC_MAINLOOP_CONTINUE
;
973 if (c
== 'q' && ts
->saw_escape
)
974 return LXC_MAINLOOP_CLOSE
;
979 ret
= lxc_write_nointr(ts
->masterfd
, &c
, 1);
981 return LXC_MAINLOOP_CLOSE
;
983 return LXC_MAINLOOP_CONTINUE
;
986 int lxc_terminal_master_cb(int fd
, uint32_t events
, void *cbdata
,
987 struct lxc_epoll_descr
*descr
)
990 char buf
[LXC_TERMINAL_BUFFER_SIZE
];
991 struct lxc_terminal_state
*ts
= cbdata
;
993 if (fd
!= ts
->masterfd
)
994 return LXC_MAINLOOP_CLOSE
;
996 r
= lxc_read_nointr(fd
, buf
, sizeof(buf
));
998 return LXC_MAINLOOP_CLOSE
;
1000 w
= lxc_write_nointr(ts
->stdoutfd
, buf
, r
);
1001 if (w
<= 0 || w
!= r
)
1002 return LXC_MAINLOOP_CLOSE
;
1004 return LXC_MAINLOOP_CONTINUE
;
1007 int lxc_terminal_getfd(struct lxc_container
*c
, int *ttynum
, int *masterfd
)
1009 return lxc_cmd_console(c
->name
, ttynum
, masterfd
, c
->config_path
);
1012 int lxc_console(struct lxc_container
*c
, int ttynum
,
1013 int stdinfd
, int stdoutfd
, int stderrfd
,
1016 int masterfd
, ret
, ttyfd
;
1017 struct lxc_epoll_descr descr
;
1018 struct termios oldtios
;
1019 struct lxc_terminal_state
*ts
;
1022 ttyfd
= lxc_cmd_console(c
->name
, &ttynum
, &masterfd
, c
->config_path
);
1028 TRACE("Process is already group leader");
1030 ts
= lxc_terminal_signal_init(stdinfd
, masterfd
);
1035 ts
->escape
= escape
;
1036 ts
->stdoutfd
= stdoutfd
;
1038 istty
= isatty(stdinfd
);
1040 lxc_terminal_winsz(stdinfd
, masterfd
);
1041 lxc_terminal_winsz(ts
->stdinfd
, ts
->masterfd
);
1043 INFO("File descriptor %d does not refer to a terminal", stdinfd
);
1046 ret
= lxc_mainloop_open(&descr
);
1048 ERROR("Failed to create mainloop");
1052 if (ts
->sigfd
!= -1) {
1053 ret
= lxc_mainloop_add_handler(&descr
, ts
->sigfd
,
1054 lxc_terminal_signalfd_cb
, ts
);
1056 ERROR("Failed to add signal handler to mainloop");
1057 goto close_mainloop
;
1061 ret
= lxc_mainloop_add_handler(&descr
, ts
->stdinfd
,
1062 lxc_terminal_stdin_cb
, ts
);
1064 ERROR("Failed to add stdin handler");
1065 goto close_mainloop
;
1068 ret
= lxc_mainloop_add_handler(&descr
, ts
->masterfd
,
1069 lxc_terminal_master_cb
, ts
);
1071 ERROR("Failed to add master handler");
1072 goto close_mainloop
;
1075 if (ts
->escape
>= 1) {
1078 "Connected to tty %1$d\n"
1079 "Type <Ctrl+%2$c q> to exit the console, "
1080 "<Ctrl+%2$c Ctrl+%2$c> to enter Ctrl+%2$c itself\n",
1081 ttynum
, 'a' + escape
- 1);
1085 ret
= lxc_setup_tios(stdinfd
, &oldtios
);
1087 goto close_mainloop
;
1090 ret
= lxc_mainloop(&descr
, -1);
1092 ERROR("The mainloop returned an error");
1100 istty
= tcsetattr(stdinfd
, TCSAFLUSH
, &oldtios
);
1102 SYSWARN("Failed to restore terminal properties");
1106 lxc_mainloop_close(&descr
);
1109 lxc_terminal_signal_fini(ts
);
1118 int lxc_make_controlling_terminal(int fd
)
1124 ret
= ioctl(fd
, TIOCSCTTY
, (char *)NULL
);
1131 int lxc_terminal_prepare_login(int fd
)
1135 ret
= lxc_make_controlling_terminal(fd
);
1139 ret
= lxc_terminal_set_stdfds(fd
);
1143 if (fd
> STDERR_FILENO
)
1149 void lxc_terminal_info_init(struct lxc_terminal_info
*terminal
)
1151 terminal
->name
[0] = '\0';
1152 terminal
->master
= -EBADF
;
1153 terminal
->slave
= -EBADF
;
1154 terminal
->busy
= -1;
1157 void lxc_terminal_init(struct lxc_terminal
*terminal
)
1159 memset(terminal
, 0, sizeof(*terminal
));
1160 terminal
->slave
= -EBADF
;
1161 terminal
->master
= -EBADF
;
1162 terminal
->peer
= -EBADF
;
1163 terminal
->log_fd
= -EBADF
;
1164 lxc_terminal_info_init(&terminal
->proxy
);
1167 void lxc_terminal_conf_free(struct lxc_terminal
*terminal
)
1169 free(terminal
->log_path
);
1170 free(terminal
->path
);
1171 if (terminal
->buffer_size
> 0 && terminal
->ringbuf
.addr
)
1172 lxc_ringbuf_release(&terminal
->ringbuf
);
1175 int lxc_terminal_map_ids(struct lxc_conf
*c
, struct lxc_terminal
*terminal
)
1179 if (lxc_list_empty(&c
->id_map
))
1182 if (strcmp(terminal
->name
, "") == 0)
1185 ret
= chown_mapped_root(terminal
->name
, c
);
1187 ERROR("Failed to chown terminal \"%s\"", terminal
->name
);
1191 TRACE("Chowned terminal \"%s\"", terminal
->name
);