]>
Commit | Line | Data |
---|---|---|
b0a33c1e | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
9afe19d6 | 7 | * Daniel Lezcano <daniel.lezcano at free.fr> |
b0a33c1e | 8 | * |
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. | |
13 | * | |
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. | |
18 | * | |
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 | |
250b1eec | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
b0a33c1e | 22 | */ |
23 | ||
9395937a CB |
24 | #include <errno.h> |
25 | #include <fcntl.h> | |
b5159817 | 26 | #include <signal.h> |
b0a33c1e | 27 | #include <stdio.h> |
e0dc0de7 | 28 | #include <stdlib.h> |
8173e600 | 29 | #include <termios.h> |
9395937a | 30 | #include <unistd.h> |
da41561c CB |
31 | #include <sys/epoll.h> |
32 | #include <sys/types.h> | |
b0a33c1e | 33 | |
948955a2 | 34 | #include <lxc/lxccontainer.h> |
f2363e38 | 35 | |
9395937a CB |
36 | #include "af_unix.h" |
37 | #include "caps.h" | |
38 | #include "commands.h" | |
00dbc43e | 39 | #include "conf.h" |
e827ff7e | 40 | #include "config.h" |
0d4137cc | 41 | #include "console.h" |
9395937a | 42 | #include "log.h" |
b5159817 | 43 | #include "lxclock.h" |
9395937a CB |
44 | #include "mainloop.h" |
45 | #include "start.h" /* for struct lxc_handler */ | |
b5159817 | 46 | #include "utils.h" |
36eb9bde | 47 | |
e827ff7e SG |
48 | #if HAVE_PTY_H |
49 | #include <pty.h> | |
50 | #else | |
51 | #include <../include/openpty.h> | |
52 | #endif | |
53 | ||
732375f5 CB |
54 | #define LXC_CONSOLE_BUFFER_SIZE 1024 |
55 | ||
f36e1654 | 56 | lxc_log_define(console, lxc); |
36eb9bde | 57 | |
b5159817 | 58 | static struct lxc_list lxc_ttys; |
724e753c | 59 | |
b5159817 | 60 | typedef void (*sighandler_t)(int); |
b5159817 | 61 | |
0519b5cc | 62 | __attribute__((constructor)) void lxc_console_init(void) |
b5159817 DE |
63 | { |
64 | lxc_list_init(&lxc_ttys); | |
65 | } | |
724e753c | 66 | |
0d4137cc | 67 | void lxc_console_winsz(int srcfd, int dstfd) |
b5159817 | 68 | { |
0519b5cc | 69 | int ret; |
b5159817 | 70 | struct winsize wsz; |
0519b5cc CB |
71 | |
72 | if (!isatty(srcfd)) | |
73 | return; | |
74 | ||
75 | ret = ioctl(srcfd, TIOCGWINSZ, &wsz); | |
76 | if (ret < 0) { | |
77 | WARN("Failed to get window size"); | |
78 | return; | |
724e753c | 79 | } |
0519b5cc CB |
80 | |
81 | ret = ioctl(dstfd, TIOCSWINSZ, &wsz); | |
82 | if (ret < 0) | |
83 | WARN("Failed to set window size"); | |
84 | else | |
85 | DEBUG("Set window size to %d columns and %d rows", wsz.ws_col, | |
86 | wsz.ws_row); | |
87 | ||
88 | return; | |
b5159817 | 89 | } |
724e753c | 90 | |
b5159817 DE |
91 | static void lxc_console_winch(struct lxc_tty_state *ts) |
92 | { | |
93 | lxc_console_winsz(ts->stdinfd, ts->masterfd); | |
0519b5cc | 94 | |
0d4137cc CB |
95 | if (ts->winch_proxy) |
96 | lxc_cmd_console_winch(ts->winch_proxy, ts->winch_proxy_lxcpath); | |
724e753c MN |
97 | } |
98 | ||
b5159817 | 99 | void lxc_console_sigwinch(int sig) |
cd453b38 | 100 | { |
025ed0f3 SH |
101 | struct lxc_list *it; |
102 | struct lxc_tty_state *ts; | |
cd453b38 | 103 | |
025ed0f3 SH |
104 | lxc_list_for_each(it, &lxc_ttys) { |
105 | ts = it->elem; | |
106 | lxc_console_winch(ts); | |
cd453b38 | 107 | } |
b5159817 | 108 | } |
cd453b38 | 109 | |
0519b5cc CB |
110 | int lxc_console_cb_signal_fd(int fd, uint32_t events, void *cbdata, |
111 | struct lxc_epoll_descr *descr) | |
b5159817 | 112 | { |
1349e92e | 113 | ssize_t ret; |
b5159817 DE |
114 | struct signalfd_siginfo siginfo; |
115 | struct lxc_tty_state *ts = cbdata; | |
116 | ||
1349e92e | 117 | ret = read(fd, &siginfo, sizeof(siginfo)); |
0d4137cc | 118 | if (ret < 0 || (size_t)ret < sizeof(siginfo)) { |
0519b5cc | 119 | ERROR("Failed to read signal info"); |
b5159817 | 120 | return -1; |
cd453b38 DL |
121 | } |
122 | ||
1349e92e CB |
123 | if (siginfo.ssi_signo == SIGTERM) { |
124 | DEBUG("Received SIGTERM. Detaching from the console"); | |
125 | return 1; | |
126 | } | |
127 | ||
128 | if (siginfo.ssi_signo == SIGWINCH) | |
129 | lxc_console_winch(ts); | |
130 | ||
b5159817 DE |
131 | return 0; |
132 | } | |
133 | ||
0519b5cc | 134 | struct lxc_tty_state *lxc_console_signal_init(int srcfd, int dstfd) |
b5159817 | 135 | { |
1349e92e | 136 | int ret; |
0519b5cc | 137 | bool istty; |
b5159817 DE |
138 | sigset_t mask; |
139 | struct lxc_tty_state *ts; | |
140 | ||
141 | ts = malloc(sizeof(*ts)); | |
142 | if (!ts) | |
143 | return NULL; | |
144 | ||
145 | memset(ts, 0, sizeof(*ts)); | |
341c2aed | 146 | ts->stdinfd = srcfd; |
b5159817 | 147 | ts->masterfd = dstfd; |
341c2aed | 148 | ts->sigfd = -1; |
b5159817 | 149 | |
1349e92e CB |
150 | sigemptyset(&mask); |
151 | ||
0519b5cc CB |
152 | istty = isatty(srcfd) == 1; |
153 | if (!istty) { | |
25964232 | 154 | INFO("fd %d does not refer to a tty device", srcfd); |
1349e92e CB |
155 | } else { |
156 | /* Add tty to list to be scanned at SIGWINCH time. */ | |
157 | lxc_list_add_elem(&ts->node, ts); | |
158 | lxc_list_add_tail(&lxc_ttys, &ts->node); | |
159 | sigaddset(&mask, SIGWINCH); | |
25964232 LF |
160 | } |
161 | ||
1349e92e CB |
162 | /* Exit the mainloop cleanly on SIGTERM. */ |
163 | sigaddset(&mask, SIGTERM); | |
b5159817 | 164 | |
1349e92e CB |
165 | ret = sigprocmask(SIG_BLOCK, &mask, &ts->oldmask); |
166 | if (ret < 0) { | |
167 | WARN("Failed to block signals"); | |
168 | goto on_error; | |
b5159817 DE |
169 | } |
170 | ||
171 | ts->sigfd = signalfd(-1, &mask, 0); | |
172 | if (ts->sigfd < 0) { | |
1349e92e | 173 | WARN("Failed to create signal fd"); |
341c2aed | 174 | sigprocmask(SIG_SETMASK, &ts->oldmask, NULL); |
1349e92e | 175 | goto on_error; |
b5159817 DE |
176 | } |
177 | ||
1349e92e CB |
178 | DEBUG("Created signal fd %d", ts->sigfd); |
179 | return ts; | |
180 | ||
181 | on_error: | |
182 | ERROR("Failed to create signal fd"); | |
183 | if (ts->sigfd >= 0) { | |
184 | close(ts->sigfd); | |
185 | ts->sigfd = -1; | |
186 | } | |
187 | if (istty) | |
188 | lxc_list_del(&ts->node); | |
b5159817 | 189 | return ts; |
cd453b38 DL |
190 | } |
191 | ||
0519b5cc | 192 | void lxc_console_signal_fini(struct lxc_tty_state *ts) |
63376d7d | 193 | { |
0e6da90b | 194 | if (ts->sigfd >= 0) { |
b5159817 | 195 | close(ts->sigfd); |
1349e92e CB |
196 | |
197 | if (sigprocmask(SIG_SETMASK, &ts->oldmask, NULL) < 0) | |
198 | WARN("%s - Failed to restore signal mask", strerror(errno)); | |
0e6da90b | 199 | } |
0d4137cc | 200 | |
1349e92e CB |
201 | if (isatty(ts->stdinfd)) |
202 | lxc_list_del(&ts->node); | |
203 | ||
b5159817 DE |
204 | free(ts); |
205 | } | |
1560f6c9 | 206 | |
84c92abd | 207 | static int lxc_console_cb_con(int fd, uint32_t events, void *data, |
b5159817 DE |
208 | struct lxc_epoll_descr *descr) |
209 | { | |
210 | struct lxc_console *console = (struct lxc_console *)data; | |
732375f5 CB |
211 | char buf[LXC_CONSOLE_BUFFER_SIZE]; |
212 | int r, w, w_log, w_rbuf; | |
e0dc0de7 | 213 | |
3e6580ec CB |
214 | w = r = lxc_read_nointr(fd, buf, sizeof(buf)); |
215 | if (r <= 0) { | |
732375f5 | 216 | INFO("Console client on fd %d has exited", fd); |
b5159817 | 217 | lxc_mainloop_del_handler(descr, fd); |
0b1e242b L |
218 | if (fd == console->peer) { |
219 | if (console->tty_state) { | |
0519b5cc | 220 | lxc_console_signal_fini(console->tty_state); |
0b1e242b L |
221 | console->tty_state = NULL; |
222 | } | |
223 | console->peer = -1; | |
224 | close(fd); | |
225 | return 0; | |
226 | } | |
b5159817 | 227 | close(fd); |
3e6580ec | 228 | return 1; |
33fcb7a0 | 229 | } |
63376d7d | 230 | |
b5159817 | 231 | if (fd == console->peer) |
3e6580ec | 232 | w = lxc_write_nointr(console->master, buf, r); |
b5159817 | 233 | |
732375f5 | 234 | w_rbuf = w_log = 0; |
b5159817 | 235 | if (fd == console->master) { |
732375f5 | 236 | /* write to peer first */ |
b5159817 | 237 | if (console->peer >= 0) |
3e6580ec | 238 | w = lxc_write_nointr(console->peer, buf, r); |
732375f5 CB |
239 | |
240 | /* write to console ringbuffer */ | |
28f3b1cd | 241 | if (console->buffer_size > 0) |
732375f5 CB |
242 | w_rbuf = lxc_ringbuf_write(&console->ringbuf, buf, r); |
243 | ||
244 | /* write to console log */ | |
245 | if (console->log_fd >= 0) | |
246 | w_log = lxc_write_nointr(console->log_fd, buf, r); | |
63376d7d DL |
247 | } |
248 | ||
b5159817 | 249 | if (w != r) |
732375f5 CB |
250 | WARN("Console short write r:%d != w:%d", r, w); |
251 | ||
252 | if (w_rbuf < 0) | |
253 | TRACE("%s - Failed to write %d bytes to console ringbuffer", | |
254 | strerror(-w_rbuf), r); | |
255 | ||
256 | if (w_log < 0) | |
257 | TRACE("Failed to write %d bytes to console log", r); | |
0d4137cc | 258 | |
b5159817 DE |
259 | return 0; |
260 | } | |
261 | ||
262 | static void lxc_console_mainloop_add_peer(struct lxc_console *console) | |
263 | { | |
264 | if (console->peer >= 0) { | |
265 | if (lxc_mainloop_add_handler(console->descr, console->peer, | |
266 | lxc_console_cb_con, console)) | |
0519b5cc | 267 | WARN("Failed to add console peer handler to mainloop"); |
63376d7d DL |
268 | } |
269 | ||
341c2aed | 270 | if (console->tty_state && console->tty_state->sigfd != -1) { |
b5159817 DE |
271 | if (lxc_mainloop_add_handler(console->descr, |
272 | console->tty_state->sigfd, | |
0519b5cc | 273 | lxc_console_cb_signal_fd, |
b5159817 | 274 | console->tty_state)) { |
0519b5cc | 275 | WARN("Failed to add signal handler to mainloop"); |
596a818d | 276 | } |
b5159817 DE |
277 | } |
278 | } | |
279 | ||
da41561c CB |
280 | extern int lxc_console_mainloop_add(struct lxc_epoll_descr *descr, |
281 | struct lxc_conf *conf) | |
b5159817 | 282 | { |
b5159817 DE |
283 | struct lxc_console *console = &conf->console; |
284 | ||
285 | if (!conf->rootfs.path) { | |
286 | INFO("no rootfs, no console."); | |
287 | return 0; | |
288 | } | |
289 | ||
290 | if (console->master < 0) { | |
291 | INFO("no console"); | |
292 | return 0; | |
596a818d DE |
293 | } |
294 | ||
b5159817 DE |
295 | if (lxc_mainloop_add_handler(descr, console->master, |
296 | lxc_console_cb_con, console)) { | |
297 | ERROR("failed to add to mainloop console handler for '%d'", | |
298 | console->master); | |
299 | return -1; | |
28a4b0e5 DL |
300 | } |
301 | ||
b5159817 DE |
302 | /* we cache the descr so that we can add an fd to it when someone |
303 | * does attach to it in lxc_console_allocate() | |
304 | */ | |
305 | console->descr = descr; | |
306 | lxc_console_mainloop_add_peer(console); | |
cd453b38 | 307 | |
b5159817 DE |
308 | return 0; |
309 | } | |
28a4b0e5 | 310 | |
0d4137cc | 311 | int lxc_setup_tios(int fd, struct termios *oldtios) |
b5159817 DE |
312 | { |
313 | struct termios newtios; | |
e0dc0de7 | 314 | |
b5159817 DE |
315 | if (!isatty(fd)) { |
316 | ERROR("'%d' is not a tty", fd); | |
317 | return -1; | |
e0dc0de7 DL |
318 | } |
319 | ||
b5159817 DE |
320 | /* Get current termios */ |
321 | if (tcgetattr(fd, oldtios)) { | |
e0dc0de7 | 322 | SYSERROR("failed to get current terminal settings"); |
b5159817 | 323 | return -1; |
e0dc0de7 DL |
324 | } |
325 | ||
4dc96430 TJ |
326 | /* ensure we don't end up in an endless loop: |
327 | * The kernel might fire SIGTTOU while an | |
328 | * ioctl() in tcsetattr() is executed. When the ioctl() | |
329 | * is resumed and retries, the signal handler interrupts it again. | |
330 | */ | |
331 | signal (SIGTTIN, SIG_IGN); | |
332 | signal (SIGTTOU, SIG_IGN); | |
333 | ||
b5159817 | 334 | newtios = *oldtios; |
e0dc0de7 | 335 | |
a7c97a40 CB |
336 | /* We use the same settings that ssh does. */ |
337 | newtios.c_iflag |= IGNPAR; | |
338 | newtios.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF); | |
339 | #ifdef IUCLC | |
340 | newtios.c_iflag &= ~IUCLC; | |
341 | #endif | |
4dc96430 | 342 | newtios.c_lflag &= ~(TOSTOP | ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL); |
a7c97a40 CB |
343 | #ifdef IEXTEN |
344 | newtios.c_lflag &= ~IEXTEN; | |
345 | #endif | |
d3893399 | 346 | newtios.c_oflag &= ~OPOST; |
b5159817 DE |
347 | newtios.c_cc[VMIN] = 1; |
348 | newtios.c_cc[VTIME] = 0; | |
e0dc0de7 | 349 | |
a7c97a40 | 350 | /* Set new attributes. */ |
b5159817 | 351 | if (tcsetattr(fd, TCSAFLUSH, &newtios)) { |
e0dc0de7 | 352 | ERROR("failed to set new terminal settings"); |
b5159817 | 353 | return -1; |
e0dc0de7 DL |
354 | } |
355 | ||
63376d7d | 356 | return 0; |
b5159817 | 357 | } |
e0dc0de7 | 358 | |
b5159817 DE |
359 | static void lxc_console_peer_proxy_free(struct lxc_console *console) |
360 | { | |
0e6da90b | 361 | if (console->tty_state) { |
0519b5cc | 362 | lxc_console_signal_fini(console->tty_state); |
b5159817 DE |
363 | console->tty_state = NULL; |
364 | } | |
365 | close(console->peerpty.master); | |
366 | close(console->peerpty.slave); | |
367 | console->peerpty.master = -1; | |
368 | console->peerpty.slave = -1; | |
369 | console->peerpty.busy = -1; | |
370 | console->peerpty.name[0] = '\0'; | |
596a818d | 371 | console->peer = -1; |
b5159817 | 372 | } |
596a818d | 373 | |
b5159817 DE |
374 | static int lxc_console_peer_proxy_alloc(struct lxc_console *console, int sockfd) |
375 | { | |
376 | struct termios oldtermio; | |
377 | struct lxc_tty_state *ts; | |
025ed0f3 | 378 | int ret; |
b5159817 DE |
379 | |
380 | if (console->master < 0) { | |
381 | ERROR("console not set up"); | |
382 | return -1; | |
383 | } | |
384 | if (console->peerpty.busy != -1 || console->peer != -1) { | |
385 | NOTICE("console already in use"); | |
386 | return -1; | |
387 | } | |
388 | if (console->tty_state) { | |
389 | ERROR("console already has tty_state"); | |
390 | return -1; | |
596a818d DE |
391 | } |
392 | ||
b5159817 DE |
393 | /* this is the proxy pty that will be given to the client, and that |
394 | * the real pty master will send to / recv from | |
395 | */ | |
025ed0f3 SH |
396 | process_lock(); |
397 | ret = openpty(&console->peerpty.master, &console->peerpty.slave, | |
398 | console->peerpty.name, NULL, NULL); | |
399 | process_unlock(); | |
400 | if (ret) { | |
b5159817 DE |
401 | SYSERROR("failed to create proxy pty"); |
402 | return -1; | |
403 | } | |
596a818d | 404 | |
0d4137cc | 405 | if (lxc_setup_tios(console->peerpty.slave, &oldtermio) < 0) |
b5159817 DE |
406 | goto err1; |
407 | ||
0519b5cc | 408 | ts = lxc_console_signal_init(console->peerpty.master, console->master); |
b5159817 DE |
409 | if (!ts) |
410 | goto err1; | |
411 | ||
412 | console->tty_state = ts; | |
413 | console->peer = console->peerpty.slave; | |
414 | console->peerpty.busy = sockfd; | |
415 | lxc_console_mainloop_add_peer(console); | |
416 | ||
417 | DEBUG("%d %s peermaster:%d sockfd:%d", getpid(), __FUNCTION__, console->peerpty.master, sockfd); | |
418 | return 0; | |
419 | ||
420 | err1: | |
421 | lxc_console_peer_proxy_free(console); | |
63376d7d DL |
422 | return -1; |
423 | } | |
424 | ||
b5159817 DE |
425 | int lxc_console_allocate(struct lxc_conf *conf, int sockfd, int *ttyreq) |
426 | { | |
427 | int masterfd = -1, ttynum; | |
428 | struct lxc_tty_info *tty_info = &conf->tty_info; | |
429 | struct lxc_console *console = &conf->console; | |
430 | ||
b5159817 DE |
431 | if (*ttyreq == 0) { |
432 | if (lxc_console_peer_proxy_alloc(console, sockfd) < 0) | |
433 | goto out; | |
434 | masterfd = console->peerpty.master; | |
435 | goto out; | |
436 | } | |
437 | ||
438 | if (*ttyreq > 0) { | |
439 | if (*ttyreq > tty_info->nbtty) | |
440 | goto out; | |
441 | ||
442 | if (tty_info->pty_info[*ttyreq - 1].busy) | |
443 | goto out; | |
444 | ||
445 | /* the requested tty is available */ | |
446 | ttynum = *ttyreq; | |
447 | goto out_tty; | |
448 | } | |
449 | ||
450 | /* search for next available tty, fixup index tty1 => [0] */ | |
0d4137cc CB |
451 | for (ttynum = 1; ttynum <= tty_info->nbtty && tty_info->pty_info[ttynum - 1].busy; ttynum++) |
452 | ; | |
b5159817 DE |
453 | |
454 | /* we didn't find any available slot for tty */ | |
455 | if (ttynum > tty_info->nbtty) | |
456 | goto out; | |
457 | ||
458 | *ttyreq = ttynum; | |
459 | ||
460 | out_tty: | |
461 | tty_info->pty_info[ttynum - 1].busy = sockfd; | |
462 | masterfd = tty_info->pty_info[ttynum - 1].master; | |
463 | out: | |
b5159817 DE |
464 | return masterfd; |
465 | } | |
466 | ||
b5159817 | 467 | void lxc_console_free(struct lxc_conf *conf, int fd) |
63376d7d | 468 | { |
b5159817 DE |
469 | int i; |
470 | struct lxc_tty_info *tty_info = &conf->tty_info; | |
471 | struct lxc_console *console = &conf->console; | |
472 | ||
b5159817 DE |
473 | for (i = 0; i < tty_info->nbtty; i++) { |
474 | if (tty_info->pty_info[i].busy == fd) | |
475 | tty_info->pty_info[i].busy = 0; | |
476 | } | |
477 | ||
478 | if (console->peerpty.busy == fd) { | |
479 | lxc_mainloop_del_handler(console->descr, console->peerpty.slave); | |
480 | lxc_console_peer_proxy_free(console); | |
481 | } | |
b5159817 DE |
482 | } |
483 | ||
467c7ff3 | 484 | static int lxc_console_peer_default(struct lxc_console *console) |
b5159817 DE |
485 | { |
486 | struct lxc_tty_state *ts; | |
487 | const char *path = console->path; | |
467c7ff3 CB |
488 | int fd; |
489 | int ret = 0; | |
b5159817 | 490 | |
467c7ff3 CB |
491 | /* If no console was given, try current controlling terminal, there |
492 | * won't be one if we were started as a daemon (-d). | |
b5159817 DE |
493 | */ |
494 | if (!path && !access("/dev/tty", F_OK)) { | |
b5159817 DE |
495 | fd = open("/dev/tty", O_RDWR); |
496 | if (fd >= 0) { | |
497 | close(fd); | |
498 | path = "/dev/tty"; | |
499 | } | |
500 | } | |
501 | ||
467c7ff3 CB |
502 | if (!path) { |
503 | errno = ENOTTY; | |
504 | DEBUG("process does not have a controlling terminal"); | |
b5159817 | 505 | goto out; |
467c7ff3 | 506 | } |
b5159817 | 507 | |
467c7ff3 CB |
508 | console->peer = lxc_unpriv(open(path, O_CLOEXEC | O_RDWR | O_CREAT | O_APPEND, 0600)); |
509 | if (console->peer < 0) { | |
6f18b9c4 | 510 | ERROR("failed to open \"%s\": %s", path, strerror(errno)); |
467c7ff3 CB |
511 | return -ENOTTY; |
512 | } | |
513 | DEBUG("using \"%s\" as peer tty device", path); | |
b5159817 | 514 | |
467c7ff3 CB |
515 | if (!isatty(console->peer)) { |
516 | ERROR("file descriptor for file \"%s\" does not refer to a tty device", path); | |
517 | goto on_error1; | |
518 | } | |
b5159817 | 519 | |
0519b5cc | 520 | ts = lxc_console_signal_init(console->peer, console->master); |
b5159817 | 521 | console->tty_state = ts; |
341c2aed | 522 | if (!ts) { |
0519b5cc | 523 | WARN("Failed to install signal handler"); |
467c7ff3 | 524 | goto on_error1; |
341c2aed | 525 | } |
b5159817 DE |
526 | |
527 | lxc_console_winsz(console->peer, console->master); | |
528 | ||
529 | console->tios = malloc(sizeof(*console->tios)); | |
530 | if (!console->tios) { | |
531 | SYSERROR("failed to allocate memory"); | |
467c7ff3 | 532 | goto on_error1; |
b5159817 DE |
533 | } |
534 | ||
0d4137cc | 535 | if (lxc_setup_tios(console->peer, console->tios) < 0) |
467c7ff3 CB |
536 | goto on_error2; |
537 | else | |
538 | goto out; | |
b5159817 | 539 | |
467c7ff3 | 540 | on_error2: |
b5159817 DE |
541 | free(console->tios); |
542 | console->tios = NULL; | |
467c7ff3 CB |
543 | |
544 | on_error1: | |
b5159817 DE |
545 | close(console->peer); |
546 | console->peer = -1; | |
467c7ff3 CB |
547 | ret = -ENOTTY; |
548 | ||
b5159817 | 549 | out: |
467c7ff3 | 550 | return ret; |
b5159817 DE |
551 | } |
552 | ||
63b74cda | 553 | int lxc_console_write_ringbuffer(struct lxc_console *console) |
40117d05 | 554 | { |
40117d05 CB |
555 | char *r_addr; |
556 | ssize_t ret; | |
557 | uint64_t used; | |
558 | struct lxc_ringbuf *buf = &console->ringbuf; | |
559 | ||
3a784510 | 560 | if (!console->buffer_log_file) |
40117d05 CB |
561 | return 0; |
562 | ||
563 | used = lxc_ringbuf_used(buf); | |
564 | if (used == 0) | |
565 | return 0; | |
566 | ||
40117d05 | 567 | r_addr = lxc_ringbuf_get_read_addr(buf); |
a0309168 | 568 | ret = lxc_write_nointr(console->buffer_log_file_fd, r_addr, used); |
40117d05 | 569 | if (ret < 0) |
63b74cda | 570 | return -EIO; |
40117d05 CB |
571 | |
572 | return 0; | |
573 | } | |
574 | ||
b5159817 DE |
575 | void lxc_console_delete(struct lxc_console *console) |
576 | { | |
69629c82 CB |
577 | int ret; |
578 | ||
40117d05 CB |
579 | ret = lxc_console_write_ringbuffer(console); |
580 | if (ret < 0) | |
581 | WARN("Failed to write console log to disk"); | |
582 | ||
69629c82 CB |
583 | if (console->tios && console->peer >= 0) { |
584 | ret = tcsetattr(console->peer, TCSAFLUSH, console->tios); | |
585 | if (ret < 0) | |
586 | WARN("%s - Failed to set old terminal settings", strerror(errno)); | |
587 | } | |
596a818d DE |
588 | free(console->tios); |
589 | console->tios = NULL; | |
590 | ||
591 | close(console->peer); | |
025ed0f3 SH |
592 | close(console->master); |
593 | close(console->slave); | |
594 | if (console->log_fd >= 0) | |
596a818d | 595 | close(console->log_fd); |
025ed0f3 | 596 | console->peer = -1; |
596a818d | 597 | console->master = -1; |
596a818d | 598 | console->slave = -1; |
025ed0f3 | 599 | console->log_fd = -1; |
a0309168 CB |
600 | if (console->buffer_log_file_fd >= 0) |
601 | close(console->buffer_log_file_fd); | |
602 | console->buffer_log_file_fd = -1; | |
603 | } | |
604 | ||
605 | /* This is the console ringbuffer log file. Please note that the console | |
606 | * ringbuffer log file is (implementation wise not content wise) independent of | |
607 | * the console log file. | |
608 | */ | |
609 | static int lxc_console_create_ringbuf_log_file(struct lxc_console *console) | |
610 | { | |
611 | if (!console->buffer_log_file) | |
612 | return 0; | |
613 | ||
614 | console->buffer_log_file_fd = lxc_unpriv(open(console->buffer_log_file, | |
615 | O_CLOEXEC | O_RDWR | O_CREAT | O_TRUNC, 0600)); | |
616 | if (console->buffer_log_file_fd < 0) { | |
617 | SYSERROR("Failed to open console ringbuffer log file \"%s\"", | |
618 | console->buffer_log_file); | |
619 | return -EIO; | |
620 | } | |
621 | ||
622 | DEBUG("Using \"%s\" as console ringbuffer log file", console->buffer_log_file); | |
623 | return 0; | |
63376d7d DL |
624 | } |
625 | ||
3b988b33 CB |
626 | /** |
627 | * Note that this function needs to run before the mainloop starts. Since we | |
628 | * register a handler for the console's masterfd when we create the mainloop | |
629 | * the console handler needs to see an allocated ringbuffer. | |
630 | */ | |
a0309168 | 631 | static int lxc_console_create_ringbuf(struct lxc_console *console) |
3b988b33 CB |
632 | { |
633 | int ret; | |
634 | struct lxc_ringbuf *buf = &console->ringbuf; | |
28f3b1cd | 635 | uint64_t size = console->buffer_size; |
3b988b33 CB |
636 | |
637 | /* no ringbuffer previously allocated and no ringbuffer requested */ | |
638 | if (!buf->addr && size <= 0) | |
639 | return 0; | |
640 | ||
641 | /* ringbuffer allocated but no new ringbuffer requested */ | |
642 | if (buf->addr && size <= 0) { | |
643 | lxc_ringbuf_release(buf); | |
644 | buf->addr = NULL; | |
645 | buf->r_off = 0; | |
646 | buf->w_off = 0; | |
647 | buf->size = 0; | |
648 | TRACE("Deallocated console ringbuffer"); | |
649 | return 0; | |
650 | } | |
651 | ||
652 | if (size <= 0) | |
653 | return 0; | |
654 | ||
655 | /* check wether the requested size for the ringbuffer has changed */ | |
656 | if (buf->addr && buf->size != size) { | |
657 | TRACE("Console ringbuffer size changed from %" PRIu64 | |
658 | " to %" PRIu64 " bytes. Deallocating console ringbuffer", | |
659 | buf->size, size); | |
660 | lxc_ringbuf_release(buf); | |
661 | } | |
662 | ||
663 | ret = lxc_ringbuf_create(buf, size); | |
664 | if (ret < 0) { | |
665 | ERROR("Failed to setup %" PRIu64 " byte console ringbuffer", size); | |
666 | return -1; | |
667 | } | |
668 | ||
669 | TRACE("Allocated %" PRIu64 " byte console ringbuffer", size); | |
670 | return 0; | |
671 | } | |
672 | ||
a0309168 CB |
673 | /** |
674 | * This is the console log file. Please note that the console log file is | |
675 | * (implementation wise not content wise) independent of the console ringbuffer. | |
676 | */ | |
677 | static int lxc_console_create_log_file(struct lxc_console *console) | |
678 | { | |
679 | if (!console->log_path) | |
680 | return 0; | |
681 | ||
682 | console->log_fd = lxc_unpriv(open(console->log_path, O_CLOEXEC | O_RDWR | O_CREAT | O_APPEND, 0600)); | |
683 | if (console->log_fd < 0) { | |
684 | SYSERROR("Failed to open console log file \"%s\"", console->log_path); | |
685 | return -1; | |
686 | } | |
687 | ||
688 | DEBUG("Using \"%s\" as console log file", console->log_path); | |
689 | return 0; | |
690 | } | |
691 | ||
b5159817 | 692 | int lxc_console_create(struct lxc_conf *conf) |
63376d7d | 693 | { |
69629c82 | 694 | int ret, saved_errno; |
b5159817 | 695 | struct lxc_console *console = &conf->console; |
63376d7d | 696 | |
467c7ff3 | 697 | if (!conf->rootfs.path) { |
69629c82 CB |
698 | INFO("Container does not have a rootfs. The console will be " |
699 | "shared with the host"); | |
b5159817 | 700 | return 0; |
467c7ff3 | 701 | } |
b5159817 | 702 | |
467c7ff3 | 703 | if (console->path && !strcmp(console->path, "none")) { |
69629c82 | 704 | INFO("No console was requested"); |
b5159817 | 705 | return 0; |
467c7ff3 | 706 | } |
b5159817 | 707 | |
025ed0f3 | 708 | process_lock(); |
467c7ff3 | 709 | ret = openpty(&console->master, &console->slave, console->name, NULL, NULL); |
69629c82 | 710 | saved_errno = errno; |
025ed0f3 | 711 | process_unlock(); |
467c7ff3 | 712 | if (ret < 0) { |
69629c82 | 713 | ERROR("%s - Failed to allocate a pty", strerror(saved_errno)); |
b5159817 DE |
714 | return -1; |
715 | } | |
716 | ||
69629c82 CB |
717 | ret = fcntl(console->master, F_SETFD, FD_CLOEXEC); |
718 | if (ret < 0) { | |
719 | SYSERROR("Failed to set FD_CLOEXEC flag on console master"); | |
b5159817 DE |
720 | goto err; |
721 | } | |
722 | ||
69629c82 CB |
723 | ret = fcntl(console->slave, F_SETFD, FD_CLOEXEC); |
724 | if (ret < 0) { | |
725 | SYSERROR("Failed to set FD_CLOEXEC flag on console slave"); | |
b5159817 DE |
726 | goto err; |
727 | } | |
728 | ||
467c7ff3 CB |
729 | ret = lxc_console_peer_default(console); |
730 | if (ret < 0) { | |
69629c82 | 731 | ERROR("Failed to allocate a peer pty device"); |
467c7ff3 CB |
732 | goto err; |
733 | } | |
b5159817 | 734 | |
a0309168 CB |
735 | /* create console log file */ |
736 | ret = lxc_console_create_log_file(console); | |
737 | if (ret < 0) | |
738 | goto err; | |
739 | ||
740 | /* create console ringbuffer */ | |
741 | ret = lxc_console_create_ringbuf(console); | |
742 | if (ret < 0) | |
743 | goto err; | |
b5159817 | 744 | |
a0309168 CB |
745 | /* create console ringbuffer log file */ |
746 | ret = lxc_console_create_ringbuf_log_file(console); | |
3b988b33 CB |
747 | if (ret < 0) |
748 | goto err; | |
749 | ||
b5159817 DE |
750 | return 0; |
751 | ||
752 | err: | |
753 | lxc_console_delete(console); | |
69629c82 | 754 | return -ENODEV; |
b5159817 DE |
755 | } |
756 | ||
39a78bbe | 757 | int lxc_console_set_stdfds(int fd) |
0d9acb99 | 758 | { |
39a78bbe | 759 | if (fd < 0) |
0d9acb99 | 760 | return 0; |
b5159817 | 761 | |
39a78bbe CB |
762 | if (isatty(STDIN_FILENO)) |
763 | if (dup2(fd, STDIN_FILENO) < 0) { | |
764 | SYSERROR("failed to duplicate stdin."); | |
765 | return -1; | |
766 | } | |
767 | ||
768 | if (isatty(STDOUT_FILENO)) | |
769 | if (dup2(fd, STDOUT_FILENO) < 0) { | |
770 | SYSERROR("failed to duplicate stdout."); | |
771 | return -1; | |
772 | } | |
773 | ||
774 | if (isatty(STDERR_FILENO)) | |
775 | if (dup2(fd, STDERR_FILENO) < 0) { | |
776 | SYSERROR("failed to duplicate stderr."); | |
777 | return -1; | |
778 | } | |
779 | ||
0d9acb99 DE |
780 | return 0; |
781 | } | |
b5159817 | 782 | |
0d4137cc CB |
783 | int lxc_console_cb_tty_stdin(int fd, uint32_t events, void *cbdata, |
784 | struct lxc_epoll_descr *descr) | |
b5159817 DE |
785 | { |
786 | struct lxc_tty_state *ts = cbdata; | |
787 | char c; | |
788 | ||
97bc2422 CB |
789 | if (fd != ts->stdinfd) |
790 | return 1; | |
791 | ||
e66b6c96 | 792 | if (lxc_read_nointr(ts->stdinfd, &c, 1) <= 0) |
63376d7d | 793 | return 1; |
63376d7d | 794 | |
525e2117 | 795 | if (ts->escape >= 1) { |
014d5e1e CB |
796 | /* we want to exit the console with Ctrl+a q */ |
797 | if (c == ts->escape && !ts->saw_escape) { | |
798 | ts->saw_escape = 1; | |
799 | return 0; | |
800 | } | |
5c294060 | 801 | |
014d5e1e CB |
802 | if (c == 'q' && ts->saw_escape) |
803 | return 1; | |
804 | ||
805 | ts->saw_escape = 0; | |
806 | } | |
63376d7d | 807 | |
e66b6c96 | 808 | if (lxc_write_nointr(ts->masterfd, &c, 1) <= 0) |
b5159817 | 809 | return 1; |
b5159817 | 810 | |
63376d7d DL |
811 | return 0; |
812 | } | |
813 | ||
0d4137cc CB |
814 | int lxc_console_cb_tty_master(int fd, uint32_t events, void *cbdata, |
815 | struct lxc_epoll_descr *descr) | |
63376d7d | 816 | { |
b5159817 | 817 | struct lxc_tty_state *ts = cbdata; |
732375f5 | 818 | char buf[LXC_CONSOLE_BUFFER_SIZE]; |
0d4137cc | 819 | int r, w; |
63376d7d | 820 | |
97bc2422 CB |
821 | if (fd != ts->masterfd) |
822 | return 1; | |
823 | ||
e66b6c96 CB |
824 | r = lxc_read_nointr(fd, buf, sizeof(buf)); |
825 | if (r <= 0) | |
b5159817 | 826 | return 1; |
1560f6c9 | 827 | |
e66b6c96 CB |
828 | w = lxc_write_nointr(ts->stdoutfd, buf, r); |
829 | if (w <= 0) { | |
830 | return 1; | |
831 | } else if (w != r) { | |
b5159817 DE |
832 | SYSERROR("failed to write"); |
833 | return 1; | |
f78a1f32 DL |
834 | } |
835 | ||
b5159817 DE |
836 | return 0; |
837 | } | |
838 | ||
839 | int lxc_console_getfd(struct lxc_container *c, int *ttynum, int *masterfd) | |
840 | { | |
841 | return lxc_cmd_console(c->name, ttynum, masterfd, c->config_path); | |
842 | } | |
843 | ||
844 | int lxc_console(struct lxc_container *c, int ttynum, | |
845 | int stdinfd, int stdoutfd, int stderrfd, | |
846 | int escape) | |
847 | { | |
848 | int ret, ttyfd, masterfd; | |
849 | struct lxc_epoll_descr descr; | |
850 | struct termios oldtios; | |
851 | struct lxc_tty_state *ts; | |
25964232 | 852 | int istty = 0; |
b5159817 | 853 | |
b5159817 | 854 | ttyfd = lxc_cmd_console(c->name, &ttynum, &masterfd, c->config_path); |
6834f805 CB |
855 | if (ttyfd < 0) |
856 | return -1; | |
b5159817 DE |
857 | |
858 | ret = setsid(); | |
33b4b411 CB |
859 | if (ret < 0) |
860 | TRACE("Process is already group leader"); | |
b5159817 | 861 | |
0519b5cc | 862 | ts = lxc_console_signal_init(stdinfd, masterfd); |
b5159817 DE |
863 | if (!ts) { |
864 | ret = -1; | |
33b4b411 | 865 | goto close_fds; |
b5159817 DE |
866 | } |
867 | ts->escape = escape; | |
868 | ts->winch_proxy = c->name; | |
869 | ts->winch_proxy_lxcpath = c->config_path; | |
3b975060 | 870 | ts->stdoutfd = stdoutfd; |
b5159817 | 871 | |
6834f805 | 872 | istty = isatty(stdinfd); |
25964232 LF |
873 | if (istty) { |
874 | lxc_console_winsz(stdinfd, masterfd); | |
875 | lxc_cmd_console_winch(ts->winch_proxy, ts->winch_proxy_lxcpath); | |
6834f805 CB |
876 | } else { |
877 | INFO("File descriptor %d does not refer to a tty device", stdinfd); | |
25964232 | 878 | } |
b5159817 DE |
879 | |
880 | ret = lxc_mainloop_open(&descr); | |
881 | if (ret) { | |
33b4b411 CB |
882 | ERROR("Failed to create mainloop"); |
883 | goto sigwinch_fini; | |
b5159817 DE |
884 | } |
885 | ||
341c2aed CB |
886 | if (ts->sigfd != -1) { |
887 | ret = lxc_mainloop_add_handler(&descr, ts->sigfd, | |
0519b5cc | 888 | lxc_console_cb_signal_fd, ts); |
33b4b411 | 889 | if (ret < 0) { |
0519b5cc | 890 | ERROR("Failed to add signal handler to mainloop"); |
33b4b411 | 891 | goto close_mainloop; |
341c2aed | 892 | } |
b5159817 DE |
893 | } |
894 | ||
895 | ret = lxc_mainloop_add_handler(&descr, ts->stdinfd, | |
896 | lxc_console_cb_tty_stdin, ts); | |
33b4b411 CB |
897 | if (ret < 0) { |
898 | ERROR("Failed to add stdin handler"); | |
899 | goto close_mainloop; | |
b5159817 DE |
900 | } |
901 | ||
902 | ret = lxc_mainloop_add_handler(&descr, ts->masterfd, | |
903 | lxc_console_cb_tty_master, ts); | |
33b4b411 CB |
904 | if (ret < 0) { |
905 | ERROR("Failed to add master handler"); | |
906 | goto close_mainloop; | |
b5159817 DE |
907 | } |
908 | ||
686df166 CB |
909 | if (ts->escape >= 1) { |
910 | fprintf(stderr, | |
911 | "\n" | |
6834f805 CB |
912 | "Connected to tty %1$d\n" |
913 | "Type <Ctrl+%2$c q> to exit the console, " | |
914 | "<Ctrl+%2$c Ctrl+%2$c> to enter Ctrl+%2$c itself\n", | |
915 | ttynum, 'a' + escape - 1); | |
686df166 | 916 | } |
6834f805 CB |
917 | |
918 | if (istty) { | |
919 | ret = lxc_setup_tios(stdinfd, &oldtios); | |
920 | if (ret < 0) | |
921 | goto close_mainloop; | |
922 | } | |
923 | ||
025ed0f3 | 924 | ret = lxc_mainloop(&descr, -1); |
33b4b411 CB |
925 | if (ret < 0) { |
926 | ERROR("The mainloop returned an error"); | |
6834f805 | 927 | goto restore_tios; |
b5159817 DE |
928 | } |
929 | ||
930 | ret = 0; | |
931 | ||
6834f805 CB |
932 | restore_tios: |
933 | if (istty) { | |
934 | istty = tcsetattr(stdinfd, TCSAFLUSH, &oldtios); | |
935 | if (istty < 0) | |
936 | WARN("%s - Failed to restore terminal properties", | |
937 | strerror(errno)); | |
938 | } | |
939 | ||
33b4b411 | 940 | close_mainloop: |
b5159817 | 941 | lxc_mainloop_close(&descr); |
33b4b411 CB |
942 | |
943 | sigwinch_fini: | |
0519b5cc | 944 | lxc_console_signal_fini(ts); |
33b4b411 CB |
945 | |
946 | close_fds: | |
b5159817 DE |
947 | close(masterfd); |
948 | close(ttyfd); | |
33b4b411 | 949 | |
b5159817 | 950 | return ret; |
63376d7d | 951 | } |