]>
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>
47 #include "memory_utils.h"
49 #include "syscall_wrappers.h"
56 #include <../include/openpty.h>
59 #define LXC_TERMINAL_BUFFER_SIZE 1024
61 lxc_log_define(terminal
, lxc
);
63 void lxc_terminal_winsz(int srcfd
, int dstfd
)
71 ret
= ioctl(srcfd
, TIOCGWINSZ
, &wsz
);
73 WARN("Failed to get window size");
77 ret
= ioctl(dstfd
, TIOCSWINSZ
, &wsz
);
79 WARN("Failed to set window size");
81 DEBUG("Set window size to %d columns and %d rows", wsz
.ws_col
,
87 static void lxc_terminal_winch(struct lxc_terminal_state
*ts
)
89 lxc_terminal_winsz(ts
->stdinfd
, ts
->masterfd
);
92 int lxc_terminal_signalfd_cb(int fd
, uint32_t events
, void *cbdata
,
93 struct lxc_epoll_descr
*descr
)
96 struct signalfd_siginfo siginfo
;
97 struct lxc_terminal_state
*ts
= cbdata
;
99 ret
= lxc_read_nointr(fd
, &siginfo
, sizeof(siginfo
));
100 if (ret
< 0 || (size_t)ret
< sizeof(siginfo
)) {
101 ERROR("Failed to read signal info");
102 return LXC_MAINLOOP_ERROR
;
105 if (siginfo
.ssi_signo
== SIGTERM
) {
106 DEBUG("Received SIGTERM. Detaching from the terminal");
107 return LXC_MAINLOOP_CLOSE
;
110 if (siginfo
.ssi_signo
== SIGWINCH
)
111 lxc_terminal_winch(ts
);
113 return LXC_MAINLOOP_CONTINUE
;
116 struct lxc_terminal_state
*lxc_terminal_signal_init(int srcfd
, int dstfd
)
121 struct lxc_terminal_state
*ts
;
123 ts
= malloc(sizeof(*ts
));
127 memset(ts
, 0, sizeof(*ts
));
129 ts
->masterfd
= dstfd
;
132 ret
= sigemptyset(&mask
);
134 SYSERROR("Failed to initialize an empty signal set");
138 istty
= (isatty(srcfd
) == 1);
140 INFO("fd %d does not refer to a tty device", srcfd
);
142 ret
= sigaddset(&mask
, SIGWINCH
);
144 SYSNOTICE("Failed to add SIGWINCH to signal set");
147 /* Exit the mainloop cleanly on SIGTERM. */
148 ret
= sigaddset(&mask
, SIGTERM
);
150 SYSERROR("Failed to add SIGWINCH to signal set");
154 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, &ts
->oldmask
);
156 WARN("Failed to block signals");
160 ts
->sigfd
= signalfd(-1, &mask
, SFD_CLOEXEC
);
162 WARN("Failed to create signal fd");
163 (void)pthread_sigmask(SIG_SETMASK
, &ts
->oldmask
, NULL
);
167 DEBUG("Created signal fd %d", ts
->sigfd
);
171 ERROR("Failed to create signal fd");
172 if (ts
->sigfd
>= 0) {
180 void lxc_terminal_signal_fini(struct lxc_terminal_state
*ts
)
182 if (ts
->sigfd
>= 0) {
185 if (pthread_sigmask(SIG_SETMASK
, &ts
->oldmask
, NULL
) < 0)
186 SYSWARN("Failed to restore signal mask");
192 static int lxc_terminal_truncate_log_file(struct lxc_terminal
*terminal
)
194 /* be very certain things are kosher */
195 if (!terminal
->log_path
|| terminal
->log_fd
< 0)
198 return lxc_unpriv(ftruncate(terminal
->log_fd
, 0));
201 static int lxc_terminal_rotate_log_file(struct lxc_terminal
*terminal
)
203 __do_free
char *tmp
= NULL
;
207 if (!terminal
->log_path
|| terminal
->log_rotate
== 0)
210 /* be very certain things are kosher */
211 if (terminal
->log_fd
< 0)
214 len
= strlen(terminal
->log_path
) + sizeof(".1");
215 tmp
= must_realloc(NULL
, len
);
217 ret
= snprintf(tmp
, len
, "%s.1", terminal
->log_path
);
218 if (ret
< 0 || (size_t)ret
>= len
)
221 close(terminal
->log_fd
);
222 terminal
->log_fd
= -1;
223 ret
= lxc_unpriv(rename(terminal
->log_path
, tmp
));
227 return lxc_terminal_create_log_file(terminal
);
230 static int lxc_terminal_write_log_file(struct lxc_terminal
*terminal
, char *buf
,
235 int64_t space_left
= -1;
237 if (terminal
->log_fd
< 0)
240 /* A log size <= 0 means that there's no limit on the size of the log
241 * file at which point we simply ignore whether the log is supposed to
244 if (terminal
->log_size
<= 0)
245 return lxc_write_nointr(terminal
->log_fd
, buf
, bytes_read
);
247 /* Get current size of the log file. */
248 ret
= fstat(terminal
->log_fd
, &st
);
250 SYSERROR("Failed to stat the terminal log file descriptor");
254 /* handle non-regular files */
255 if ((st
.st_mode
& S_IFMT
) != S_IFREG
) {
256 /* This isn't a regular file. so rotating the file seems a
257 * dangerous thing to do, size limits are also very
258 * questionable. Let's not risk anything and tell the user that
259 * he's requesting us to do weird stuff.
261 if (terminal
->log_rotate
> 0 || terminal
->log_size
> 0)
264 /* I mean, sure log wherever you want to. */
265 return lxc_write_nointr(terminal
->log_fd
, buf
, bytes_read
);
268 space_left
= terminal
->log_size
- st
.st_size
;
270 /* User doesn't want to rotate the log file and there's no more space
271 * left so simply truncate it.
273 if (space_left
<= 0 && terminal
->log_rotate
<= 0) {
274 ret
= lxc_terminal_truncate_log_file(terminal
);
278 if (bytes_read
<= terminal
->log_size
)
279 return lxc_write_nointr(terminal
->log_fd
, buf
, bytes_read
);
281 /* Write as much as we can into the buffer and loose the rest. */
282 return lxc_write_nointr(terminal
->log_fd
, buf
, terminal
->log_size
);
285 /* There's enough space left. */
286 if (bytes_read
<= space_left
)
287 return lxc_write_nointr(terminal
->log_fd
, buf
, bytes_read
);
289 /* There's not enough space left but at least write as much as we can
290 * into the old log file.
292 ret
= lxc_write_nointr(terminal
->log_fd
, buf
, space_left
);
296 /* Calculate how many bytes we still need to write. */
297 bytes_read
-= space_left
;
299 /* There'd be more to write but we aren't instructed to rotate the log
300 * file so simply return. There's no error on our side here.
302 if (terminal
->log_rotate
> 0)
303 ret
= lxc_terminal_rotate_log_file(terminal
);
305 ret
= lxc_terminal_truncate_log_file(terminal
);
309 if (terminal
->log_size
< bytes_read
) {
310 /* Well, this is unfortunate because it means that there is more
311 * to write than the user has granted us space. There are
312 * multiple ways to handle this but let's use the simplest one:
313 * write as much as we can, tell the user that there was more
314 * stuff to write and move on.
315 * Note that this scenario shouldn't actually happen with the
316 * standard pty-based terminal that LXC allocates since it will
317 * be switched into raw mode. In raw mode only 1 byte at a time
318 * should be read and written.
320 WARN("Size of terminal log file is smaller than the bytes to write");
321 ret
= lxc_write_nointr(terminal
->log_fd
, buf
, terminal
->log_size
);
328 /* Yay, we made it. */
329 ret
= lxc_write_nointr(terminal
->log_fd
, buf
, bytes_read
);
336 int lxc_terminal_io_cb(int fd
, uint32_t events
, void *data
,
337 struct lxc_epoll_descr
*descr
)
339 struct lxc_terminal
*terminal
= data
;
340 char buf
[LXC_TERMINAL_BUFFER_SIZE
];
341 int r
, w
, w_log
, w_rbuf
;
343 w
= r
= lxc_read_nointr(fd
, buf
, sizeof(buf
));
345 INFO("Terminal client on fd %d has exited", fd
);
346 lxc_mainloop_del_handler(descr
, fd
);
348 if (fd
== terminal
->master
) {
349 terminal
->master
= -EBADF
;
350 } else if (fd
== terminal
->peer
) {
351 if (terminal
->tty_state
) {
352 lxc_terminal_signal_fini(terminal
->tty_state
);
353 terminal
->tty_state
= NULL
;
355 terminal
->peer
= -EBADF
;
357 ERROR("Handler received unexpected file descriptor");
361 return LXC_MAINLOOP_CLOSE
;
364 if (fd
== terminal
->peer
)
365 w
= lxc_write_nointr(terminal
->master
, buf
, r
);
368 if (fd
== terminal
->master
) {
369 /* write to peer first */
370 if (terminal
->peer
>= 0)
371 w
= lxc_write_nointr(terminal
->peer
, buf
, r
);
373 /* write to terminal ringbuffer */
374 if (terminal
->buffer_size
> 0)
375 w_rbuf
= lxc_ringbuf_write(&terminal
->ringbuf
, buf
, r
);
377 /* write to terminal log */
378 if (terminal
->log_fd
>= 0)
379 w_log
= lxc_terminal_write_log_file(terminal
, buf
, r
);
383 WARN("Short write on terminal r:%d != w:%d", r
, w
);
387 SYSTRACE("Failed to write %d bytes to terminal ringbuffer", r
);
391 TRACE("Failed to write %d bytes to terminal log", r
);
393 return LXC_MAINLOOP_CONTINUE
;
396 static int lxc_terminal_mainloop_add_peer(struct lxc_terminal
*terminal
)
400 if (terminal
->peer
>= 0) {
401 ret
= lxc_mainloop_add_handler(terminal
->descr
, terminal
->peer
,
402 lxc_terminal_io_cb
, terminal
);
404 WARN("Failed to add terminal peer handler to mainloop");
409 if (!terminal
->tty_state
|| terminal
->tty_state
->sigfd
< 0)
412 ret
= lxc_mainloop_add_handler(terminal
->descr
, terminal
->tty_state
->sigfd
,
413 lxc_terminal_signalfd_cb
, terminal
->tty_state
);
415 WARN("Failed to add signal handler to mainloop");
422 int lxc_terminal_mainloop_add(struct lxc_epoll_descr
*descr
,
423 struct lxc_terminal
*terminal
)
427 if (terminal
->master
< 0) {
428 INFO("Terminal is not initialized");
432 ret
= lxc_mainloop_add_handler(descr
, terminal
->master
,
433 lxc_terminal_io_cb
, terminal
);
435 ERROR("Failed to add handler for terminal master fd %d to "
436 "mainloop", terminal
->master
);
440 /* We cache the descr so that we can add an fd to it when someone
441 * does attach to it in lxc_terminal_allocate().
443 terminal
->descr
= descr
;
445 return lxc_terminal_mainloop_add_peer(terminal
);
448 int lxc_setup_tios(int fd
, struct termios
*oldtios
)
451 struct termios newtios
;
454 ERROR("File descriptor %d does not refert to a terminal", fd
);
458 /* Get current termios. */
459 ret
= tcgetattr(fd
, oldtios
);
461 SYSERROR("Failed to get current terminal settings");
465 /* ensure we don't end up in an endless loop:
466 * The kernel might fire SIGTTOU while an
467 * ioctl() in tcsetattr() is executed. When the ioctl()
468 * is resumed and retries, the signal handler interrupts it again.
470 signal (SIGTTIN
, SIG_IGN
);
471 signal (SIGTTOU
, SIG_IGN
);
475 /* We use the same settings that ssh does. */
476 newtios
.c_iflag
|= IGNPAR
;
477 newtios
.c_iflag
&= ~(ISTRIP
| INLCR
| IGNCR
| ICRNL
| IXON
| IXANY
| IXOFF
);
479 newtios
.c_iflag
&= ~IUCLC
;
481 newtios
.c_lflag
&= ~(TOSTOP
| ISIG
| ICANON
| ECHO
| ECHOE
| ECHOK
| ECHONL
);
483 newtios
.c_lflag
&= ~IEXTEN
;
485 newtios
.c_oflag
&= ~ONLCR
;
486 newtios
.c_oflag
|= OPOST
;
487 newtios
.c_cc
[VMIN
] = 1;
488 newtios
.c_cc
[VTIME
] = 0;
490 /* Set new attributes. */
491 ret
= tcsetattr(fd
, TCSAFLUSH
, &newtios
);
493 ERROR("Failed to set new terminal settings");
500 static void lxc_terminal_peer_proxy_free(struct lxc_terminal
*terminal
)
502 if (terminal
->tty_state
) {
503 lxc_terminal_signal_fini(terminal
->tty_state
);
504 terminal
->tty_state
= NULL
;
507 close(terminal
->proxy
.master
);
508 terminal
->proxy
.master
= -1;
510 close(terminal
->proxy
.slave
);
511 terminal
->proxy
.slave
= -1;
513 terminal
->proxy
.busy
= -1;
515 terminal
->proxy
.name
[0] = '\0';
520 static int lxc_terminal_peer_proxy_alloc(struct lxc_terminal
*terminal
,
524 struct termios oldtermio
;
525 struct lxc_terminal_state
*ts
;
527 if (terminal
->master
< 0) {
528 ERROR("Terminal not set up");
532 if (terminal
->proxy
.busy
!= -1 || terminal
->peer
!= -1) {
533 NOTICE("Terminal already in use");
537 if (terminal
->tty_state
) {
538 ERROR("Terminal has already been initialized");
542 /* This is the proxy terminal that will be given to the client, and
543 * that the real terminal master will send to / recv from.
545 ret
= openpty(&terminal
->proxy
.master
, &terminal
->proxy
.slave
, NULL
,
548 SYSERROR("Failed to open proxy terminal");
552 ret
= ttyname_r(terminal
->proxy
.slave
, terminal
->proxy
.name
,
553 sizeof(terminal
->proxy
.name
));
555 SYSERROR("Failed to retrieve name of proxy terminal slave");
559 ret
= fd_cloexec(terminal
->proxy
.master
, true);
561 SYSERROR("Failed to set FD_CLOEXEC flag on proxy terminal master");
565 ret
= fd_cloexec(terminal
->proxy
.slave
, true);
567 SYSERROR("Failed to set FD_CLOEXEC flag on proxy terminal slave");
571 ret
= lxc_setup_tios(terminal
->proxy
.slave
, &oldtermio
);
575 ts
= lxc_terminal_signal_init(terminal
->proxy
.master
, terminal
->master
);
579 terminal
->tty_state
= ts
;
580 terminal
->peer
= terminal
->proxy
.slave
;
581 terminal
->proxy
.busy
= sockfd
;
582 ret
= lxc_terminal_mainloop_add_peer(terminal
);
586 NOTICE("Opened proxy terminal with master fd %d and slave fd %d",
587 terminal
->proxy
.master
, terminal
->proxy
.slave
);
591 lxc_terminal_peer_proxy_free(terminal
);
595 int lxc_terminal_allocate(struct lxc_conf
*conf
, int sockfd
, int *ttyreq
)
599 struct lxc_tty_info
*ttys
= &conf
->ttys
;
600 struct lxc_terminal
*terminal
= &conf
->console
;
605 ret
= lxc_terminal_peer_proxy_alloc(terminal
, sockfd
);
609 masterfd
= terminal
->proxy
.master
;
614 if (*ttyreq
> ttys
->max
)
617 if (ttys
->tty
[*ttyreq
- 1].busy
)
620 /* The requested tty is available. */
625 /* Search for next available tty, fixup index tty1 => [0]. */
626 for (ttynum
= 1; ttynum
<= ttys
->max
&& ttys
->tty
[ttynum
- 1].busy
; ttynum
++) {
630 /* We didn't find any available slot for tty. */
631 if (ttynum
> ttys
->max
)
637 ttys
->tty
[ttynum
- 1].busy
= sockfd
;
638 masterfd
= ttys
->tty
[ttynum
- 1].master
;
644 void lxc_terminal_free(struct lxc_conf
*conf
, int fd
)
647 struct lxc_tty_info
*ttys
= &conf
->ttys
;
648 struct lxc_terminal
*terminal
= &conf
->console
;
650 for (i
= 0; i
< ttys
->max
; i
++)
651 if (ttys
->tty
[i
].busy
== fd
)
652 ttys
->tty
[i
].busy
= 0;
654 if (terminal
->proxy
.busy
!= fd
)
657 lxc_mainloop_del_handler(terminal
->descr
, terminal
->proxy
.slave
);
658 lxc_terminal_peer_proxy_free(terminal
);
661 static int lxc_terminal_peer_default(struct lxc_terminal
*terminal
)
663 struct lxc_terminal_state
*ts
;
668 path
= terminal
->path
;
672 terminal
->peer
= lxc_unpriv(open(path
, O_RDWR
| O_CLOEXEC
));
673 if (terminal
->peer
< 0) {
674 if (!terminal
->path
) {
676 SYSDEBUG("The process does not have a controlling terminal");
680 SYSERROR("Failed to open proxy terminal \"%s\"", path
);
683 DEBUG("Using terminal \"%s\" as proxy", path
);
685 if (!isatty(terminal
->peer
)) {
686 ERROR("File descriptor for \"%s\" does not refer to a terminal", path
);
687 goto on_error_free_tios
;
690 ts
= lxc_terminal_signal_init(terminal
->peer
, terminal
->master
);
691 terminal
->tty_state
= ts
;
693 WARN("Failed to install signal handler");
694 goto on_error_free_tios
;
697 lxc_terminal_winsz(terminal
->peer
, terminal
->master
);
699 terminal
->tios
= malloc(sizeof(*terminal
->tios
));
701 goto on_error_free_tios
;
703 ret
= lxc_setup_tios(terminal
->peer
, terminal
->tios
);
705 goto on_error_close_peer
;
710 free(terminal
->tios
);
711 terminal
->tios
= NULL
;
714 close(terminal
->peer
);
722 int lxc_terminal_write_ringbuffer(struct lxc_terminal
*terminal
)
727 struct lxc_ringbuf
*buf
= &terminal
->ringbuf
;
729 /* There's not log file where we can dump the ringbuffer to. */
730 if (terminal
->log_fd
< 0)
733 used
= lxc_ringbuf_used(buf
);
737 ret
= lxc_terminal_truncate_log_file(terminal
);
741 /* Write as much as we can without exceeding the limit. */
742 if (terminal
->log_size
< used
)
743 used
= terminal
->log_size
;
745 r_addr
= lxc_ringbuf_get_read_addr(buf
);
746 ret
= lxc_write_nointr(terminal
->log_fd
, r_addr
, used
);
753 void lxc_terminal_delete(struct lxc_terminal
*terminal
)
757 ret
= lxc_terminal_write_ringbuffer(terminal
);
759 WARN("Failed to write terminal log to disk");
761 if (terminal
->tios
&& terminal
->peer
>= 0) {
762 ret
= tcsetattr(terminal
->peer
, TCSAFLUSH
, terminal
->tios
);
764 SYSWARN("Failed to set old terminal settings");
766 free(terminal
->tios
);
767 terminal
->tios
= NULL
;
769 if (terminal
->peer
>= 0)
770 close(terminal
->peer
);
773 if (terminal
->master
>= 0)
774 close(terminal
->master
);
775 terminal
->master
= -1;
777 if (terminal
->slave
>= 0)
778 close(terminal
->slave
);
779 terminal
->slave
= -1;
781 if (terminal
->log_fd
>= 0)
782 close(terminal
->log_fd
);
783 terminal
->log_fd
= -1;
787 * Note that this function needs to run before the mainloop starts. Since we
788 * register a handler for the terminal's masterfd when we create the mainloop
789 * the terminal handler needs to see an allocated ringbuffer.
791 static int lxc_terminal_create_ringbuf(struct lxc_terminal
*terminal
)
794 struct lxc_ringbuf
*buf
= &terminal
->ringbuf
;
795 uint64_t size
= terminal
->buffer_size
;
797 /* no ringbuffer previously allocated and no ringbuffer requested */
798 if (!buf
->addr
&& size
<= 0)
801 /* ringbuffer allocated but no new ringbuffer requested */
802 if (buf
->addr
&& size
<= 0) {
803 lxc_ringbuf_release(buf
);
808 TRACE("Deallocated terminal ringbuffer");
815 /* check wether the requested size for the ringbuffer has changed */
816 if (buf
->addr
&& buf
->size
!= size
) {
817 TRACE("Terminal ringbuffer size changed from %" PRIu64
818 " to %" PRIu64
" bytes. Deallocating terminal ringbuffer",
820 lxc_ringbuf_release(buf
);
823 ret
= lxc_ringbuf_create(buf
, size
);
825 ERROR("Failed to setup %" PRIu64
" byte terminal ringbuffer", size
);
829 TRACE("Allocated %" PRIu64
" byte terminal ringbuffer", size
);
834 * This is the terminal log file. Please note that the terminal log file is
835 * (implementation wise not content wise) independent of the terminal ringbuffer.
837 int lxc_terminal_create_log_file(struct lxc_terminal
*terminal
)
839 if (!terminal
->log_path
)
842 terminal
->log_fd
= lxc_unpriv(open(terminal
->log_path
, O_CLOEXEC
| O_RDWR
| O_CREAT
| O_APPEND
, 0600));
843 if (terminal
->log_fd
< 0) {
844 SYSERROR("Failed to open terminal log file \"%s\"", terminal
->log_path
);
848 DEBUG("Using \"%s\" as terminal log file", terminal
->log_path
);
852 int lxc_terminal_create(struct lxc_terminal
*terminal
)
856 ret
= openpty(&terminal
->master
, &terminal
->slave
, NULL
, NULL
, NULL
);
858 SYSERROR("Failed to open terminal");
862 ret
= ttyname_r(terminal
->slave
, terminal
->name
, sizeof(terminal
->name
));
864 SYSERROR("Failed to retrieve name of terminal slave");
868 ret
= fd_cloexec(terminal
->master
, true);
870 SYSERROR("Failed to set FD_CLOEXEC flag on terminal master");
874 ret
= fd_cloexec(terminal
->slave
, true);
876 SYSERROR("Failed to set FD_CLOEXEC flag on terminal slave");
880 ret
= lxc_terminal_peer_default(terminal
);
882 ERROR("Failed to allocate proxy terminal");
889 lxc_terminal_delete(terminal
);
893 int lxc_terminal_setup(struct lxc_conf
*conf
)
896 struct lxc_terminal
*terminal
= &conf
->console
;
898 if (terminal
->path
&& strcmp(terminal
->path
, "none") == 0) {
899 INFO("No terminal requested");
903 ret
= lxc_terminal_create(terminal
);
907 ret
= lxc_terminal_create_log_file(terminal
);
911 ret
= lxc_terminal_create_ringbuf(terminal
);
918 lxc_terminal_delete(terminal
);
922 static bool __terminal_dup2(int duplicate
, int original
)
926 if (!isatty(original
))
929 ret
= dup2(duplicate
, original
);
931 SYSERROR("Failed to dup2(%d, %d)", duplicate
, original
);
938 int lxc_terminal_set_stdfds(int fd
)
945 for (i
= 0; i
< 3; i
++)
946 if (!__terminal_dup2(fd
, (int[]){STDIN_FILENO
, STDOUT_FILENO
,
953 int lxc_terminal_stdin_cb(int fd
, uint32_t events
, void *cbdata
,
954 struct lxc_epoll_descr
*descr
)
958 struct lxc_terminal_state
*ts
= cbdata
;
960 if (fd
!= ts
->stdinfd
)
961 return LXC_MAINLOOP_CLOSE
;
963 ret
= lxc_read_nointr(ts
->stdinfd
, &c
, 1);
965 return LXC_MAINLOOP_CLOSE
;
967 if (ts
->escape
>= 1) {
968 /* we want to exit the terminal with Ctrl+a q */
969 if (c
== ts
->escape
&& !ts
->saw_escape
) {
971 return LXC_MAINLOOP_CONTINUE
;
974 if (c
== 'q' && ts
->saw_escape
)
975 return LXC_MAINLOOP_CLOSE
;
980 ret
= lxc_write_nointr(ts
->masterfd
, &c
, 1);
982 return LXC_MAINLOOP_CLOSE
;
984 return LXC_MAINLOOP_CONTINUE
;
987 int lxc_terminal_master_cb(int fd
, uint32_t events
, void *cbdata
,
988 struct lxc_epoll_descr
*descr
)
991 char buf
[LXC_TERMINAL_BUFFER_SIZE
];
992 struct lxc_terminal_state
*ts
= cbdata
;
994 if (fd
!= ts
->masterfd
)
995 return LXC_MAINLOOP_CLOSE
;
997 r
= lxc_read_nointr(fd
, buf
, sizeof(buf
));
999 return LXC_MAINLOOP_CLOSE
;
1001 w
= lxc_write_nointr(ts
->stdoutfd
, buf
, r
);
1002 if (w
<= 0 || w
!= r
)
1003 return LXC_MAINLOOP_CLOSE
;
1005 return LXC_MAINLOOP_CONTINUE
;
1008 int lxc_terminal_getfd(struct lxc_container
*c
, int *ttynum
, int *masterfd
)
1010 return lxc_cmd_console(c
->name
, ttynum
, masterfd
, c
->config_path
);
1013 int lxc_console(struct lxc_container
*c
, int ttynum
,
1014 int stdinfd
, int stdoutfd
, int stderrfd
,
1017 int masterfd
, ret
, ttyfd
;
1018 struct lxc_epoll_descr descr
;
1019 struct termios oldtios
;
1020 struct lxc_terminal_state
*ts
;
1023 ttyfd
= lxc_cmd_console(c
->name
, &ttynum
, &masterfd
, c
->config_path
);
1029 TRACE("Process is already group leader");
1031 ts
= lxc_terminal_signal_init(stdinfd
, masterfd
);
1036 ts
->escape
= escape
;
1037 ts
->stdoutfd
= stdoutfd
;
1039 istty
= isatty(stdinfd
);
1041 lxc_terminal_winsz(stdinfd
, masterfd
);
1042 lxc_terminal_winsz(ts
->stdinfd
, ts
->masterfd
);
1044 INFO("File descriptor %d does not refer to a terminal", stdinfd
);
1047 ret
= lxc_mainloop_open(&descr
);
1049 ERROR("Failed to create mainloop");
1053 if (ts
->sigfd
!= -1) {
1054 ret
= lxc_mainloop_add_handler(&descr
, ts
->sigfd
,
1055 lxc_terminal_signalfd_cb
, ts
);
1057 ERROR("Failed to add signal handler to mainloop");
1058 goto close_mainloop
;
1062 ret
= lxc_mainloop_add_handler(&descr
, ts
->stdinfd
,
1063 lxc_terminal_stdin_cb
, ts
);
1065 ERROR("Failed to add stdin handler");
1066 goto close_mainloop
;
1069 ret
= lxc_mainloop_add_handler(&descr
, ts
->masterfd
,
1070 lxc_terminal_master_cb
, ts
);
1072 ERROR("Failed to add master handler");
1073 goto close_mainloop
;
1076 if (ts
->escape
>= 1) {
1079 "Connected to tty %1$d\n"
1080 "Type <Ctrl+%2$c q> to exit the console, "
1081 "<Ctrl+%2$c Ctrl+%2$c> to enter Ctrl+%2$c itself\n",
1082 ttynum
, 'a' + escape
- 1);
1086 ret
= lxc_setup_tios(stdinfd
, &oldtios
);
1088 goto close_mainloop
;
1091 ret
= lxc_mainloop(&descr
, -1);
1093 ERROR("The mainloop returned an error");
1101 istty
= tcsetattr(stdinfd
, TCSAFLUSH
, &oldtios
);
1103 SYSWARN("Failed to restore terminal properties");
1107 lxc_mainloop_close(&descr
);
1110 lxc_terminal_signal_fini(ts
);
1119 int lxc_make_controlling_terminal(int fd
)
1125 ret
= ioctl(fd
, TIOCSCTTY
, (char *)NULL
);
1132 int lxc_terminal_prepare_login(int fd
)
1136 ret
= lxc_make_controlling_terminal(fd
);
1140 ret
= lxc_terminal_set_stdfds(fd
);
1144 if (fd
> STDERR_FILENO
)
1150 void lxc_terminal_info_init(struct lxc_terminal_info
*terminal
)
1152 terminal
->name
[0] = '\0';
1153 terminal
->master
= -EBADF
;
1154 terminal
->slave
= -EBADF
;
1155 terminal
->busy
= -1;
1158 void lxc_terminal_init(struct lxc_terminal
*terminal
)
1160 memset(terminal
, 0, sizeof(*terminal
));
1161 terminal
->slave
= -EBADF
;
1162 terminal
->master
= -EBADF
;
1163 terminal
->peer
= -EBADF
;
1164 terminal
->log_fd
= -EBADF
;
1165 lxc_terminal_info_init(&terminal
->proxy
);
1168 void lxc_terminal_conf_free(struct lxc_terminal
*terminal
)
1170 free(terminal
->log_path
);
1171 free(terminal
->path
);
1172 if (terminal
->buffer_size
> 0 && terminal
->ringbuf
.addr
)
1173 lxc_ringbuf_release(&terminal
->ringbuf
);
1176 int lxc_terminal_map_ids(struct lxc_conf
*c
, struct lxc_terminal
*terminal
)
1180 if (lxc_list_empty(&c
->id_map
))
1183 if (strcmp(terminal
->name
, "") == 0)
1186 ret
= chown_mapped_root(terminal
->name
, c
);
1188 ERROR("Failed to chown terminal \"%s\"", terminal
->name
);
1192 TRACE("Chowned terminal \"%s\"", terminal
->name
);