]>
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
27 #include <lxc/lxccontainer.h>
32 #include <sys/epoll.h>
33 #include <sys/types.h>
52 #include <../include/openpty.h>
55 #define LXC_TERMINAL_BUFFER_SIZE 1024
57 lxc_log_define(terminal
, lxc
);
59 static struct lxc_list lxc_ttys
;
61 typedef void (*sighandler_t
)(int);
63 __attribute__((constructor
)) void lxc_terminal_init_global(void)
65 lxc_list_init(&lxc_ttys
);
68 void lxc_terminal_winsz(int srcfd
, int dstfd
)
76 ret
= ioctl(srcfd
, TIOCGWINSZ
, &wsz
);
78 WARN("Failed to get window size");
82 ret
= ioctl(dstfd
, TIOCSWINSZ
, &wsz
);
84 WARN("Failed to set window size");
86 DEBUG("Set window size to %d columns and %d rows", wsz
.ws_col
,
92 static void lxc_terminal_winch(struct lxc_terminal_state
*ts
)
94 lxc_terminal_winsz(ts
->stdinfd
, ts
->masterfd
);
97 lxc_cmd_terminal_winch(ts
->winch_proxy
, ts
->winch_proxy_lxcpath
);
100 void lxc_terminal_sigwinch(int sig
)
103 struct lxc_terminal_state
*ts
;
105 lxc_list_for_each(it
, &lxc_ttys
) {
107 lxc_terminal_winch(ts
);
111 int lxc_terminal_signalfd_cb(int fd
, uint32_t events
, void *cbdata
,
112 struct lxc_epoll_descr
*descr
)
115 struct signalfd_siginfo siginfo
;
116 struct lxc_terminal_state
*ts
= cbdata
;
118 ret
= read(fd
, &siginfo
, sizeof(siginfo
));
119 if (ret
< 0 || (size_t)ret
< sizeof(siginfo
)) {
120 ERROR("Failed to read signal info");
121 return LXC_MAINLOOP_ERROR
;
124 if (siginfo
.ssi_signo
== SIGTERM
) {
125 DEBUG("Received SIGTERM. Detaching from the terminal");
126 return LXC_MAINLOOP_CLOSE
;
129 if (siginfo
.ssi_signo
== SIGWINCH
)
130 lxc_terminal_winch(ts
);
132 return LXC_MAINLOOP_CONTINUE
;
135 struct lxc_terminal_state
*lxc_terminal_signal_init(int srcfd
, int dstfd
)
140 struct lxc_terminal_state
*ts
;
142 ts
= malloc(sizeof(*ts
));
146 memset(ts
, 0, sizeof(*ts
));
148 ts
->masterfd
= dstfd
;
151 ret
= sigemptyset(&mask
);
153 SYSERROR("Failed to initialize an empty signal set");
157 istty
= (isatty(srcfd
) == 1);
159 INFO("fd %d does not refer to a tty device", srcfd
);
161 /* Add tty to list to be scanned at SIGWINCH time. */
162 lxc_list_add_elem(&ts
->node
, ts
);
163 lxc_list_add_tail(&lxc_ttys
, &ts
->node
);
164 ret
= sigaddset(&mask
, SIGWINCH
);
166 SYSNOTICE("Failed to add SIGWINCH to signal set");
169 /* Exit the mainloop cleanly on SIGTERM. */
170 ret
= sigaddset(&mask
, SIGTERM
);
172 SYSERROR("Failed to add SIGWINCH to signal set");
176 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, &ts
->oldmask
);
178 WARN("Failed to block signals");
182 ts
->sigfd
= signalfd(-1, &mask
, SFD_CLOEXEC
);
184 WARN("Failed to create signal fd");
185 (void)pthread_sigmask(SIG_SETMASK
, &ts
->oldmask
, NULL
);
189 DEBUG("Created signal fd %d", ts
->sigfd
);
193 ERROR("Failed to create signal fd");
194 if (ts
->sigfd
>= 0) {
200 lxc_list_del(&ts
->node
);
205 void lxc_terminal_signal_fini(struct lxc_terminal_state
*ts
)
207 if (ts
->sigfd
>= 0) {
210 if (pthread_sigmask(SIG_SETMASK
, &ts
->oldmask
, NULL
) < 0)
211 SYSWARN("Failed to restore signal mask");
214 if (isatty(ts
->stdinfd
))
215 lxc_list_del(&ts
->node
);
220 static int lxc_terminal_truncate_log_file(struct lxc_terminal
*terminal
)
222 /* be very certain things are kosher */
223 if (!terminal
->log_path
|| terminal
->log_fd
< 0)
226 return lxc_unpriv(ftruncate(terminal
->log_fd
, 0));
229 static int lxc_terminal_rotate_log_file(struct lxc_terminal
*terminal
)
235 if (!terminal
->log_path
|| terminal
->log_rotate
== 0)
238 /* be very certain things are kosher */
239 if (terminal
->log_fd
< 0)
242 len
= strlen(terminal
->log_path
) + sizeof(".1");
245 ret
= snprintf(tmp
, len
, "%s.1", terminal
->log_path
);
246 if (ret
< 0 || (size_t)ret
>= len
)
249 close(terminal
->log_fd
);
250 terminal
->log_fd
= -1;
251 ret
= lxc_unpriv(rename(terminal
->log_path
, tmp
));
255 return lxc_terminal_create_log_file(terminal
);
258 static int lxc_terminal_write_log_file(struct lxc_terminal
*terminal
, char *buf
,
263 int64_t space_left
= -1;
265 if (terminal
->log_fd
< 0)
268 /* A log size <= 0 means that there's no limit on the size of the log
269 * file at which point we simply ignore whether the log is supposed to
272 if (terminal
->log_size
<= 0)
273 return lxc_write_nointr(terminal
->log_fd
, buf
, bytes_read
);
275 /* Get current size of the log file. */
276 ret
= fstat(terminal
->log_fd
, &st
);
278 SYSERROR("Failed to stat the terminal log file descriptor");
282 /* handle non-regular files */
283 if ((st
.st_mode
& S_IFMT
) != S_IFREG
) {
284 /* This isn't a regular file. so rotating the file seems a
285 * dangerous thing to do, size limits are also very
286 * questionable. Let's not risk anything and tell the user that
287 * he's requesting us to do weird stuff.
289 if (terminal
->log_rotate
> 0 || terminal
->log_size
> 0)
292 /* I mean, sure log wherever you want to. */
293 return lxc_write_nointr(terminal
->log_fd
, buf
, bytes_read
);
296 space_left
= terminal
->log_size
- st
.st_size
;
298 /* User doesn't want to rotate the log file and there's no more space
299 * left so simply truncate it.
301 if (space_left
<= 0 && terminal
->log_rotate
<= 0) {
302 ret
= lxc_terminal_truncate_log_file(terminal
);
306 if (bytes_read
<= terminal
->log_size
)
307 return lxc_write_nointr(terminal
->log_fd
, buf
, bytes_read
);
309 /* Write as much as we can into the buffer and loose the rest. */
310 return lxc_write_nointr(terminal
->log_fd
, buf
, terminal
->log_size
);
313 /* There's enough space left. */
314 if (bytes_read
<= space_left
)
315 return lxc_write_nointr(terminal
->log_fd
, buf
, bytes_read
);
317 /* There's not enough space left but at least write as much as we can
318 * into the old log file.
320 ret
= lxc_write_nointr(terminal
->log_fd
, buf
, space_left
);
324 /* Calculate how many bytes we still need to write. */
325 bytes_read
-= space_left
;
327 /* There'd be more to write but we aren't instructed to rotate the log
328 * file so simply return. There's no error on our side here.
330 if (terminal
->log_rotate
> 0)
331 ret
= lxc_terminal_rotate_log_file(terminal
);
333 ret
= lxc_terminal_truncate_log_file(terminal
);
337 if (terminal
->log_size
< bytes_read
) {
338 /* Well, this is unfortunate because it means that there is more
339 * to write than the user has granted us space. There are
340 * multiple ways to handle this but let's use the simplest one:
341 * write as much as we can, tell the user that there was more
342 * stuff to write and move on.
343 * Note that this scenario shouldn't actually happen with the
344 * standard pty-based terminal that LXC allocates since it will
345 * be switched into raw mode. In raw mode only 1 byte at a time
346 * should be read and written.
348 WARN("Size of terminal log file is smaller than the bytes to write");
349 ret
= lxc_write_nointr(terminal
->log_fd
, buf
, terminal
->log_size
);
356 /* Yay, we made it. */
357 ret
= lxc_write_nointr(terminal
->log_fd
, buf
, bytes_read
);
364 int lxc_terminal_io_cb(int fd
, uint32_t events
, void *data
,
365 struct lxc_epoll_descr
*descr
)
367 struct lxc_terminal
*terminal
= data
;
368 char buf
[LXC_TERMINAL_BUFFER_SIZE
];
369 int r
, w
, w_log
, w_rbuf
;
371 w
= r
= lxc_read_nointr(fd
, buf
, sizeof(buf
));
373 INFO("Terminal client on fd %d has exited", fd
);
374 lxc_mainloop_del_handler(descr
, fd
);
376 if (fd
== terminal
->master
) {
377 terminal
->master
= -EBADF
;
378 } else if (fd
== terminal
->peer
) {
379 if (terminal
->tty_state
) {
380 lxc_terminal_signal_fini(terminal
->tty_state
);
381 terminal
->tty_state
= NULL
;
383 terminal
->peer
= -EBADF
;
385 ERROR("Handler received unexpected file descriptor");
389 return LXC_MAINLOOP_CLOSE
;
392 if (fd
== terminal
->peer
)
393 w
= lxc_write_nointr(terminal
->master
, buf
, r
);
396 if (fd
== terminal
->master
) {
397 /* write to peer first */
398 if (terminal
->peer
>= 0)
399 w
= lxc_write_nointr(terminal
->peer
, buf
, r
);
401 /* write to terminal ringbuffer */
402 if (terminal
->buffer_size
> 0)
403 w_rbuf
= lxc_ringbuf_write(&terminal
->ringbuf
, buf
, r
);
405 /* write to terminal log */
406 if (terminal
->log_fd
>= 0)
407 w_log
= lxc_terminal_write_log_file(terminal
, buf
, r
);
411 WARN("Short write on terminal r:%d != w:%d", r
, w
);
415 SYSTRACE("Failed to write %d bytes to terminal ringbuffer", r
);
419 TRACE("Failed to write %d bytes to terminal log", r
);
421 return LXC_MAINLOOP_CONTINUE
;
424 static int lxc_terminal_mainloop_add_peer(struct lxc_terminal
*terminal
)
428 if (terminal
->peer
>= 0) {
429 ret
= lxc_mainloop_add_handler(terminal
->descr
, terminal
->peer
,
430 lxc_terminal_io_cb
, terminal
);
432 WARN("Failed to add terminal peer handler to mainloop");
437 if (!terminal
->tty_state
|| terminal
->tty_state
->sigfd
< 0)
440 ret
= lxc_mainloop_add_handler(terminal
->descr
, terminal
->tty_state
->sigfd
,
441 lxc_terminal_signalfd_cb
, terminal
->tty_state
);
443 WARN("Failed to add signal handler to mainloop");
450 int lxc_terminal_mainloop_add(struct lxc_epoll_descr
*descr
,
451 struct lxc_terminal
*terminal
)
455 if (terminal
->master
< 0) {
456 INFO("Terminal is not initialized");
460 ret
= lxc_mainloop_add_handler(descr
, terminal
->master
,
461 lxc_terminal_io_cb
, terminal
);
463 ERROR("Failed to add handler for terminal master fd %d to "
464 "mainloop", terminal
->master
);
468 /* We cache the descr so that we can add an fd to it when someone
469 * does attach to it in lxc_terminal_allocate().
471 terminal
->descr
= descr
;
473 return lxc_terminal_mainloop_add_peer(terminal
);
476 int lxc_setup_tios(int fd
, struct termios
*oldtios
)
479 struct termios newtios
;
482 ERROR("File descriptor %d does not refert to a terminal", fd
);
486 /* Get current termios. */
487 ret
= tcgetattr(fd
, oldtios
);
489 SYSERROR("Failed to get current terminal settings");
493 /* ensure we don't end up in an endless loop:
494 * The kernel might fire SIGTTOU while an
495 * ioctl() in tcsetattr() is executed. When the ioctl()
496 * is resumed and retries, the signal handler interrupts it again.
498 signal (SIGTTIN
, SIG_IGN
);
499 signal (SIGTTOU
, SIG_IGN
);
503 /* We use the same settings that ssh does. */
504 newtios
.c_iflag
|= IGNPAR
;
505 newtios
.c_iflag
&= ~(ISTRIP
| INLCR
| IGNCR
| ICRNL
| IXON
| IXANY
| IXOFF
);
507 newtios
.c_iflag
&= ~IUCLC
;
509 newtios
.c_lflag
&= ~(TOSTOP
| ISIG
| ICANON
| ECHO
| ECHOE
| ECHOK
| ECHONL
);
511 newtios
.c_lflag
&= ~IEXTEN
;
513 newtios
.c_oflag
&= ~ONLCR
;
514 newtios
.c_oflag
|= OPOST
;
515 newtios
.c_cc
[VMIN
] = 1;
516 newtios
.c_cc
[VTIME
] = 0;
518 /* Set new attributes. */
519 ret
= tcsetattr(fd
, TCSAFLUSH
, &newtios
);
521 ERROR("Failed to set new terminal settings");
528 static void lxc_terminal_peer_proxy_free(struct lxc_terminal
*terminal
)
530 if (terminal
->tty_state
) {
531 lxc_terminal_signal_fini(terminal
->tty_state
);
532 terminal
->tty_state
= NULL
;
535 close(terminal
->proxy
.master
);
536 terminal
->proxy
.master
= -1;
538 close(terminal
->proxy
.slave
);
539 terminal
->proxy
.slave
= -1;
541 terminal
->proxy
.busy
= -1;
543 terminal
->proxy
.name
[0] = '\0';
548 static int lxc_terminal_peer_proxy_alloc(struct lxc_terminal
*terminal
,
552 struct termios oldtermio
;
553 struct lxc_terminal_state
*ts
;
555 if (terminal
->master
< 0) {
556 ERROR("Terminal not set up");
560 if (terminal
->proxy
.busy
!= -1 || terminal
->peer
!= -1) {
561 NOTICE("Terminal already in use");
565 if (terminal
->tty_state
) {
566 ERROR("Terminal has already been initialized");
570 /* This is the proxy terminal that will be given to the client, and
571 * that the real terminal master will send to / recv from.
573 ret
= openpty(&terminal
->proxy
.master
, &terminal
->proxy
.slave
,
574 terminal
->proxy
.name
, NULL
, NULL
);
576 SYSERROR("Failed to open proxy terminal");
580 ret
= lxc_setup_tios(terminal
->proxy
.slave
, &oldtermio
);
584 ts
= lxc_terminal_signal_init(terminal
->proxy
.master
, terminal
->master
);
588 terminal
->tty_state
= ts
;
589 terminal
->peer
= terminal
->proxy
.slave
;
590 terminal
->proxy
.busy
= sockfd
;
591 ret
= lxc_terminal_mainloop_add_peer(terminal
);
595 NOTICE("Opened proxy terminal with master fd %d and slave fd %d",
596 terminal
->proxy
.master
, terminal
->proxy
.slave
);
600 lxc_terminal_peer_proxy_free(terminal
);
604 int lxc_terminal_allocate(struct lxc_conf
*conf
, int sockfd
, int *ttyreq
)
608 struct lxc_tty_info
*ttys
= &conf
->ttys
;
609 struct lxc_terminal
*terminal
= &conf
->console
;
614 ret
= lxc_terminal_peer_proxy_alloc(terminal
, sockfd
);
618 masterfd
= terminal
->proxy
.master
;
623 if (*ttyreq
> ttys
->max
)
626 if (ttys
->tty
[*ttyreq
- 1].busy
)
629 /* The requested tty is available. */
634 /* Search for next available tty, fixup index tty1 => [0]. */
635 for (ttynum
= 1; ttynum
<= ttys
->max
&& ttys
->tty
[ttynum
- 1].busy
; ttynum
++) {
639 /* We didn't find any available slot for tty. */
640 if (ttynum
> ttys
->max
)
646 ttys
->tty
[ttynum
- 1].busy
= sockfd
;
647 masterfd
= ttys
->tty
[ttynum
- 1].master
;
653 void lxc_terminal_free(struct lxc_conf
*conf
, int fd
)
656 struct lxc_tty_info
*ttys
= &conf
->ttys
;
657 struct lxc_terminal
*terminal
= &conf
->console
;
659 for (i
= 0; i
< ttys
->max
; i
++)
660 if (ttys
->tty
[i
].busy
== fd
)
661 ttys
->tty
[i
].busy
= 0;
663 if (terminal
->proxy
.busy
!= fd
)
666 lxc_mainloop_del_handler(terminal
->descr
, terminal
->proxy
.slave
);
667 lxc_terminal_peer_proxy_free(terminal
);
670 static int lxc_terminal_peer_default(struct lxc_terminal
*terminal
)
672 struct lxc_terminal_state
*ts
;
677 path
= terminal
->path
;
681 terminal
->peer
= lxc_unpriv(open(path
, O_RDWR
| O_CLOEXEC
));
682 if (terminal
->peer
< 0) {
683 if (!terminal
->path
) {
685 SYSDEBUG("The process does not have a controlling terminal");
689 SYSERROR("Failed to open proxy terminal \"%s\"", path
);
692 DEBUG("Using terminal \"%s\" as proxy", path
);
694 if (!isatty(terminal
->peer
)) {
695 ERROR("File descriptor for \"%s\" does not refer to a terminal", path
);
696 goto on_error_free_tios
;
699 ts
= lxc_terminal_signal_init(terminal
->peer
, terminal
->master
);
700 terminal
->tty_state
= ts
;
702 WARN("Failed to install signal handler");
703 goto on_error_free_tios
;
706 lxc_terminal_winsz(terminal
->peer
, terminal
->master
);
708 terminal
->tios
= malloc(sizeof(*terminal
->tios
));
710 goto on_error_free_tios
;
712 ret
= lxc_setup_tios(terminal
->peer
, terminal
->tios
);
714 goto on_error_close_peer
;
719 free(terminal
->tios
);
720 terminal
->tios
= NULL
;
723 close(terminal
->peer
);
731 int lxc_terminal_write_ringbuffer(struct lxc_terminal
*terminal
)
736 struct lxc_ringbuf
*buf
= &terminal
->ringbuf
;
738 /* There's not log file where we can dump the ringbuffer to. */
739 if (terminal
->log_fd
< 0)
742 used
= lxc_ringbuf_used(buf
);
746 ret
= lxc_terminal_truncate_log_file(terminal
);
750 /* Write as much as we can without exceeding the limit. */
751 if (terminal
->log_size
< used
)
752 used
= terminal
->log_size
;
754 r_addr
= lxc_ringbuf_get_read_addr(buf
);
755 ret
= lxc_write_nointr(terminal
->log_fd
, r_addr
, used
);
762 void lxc_terminal_delete(struct lxc_terminal
*terminal
)
766 ret
= lxc_terminal_write_ringbuffer(terminal
);
768 WARN("Failed to write terminal log to disk");
770 if (terminal
->tios
&& terminal
->peer
>= 0) {
771 ret
= tcsetattr(terminal
->peer
, TCSAFLUSH
, terminal
->tios
);
773 SYSWARN("Failed to set old terminal settings");
775 free(terminal
->tios
);
776 terminal
->tios
= NULL
;
778 if (terminal
->peer
>= 0)
779 close(terminal
->peer
);
782 if (terminal
->master
>= 0)
783 close(terminal
->master
);
784 terminal
->master
= -1;
786 if (terminal
->slave
>= 0)
787 close(terminal
->slave
);
788 terminal
->slave
= -1;
790 if (terminal
->log_fd
>= 0)
791 close(terminal
->log_fd
);
792 terminal
->log_fd
= -1;
796 * Note that this function needs to run before the mainloop starts. Since we
797 * register a handler for the terminal's masterfd when we create the mainloop
798 * the terminal handler needs to see an allocated ringbuffer.
800 static int lxc_terminal_create_ringbuf(struct lxc_terminal
*terminal
)
803 struct lxc_ringbuf
*buf
= &terminal
->ringbuf
;
804 uint64_t size
= terminal
->buffer_size
;
806 /* no ringbuffer previously allocated and no ringbuffer requested */
807 if (!buf
->addr
&& size
<= 0)
810 /* ringbuffer allocated but no new ringbuffer requested */
811 if (buf
->addr
&& size
<= 0) {
812 lxc_ringbuf_release(buf
);
817 TRACE("Deallocated terminal ringbuffer");
824 /* check wether the requested size for the ringbuffer has changed */
825 if (buf
->addr
&& buf
->size
!= size
) {
826 TRACE("Terminal ringbuffer size changed from %" PRIu64
827 " to %" PRIu64
" bytes. Deallocating terminal ringbuffer",
829 lxc_ringbuf_release(buf
);
832 ret
= lxc_ringbuf_create(buf
, size
);
834 ERROR("Failed to setup %" PRIu64
" byte terminal ringbuffer", size
);
838 TRACE("Allocated %" PRIu64
" byte terminal ringbuffer", size
);
843 * This is the terminal log file. Please note that the terminal log file is
844 * (implementation wise not content wise) independent of the terminal ringbuffer.
846 int lxc_terminal_create_log_file(struct lxc_terminal
*terminal
)
848 if (!terminal
->log_path
)
851 terminal
->log_fd
= lxc_unpriv(open(terminal
->log_path
, O_CLOEXEC
| O_RDWR
| O_CREAT
| O_APPEND
, 0600));
852 if (terminal
->log_fd
< 0) {
853 SYSERROR("Failed to open terminal log file \"%s\"", terminal
->log_path
);
857 DEBUG("Using \"%s\" as terminal log file", terminal
->log_path
);
861 int lxc_terminal_create(struct lxc_terminal
*terminal
)
865 ret
= openpty(&terminal
->master
, &terminal
->slave
, terminal
->name
, NULL
, NULL
);
867 SYSERROR("Failed to open terminal");
871 ret
= fcntl(terminal
->master
, F_SETFD
, FD_CLOEXEC
);
873 SYSERROR("Failed to set FD_CLOEXEC flag on terminal master");
877 ret
= fcntl(terminal
->slave
, F_SETFD
, FD_CLOEXEC
);
879 SYSERROR("Failed to set FD_CLOEXEC flag on terminal slave");
883 ret
= lxc_terminal_peer_default(terminal
);
885 ERROR("Failed to allocate proxy terminal");
892 lxc_terminal_delete(terminal
);
896 int lxc_terminal_setup(struct lxc_conf
*conf
)
899 struct lxc_terminal
*terminal
= &conf
->console
;
900 struct termios oldtios
;
902 if (terminal
->path
&& strcmp(terminal
->path
, "none") == 0) {
903 INFO("No terminal requested");
907 ret
= lxc_terminal_create(terminal
);
911 ret
= lxc_setup_tios(terminal
->master
, &oldtios
);
915 ret
= lxc_terminal_create_log_file(terminal
);
919 ret
= lxc_terminal_create_ringbuf(terminal
);
926 lxc_terminal_delete(terminal
);
930 static bool __terminal_dup2(int duplicate
, int original
)
934 if (!isatty(original
))
937 ret
= dup2(duplicate
, original
);
939 SYSERROR("Failed to dup2(%d, %d)", duplicate
, original
);
946 int lxc_terminal_set_stdfds(int fd
)
953 for (i
= 0; i
< 3; i
++)
954 if (!__terminal_dup2(fd
, (int[]){STDIN_FILENO
, STDOUT_FILENO
,
961 int lxc_terminal_stdin_cb(int fd
, uint32_t events
, void *cbdata
,
962 struct lxc_epoll_descr
*descr
)
966 struct lxc_terminal_state
*ts
= cbdata
;
968 if (fd
!= ts
->stdinfd
)
969 return LXC_MAINLOOP_CLOSE
;
971 ret
= lxc_read_nointr(ts
->stdinfd
, &c
, 1);
973 return LXC_MAINLOOP_CLOSE
;
975 if (ts
->escape
>= 1) {
976 /* we want to exit the terminal with Ctrl+a q */
977 if (c
== ts
->escape
&& !ts
->saw_escape
) {
979 return LXC_MAINLOOP_CONTINUE
;
982 if (c
== 'q' && ts
->saw_escape
)
983 return LXC_MAINLOOP_CLOSE
;
988 ret
= lxc_write_nointr(ts
->masterfd
, &c
, 1);
990 return LXC_MAINLOOP_CLOSE
;
992 return LXC_MAINLOOP_CONTINUE
;
995 int lxc_terminal_master_cb(int fd
, uint32_t events
, void *cbdata
,
996 struct lxc_epoll_descr
*descr
)
999 char buf
[LXC_TERMINAL_BUFFER_SIZE
];
1000 struct lxc_terminal_state
*ts
= cbdata
;
1002 if (fd
!= ts
->masterfd
)
1003 return LXC_MAINLOOP_CLOSE
;
1005 r
= lxc_read_nointr(fd
, buf
, sizeof(buf
));
1007 return LXC_MAINLOOP_CLOSE
;
1009 w
= lxc_write_nointr(ts
->stdoutfd
, buf
, r
);
1010 if (w
<= 0 || w
!= r
)
1011 return LXC_MAINLOOP_CLOSE
;
1013 return LXC_MAINLOOP_CONTINUE
;
1016 int lxc_terminal_getfd(struct lxc_container
*c
, int *ttynum
, int *masterfd
)
1018 return lxc_cmd_console(c
->name
, ttynum
, masterfd
, c
->config_path
);
1021 int lxc_console(struct lxc_container
*c
, int ttynum
,
1022 int stdinfd
, int stdoutfd
, int stderrfd
,
1025 int masterfd
, ret
, ttyfd
;
1026 struct lxc_epoll_descr descr
;
1027 struct termios oldtios
;
1028 struct lxc_terminal_state
*ts
;
1031 ttyfd
= lxc_cmd_console(c
->name
, &ttynum
, &masterfd
, c
->config_path
);
1037 TRACE("Process is already group leader");
1039 ts
= lxc_terminal_signal_init(stdinfd
, masterfd
);
1044 ts
->escape
= escape
;
1045 ts
->winch_proxy
= c
->name
;
1046 ts
->winch_proxy_lxcpath
= c
->config_path
;
1047 ts
->stdoutfd
= stdoutfd
;
1049 istty
= isatty(stdinfd
);
1051 lxc_terminal_winsz(stdinfd
, masterfd
);
1052 lxc_cmd_terminal_winch(ts
->winch_proxy
, ts
->winch_proxy_lxcpath
);
1054 INFO("File descriptor %d does not refer to a terminal", stdinfd
);
1057 ret
= lxc_mainloop_open(&descr
);
1059 ERROR("Failed to create mainloop");
1063 if (ts
->sigfd
!= -1) {
1064 ret
= lxc_mainloop_add_handler(&descr
, ts
->sigfd
,
1065 lxc_terminal_signalfd_cb
, ts
);
1067 ERROR("Failed to add signal handler to mainloop");
1068 goto close_mainloop
;
1072 ret
= lxc_mainloop_add_handler(&descr
, ts
->stdinfd
,
1073 lxc_terminal_stdin_cb
, ts
);
1075 ERROR("Failed to add stdin handler");
1076 goto close_mainloop
;
1079 ret
= lxc_mainloop_add_handler(&descr
, ts
->masterfd
,
1080 lxc_terminal_master_cb
, ts
);
1082 ERROR("Failed to add master handler");
1083 goto close_mainloop
;
1086 if (ts
->escape
>= 1) {
1089 "Connected to tty %1$d\n"
1090 "Type <Ctrl+%2$c q> to exit the console, "
1091 "<Ctrl+%2$c Ctrl+%2$c> to enter Ctrl+%2$c itself\n",
1092 ttynum
, 'a' + escape
- 1);
1096 ret
= lxc_setup_tios(stdinfd
, &oldtios
);
1098 goto close_mainloop
;
1101 ret
= lxc_mainloop(&descr
, -1);
1103 ERROR("The mainloop returned an error");
1111 istty
= tcsetattr(stdinfd
, TCSAFLUSH
, &oldtios
);
1113 SYSWARN("Failed to restore terminal properties");
1117 lxc_mainloop_close(&descr
);
1120 lxc_terminal_signal_fini(ts
);
1129 int lxc_make_controlling_terminal(int fd
)
1135 ret
= ioctl(fd
, TIOCSCTTY
, (char *)NULL
);
1142 int lxc_terminal_prepare_login(int fd
)
1146 ret
= lxc_make_controlling_terminal(fd
);
1150 ret
= lxc_terminal_set_stdfds(fd
);
1154 if (fd
> STDERR_FILENO
)
1160 void lxc_terminal_info_init(struct lxc_terminal_info
*terminal
)
1162 terminal
->name
[0] = '\0';
1163 terminal
->master
= -EBADF
;
1164 terminal
->slave
= -EBADF
;
1165 terminal
->busy
= -1;
1168 void lxc_terminal_init(struct lxc_terminal
*terminal
)
1170 memset(terminal
, 0, sizeof(*terminal
));
1171 terminal
->slave
= -EBADF
;
1172 terminal
->master
= -EBADF
;
1173 terminal
->peer
= -EBADF
;
1174 terminal
->log_fd
= -EBADF
;
1175 lxc_terminal_info_init(&terminal
->proxy
);
1178 void lxc_terminal_conf_free(struct lxc_terminal
*terminal
)
1180 free(terminal
->log_path
);
1181 free(terminal
->path
);
1182 if (terminal
->buffer_size
> 0 && terminal
->ringbuf
.addr
)
1183 lxc_ringbuf_release(&terminal
->ringbuf
);
1186 int lxc_terminal_map_ids(struct lxc_conf
*c
, struct lxc_terminal
*terminal
)
1190 if (lxc_list_empty(&c
->id_map
))
1193 if (strcmp(terminal
->name
, "") == 0)
1196 ret
= chown_mapped_root(terminal
->name
, c
);
1198 ERROR("Failed to chown terminal \"%s\"", terminal
->name
);
1202 TRACE("Chowned terminal \"%s\"", terminal
->name
);