]>
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 | ||
9d465140 | 24 | #define _GNU_SOURCE |
9395937a CB |
25 | #include <errno.h> |
26 | #include <fcntl.h> | |
e62912bd | 27 | #include <lxc/lxccontainer.h> |
b467714b | 28 | #include <pthread.h> |
b5159817 | 29 | #include <signal.h> |
b0a33c1e | 30 | #include <stdio.h> |
e0dc0de7 | 31 | #include <stdlib.h> |
da41561c CB |
32 | #include <sys/epoll.h> |
33 | #include <sys/types.h> | |
e62912bd CB |
34 | #include <termios.h> |
35 | #include <unistd.h> | |
f2363e38 | 36 | |
9395937a CB |
37 | #include "af_unix.h" |
38 | #include "caps.h" | |
39 | #include "commands.h" | |
00dbc43e | 40 | #include "conf.h" |
e827ff7e | 41 | #include "config.h" |
9395937a | 42 | #include "log.h" |
b5159817 | 43 | #include "lxclock.h" |
9395937a | 44 | #include "mainloop.h" |
e62912bd | 45 | #include "start.h" |
0ed9b1bc | 46 | #include "terminal.h" |
b5159817 | 47 | #include "utils.h" |
36eb9bde | 48 | |
e827ff7e SG |
49 | #if HAVE_PTY_H |
50 | #include <pty.h> | |
51 | #else | |
52 | #include <../include/openpty.h> | |
53 | #endif | |
54 | ||
de708fb7 | 55 | #define LXC_TERMINAL_BUFFER_SIZE 1024 |
732375f5 | 56 | |
2083d59d | 57 | lxc_log_define(terminal, lxc); |
36eb9bde | 58 | |
b5159817 | 59 | static struct lxc_list lxc_ttys; |
724e753c | 60 | |
b5159817 | 61 | typedef void (*sighandler_t)(int); |
b5159817 | 62 | |
fc26f086 | 63 | __attribute__((constructor)) void lxc_terminal_init_global(void) |
b5159817 DE |
64 | { |
65 | lxc_list_init(&lxc_ttys); | |
66 | } | |
724e753c | 67 | |
4e9c0330 | 68 | void lxc_terminal_winsz(int srcfd, int dstfd) |
b5159817 | 69 | { |
0519b5cc | 70 | int ret; |
b5159817 | 71 | struct winsize wsz; |
0519b5cc CB |
72 | |
73 | if (!isatty(srcfd)) | |
74 | return; | |
75 | ||
76 | ret = ioctl(srcfd, TIOCGWINSZ, &wsz); | |
77 | if (ret < 0) { | |
78 | WARN("Failed to get window size"); | |
79 | return; | |
724e753c | 80 | } |
0519b5cc CB |
81 | |
82 | ret = ioctl(dstfd, TIOCSWINSZ, &wsz); | |
83 | if (ret < 0) | |
84 | WARN("Failed to set window size"); | |
85 | else | |
86 | DEBUG("Set window size to %d columns and %d rows", wsz.ws_col, | |
87 | wsz.ws_row); | |
88 | ||
89 | return; | |
b5159817 | 90 | } |
724e753c | 91 | |
5b55021f | 92 | static void lxc_terminal_winch(struct lxc_terminal_state *ts) |
b5159817 | 93 | { |
4e9c0330 | 94 | lxc_terminal_winsz(ts->stdinfd, ts->masterfd); |
0519b5cc | 95 | |
0d4137cc | 96 | if (ts->winch_proxy) |
8ccbbf94 | 97 | lxc_cmd_terminal_winch(ts->winch_proxy, ts->winch_proxy_lxcpath); |
724e753c MN |
98 | } |
99 | ||
dad4a039 | 100 | void lxc_terminal_sigwinch(int sig) |
cd453b38 | 101 | { |
025ed0f3 | 102 | struct lxc_list *it; |
5b55021f | 103 | struct lxc_terminal_state *ts; |
cd453b38 | 104 | |
025ed0f3 SH |
105 | lxc_list_for_each(it, &lxc_ttys) { |
106 | ts = it->elem; | |
7a10164a | 107 | lxc_terminal_winch(ts); |
cd453b38 | 108 | } |
b5159817 | 109 | } |
cd453b38 | 110 | |
9bafc8cb CB |
111 | int lxc_terminal_signalfd_cb(int fd, uint32_t events, void *cbdata, |
112 | struct lxc_epoll_descr *descr) | |
b5159817 | 113 | { |
1349e92e | 114 | ssize_t ret; |
b5159817 | 115 | struct signalfd_siginfo siginfo; |
5b55021f | 116 | struct lxc_terminal_state *ts = cbdata; |
b5159817 | 117 | |
1349e92e | 118 | ret = read(fd, &siginfo, sizeof(siginfo)); |
0d4137cc | 119 | if (ret < 0 || (size_t)ret < sizeof(siginfo)) { |
0519b5cc | 120 | ERROR("Failed to read signal info"); |
2e943b7b | 121 | return LXC_MAINLOOP_ERROR; |
cd453b38 DL |
122 | } |
123 | ||
1349e92e | 124 | if (siginfo.ssi_signo == SIGTERM) { |
9bafc8cb | 125 | DEBUG("Received SIGTERM. Detaching from the terminal"); |
a529bc25 | 126 | return LXC_MAINLOOP_CLOSE; |
1349e92e CB |
127 | } |
128 | ||
129 | if (siginfo.ssi_signo == SIGWINCH) | |
7a10164a | 130 | lxc_terminal_winch(ts); |
1349e92e | 131 | |
2e943b7b | 132 | return LXC_MAINLOOP_CONTINUE; |
b5159817 DE |
133 | } |
134 | ||
5b55021f | 135 | struct lxc_terminal_state *lxc_terminal_signal_init(int srcfd, int dstfd) |
b5159817 | 136 | { |
1349e92e | 137 | int ret; |
b6d5de95 | 138 | bool istty = false; |
b5159817 | 139 | sigset_t mask; |
5b55021f | 140 | struct lxc_terminal_state *ts; |
b5159817 DE |
141 | |
142 | ts = malloc(sizeof(*ts)); | |
143 | if (!ts) | |
144 | return NULL; | |
145 | ||
146 | memset(ts, 0, sizeof(*ts)); | |
341c2aed | 147 | ts->stdinfd = srcfd; |
b5159817 | 148 | ts->masterfd = dstfd; |
341c2aed | 149 | ts->sigfd = -1; |
b5159817 | 150 | |
b6d5de95 CB |
151 | ret = sigemptyset(&mask); |
152 | if (ret < 0) { | |
153 | SYSERROR("Failed to initialize an empty signal set"); | |
154 | goto on_error; | |
155 | } | |
1349e92e | 156 | |
b6d5de95 | 157 | istty = (isatty(srcfd) == 1); |
0519b5cc | 158 | if (!istty) { |
25964232 | 159 | INFO("fd %d does not refer to a tty device", srcfd); |
1349e92e CB |
160 | } else { |
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); | |
b6d5de95 CB |
164 | ret = sigaddset(&mask, SIGWINCH); |
165 | if (ret < 0) | |
7874d81a | 166 | SYSNOTICE("Failed to add SIGWINCH to signal set"); |
25964232 LF |
167 | } |
168 | ||
1349e92e | 169 | /* Exit the mainloop cleanly on SIGTERM. */ |
b6d5de95 CB |
170 | ret = sigaddset(&mask, SIGTERM); |
171 | if (ret < 0) { | |
172 | SYSERROR("Failed to add SIGWINCH to signal set"); | |
173 | goto on_error; | |
174 | } | |
b5159817 | 175 | |
b467714b | 176 | ret = pthread_sigmask(SIG_BLOCK, &mask, &ts->oldmask); |
1349e92e CB |
177 | if (ret < 0) { |
178 | WARN("Failed to block signals"); | |
179 | goto on_error; | |
b5159817 DE |
180 | } |
181 | ||
dc5f6125 | 182 | ts->sigfd = signalfd(-1, &mask, SFD_CLOEXEC); |
b5159817 | 183 | if (ts->sigfd < 0) { |
1349e92e | 184 | WARN("Failed to create signal fd"); |
b467714b | 185 | (void)pthread_sigmask(SIG_SETMASK, &ts->oldmask, NULL); |
1349e92e | 186 | goto on_error; |
b5159817 DE |
187 | } |
188 | ||
1349e92e CB |
189 | DEBUG("Created signal fd %d", ts->sigfd); |
190 | return ts; | |
191 | ||
192 | on_error: | |
193 | ERROR("Failed to create signal fd"); | |
194 | if (ts->sigfd >= 0) { | |
195 | close(ts->sigfd); | |
196 | ts->sigfd = -1; | |
197 | } | |
b6d5de95 | 198 | |
1349e92e CB |
199 | if (istty) |
200 | lxc_list_del(&ts->node); | |
b6d5de95 | 201 | |
b5159817 | 202 | return ts; |
cd453b38 DL |
203 | } |
204 | ||
5b55021f | 205 | void lxc_terminal_signal_fini(struct lxc_terminal_state *ts) |
63376d7d | 206 | { |
0e6da90b | 207 | if (ts->sigfd >= 0) { |
b5159817 | 208 | close(ts->sigfd); |
1349e92e | 209 | |
b467714b | 210 | if (pthread_sigmask(SIG_SETMASK, &ts->oldmask, NULL) < 0) |
a24c5678 | 211 | SYSWARN("Failed to restore signal mask"); |
0e6da90b | 212 | } |
0d4137cc | 213 | |
1349e92e CB |
214 | if (isatty(ts->stdinfd)) |
215 | lxc_list_del(&ts->node); | |
216 | ||
b5159817 DE |
217 | free(ts); |
218 | } | |
1560f6c9 | 219 | |
99a04585 | 220 | static int lxc_terminal_truncate_log_file(struct lxc_terminal *terminal) |
861813e5 CB |
221 | { |
222 | /* be very certain things are kosher */ | |
2083d59d | 223 | if (!terminal->log_path || terminal->log_fd < 0) |
861813e5 CB |
224 | return -EBADF; |
225 | ||
2083d59d | 226 | return lxc_unpriv(ftruncate(terminal->log_fd, 0)); |
861813e5 CB |
227 | } |
228 | ||
468724d3 | 229 | static int lxc_terminal_rotate_log_file(struct lxc_terminal *terminal) |
861813e5 CB |
230 | { |
231 | int ret; | |
232 | size_t len; | |
233 | char *tmp; | |
234 | ||
99a04585 | 235 | if (!terminal->log_path || terminal->log_rotate == 0) |
861813e5 CB |
236 | return -EOPNOTSUPP; |
237 | ||
238 | /* be very certain things are kosher */ | |
99a04585 | 239 | if (terminal->log_fd < 0) |
861813e5 CB |
240 | return -EBADF; |
241 | ||
99a04585 | 242 | len = strlen(terminal->log_path) + sizeof(".1"); |
861813e5 CB |
243 | tmp = alloca(len); |
244 | ||
99a04585 | 245 | ret = snprintf(tmp, len, "%s.1", terminal->log_path); |
861813e5 CB |
246 | if (ret < 0 || (size_t)ret >= len) |
247 | return -EFBIG; | |
248 | ||
99a04585 CB |
249 | close(terminal->log_fd); |
250 | terminal->log_fd = -1; | |
251 | ret = lxc_unpriv(rename(terminal->log_path, tmp)); | |
861813e5 CB |
252 | if (ret < 0) |
253 | return ret; | |
254 | ||
99a04585 | 255 | return lxc_terminal_create_log_file(terminal); |
861813e5 CB |
256 | } |
257 | ||
a44ae1a9 | 258 | static int lxc_terminal_write_log_file(struct lxc_terminal *terminal, char *buf, |
8903fb08 | 259 | int bytes_read) |
861813e5 CB |
260 | { |
261 | int ret; | |
861813e5 | 262 | struct stat st; |
8903fb08 | 263 | int64_t space_left = -1; |
861813e5 | 264 | |
99a04585 | 265 | if (terminal->log_fd < 0) |
861813e5 CB |
266 | return 0; |
267 | ||
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 | |
270 | * be rotated or not. | |
271 | */ | |
99a04585 CB |
272 | if (terminal->log_size <= 0) |
273 | return lxc_write_nointr(terminal->log_fd, buf, bytes_read); | |
861813e5 CB |
274 | |
275 | /* Get current size of the log file. */ | |
99a04585 | 276 | ret = fstat(terminal->log_fd, &st); |
861813e5 | 277 | if (ret < 0) { |
99a04585 | 278 | SYSERROR("Failed to stat the terminal log file descriptor"); |
861813e5 CB |
279 | return -1; |
280 | } | |
281 | ||
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. | |
288 | */ | |
99a04585 | 289 | if (terminal->log_rotate > 0 || terminal->log_size > 0) |
861813e5 CB |
290 | return -EINVAL; |
291 | ||
292 | /* I mean, sure log wherever you want to. */ | |
99a04585 | 293 | return lxc_write_nointr(terminal->log_fd, buf, bytes_read); |
861813e5 CB |
294 | } |
295 | ||
99a04585 | 296 | space_left = terminal->log_size - st.st_size; |
861813e5 CB |
297 | |
298 | /* User doesn't want to rotate the log file and there's no more space | |
299 | * left so simply truncate it. | |
300 | */ | |
99a04585 CB |
301 | if (space_left <= 0 && terminal->log_rotate <= 0) { |
302 | ret = lxc_terminal_truncate_log_file(terminal); | |
861813e5 CB |
303 | if (ret < 0) |
304 | return ret; | |
305 | ||
99a04585 CB |
306 | if (bytes_read <= terminal->log_size) |
307 | return lxc_write_nointr(terminal->log_fd, buf, bytes_read); | |
861813e5 CB |
308 | |
309 | /* Write as much as we can into the buffer and loose the rest. */ | |
99a04585 | 310 | return lxc_write_nointr(terminal->log_fd, buf, terminal->log_size); |
861813e5 CB |
311 | } |
312 | ||
313 | /* There's enough space left. */ | |
314 | if (bytes_read <= space_left) | |
99a04585 | 315 | return lxc_write_nointr(terminal->log_fd, buf, bytes_read); |
861813e5 CB |
316 | |
317 | /* There's not enough space left but at least write as much as we can | |
318 | * into the old log file. | |
319 | */ | |
99a04585 | 320 | ret = lxc_write_nointr(terminal->log_fd, buf, space_left); |
861813e5 CB |
321 | if (ret < 0) |
322 | return -1; | |
323 | ||
324 | /* Calculate how many bytes we still need to write. */ | |
325 | bytes_read -= space_left; | |
326 | ||
89962c6c | 327 | /* There'd be more to write but we aren't instructed to rotate the log |
861813e5 CB |
328 | * file so simply return. There's no error on our side here. |
329 | */ | |
99a04585 | 330 | if (terminal->log_rotate > 0) |
468724d3 | 331 | ret = lxc_terminal_rotate_log_file(terminal); |
861813e5 | 332 | else |
99a04585 | 333 | ret = lxc_terminal_truncate_log_file(terminal); |
861813e5 CB |
334 | if (ret < 0) |
335 | return ret; | |
336 | ||
99a04585 | 337 | if (terminal->log_size < bytes_read) { |
861813e5 CB |
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 | |
99a04585 | 344 | * standard pty-based terminal that LXC allocates since it will |
861813e5 CB |
345 | * be switched into raw mode. In raw mode only 1 byte at a time |
346 | * should be read and written. | |
347 | */ | |
99a04585 CB |
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); | |
861813e5 CB |
350 | if (ret < 0) |
351 | return -1; | |
352 | bytes_read -= ret; | |
353 | return bytes_read; | |
354 | } | |
355 | ||
356 | /* Yay, we made it. */ | |
99a04585 | 357 | ret = lxc_write_nointr(terminal->log_fd, buf, bytes_read); |
861813e5 CB |
358 | if (ret < 0) |
359 | return -1; | |
360 | bytes_read -= ret; | |
361 | return bytes_read; | |
362 | } | |
363 | ||
de708fb7 | 364 | int lxc_terminal_io_cb(int fd, uint32_t events, void *data, |
a529bc25 | 365 | struct lxc_epoll_descr *descr) |
b5159817 | 366 | { |
dcad02f8 | 367 | struct lxc_terminal *terminal = data; |
de708fb7 | 368 | char buf[LXC_TERMINAL_BUFFER_SIZE]; |
732375f5 | 369 | int r, w, w_log, w_rbuf; |
e0dc0de7 | 370 | |
3e6580ec CB |
371 | w = r = lxc_read_nointr(fd, buf, sizeof(buf)); |
372 | if (r <= 0) { | |
de708fb7 | 373 | INFO("Terminal client on fd %d has exited", fd); |
b5159817 | 374 | lxc_mainloop_del_handler(descr, fd); |
c06a0555 | 375 | |
de708fb7 CB |
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; | |
0b1e242b | 382 | } |
de708fb7 | 383 | terminal->peer = -EBADF; |
c06a0555 CB |
384 | } else { |
385 | ERROR("Handler received unexpected file descriptor"); | |
0b1e242b | 386 | } |
b5159817 | 387 | close(fd); |
c06a0555 | 388 | |
a529bc25 | 389 | return LXC_MAINLOOP_CLOSE; |
33fcb7a0 | 390 | } |
63376d7d | 391 | |
de708fb7 CB |
392 | if (fd == terminal->peer) |
393 | w = lxc_write_nointr(terminal->master, buf, r); | |
b5159817 | 394 | |
732375f5 | 395 | w_rbuf = w_log = 0; |
de708fb7 | 396 | if (fd == terminal->master) { |
732375f5 | 397 | /* write to peer first */ |
de708fb7 CB |
398 | if (terminal->peer >= 0) |
399 | w = lxc_write_nointr(terminal->peer, buf, r); | |
732375f5 | 400 | |
de708fb7 CB |
401 | /* write to terminal ringbuffer */ |
402 | if (terminal->buffer_size > 0) | |
403 | w_rbuf = lxc_ringbuf_write(&terminal->ringbuf, buf, r); | |
861813e5 | 404 | |
de708fb7 CB |
405 | /* write to terminal log */ |
406 | if (terminal->log_fd >= 0) | |
a44ae1a9 | 407 | w_log = lxc_terminal_write_log_file(terminal, buf, r); |
63376d7d DL |
408 | } |
409 | ||
b5159817 | 410 | if (w != r) |
de708fb7 | 411 | WARN("Short write on terminal r:%d != w:%d", r, w); |
732375f5 | 412 | |
7874d81a | 413 | if (w_rbuf < 0) { |
414 | errno = -w_rbuf; | |
415 | SYSTRACE("Failed to write %d bytes to terminal ringbuffer", r); | |
416 | } | |
732375f5 CB |
417 | |
418 | if (w_log < 0) | |
de708fb7 | 419 | TRACE("Failed to write %d bytes to terminal log", r); |
0d4137cc | 420 | |
2b8bf299 | 421 | return LXC_MAINLOOP_CONTINUE; |
b5159817 DE |
422 | } |
423 | ||
dcad02f8 | 424 | static int lxc_terminal_mainloop_add_peer(struct lxc_terminal *terminal) |
b5159817 | 425 | { |
a529bc25 CB |
426 | int ret; |
427 | ||
2083d59d CB |
428 | if (terminal->peer >= 0) { |
429 | ret = lxc_mainloop_add_handler(terminal->descr, terminal->peer, | |
430 | lxc_terminal_io_cb, terminal); | |
a529bc25 | 431 | if (ret < 0) { |
2083d59d | 432 | WARN("Failed to add terminal peer handler to mainloop"); |
a529bc25 CB |
433 | return -1; |
434 | } | |
63376d7d DL |
435 | } |
436 | ||
2083d59d | 437 | if (!terminal->tty_state || terminal->tty_state->sigfd < 0) |
a529bc25 CB |
438 | return 0; |
439 | ||
2083d59d CB |
440 | ret = lxc_mainloop_add_handler(terminal->descr, terminal->tty_state->sigfd, |
441 | lxc_terminal_signalfd_cb, terminal->tty_state); | |
a529bc25 CB |
442 | if (ret < 0) { |
443 | WARN("Failed to add signal handler to mainloop"); | |
444 | return -1; | |
b5159817 | 445 | } |
a529bc25 CB |
446 | |
447 | return 0; | |
b5159817 DE |
448 | } |
449 | ||
093bce5f | 450 | int lxc_terminal_mainloop_add(struct lxc_epoll_descr *descr, |
ea5b3c23 | 451 | struct lxc_terminal *terminal) |
b5159817 | 452 | { |
a529bc25 | 453 | int ret; |
b5159817 | 454 | |
2083d59d CB |
455 | if (terminal->master < 0) { |
456 | INFO("Terminal is not initialized"); | |
b5159817 | 457 | return 0; |
596a818d DE |
458 | } |
459 | ||
2083d59d CB |
460 | ret = lxc_mainloop_add_handler(descr, terminal->master, |
461 | lxc_terminal_io_cb, terminal); | |
30a33fbd | 462 | if (ret < 0) { |
ea5b3c23 CB |
463 | ERROR("Failed to add handler for terminal master fd %d to " |
464 | "mainloop", terminal->master); | |
b5159817 | 465 | return -1; |
28a4b0e5 DL |
466 | } |
467 | ||
30a33fbd | 468 | /* We cache the descr so that we can add an fd to it when someone |
c1ee47cd | 469 | * does attach to it in lxc_terminal_allocate(). |
b5159817 | 470 | */ |
2083d59d | 471 | terminal->descr = descr; |
cd453b38 | 472 | |
ea5b3c23 | 473 | return lxc_terminal_mainloop_add_peer(terminal); |
b5159817 | 474 | } |
28a4b0e5 | 475 | |
0d4137cc | 476 | int lxc_setup_tios(int fd, struct termios *oldtios) |
b5159817 | 477 | { |
e4953e62 | 478 | int ret; |
b5159817 | 479 | struct termios newtios; |
e0dc0de7 | 480 | |
b5159817 | 481 | if (!isatty(fd)) { |
e4953e62 | 482 | ERROR("File descriptor %d does not refert to a terminal", fd); |
b5159817 | 483 | return -1; |
e0dc0de7 DL |
484 | } |
485 | ||
e4953e62 CB |
486 | /* Get current termios. */ |
487 | ret = tcgetattr(fd, oldtios); | |
488 | if (ret < 0) { | |
489 | SYSERROR("Failed to get current terminal settings"); | |
b5159817 | 490 | return -1; |
e0dc0de7 DL |
491 | } |
492 | ||
4dc96430 TJ |
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. | |
497 | */ | |
498 | signal (SIGTTIN, SIG_IGN); | |
499 | signal (SIGTTOU, SIG_IGN); | |
500 | ||
b5159817 | 501 | newtios = *oldtios; |
e0dc0de7 | 502 | |
a7c97a40 CB |
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); | |
506 | #ifdef IUCLC | |
507 | newtios.c_iflag &= ~IUCLC; | |
508 | #endif | |
4dc96430 | 509 | newtios.c_lflag &= ~(TOSTOP | ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL); |
a7c97a40 CB |
510 | #ifdef IEXTEN |
511 | newtios.c_lflag &= ~IEXTEN; | |
512 | #endif | |
300ec717 | 513 | newtios.c_oflag |= OPOST; |
b5159817 DE |
514 | newtios.c_cc[VMIN] = 1; |
515 | newtios.c_cc[VTIME] = 0; | |
e0dc0de7 | 516 | |
a7c97a40 | 517 | /* Set new attributes. */ |
e4953e62 CB |
518 | ret = tcsetattr(fd, TCSAFLUSH, &newtios); |
519 | if (ret < 0) { | |
520 | ERROR("Failed to set new terminal settings"); | |
b5159817 | 521 | return -1; |
e0dc0de7 DL |
522 | } |
523 | ||
63376d7d | 524 | return 0; |
b5159817 | 525 | } |
e0dc0de7 | 526 | |
dcad02f8 | 527 | static void lxc_terminal_peer_proxy_free(struct lxc_terminal *terminal) |
b5159817 | 528 | { |
2083d59d CB |
529 | if (terminal->tty_state) { |
530 | lxc_terminal_signal_fini(terminal->tty_state); | |
531 | terminal->tty_state = NULL; | |
b5159817 | 532 | } |
e788f4ac | 533 | |
fb87aa6a | 534 | close(terminal->proxy.master); |
fb87aa6a | 535 | terminal->proxy.master = -1; |
e788f4ac CB |
536 | |
537 | close(terminal->proxy.slave); | |
fb87aa6a | 538 | terminal->proxy.slave = -1; |
e788f4ac | 539 | |
fb87aa6a | 540 | terminal->proxy.busy = -1; |
e788f4ac | 541 | |
fb87aa6a | 542 | terminal->proxy.name[0] = '\0'; |
e788f4ac | 543 | |
2083d59d | 544 | terminal->peer = -1; |
b5159817 | 545 | } |
596a818d | 546 | |
60dd8ef4 CB |
547 | static int lxc_terminal_peer_proxy_alloc(struct lxc_terminal *terminal, |
548 | int sockfd) | |
b5159817 | 549 | { |
60dd8ef4 | 550 | int ret; |
b5159817 | 551 | struct termios oldtermio; |
5b55021f | 552 | struct lxc_terminal_state *ts; |
b5159817 | 553 | |
2083d59d CB |
554 | if (terminal->master < 0) { |
555 | ERROR("Terminal not set up"); | |
b5159817 DE |
556 | return -1; |
557 | } | |
60dd8ef4 | 558 | |
fb87aa6a | 559 | if (terminal->proxy.busy != -1 || terminal->peer != -1) { |
2083d59d | 560 | NOTICE("Terminal already in use"); |
b5159817 DE |
561 | return -1; |
562 | } | |
60dd8ef4 | 563 | |
2083d59d | 564 | if (terminal->tty_state) { |
60dd8ef4 | 565 | ERROR("Terminal has already been initialized"); |
b5159817 | 566 | return -1; |
596a818d DE |
567 | } |
568 | ||
d712f9e8 CB |
569 | /* This is the proxy terminal that will be given to the client, and |
570 | * that the real terminal master will send to / recv from. | |
b5159817 | 571 | */ |
fb87aa6a | 572 | ret = openpty(&terminal->proxy.master, &terminal->proxy.slave, |
60dd8ef4 CB |
573 | terminal->proxy.name, NULL, NULL); |
574 | if (ret < 0) { | |
575 | SYSERROR("Failed to open proxy terminal"); | |
b5159817 DE |
576 | return -1; |
577 | } | |
596a818d | 578 | |
60dd8ef4 CB |
579 | ret = lxc_setup_tios(terminal->proxy.slave, &oldtermio); |
580 | if (ret < 0) | |
581 | goto on_error; | |
b5159817 | 582 | |
fb87aa6a | 583 | ts = lxc_terminal_signal_init(terminal->proxy.master, terminal->master); |
b5159817 | 584 | if (!ts) |
60dd8ef4 | 585 | goto on_error; |
b5159817 | 586 | |
2083d59d | 587 | terminal->tty_state = ts; |
fb87aa6a CB |
588 | terminal->peer = terminal->proxy.slave; |
589 | terminal->proxy.busy = sockfd; | |
2083d59d | 590 | ret = lxc_terminal_mainloop_add_peer(terminal); |
a529bc25 | 591 | if (ret < 0) |
60dd8ef4 | 592 | goto on_error; |
b5159817 | 593 | |
60dd8ef4 CB |
594 | NOTICE("Opened proxy terminal with master fd %d and slave fd %d", |
595 | terminal->proxy.master, terminal->proxy.slave); | |
b5159817 DE |
596 | return 0; |
597 | ||
60dd8ef4 | 598 | on_error: |
2083d59d | 599 | lxc_terminal_peer_proxy_free(terminal); |
63376d7d DL |
600 | return -1; |
601 | } | |
602 | ||
c1ee47cd | 603 | int lxc_terminal_allocate(struct lxc_conf *conf, int sockfd, int *ttyreq) |
b5159817 | 604 | { |
12c2eaaa CB |
605 | int ttynum; |
606 | int masterfd = -1; | |
0e4be3cf | 607 | struct lxc_tty_info *ttys = &conf->ttys; |
dcad02f8 | 608 | struct lxc_terminal *terminal = &conf->console; |
b5159817 | 609 | |
b5159817 | 610 | if (*ttyreq == 0) { |
12c2eaaa CB |
611 | int ret; |
612 | ||
613 | ret = lxc_terminal_peer_proxy_alloc(terminal, sockfd); | |
614 | if (ret < 0) | |
b5159817 | 615 | goto out; |
12c2eaaa | 616 | |
fb87aa6a | 617 | masterfd = terminal->proxy.master; |
b5159817 DE |
618 | goto out; |
619 | } | |
620 | ||
621 | if (*ttyreq > 0) { | |
885766f5 | 622 | if (*ttyreq > ttys->max) |
b5159817 DE |
623 | goto out; |
624 | ||
0e4be3cf | 625 | if (ttys->tty[*ttyreq - 1].busy) |
b5159817 DE |
626 | goto out; |
627 | ||
12c2eaaa | 628 | /* The requested tty is available. */ |
b5159817 DE |
629 | ttynum = *ttyreq; |
630 | goto out_tty; | |
631 | } | |
632 | ||
12c2eaaa | 633 | /* Search for next available tty, fixup index tty1 => [0]. */ |
885766f5 | 634 | for (ttynum = 1; ttynum <= ttys->max && ttys->tty[ttynum - 1].busy; ttynum++) { |
0d4137cc | 635 | ; |
12c2eaaa | 636 | } |
b5159817 | 637 | |
12c2eaaa | 638 | /* We didn't find any available slot for tty. */ |
885766f5 | 639 | if (ttynum > ttys->max) |
b5159817 DE |
640 | goto out; |
641 | ||
642 | *ttyreq = ttynum; | |
643 | ||
644 | out_tty: | |
0e4be3cf CB |
645 | ttys->tty[ttynum - 1].busy = sockfd; |
646 | masterfd = ttys->tty[ttynum - 1].master; | |
12c2eaaa | 647 | |
b5159817 | 648 | out: |
b5159817 DE |
649 | return masterfd; |
650 | } | |
651 | ||
3dfe6f8d | 652 | void lxc_terminal_free(struct lxc_conf *conf, int fd) |
63376d7d | 653 | { |
b5159817 | 654 | int i; |
0e4be3cf | 655 | struct lxc_tty_info *ttys = &conf->ttys; |
dcad02f8 | 656 | struct lxc_terminal *terminal = &conf->console; |
b5159817 | 657 | |
885766f5 | 658 | for (i = 0; i < ttys->max; i++) |
0e4be3cf CB |
659 | if (ttys->tty[i].busy == fd) |
660 | ttys->tty[i].busy = 0; | |
b5159817 | 661 | |
1b5e93c4 CB |
662 | if (terminal->proxy.busy != fd) |
663 | return; | |
664 | ||
665 | lxc_mainloop_del_handler(terminal->descr, terminal->proxy.slave); | |
666 | lxc_terminal_peer_proxy_free(terminal); | |
b5159817 DE |
667 | } |
668 | ||
dcad02f8 | 669 | static int lxc_terminal_peer_default(struct lxc_terminal *terminal) |
b5159817 | 670 | { |
5b55021f | 671 | struct lxc_terminal_state *ts; |
46768cce | 672 | const char *path; |
467c7ff3 | 673 | int ret = 0; |
b5159817 | 674 | |
46768cce CB |
675 | if (terminal->path) |
676 | path = terminal->path; | |
677 | else | |
678 | path = "/dev/tty"; | |
b5159817 | 679 | |
2083d59d CB |
680 | terminal->peer = lxc_unpriv(open(path, O_RDWR | O_CLOEXEC)); |
681 | if (terminal->peer < 0) { | |
46768cce CB |
682 | if (!terminal->path) { |
683 | errno = ENODEV; | |
7874d81a | 684 | SYSDEBUG("The process does not have a controlling terminal"); |
46768cce CB |
685 | goto on_succes; |
686 | } | |
687 | ||
49cd0656 CB |
688 | ERROR("%s - Failed to open proxy terminal \"%s\"", |
689 | strerror(errno), path); | |
467c7ff3 CB |
690 | return -ENOTTY; |
691 | } | |
49cd0656 | 692 | DEBUG("Using terminal \"%s\" as proxy", path); |
b5159817 | 693 | |
2083d59d | 694 | if (!isatty(terminal->peer)) { |
49cd0656 CB |
695 | ERROR("File descriptor for \"%s\" does not refer to a terminal", path); |
696 | goto on_error_free_tios; | |
467c7ff3 | 697 | } |
b5159817 | 698 | |
2083d59d CB |
699 | ts = lxc_terminal_signal_init(terminal->peer, terminal->master); |
700 | terminal->tty_state = ts; | |
341c2aed | 701 | if (!ts) { |
0519b5cc | 702 | WARN("Failed to install signal handler"); |
49cd0656 | 703 | goto on_error_free_tios; |
341c2aed | 704 | } |
b5159817 | 705 | |
2083d59d | 706 | lxc_terminal_winsz(terminal->peer, terminal->master); |
b5159817 | 707 | |
2083d59d | 708 | terminal->tios = malloc(sizeof(*terminal->tios)); |
49cd0656 CB |
709 | if (!terminal->tios) |
710 | goto on_error_free_tios; | |
b5159817 | 711 | |
49cd0656 CB |
712 | ret = lxc_setup_tios(terminal->peer, terminal->tios); |
713 | if (ret < 0) | |
714 | goto on_error_close_peer; | |
467c7ff3 | 715 | else |
49cd0656 | 716 | goto on_succes; |
b5159817 | 717 | |
49cd0656 | 718 | on_error_free_tios: |
2083d59d CB |
719 | free(terminal->tios); |
720 | terminal->tios = NULL; | |
467c7ff3 | 721 | |
49cd0656 | 722 | on_error_close_peer: |
2083d59d CB |
723 | close(terminal->peer); |
724 | terminal->peer = -1; | |
467c7ff3 CB |
725 | ret = -ENOTTY; |
726 | ||
49cd0656 | 727 | on_succes: |
467c7ff3 | 728 | return ret; |
b5159817 DE |
729 | } |
730 | ||
dcad02f8 | 731 | int lxc_terminal_write_ringbuffer(struct lxc_terminal *terminal) |
39c6cdb7 CB |
732 | { |
733 | char *r_addr; | |
734 | ssize_t ret; | |
735 | uint64_t used; | |
2083d59d | 736 | struct lxc_ringbuf *buf = &terminal->ringbuf; |
39c6cdb7 CB |
737 | |
738 | /* There's not log file where we can dump the ringbuffer to. */ | |
2083d59d | 739 | if (terminal->log_fd < 0) |
39c6cdb7 CB |
740 | return 0; |
741 | ||
39c6cdb7 CB |
742 | used = lxc_ringbuf_used(buf); |
743 | if (used == 0) | |
744 | return 0; | |
745 | ||
2083d59d | 746 | ret = lxc_terminal_truncate_log_file(terminal); |
39c6cdb7 CB |
747 | if (ret < 0) |
748 | return ret; | |
749 | ||
750 | /* Write as much as we can without exceeding the limit. */ | |
2083d59d CB |
751 | if (terminal->log_size < used) |
752 | used = terminal->log_size; | |
39c6cdb7 CB |
753 | |
754 | r_addr = lxc_ringbuf_get_read_addr(buf); | |
2083d59d | 755 | ret = lxc_write_nointr(terminal->log_fd, r_addr, used); |
39c6cdb7 CB |
756 | if (ret < 0) |
757 | return -EIO; | |
758 | ||
759 | return 0; | |
760 | } | |
761 | ||
dcad02f8 | 762 | void lxc_terminal_delete(struct lxc_terminal *terminal) |
b5159817 | 763 | { |
69629c82 CB |
764 | int ret; |
765 | ||
2083d59d | 766 | ret = lxc_terminal_write_ringbuffer(terminal); |
39c6cdb7 | 767 | if (ret < 0) |
2083d59d | 768 | WARN("Failed to write terminal log to disk"); |
39c6cdb7 | 769 | |
2083d59d CB |
770 | if (terminal->tios && terminal->peer >= 0) { |
771 | ret = tcsetattr(terminal->peer, TCSAFLUSH, terminal->tios); | |
69629c82 | 772 | if (ret < 0) |
a24c5678 | 773 | SYSWARN("Failed to set old terminal settings"); |
69629c82 | 774 | } |
2083d59d CB |
775 | free(terminal->tios); |
776 | terminal->tios = NULL; | |
596a818d | 777 | |
2083d59d CB |
778 | if (terminal->peer >= 0) |
779 | close(terminal->peer); | |
780 | terminal->peer = -1; | |
bc9724f7 | 781 | |
2083d59d CB |
782 | if (terminal->master >= 0) |
783 | close(terminal->master); | |
784 | terminal->master = -1; | |
bc9724f7 | 785 | |
2083d59d CB |
786 | if (terminal->slave >= 0) |
787 | close(terminal->slave); | |
788 | terminal->slave = -1; | |
bc9724f7 | 789 | |
2083d59d CB |
790 | if (terminal->log_fd >= 0) |
791 | close(terminal->log_fd); | |
792 | terminal->log_fd = -1; | |
63376d7d DL |
793 | } |
794 | ||
3b988b33 CB |
795 | /** |
796 | * Note that this function needs to run before the mainloop starts. Since we | |
2083d59d CB |
797 | * register a handler for the terminal's masterfd when we create the mainloop |
798 | * the terminal handler needs to see an allocated ringbuffer. | |
3b988b33 | 799 | */ |
dcad02f8 | 800 | static int lxc_terminal_create_ringbuf(struct lxc_terminal *terminal) |
3b988b33 CB |
801 | { |
802 | int ret; | |
2083d59d CB |
803 | struct lxc_ringbuf *buf = &terminal->ringbuf; |
804 | uint64_t size = terminal->buffer_size; | |
3b988b33 CB |
805 | |
806 | /* no ringbuffer previously allocated and no ringbuffer requested */ | |
807 | if (!buf->addr && size <= 0) | |
808 | return 0; | |
809 | ||
810 | /* ringbuffer allocated but no new ringbuffer requested */ | |
811 | if (buf->addr && size <= 0) { | |
812 | lxc_ringbuf_release(buf); | |
813 | buf->addr = NULL; | |
814 | buf->r_off = 0; | |
815 | buf->w_off = 0; | |
816 | buf->size = 0; | |
2083d59d | 817 | TRACE("Deallocated terminal ringbuffer"); |
3b988b33 CB |
818 | return 0; |
819 | } | |
820 | ||
821 | if (size <= 0) | |
822 | return 0; | |
823 | ||
824 | /* check wether the requested size for the ringbuffer has changed */ | |
825 | if (buf->addr && buf->size != size) { | |
2083d59d CB |
826 | TRACE("Terminal ringbuffer size changed from %" PRIu64 |
827 | " to %" PRIu64 " bytes. Deallocating terminal ringbuffer", | |
3b988b33 CB |
828 | buf->size, size); |
829 | lxc_ringbuf_release(buf); | |
830 | } | |
831 | ||
832 | ret = lxc_ringbuf_create(buf, size); | |
833 | if (ret < 0) { | |
2083d59d | 834 | ERROR("Failed to setup %" PRIu64 " byte terminal ringbuffer", size); |
3b988b33 CB |
835 | return -1; |
836 | } | |
837 | ||
2083d59d | 838 | TRACE("Allocated %" PRIu64 " byte terminal ringbuffer", size); |
3b988b33 CB |
839 | return 0; |
840 | } | |
841 | ||
a0309168 | 842 | /** |
2083d59d CB |
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. | |
a0309168 | 845 | */ |
dcad02f8 | 846 | int lxc_terminal_create_log_file(struct lxc_terminal *terminal) |
a0309168 | 847 | { |
2083d59d | 848 | if (!terminal->log_path) |
a0309168 CB |
849 | return 0; |
850 | ||
2083d59d CB |
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); | |
a0309168 CB |
854 | return -1; |
855 | } | |
856 | ||
2083d59d | 857 | DEBUG("Using \"%s\" as terminal log file", terminal->log_path); |
a0309168 CB |
858 | return 0; |
859 | } | |
860 | ||
dcad02f8 | 861 | int lxc_terminal_create(struct lxc_terminal *terminal) |
63376d7d | 862 | { |
8ded9244 | 863 | int ret; |
b5159817 | 864 | |
8ded9244 | 865 | ret = openpty(&terminal->master, &terminal->slave, terminal->name, NULL, NULL); |
467c7ff3 | 866 | if (ret < 0) { |
8ded9244 | 867 | SYSERROR("Failed to open terminal"); |
b5159817 DE |
868 | return -1; |
869 | } | |
870 | ||
2083d59d | 871 | ret = fcntl(terminal->master, F_SETFD, FD_CLOEXEC); |
69629c82 | 872 | if (ret < 0) { |
2083d59d | 873 | SYSERROR("Failed to set FD_CLOEXEC flag on terminal master"); |
b5159817 DE |
874 | goto err; |
875 | } | |
876 | ||
2083d59d | 877 | ret = fcntl(terminal->slave, F_SETFD, FD_CLOEXEC); |
69629c82 | 878 | if (ret < 0) { |
2083d59d | 879 | SYSERROR("Failed to set FD_CLOEXEC flag on terminal slave"); |
b5159817 DE |
880 | goto err; |
881 | } | |
882 | ||
2083d59d | 883 | ret = lxc_terminal_peer_default(terminal); |
467c7ff3 | 884 | if (ret < 0) { |
8ded9244 | 885 | ERROR("Failed to allocate proxy terminal"); |
467c7ff3 CB |
886 | goto err; |
887 | } | |
b5159817 | 888 | |
5777fe90 CB |
889 | return 0; |
890 | ||
891 | err: | |
2083d59d | 892 | lxc_terminal_delete(terminal); |
5777fe90 CB |
893 | return -ENODEV; |
894 | } | |
895 | ||
564e31c4 | 896 | int lxc_terminal_setup(struct lxc_conf *conf) |
5777fe90 CB |
897 | { |
898 | int ret; | |
dcad02f8 | 899 | struct lxc_terminal *terminal = &conf->console; |
5777fe90 | 900 | |
1a443ac1 CB |
901 | if (terminal->path && strcmp(terminal->path, "none") == 0) { |
902 | INFO("No terminal requested"); | |
5777fe90 CB |
903 | return 0; |
904 | } | |
905 | ||
96eee564 | 906 | ret = lxc_terminal_create(terminal); |
5777fe90 CB |
907 | if (ret < 0) |
908 | return -1; | |
909 | ||
2083d59d | 910 | ret = lxc_terminal_create_log_file(terminal); |
a0309168 CB |
911 | if (ret < 0) |
912 | goto err; | |
913 | ||
2083d59d | 914 | ret = lxc_terminal_create_ringbuf(terminal); |
a0309168 CB |
915 | if (ret < 0) |
916 | goto err; | |
b5159817 DE |
917 | |
918 | return 0; | |
919 | ||
920 | err: | |
2083d59d | 921 | lxc_terminal_delete(terminal); |
69629c82 | 922 | return -ENODEV; |
b5159817 DE |
923 | } |
924 | ||
8ca7b374 CB |
925 | static bool __terminal_dup2(int duplicate, int original) |
926 | { | |
927 | int ret; | |
928 | ||
929 | if (!isatty(original)) | |
930 | return true; | |
931 | ||
932 | ret = dup2(duplicate, original); | |
933 | if (ret < 0) { | |
934 | SYSERROR("Failed to dup2(%d, %d)", duplicate, original); | |
935 | return false; | |
936 | } | |
937 | ||
938 | return true; | |
939 | } | |
940 | ||
ae6d3913 | 941 | int lxc_terminal_set_stdfds(int fd) |
0d9acb99 | 942 | { |
8ca7b374 CB |
943 | int i; |
944 | ||
39a78bbe | 945 | if (fd < 0) |
0d9acb99 | 946 | return 0; |
b5159817 | 947 | |
8ca7b374 CB |
948 | for (i = 0; i < 3; i++) |
949 | if (!__terminal_dup2(fd, (int[]){STDIN_FILENO, STDOUT_FILENO, | |
950 | STDERR_FILENO}[i])) | |
39a78bbe | 951 | return -1; |
39a78bbe | 952 | |
0d9acb99 DE |
953 | return 0; |
954 | } | |
b5159817 | 955 | |
52f9292f | 956 | int lxc_terminal_stdin_cb(int fd, uint32_t events, void *cbdata, |
15085292 | 957 | struct lxc_epoll_descr *descr) |
b5159817 | 958 | { |
15085292 | 959 | int ret; |
b5159817 | 960 | char c; |
15085292 | 961 | struct lxc_terminal_state *ts = cbdata; |
b5159817 | 962 | |
97bc2422 | 963 | if (fd != ts->stdinfd) |
a529bc25 | 964 | return LXC_MAINLOOP_CLOSE; |
97bc2422 | 965 | |
15085292 CB |
966 | ret = lxc_read_nointr(ts->stdinfd, &c, 1); |
967 | if (ret <= 0) | |
a529bc25 | 968 | return LXC_MAINLOOP_CLOSE; |
63376d7d | 969 | |
525e2117 | 970 | if (ts->escape >= 1) { |
2083d59d | 971 | /* we want to exit the terminal with Ctrl+a q */ |
014d5e1e CB |
972 | if (c == ts->escape && !ts->saw_escape) { |
973 | ts->saw_escape = 1; | |
15085292 | 974 | return LXC_MAINLOOP_CONTINUE; |
014d5e1e | 975 | } |
5c294060 | 976 | |
014d5e1e | 977 | if (c == 'q' && ts->saw_escape) |
a529bc25 | 978 | return LXC_MAINLOOP_CLOSE; |
014d5e1e CB |
979 | |
980 | ts->saw_escape = 0; | |
981 | } | |
63376d7d | 982 | |
15085292 CB |
983 | ret = lxc_write_nointr(ts->masterfd, &c, 1); |
984 | if (ret <= 0) | |
a529bc25 | 985 | return LXC_MAINLOOP_CLOSE; |
b5159817 | 986 | |
15085292 | 987 | return LXC_MAINLOOP_CONTINUE; |
63376d7d DL |
988 | } |
989 | ||
ee9102ff CB |
990 | int lxc_terminal_master_cb(int fd, uint32_t events, void *cbdata, |
991 | struct lxc_epoll_descr *descr) | |
63376d7d | 992 | { |
0d4137cc | 993 | int r, w; |
5bd171bd CB |
994 | char buf[LXC_TERMINAL_BUFFER_SIZE]; |
995 | struct lxc_terminal_state *ts = cbdata; | |
63376d7d | 996 | |
97bc2422 | 997 | if (fd != ts->masterfd) |
a529bc25 | 998 | return LXC_MAINLOOP_CLOSE; |
97bc2422 | 999 | |
e66b6c96 CB |
1000 | r = lxc_read_nointr(fd, buf, sizeof(buf)); |
1001 | if (r <= 0) | |
a529bc25 | 1002 | return LXC_MAINLOOP_CLOSE; |
1560f6c9 | 1003 | |
e66b6c96 | 1004 | w = lxc_write_nointr(ts->stdoutfd, buf, r); |
5bd171bd | 1005 | if (w <= 0 || w != r) |
a529bc25 | 1006 | return LXC_MAINLOOP_CLOSE; |
f78a1f32 | 1007 | |
5bd171bd | 1008 | return LXC_MAINLOOP_CONTINUE; |
b5159817 DE |
1009 | } |
1010 | ||
c86e2584 | 1011 | int lxc_terminal_getfd(struct lxc_container *c, int *ttynum, int *masterfd) |
b5159817 DE |
1012 | { |
1013 | return lxc_cmd_console(c->name, ttynum, masterfd, c->config_path); | |
1014 | } | |
1015 | ||
1016 | int lxc_console(struct lxc_container *c, int ttynum, | |
1017 | int stdinfd, int stdoutfd, int stderrfd, | |
1018 | int escape) | |
1019 | { | |
71ac3f07 | 1020 | int masterfd, ret, ttyfd; |
b5159817 DE |
1021 | struct lxc_epoll_descr descr; |
1022 | struct termios oldtios; | |
5b55021f | 1023 | struct lxc_terminal_state *ts; |
25964232 | 1024 | int istty = 0; |
b5159817 | 1025 | |
b5159817 | 1026 | ttyfd = lxc_cmd_console(c->name, &ttynum, &masterfd, c->config_path); |
6834f805 CB |
1027 | if (ttyfd < 0) |
1028 | return -1; | |
b5159817 DE |
1029 | |
1030 | ret = setsid(); | |
33b4b411 CB |
1031 | if (ret < 0) |
1032 | TRACE("Process is already group leader"); | |
b5159817 | 1033 | |
dc8c7883 | 1034 | ts = lxc_terminal_signal_init(stdinfd, masterfd); |
b5159817 DE |
1035 | if (!ts) { |
1036 | ret = -1; | |
33b4b411 | 1037 | goto close_fds; |
b5159817 DE |
1038 | } |
1039 | ts->escape = escape; | |
1040 | ts->winch_proxy = c->name; | |
1041 | ts->winch_proxy_lxcpath = c->config_path; | |
3b975060 | 1042 | ts->stdoutfd = stdoutfd; |
b5159817 | 1043 | |
6834f805 | 1044 | istty = isatty(stdinfd); |
25964232 | 1045 | if (istty) { |
4e9c0330 | 1046 | lxc_terminal_winsz(stdinfd, masterfd); |
8ccbbf94 | 1047 | lxc_cmd_terminal_winch(ts->winch_proxy, ts->winch_proxy_lxcpath); |
6834f805 | 1048 | } else { |
71ac3f07 | 1049 | INFO("File descriptor %d does not refer to a terminal", stdinfd); |
25964232 | 1050 | } |
b5159817 DE |
1051 | |
1052 | ret = lxc_mainloop_open(&descr); | |
1053 | if (ret) { | |
33b4b411 CB |
1054 | ERROR("Failed to create mainloop"); |
1055 | goto sigwinch_fini; | |
b5159817 DE |
1056 | } |
1057 | ||
341c2aed CB |
1058 | if (ts->sigfd != -1) { |
1059 | ret = lxc_mainloop_add_handler(&descr, ts->sigfd, | |
9bafc8cb | 1060 | lxc_terminal_signalfd_cb, ts); |
33b4b411 | 1061 | if (ret < 0) { |
0519b5cc | 1062 | ERROR("Failed to add signal handler to mainloop"); |
33b4b411 | 1063 | goto close_mainloop; |
341c2aed | 1064 | } |
b5159817 DE |
1065 | } |
1066 | ||
1067 | ret = lxc_mainloop_add_handler(&descr, ts->stdinfd, | |
52f9292f | 1068 | lxc_terminal_stdin_cb, ts); |
33b4b411 CB |
1069 | if (ret < 0) { |
1070 | ERROR("Failed to add stdin handler"); | |
1071 | goto close_mainloop; | |
b5159817 DE |
1072 | } |
1073 | ||
1074 | ret = lxc_mainloop_add_handler(&descr, ts->masterfd, | |
ee9102ff | 1075 | lxc_terminal_master_cb, ts); |
33b4b411 CB |
1076 | if (ret < 0) { |
1077 | ERROR("Failed to add master handler"); | |
1078 | goto close_mainloop; | |
b5159817 DE |
1079 | } |
1080 | ||
686df166 CB |
1081 | if (ts->escape >= 1) { |
1082 | fprintf(stderr, | |
1083 | "\n" | |
6834f805 CB |
1084 | "Connected to tty %1$d\n" |
1085 | "Type <Ctrl+%2$c q> to exit the console, " | |
1086 | "<Ctrl+%2$c Ctrl+%2$c> to enter Ctrl+%2$c itself\n", | |
1087 | ttynum, 'a' + escape - 1); | |
686df166 | 1088 | } |
6834f805 CB |
1089 | |
1090 | if (istty) { | |
1091 | ret = lxc_setup_tios(stdinfd, &oldtios); | |
1092 | if (ret < 0) | |
1093 | goto close_mainloop; | |
1094 | } | |
1095 | ||
025ed0f3 | 1096 | ret = lxc_mainloop(&descr, -1); |
33b4b411 CB |
1097 | if (ret < 0) { |
1098 | ERROR("The mainloop returned an error"); | |
6834f805 | 1099 | goto restore_tios; |
b5159817 DE |
1100 | } |
1101 | ||
1102 | ret = 0; | |
1103 | ||
6834f805 CB |
1104 | restore_tios: |
1105 | if (istty) { | |
1106 | istty = tcsetattr(stdinfd, TCSAFLUSH, &oldtios); | |
1107 | if (istty < 0) | |
a24c5678 | 1108 | SYSWARN("Failed to restore terminal properties"); |
6834f805 CB |
1109 | } |
1110 | ||
33b4b411 | 1111 | close_mainloop: |
b5159817 | 1112 | lxc_mainloop_close(&descr); |
33b4b411 CB |
1113 | |
1114 | sigwinch_fini: | |
a8867251 | 1115 | lxc_terminal_signal_fini(ts); |
33b4b411 CB |
1116 | |
1117 | close_fds: | |
b5159817 DE |
1118 | close(masterfd); |
1119 | close(ttyfd); | |
33b4b411 | 1120 | |
b5159817 | 1121 | return ret; |
63376d7d | 1122 | } |
e98affda | 1123 | |
cd0a2b2f | 1124 | int lxc_make_controlling_terminal(int fd) |
e98affda CB |
1125 | { |
1126 | int ret; | |
1127 | ||
1128 | setsid(); | |
1129 | ||
1130 | ret = ioctl(fd, TIOCSCTTY, (char *)NULL); | |
1131 | if (ret < 0) | |
1132 | return -1; | |
1133 | ||
1134 | return 0; | |
1135 | } | |
1136 | ||
d049f0e9 | 1137 | int lxc_terminal_prepare_login(int fd) |
e98affda CB |
1138 | { |
1139 | int ret; | |
1140 | ||
cd0a2b2f | 1141 | ret = lxc_make_controlling_terminal(fd); |
e98affda CB |
1142 | if (ret < 0) |
1143 | return -1; | |
1144 | ||
ae6d3913 | 1145 | ret = lxc_terminal_set_stdfds(fd); |
e98affda CB |
1146 | if (ret < 0) |
1147 | return -1; | |
1148 | ||
1149 | if (fd > STDERR_FILENO) | |
1150 | close(fd); | |
1151 | ||
1152 | return 0; | |
1153 | } | |
1154 | ||
e9a55b51 | 1155 | void lxc_terminal_info_init(struct lxc_terminal_info *terminal) |
e98affda | 1156 | { |
e9a55b51 CB |
1157 | terminal->name[0] = '\0'; |
1158 | terminal->master = -EBADF; | |
1159 | terminal->slave = -EBADF; | |
1160 | terminal->busy = -1; | |
e98affda CB |
1161 | } |
1162 | ||
e9a55b51 | 1163 | void lxc_terminal_init(struct lxc_terminal *terminal) |
e98affda | 1164 | { |
e9a55b51 CB |
1165 | memset(terminal, 0, sizeof(*terminal)); |
1166 | terminal->slave = -EBADF; | |
1167 | terminal->master = -EBADF; | |
1168 | terminal->peer = -EBADF; | |
1169 | terminal->log_fd = -EBADF; | |
1170 | lxc_terminal_info_init(&terminal->proxy); | |
e98affda CB |
1171 | } |
1172 | ||
dcad02f8 | 1173 | void lxc_terminal_conf_free(struct lxc_terminal *terminal) |
e98affda | 1174 | { |
2083d59d CB |
1175 | free(terminal->log_path); |
1176 | free(terminal->path); | |
1177 | if (terminal->buffer_size > 0 && terminal->ringbuf.addr) | |
1178 | lxc_ringbuf_release(&terminal->ringbuf); | |
e98affda | 1179 | } |
7cfeddd7 | 1180 | |
03700cab | 1181 | int lxc_terminal_map_ids(struct lxc_conf *c, struct lxc_terminal *terminal) |
7cfeddd7 CB |
1182 | { |
1183 | int ret; | |
1184 | ||
1185 | if (lxc_list_empty(&c->id_map)) | |
1186 | return 0; | |
1187 | ||
03700cab | 1188 | if (strcmp(terminal->name, "") == 0) |
7cfeddd7 CB |
1189 | return 0; |
1190 | ||
03700cab | 1191 | ret = chown_mapped_root(terminal->name, c); |
7cfeddd7 | 1192 | if (ret < 0) { |
03700cab | 1193 | ERROR("Failed to chown terminal \"%s\"", terminal->name); |
7cfeddd7 CB |
1194 | return -1; |
1195 | } | |
1196 | ||
03700cab | 1197 | TRACE("Chowned terminal \"%s\"", terminal->name); |
7cfeddd7 CB |
1198 | |
1199 | return 0; | |
1200 | } |