]>
Commit | Line | Data |
---|---|---|
cc73685d | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
b0a33c1e | 2 | |
d38dd64a CB |
3 | #ifndef _GNU_SOURCE |
4 | #define _GNU_SOURCE 1 | |
5 | #endif | |
9395937a CB |
6 | #include <errno.h> |
7 | #include <fcntl.h> | |
e62912bd | 8 | #include <lxc/lxccontainer.h> |
b467714b | 9 | #include <pthread.h> |
b5159817 | 10 | #include <signal.h> |
b0a33c1e | 11 | #include <stdio.h> |
e0dc0de7 | 12 | #include <stdlib.h> |
da41561c CB |
13 | #include <sys/epoll.h> |
14 | #include <sys/types.h> | |
e62912bd CB |
15 | #include <termios.h> |
16 | #include <unistd.h> | |
f2363e38 | 17 | |
9395937a CB |
18 | #include "af_unix.h" |
19 | #include "caps.h" | |
20 | #include "commands.h" | |
00dbc43e | 21 | #include "conf.h" |
e827ff7e | 22 | #include "config.h" |
9395937a | 23 | #include "log.h" |
b5159817 | 24 | #include "lxclock.h" |
9395937a | 25 | #include "mainloop.h" |
2530ba95 | 26 | #include "memory_utils.h" |
e62912bd | 27 | #include "start.h" |
303037d2 | 28 | #include "syscall_wrappers.h" |
0ed9b1bc | 29 | #include "terminal.h" |
b5159817 | 30 | #include "utils.h" |
36eb9bde | 31 | |
35eb5cdc | 32 | #if HAVE_OPENPTY |
e827ff7e SG |
33 | #include <pty.h> |
34 | #else | |
35 | #include <../include/openpty.h> | |
36 | #endif | |
37 | ||
de708fb7 | 38 | #define LXC_TERMINAL_BUFFER_SIZE 1024 |
732375f5 | 39 | |
2083d59d | 40 | lxc_log_define(terminal, lxc); |
36eb9bde | 41 | |
4e9c0330 | 42 | void lxc_terminal_winsz(int srcfd, int dstfd) |
b5159817 | 43 | { |
0519b5cc | 44 | int ret; |
b5159817 | 45 | struct winsize wsz; |
0519b5cc CB |
46 | |
47 | if (!isatty(srcfd)) | |
48 | return; | |
49 | ||
50 | ret = ioctl(srcfd, TIOCGWINSZ, &wsz); | |
51 | if (ret < 0) { | |
52 | WARN("Failed to get window size"); | |
53 | return; | |
724e753c | 54 | } |
0519b5cc CB |
55 | |
56 | ret = ioctl(dstfd, TIOCSWINSZ, &wsz); | |
57 | if (ret < 0) | |
58 | WARN("Failed to set window size"); | |
59 | else | |
60 | DEBUG("Set window size to %d columns and %d rows", wsz.ws_col, | |
61 | wsz.ws_row); | |
62 | ||
63 | return; | |
b5159817 | 64 | } |
724e753c | 65 | |
5b55021f | 66 | static void lxc_terminal_winch(struct lxc_terminal_state *ts) |
b5159817 | 67 | { |
36a94ce8 | 68 | lxc_terminal_winsz(ts->stdinfd, ts->ptxfd); |
b5159817 | 69 | } |
cd453b38 | 70 | |
9bafc8cb | 71 | int lxc_terminal_signalfd_cb(int fd, uint32_t events, void *cbdata, |
3298b37d | 72 | struct lxc_async_descr *descr) |
b5159817 | 73 | { |
1349e92e | 74 | ssize_t ret; |
b5159817 | 75 | struct signalfd_siginfo siginfo; |
5b55021f | 76 | struct lxc_terminal_state *ts = cbdata; |
b5159817 | 77 | |
72f7c19b | 78 | ret = lxc_read_nointr(fd, &siginfo, sizeof(siginfo)); |
0d4137cc | 79 | if (ret < 0 || (size_t)ret < sizeof(siginfo)) { |
0519b5cc | 80 | ERROR("Failed to read signal info"); |
2e943b7b | 81 | return LXC_MAINLOOP_ERROR; |
cd453b38 DL |
82 | } |
83 | ||
1349e92e | 84 | if (siginfo.ssi_signo == SIGTERM) { |
9bafc8cb | 85 | DEBUG("Received SIGTERM. Detaching from the terminal"); |
a529bc25 | 86 | return LXC_MAINLOOP_CLOSE; |
1349e92e CB |
87 | } |
88 | ||
89 | if (siginfo.ssi_signo == SIGWINCH) | |
7a10164a | 90 | lxc_terminal_winch(ts); |
1349e92e | 91 | |
2e943b7b | 92 | return LXC_MAINLOOP_CONTINUE; |
b5159817 DE |
93 | } |
94 | ||
5b55021f | 95 | struct lxc_terminal_state *lxc_terminal_signal_init(int srcfd, int dstfd) |
b5159817 | 96 | { |
f62cf1d4 | 97 | __do_close int signal_fd = -EBADF; |
28327a43 | 98 | __do_free struct lxc_terminal_state *ts = NULL; |
28327a43 | 99 | int ret; |
b5159817 | 100 | sigset_t mask; |
b5159817 DE |
101 | |
102 | ts = malloc(sizeof(*ts)); | |
103 | if (!ts) | |
104 | return NULL; | |
105 | ||
106 | memset(ts, 0, sizeof(*ts)); | |
341c2aed | 107 | ts->stdinfd = srcfd; |
36a94ce8 | 108 | ts->ptxfd = dstfd; |
341c2aed | 109 | ts->sigfd = -1; |
b5159817 | 110 | |
b6d5de95 CB |
111 | ret = sigemptyset(&mask); |
112 | if (ret < 0) { | |
113 | SYSERROR("Failed to initialize an empty signal set"); | |
1ba4ae89 | 114 | return NULL; |
b6d5de95 | 115 | } |
1349e92e | 116 | |
1ba4ae89 | 117 | if (isatty(srcfd)) { |
b6d5de95 CB |
118 | ret = sigaddset(&mask, SIGWINCH); |
119 | if (ret < 0) | |
7874d81a | 120 | SYSNOTICE("Failed to add SIGWINCH to signal set"); |
1ba4ae89 CB |
121 | } else { |
122 | INFO("fd %d does not refer to a tty device", srcfd); | |
25964232 LF |
123 | } |
124 | ||
1349e92e | 125 | /* Exit the mainloop cleanly on SIGTERM. */ |
b6d5de95 CB |
126 | ret = sigaddset(&mask, SIGTERM); |
127 | if (ret < 0) { | |
128 | SYSERROR("Failed to add SIGWINCH to signal set"); | |
1ba4ae89 | 129 | return NULL; |
b6d5de95 | 130 | } |
b5159817 | 131 | |
b467714b | 132 | ret = pthread_sigmask(SIG_BLOCK, &mask, &ts->oldmask); |
1349e92e CB |
133 | if (ret < 0) { |
134 | WARN("Failed to block signals"); | |
1ba4ae89 | 135 | return NULL; |
b5159817 DE |
136 | } |
137 | ||
1ba4ae89 CB |
138 | signal_fd = signalfd(-1, &mask, SFD_CLOEXEC); |
139 | if (signal_fd < 0) { | |
1349e92e | 140 | WARN("Failed to create signal fd"); |
b467714b | 141 | (void)pthread_sigmask(SIG_SETMASK, &ts->oldmask, NULL); |
1ba4ae89 | 142 | return NULL; |
1349e92e | 143 | } |
1ba4ae89 CB |
144 | ts->sigfd = move_fd(signal_fd); |
145 | TRACE("Created signal fd %d", ts->sigfd); | |
b6d5de95 | 146 | |
1ba4ae89 | 147 | return move_ptr(ts); |
cd453b38 DL |
148 | } |
149 | ||
3e3f79bd CB |
150 | int lxc_terminal_signal_sigmask_safe_blocked(struct lxc_terminal *terminal) |
151 | { | |
152 | struct lxc_terminal_state *state = terminal->tty_state; | |
153 | ||
154 | if (!state) | |
155 | return 0; | |
156 | ||
157 | return pthread_sigmask(SIG_SETMASK, &state->oldmask, NULL); | |
158 | } | |
159 | ||
26ed61e0 CB |
160 | /** |
161 | * lxc_terminal_signal_fini: uninstall signal handler | |
162 | * | |
163 | * @terminal: terminal instance | |
164 | * | |
165 | * Restore the saved signal handler that was in effect at the time | |
166 | * lxc_terminal_signal_init() was called. | |
167 | */ | |
168 | static void lxc_terminal_signal_fini(struct lxc_terminal *terminal) | |
63376d7d | 169 | { |
28327a43 | 170 | struct lxc_terminal_state *state = terminal->tty_state; |
1349e92e | 171 | |
28327a43 CB |
172 | if (!terminal->tty_state) |
173 | return; | |
174 | ||
175 | state = terminal->tty_state; | |
176 | if (state->sigfd >= 0) { | |
177 | close(state->sigfd); | |
178 | ||
179 | if (pthread_sigmask(SIG_SETMASK, &state->oldmask, NULL) < 0) | |
a24c5678 | 180 | SYSWARN("Failed to restore signal mask"); |
0e6da90b | 181 | } |
0d4137cc | 182 | |
28327a43 CB |
183 | free(terminal->tty_state); |
184 | terminal->tty_state = NULL; | |
b5159817 | 185 | } |
1560f6c9 | 186 | |
99a04585 | 187 | static int lxc_terminal_truncate_log_file(struct lxc_terminal *terminal) |
861813e5 CB |
188 | { |
189 | /* be very certain things are kosher */ | |
2083d59d | 190 | if (!terminal->log_path || terminal->log_fd < 0) |
861813e5 CB |
191 | return -EBADF; |
192 | ||
2083d59d | 193 | return lxc_unpriv(ftruncate(terminal->log_fd, 0)); |
861813e5 CB |
194 | } |
195 | ||
468724d3 | 196 | static int lxc_terminal_rotate_log_file(struct lxc_terminal *terminal) |
861813e5 | 197 | { |
2530ba95 | 198 | __do_free char *tmp = NULL; |
861813e5 CB |
199 | int ret; |
200 | size_t len; | |
861813e5 | 201 | |
99a04585 | 202 | if (!terminal->log_path || terminal->log_rotate == 0) |
861813e5 CB |
203 | return -EOPNOTSUPP; |
204 | ||
205 | /* be very certain things are kosher */ | |
99a04585 | 206 | if (terminal->log_fd < 0) |
861813e5 CB |
207 | return -EBADF; |
208 | ||
99a04585 | 209 | len = strlen(terminal->log_path) + sizeof(".1"); |
2530ba95 | 210 | tmp = must_realloc(NULL, len); |
861813e5 | 211 | |
93d5ebf1 CB |
212 | ret = strnprintf(tmp, len, "%s.1", terminal->log_path); |
213 | if (ret < 0) | |
861813e5 CB |
214 | return -EFBIG; |
215 | ||
99a04585 CB |
216 | close(terminal->log_fd); |
217 | terminal->log_fd = -1; | |
218 | ret = lxc_unpriv(rename(terminal->log_path, tmp)); | |
861813e5 CB |
219 | if (ret < 0) |
220 | return ret; | |
221 | ||
99a04585 | 222 | return lxc_terminal_create_log_file(terminal); |
861813e5 CB |
223 | } |
224 | ||
a44ae1a9 | 225 | static int lxc_terminal_write_log_file(struct lxc_terminal *terminal, char *buf, |
8903fb08 | 226 | int bytes_read) |
861813e5 CB |
227 | { |
228 | int ret; | |
861813e5 | 229 | struct stat st; |
8903fb08 | 230 | int64_t space_left = -1; |
861813e5 | 231 | |
99a04585 | 232 | if (terminal->log_fd < 0) |
861813e5 CB |
233 | return 0; |
234 | ||
235 | /* A log size <= 0 means that there's no limit on the size of the log | |
236 | * file at which point we simply ignore whether the log is supposed to | |
237 | * be rotated or not. | |
238 | */ | |
99a04585 CB |
239 | if (terminal->log_size <= 0) |
240 | return lxc_write_nointr(terminal->log_fd, buf, bytes_read); | |
861813e5 CB |
241 | |
242 | /* Get current size of the log file. */ | |
99a04585 | 243 | ret = fstat(terminal->log_fd, &st); |
861813e5 | 244 | if (ret < 0) { |
99a04585 | 245 | SYSERROR("Failed to stat the terminal log file descriptor"); |
861813e5 CB |
246 | return -1; |
247 | } | |
248 | ||
249 | /* handle non-regular files */ | |
250 | if ((st.st_mode & S_IFMT) != S_IFREG) { | |
251 | /* This isn't a regular file. so rotating the file seems a | |
252 | * dangerous thing to do, size limits are also very | |
253 | * questionable. Let's not risk anything and tell the user that | |
f48e8071 | 254 | * they're requesting us to do weird stuff. |
861813e5 | 255 | */ |
99a04585 | 256 | if (terminal->log_rotate > 0 || terminal->log_size > 0) |
861813e5 CB |
257 | return -EINVAL; |
258 | ||
259 | /* I mean, sure log wherever you want to. */ | |
99a04585 | 260 | return lxc_write_nointr(terminal->log_fd, buf, bytes_read); |
861813e5 CB |
261 | } |
262 | ||
99a04585 | 263 | space_left = terminal->log_size - st.st_size; |
861813e5 CB |
264 | |
265 | /* User doesn't want to rotate the log file and there's no more space | |
266 | * left so simply truncate it. | |
267 | */ | |
99a04585 CB |
268 | if (space_left <= 0 && terminal->log_rotate <= 0) { |
269 | ret = lxc_terminal_truncate_log_file(terminal); | |
861813e5 CB |
270 | if (ret < 0) |
271 | return ret; | |
272 | ||
99a04585 CB |
273 | if (bytes_read <= terminal->log_size) |
274 | return lxc_write_nointr(terminal->log_fd, buf, bytes_read); | |
861813e5 CB |
275 | |
276 | /* Write as much as we can into the buffer and loose the rest. */ | |
99a04585 | 277 | return lxc_write_nointr(terminal->log_fd, buf, terminal->log_size); |
861813e5 CB |
278 | } |
279 | ||
280 | /* There's enough space left. */ | |
281 | if (bytes_read <= space_left) | |
99a04585 | 282 | return lxc_write_nointr(terminal->log_fd, buf, bytes_read); |
861813e5 CB |
283 | |
284 | /* There's not enough space left but at least write as much as we can | |
285 | * into the old log file. | |
286 | */ | |
99a04585 | 287 | ret = lxc_write_nointr(terminal->log_fd, buf, space_left); |
861813e5 CB |
288 | if (ret < 0) |
289 | return -1; | |
290 | ||
291 | /* Calculate how many bytes we still need to write. */ | |
292 | bytes_read -= space_left; | |
293 | ||
89962c6c | 294 | /* There'd be more to write but we aren't instructed to rotate the log |
861813e5 CB |
295 | * file so simply return. There's no error on our side here. |
296 | */ | |
99a04585 | 297 | if (terminal->log_rotate > 0) |
468724d3 | 298 | ret = lxc_terminal_rotate_log_file(terminal); |
861813e5 | 299 | else |
99a04585 | 300 | ret = lxc_terminal_truncate_log_file(terminal); |
861813e5 CB |
301 | if (ret < 0) |
302 | return ret; | |
303 | ||
99a04585 | 304 | if (terminal->log_size < bytes_read) { |
861813e5 CB |
305 | /* Well, this is unfortunate because it means that there is more |
306 | * to write than the user has granted us space. There are | |
307 | * multiple ways to handle this but let's use the simplest one: | |
308 | * write as much as we can, tell the user that there was more | |
309 | * stuff to write and move on. | |
310 | * Note that this scenario shouldn't actually happen with the | |
99a04585 | 311 | * standard pty-based terminal that LXC allocates since it will |
861813e5 CB |
312 | * be switched into raw mode. In raw mode only 1 byte at a time |
313 | * should be read and written. | |
314 | */ | |
99a04585 CB |
315 | WARN("Size of terminal log file is smaller than the bytes to write"); |
316 | ret = lxc_write_nointr(terminal->log_fd, buf, terminal->log_size); | |
861813e5 CB |
317 | if (ret < 0) |
318 | return -1; | |
319 | bytes_read -= ret; | |
320 | return bytes_read; | |
321 | } | |
322 | ||
323 | /* Yay, we made it. */ | |
99a04585 | 324 | ret = lxc_write_nointr(terminal->log_fd, buf, bytes_read); |
861813e5 CB |
325 | if (ret < 0) |
326 | return -1; | |
327 | bytes_read -= ret; | |
328 | return bytes_read; | |
329 | } | |
330 | ||
543d2f83 | 331 | static int lxc_terminal_ptx_io(struct lxc_terminal *terminal) |
b5159817 | 332 | { |
de708fb7 | 333 | char buf[LXC_TERMINAL_BUFFER_SIZE]; |
732375f5 | 334 | int r, w, w_log, w_rbuf; |
e0dc0de7 | 335 | |
543d2f83 CB |
336 | w = r = lxc_read_nointr(terminal->ptx, buf, sizeof(buf)); |
337 | if (r <= 0) | |
338 | return -1; | |
c06a0555 | 339 | |
543d2f83 CB |
340 | w_rbuf = w_log = 0; |
341 | /* write to peer first */ | |
342 | if (terminal->peer >= 0) | |
343 | w = lxc_write_nointr(terminal->peer, buf, r); | |
63376d7d | 344 | |
543d2f83 CB |
345 | /* write to terminal ringbuffer */ |
346 | if (terminal->buffer_size > 0) | |
347 | w_rbuf = lxc_ringbuf_write(&terminal->ringbuf, buf, r); | |
b5159817 | 348 | |
543d2f83 CB |
349 | /* write to terminal log */ |
350 | if (terminal->log_fd >= 0) | |
351 | w_log = lxc_terminal_write_log_file(terminal, buf, r); | |
63376d7d | 352 | |
b5159817 | 353 | if (w != r) |
de708fb7 | 354 | WARN("Short write on terminal r:%d != w:%d", r, w); |
732375f5 | 355 | |
7874d81a | 356 | if (w_rbuf < 0) { |
357 | errno = -w_rbuf; | |
358 | SYSTRACE("Failed to write %d bytes to terminal ringbuffer", r); | |
359 | } | |
732375f5 CB |
360 | |
361 | if (w_log < 0) | |
de708fb7 | 362 | TRACE("Failed to write %d bytes to terminal log", r); |
0d4137cc | 363 | |
543d2f83 CB |
364 | return 0; |
365 | } | |
366 | ||
367 | static int lxc_terminal_peer_io(struct lxc_terminal *terminal) | |
368 | { | |
369 | char buf[LXC_TERMINAL_BUFFER_SIZE]; | |
370 | int r, w; | |
371 | ||
372 | w = r = lxc_read_nointr(terminal->peer, buf, sizeof(buf)); | |
373 | if (r <= 0) | |
374 | return -1; | |
375 | ||
376 | w = lxc_write_nointr(terminal->ptx, buf, r); | |
377 | if (w != r) | |
378 | WARN("Short write on terminal r:%d != w:%d", r, w); | |
379 | ||
380 | return 0; | |
381 | } | |
382 | ||
383 | static int lxc_terminal_ptx_io_handler(int fd, uint32_t events, void *data, | |
384 | struct lxc_async_descr *descr) | |
385 | { | |
386 | struct lxc_terminal *terminal = data; | |
387 | int ret; | |
388 | ||
389 | ret = lxc_terminal_ptx_io(data); | |
390 | if (ret < 0) | |
391 | return log_info(LXC_MAINLOOP_CLOSE, | |
392 | "Terminal client on fd %d has exited", | |
393 | terminal->ptx); | |
394 | ||
395 | return LXC_MAINLOOP_CONTINUE; | |
396 | } | |
397 | ||
398 | static int lxc_terminal_peer_io_handler(int fd, uint32_t events, void *data, | |
399 | struct lxc_async_descr *descr) | |
400 | { | |
401 | struct lxc_terminal *terminal = data; | |
402 | int ret; | |
403 | ||
404 | ret = lxc_terminal_peer_io(data); | |
405 | if (ret < 0) | |
406 | return log_info(LXC_MAINLOOP_CLOSE, | |
407 | "Terminal client on fd %d has exited", | |
408 | terminal->peer); | |
409 | ||
2b8bf299 | 410 | return LXC_MAINLOOP_CONTINUE; |
b5159817 DE |
411 | } |
412 | ||
dcad02f8 | 413 | static int lxc_terminal_mainloop_add_peer(struct lxc_terminal *terminal) |
b5159817 | 414 | { |
a529bc25 CB |
415 | int ret; |
416 | ||
2083d59d CB |
417 | if (terminal->peer >= 0) { |
418 | ret = lxc_mainloop_add_handler(terminal->descr, terminal->peer, | |
543d2f83 CB |
419 | lxc_terminal_peer_io_handler, |
420 | default_cleanup_handler, | |
421 | terminal, "lxc_terminal_peer_io_handler"); | |
a529bc25 | 422 | if (ret < 0) { |
2083d59d | 423 | WARN("Failed to add terminal peer handler to mainloop"); |
a529bc25 CB |
424 | return -1; |
425 | } | |
63376d7d DL |
426 | } |
427 | ||
2083d59d | 428 | if (!terminal->tty_state || terminal->tty_state->sigfd < 0) |
a529bc25 CB |
429 | return 0; |
430 | ||
543d2f83 CB |
431 | ret = lxc_mainloop_add_handler(terminal->descr, |
432 | terminal->tty_state->sigfd, | |
433 | lxc_terminal_signalfd_cb, | |
434 | default_cleanup_handler, | |
435 | terminal->tty_state, | |
436 | "lxc_terminal_signalfd_cb"); | |
a529bc25 CB |
437 | if (ret < 0) { |
438 | WARN("Failed to add signal handler to mainloop"); | |
439 | return -1; | |
b5159817 | 440 | } |
a529bc25 CB |
441 | |
442 | return 0; | |
b5159817 DE |
443 | } |
444 | ||
3298b37d | 445 | int lxc_terminal_mainloop_add(struct lxc_async_descr *descr, |
ea5b3c23 | 446 | struct lxc_terminal *terminal) |
b5159817 | 447 | { |
a529bc25 | 448 | int ret; |
b5159817 | 449 | |
36a94ce8 | 450 | if (terminal->ptx < 0) { |
2083d59d | 451 | INFO("Terminal is not initialized"); |
b5159817 | 452 | return 0; |
596a818d DE |
453 | } |
454 | ||
36a94ce8 | 455 | ret = lxc_mainloop_add_handler(descr, terminal->ptx, |
543d2f83 CB |
456 | lxc_terminal_ptx_io_handler, |
457 | default_cleanup_handler, | |
458 | terminal, "lxc_terminal_ptx_io_handler"); | |
30a33fbd | 459 | if (ret < 0) { |
543d2f83 | 460 | ERROR("Failed to add handler for terminal ptx fd %d to mainloop", terminal->ptx); |
b5159817 | 461 | return -1; |
28a4b0e5 DL |
462 | } |
463 | ||
30a33fbd | 464 | /* We cache the descr so that we can add an fd to it when someone |
c1ee47cd | 465 | * does attach to it in lxc_terminal_allocate(). |
b5159817 | 466 | */ |
2083d59d | 467 | terminal->descr = descr; |
cd453b38 | 468 | |
ea5b3c23 | 469 | return lxc_terminal_mainloop_add_peer(terminal); |
b5159817 | 470 | } |
28a4b0e5 | 471 | |
0d4137cc | 472 | int lxc_setup_tios(int fd, struct termios *oldtios) |
b5159817 | 473 | { |
e4953e62 | 474 | int ret; |
b5159817 | 475 | struct termios newtios; |
e0dc0de7 | 476 | |
b5159817 | 477 | if (!isatty(fd)) { |
8332a09c | 478 | ERROR("File descriptor %d does not refer to a terminal", fd); |
b5159817 | 479 | return -1; |
e0dc0de7 DL |
480 | } |
481 | ||
e4953e62 CB |
482 | /* Get current termios. */ |
483 | ret = tcgetattr(fd, oldtios); | |
484 | if (ret < 0) { | |
485 | SYSERROR("Failed to get current terminal settings"); | |
b5159817 | 486 | return -1; |
e0dc0de7 DL |
487 | } |
488 | ||
4dc96430 TJ |
489 | /* ensure we don't end up in an endless loop: |
490 | * The kernel might fire SIGTTOU while an | |
491 | * ioctl() in tcsetattr() is executed. When the ioctl() | |
492 | * is resumed and retries, the signal handler interrupts it again. | |
493 | */ | |
494 | signal (SIGTTIN, SIG_IGN); | |
495 | signal (SIGTTOU, SIG_IGN); | |
496 | ||
b5159817 | 497 | newtios = *oldtios; |
e0dc0de7 | 498 | |
a7c97a40 CB |
499 | /* We use the same settings that ssh does. */ |
500 | newtios.c_iflag |= IGNPAR; | |
501 | newtios.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF); | |
502 | #ifdef IUCLC | |
503 | newtios.c_iflag &= ~IUCLC; | |
504 | #endif | |
4dc96430 | 505 | newtios.c_lflag &= ~(TOSTOP | ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL); |
a7c97a40 CB |
506 | #ifdef IEXTEN |
507 | newtios.c_lflag &= ~IEXTEN; | |
508 | #endif | |
90275474 | 509 | newtios.c_oflag |= ONLCR; |
300ec717 | 510 | newtios.c_oflag |= OPOST; |
b5159817 DE |
511 | newtios.c_cc[VMIN] = 1; |
512 | newtios.c_cc[VTIME] = 0; | |
e0dc0de7 | 513 | |
a7c97a40 | 514 | /* Set new attributes. */ |
e4953e62 CB |
515 | ret = tcsetattr(fd, TCSAFLUSH, &newtios); |
516 | if (ret < 0) { | |
517 | ERROR("Failed to set new terminal settings"); | |
b5159817 | 518 | return -1; |
e0dc0de7 DL |
519 | } |
520 | ||
63376d7d | 521 | return 0; |
b5159817 | 522 | } |
e0dc0de7 | 523 | |
dcad02f8 | 524 | static void lxc_terminal_peer_proxy_free(struct lxc_terminal *terminal) |
b5159817 | 525 | { |
28327a43 | 526 | lxc_terminal_signal_fini(terminal); |
e788f4ac | 527 | |
36a94ce8 CB |
528 | close(terminal->proxy.ptx); |
529 | terminal->proxy.ptx = -1; | |
e788f4ac | 530 | |
41808e20 CB |
531 | close(terminal->proxy.pty); |
532 | terminal->proxy.pty = -1; | |
e788f4ac | 533 | |
fb87aa6a | 534 | terminal->proxy.busy = -1; |
e788f4ac | 535 | |
fb87aa6a | 536 | terminal->proxy.name[0] = '\0'; |
e788f4ac | 537 | |
2083d59d | 538 | terminal->peer = -1; |
b5159817 | 539 | } |
596a818d | 540 | |
60dd8ef4 CB |
541 | static int lxc_terminal_peer_proxy_alloc(struct lxc_terminal *terminal, |
542 | int sockfd) | |
b5159817 | 543 | { |
60dd8ef4 | 544 | int ret; |
b5159817 | 545 | struct termios oldtermio; |
5b55021f | 546 | struct lxc_terminal_state *ts; |
b5159817 | 547 | |
36a94ce8 | 548 | if (terminal->ptx < 0) { |
2083d59d | 549 | ERROR("Terminal not set up"); |
b5159817 DE |
550 | return -1; |
551 | } | |
60dd8ef4 | 552 | |
fb87aa6a | 553 | if (terminal->proxy.busy != -1 || terminal->peer != -1) { |
2083d59d | 554 | NOTICE("Terminal already in use"); |
b5159817 DE |
555 | return -1; |
556 | } | |
60dd8ef4 | 557 | |
2083d59d | 558 | if (terminal->tty_state) { |
60dd8ef4 | 559 | ERROR("Terminal has already been initialized"); |
b5159817 | 560 | return -1; |
596a818d DE |
561 | } |
562 | ||
d712f9e8 | 563 | /* This is the proxy terminal that will be given to the client, and |
36a94ce8 | 564 | * that the real terminal ptx will send to / recv from. |
b5159817 | 565 | */ |
41808e20 | 566 | ret = openpty(&terminal->proxy.ptx, &terminal->proxy.pty, NULL, |
3f15bdd9 | 567 | NULL, NULL); |
60dd8ef4 CB |
568 | if (ret < 0) { |
569 | SYSERROR("Failed to open proxy terminal"); | |
b5159817 DE |
570 | return -1; |
571 | } | |
596a818d | 572 | |
41808e20 | 573 | ret = ttyname_r(terminal->proxy.pty, terminal->proxy.name, |
3f15bdd9 CB |
574 | sizeof(terminal->proxy.name)); |
575 | if (ret < 0) { | |
41808e20 | 576 | SYSERROR("Failed to retrieve name of proxy terminal pty"); |
3f15bdd9 CB |
577 | goto on_error; |
578 | } | |
579 | ||
36a94ce8 | 580 | ret = fd_cloexec(terminal->proxy.ptx, true); |
408c18a1 | 581 | if (ret < 0) { |
36a94ce8 | 582 | SYSERROR("Failed to set FD_CLOEXEC flag on proxy terminal ptx"); |
408c18a1 CB |
583 | goto on_error; |
584 | } | |
585 | ||
41808e20 | 586 | ret = fd_cloexec(terminal->proxy.pty, true); |
408c18a1 | 587 | if (ret < 0) { |
41808e20 | 588 | SYSERROR("Failed to set FD_CLOEXEC flag on proxy terminal pty"); |
408c18a1 CB |
589 | goto on_error; |
590 | } | |
591 | ||
41808e20 | 592 | ret = lxc_setup_tios(terminal->proxy.pty, &oldtermio); |
60dd8ef4 CB |
593 | if (ret < 0) |
594 | goto on_error; | |
b5159817 | 595 | |
36a94ce8 | 596 | ts = lxc_terminal_signal_init(terminal->proxy.ptx, terminal->ptx); |
b5159817 | 597 | if (!ts) |
60dd8ef4 | 598 | goto on_error; |
b5159817 | 599 | |
2083d59d | 600 | terminal->tty_state = ts; |
41808e20 | 601 | terminal->peer = terminal->proxy.pty; |
fb87aa6a | 602 | terminal->proxy.busy = sockfd; |
2083d59d | 603 | ret = lxc_terminal_mainloop_add_peer(terminal); |
a529bc25 | 604 | if (ret < 0) |
60dd8ef4 | 605 | goto on_error; |
b5159817 | 606 | |
41808e20 CB |
607 | NOTICE("Opened proxy terminal with ptx fd %d and pty fd %d", |
608 | terminal->proxy.ptx, terminal->proxy.pty); | |
b5159817 DE |
609 | return 0; |
610 | ||
60dd8ef4 | 611 | on_error: |
2083d59d | 612 | lxc_terminal_peer_proxy_free(terminal); |
63376d7d DL |
613 | return -1; |
614 | } | |
615 | ||
c1ee47cd | 616 | int lxc_terminal_allocate(struct lxc_conf *conf, int sockfd, int *ttyreq) |
b5159817 | 617 | { |
12c2eaaa | 618 | int ttynum; |
36a94ce8 | 619 | int ptxfd = -1; |
0e4be3cf | 620 | struct lxc_tty_info *ttys = &conf->ttys; |
dcad02f8 | 621 | struct lxc_terminal *terminal = &conf->console; |
b5159817 | 622 | |
b5159817 | 623 | if (*ttyreq == 0) { |
12c2eaaa CB |
624 | int ret; |
625 | ||
626 | ret = lxc_terminal_peer_proxy_alloc(terminal, sockfd); | |
627 | if (ret < 0) | |
b5159817 | 628 | goto out; |
12c2eaaa | 629 | |
36a94ce8 | 630 | ptxfd = terminal->proxy.ptx; |
b5159817 DE |
631 | goto out; |
632 | } | |
633 | ||
634 | if (*ttyreq > 0) { | |
885766f5 | 635 | if (*ttyreq > ttys->max) |
b5159817 DE |
636 | goto out; |
637 | ||
730aaf46 | 638 | if (ttys->tty[*ttyreq - 1].busy >= 0) |
b5159817 DE |
639 | goto out; |
640 | ||
12c2eaaa | 641 | /* The requested tty is available. */ |
b5159817 DE |
642 | ttynum = *ttyreq; |
643 | goto out_tty; | |
644 | } | |
645 | ||
12c2eaaa | 646 | /* Search for next available tty, fixup index tty1 => [0]. */ |
730aaf46 | 647 | for (ttynum = 1; ttynum <= ttys->max && ttys->tty[ttynum - 1].busy >= 0; ttynum++) { |
0d4137cc | 648 | ; |
12c2eaaa | 649 | } |
b5159817 | 650 | |
12c2eaaa | 651 | /* We didn't find any available slot for tty. */ |
885766f5 | 652 | if (ttynum > ttys->max) |
b5159817 DE |
653 | goto out; |
654 | ||
655 | *ttyreq = ttynum; | |
656 | ||
657 | out_tty: | |
0e4be3cf | 658 | ttys->tty[ttynum - 1].busy = sockfd; |
36a94ce8 | 659 | ptxfd = ttys->tty[ttynum - 1].ptx; |
12c2eaaa | 660 | |
b5159817 | 661 | out: |
36a94ce8 | 662 | return ptxfd; |
b5159817 DE |
663 | } |
664 | ||
3dfe6f8d | 665 | void lxc_terminal_free(struct lxc_conf *conf, int fd) |
63376d7d | 666 | { |
b5159817 | 667 | int i; |
0e4be3cf | 668 | struct lxc_tty_info *ttys = &conf->ttys; |
dcad02f8 | 669 | struct lxc_terminal *terminal = &conf->console; |
b5159817 | 670 | |
885766f5 | 671 | for (i = 0; i < ttys->max; i++) |
0e4be3cf | 672 | if (ttys->tty[i].busy == fd) |
730aaf46 | 673 | ttys->tty[i].busy = -1; |
b5159817 | 674 | |
1b5e93c4 CB |
675 | if (terminal->proxy.busy != fd) |
676 | return; | |
677 | ||
41808e20 | 678 | lxc_mainloop_del_handler(terminal->descr, terminal->proxy.pty); |
1b5e93c4 | 679 | lxc_terminal_peer_proxy_free(terminal); |
b5159817 DE |
680 | } |
681 | ||
dcad02f8 | 682 | static int lxc_terminal_peer_default(struct lxc_terminal *terminal) |
b5159817 | 683 | { |
5b55021f | 684 | struct lxc_terminal_state *ts; |
46768cce | 685 | const char *path; |
467c7ff3 | 686 | int ret = 0; |
b5159817 | 687 | |
46768cce CB |
688 | if (terminal->path) |
689 | path = terminal->path; | |
690 | else | |
691 | path = "/dev/tty"; | |
b5159817 | 692 | |
2083d59d CB |
693 | terminal->peer = lxc_unpriv(open(path, O_RDWR | O_CLOEXEC)); |
694 | if (terminal->peer < 0) { | |
46768cce CB |
695 | if (!terminal->path) { |
696 | errno = ENODEV; | |
7874d81a | 697 | SYSDEBUG("The process does not have a controlling terminal"); |
46768cce CB |
698 | goto on_succes; |
699 | } | |
700 | ||
6d1400b5 | 701 | SYSERROR("Failed to open proxy terminal \"%s\"", path); |
467c7ff3 CB |
702 | return -ENOTTY; |
703 | } | |
49cd0656 | 704 | DEBUG("Using terminal \"%s\" as proxy", path); |
b5159817 | 705 | |
2083d59d | 706 | if (!isatty(terminal->peer)) { |
49cd0656 CB |
707 | ERROR("File descriptor for \"%s\" does not refer to a terminal", path); |
708 | goto on_error_free_tios; | |
467c7ff3 | 709 | } |
b5159817 | 710 | |
36a94ce8 | 711 | ts = lxc_terminal_signal_init(terminal->peer, terminal->ptx); |
2083d59d | 712 | terminal->tty_state = ts; |
341c2aed | 713 | if (!ts) { |
0519b5cc | 714 | WARN("Failed to install signal handler"); |
49cd0656 | 715 | goto on_error_free_tios; |
341c2aed | 716 | } |
b5159817 | 717 | |
36a94ce8 | 718 | lxc_terminal_winsz(terminal->peer, terminal->ptx); |
b5159817 | 719 | |
2083d59d | 720 | terminal->tios = malloc(sizeof(*terminal->tios)); |
49cd0656 CB |
721 | if (!terminal->tios) |
722 | goto on_error_free_tios; | |
b5159817 | 723 | |
49cd0656 CB |
724 | ret = lxc_setup_tios(terminal->peer, terminal->tios); |
725 | if (ret < 0) | |
726 | goto on_error_close_peer; | |
467c7ff3 | 727 | else |
49cd0656 | 728 | goto on_succes; |
b5159817 | 729 | |
49cd0656 | 730 | on_error_free_tios: |
2083d59d CB |
731 | free(terminal->tios); |
732 | terminal->tios = NULL; | |
467c7ff3 | 733 | |
49cd0656 | 734 | on_error_close_peer: |
2083d59d CB |
735 | close(terminal->peer); |
736 | terminal->peer = -1; | |
467c7ff3 CB |
737 | ret = -ENOTTY; |
738 | ||
49cd0656 | 739 | on_succes: |
467c7ff3 | 740 | return ret; |
b5159817 DE |
741 | } |
742 | ||
dcad02f8 | 743 | int lxc_terminal_write_ringbuffer(struct lxc_terminal *terminal) |
39c6cdb7 CB |
744 | { |
745 | char *r_addr; | |
746 | ssize_t ret; | |
747 | uint64_t used; | |
2083d59d | 748 | struct lxc_ringbuf *buf = &terminal->ringbuf; |
39c6cdb7 CB |
749 | |
750 | /* There's not log file where we can dump the ringbuffer to. */ | |
2083d59d | 751 | if (terminal->log_fd < 0) |
39c6cdb7 CB |
752 | return 0; |
753 | ||
39c6cdb7 CB |
754 | used = lxc_ringbuf_used(buf); |
755 | if (used == 0) | |
756 | return 0; | |
757 | ||
2083d59d | 758 | ret = lxc_terminal_truncate_log_file(terminal); |
39c6cdb7 CB |
759 | if (ret < 0) |
760 | return ret; | |
761 | ||
762 | /* Write as much as we can without exceeding the limit. */ | |
2083d59d CB |
763 | if (terminal->log_size < used) |
764 | used = terminal->log_size; | |
39c6cdb7 CB |
765 | |
766 | r_addr = lxc_ringbuf_get_read_addr(buf); | |
2083d59d | 767 | ret = lxc_write_nointr(terminal->log_fd, r_addr, used); |
39c6cdb7 CB |
768 | if (ret < 0) |
769 | return -EIO; | |
770 | ||
771 | return 0; | |
772 | } | |
773 | ||
dcad02f8 | 774 | void lxc_terminal_delete(struct lxc_terminal *terminal) |
b5159817 | 775 | { |
69629c82 CB |
776 | int ret; |
777 | ||
2083d59d | 778 | ret = lxc_terminal_write_ringbuffer(terminal); |
39c6cdb7 | 779 | if (ret < 0) |
2083d59d | 780 | WARN("Failed to write terminal log to disk"); |
39c6cdb7 | 781 | |
2083d59d CB |
782 | if (terminal->tios && terminal->peer >= 0) { |
783 | ret = tcsetattr(terminal->peer, TCSAFLUSH, terminal->tios); | |
69629c82 | 784 | if (ret < 0) |
a24c5678 | 785 | SYSWARN("Failed to set old terminal settings"); |
69629c82 | 786 | } |
2083d59d CB |
787 | free(terminal->tios); |
788 | terminal->tios = NULL; | |
596a818d | 789 | |
2083d59d CB |
790 | if (terminal->peer >= 0) |
791 | close(terminal->peer); | |
792 | terminal->peer = -1; | |
bc9724f7 | 793 | |
36a94ce8 CB |
794 | if (terminal->ptx >= 0) |
795 | close(terminal->ptx); | |
796 | terminal->ptx = -1; | |
bc9724f7 | 797 | |
41808e20 CB |
798 | if (terminal->pty >= 0) |
799 | close(terminal->pty); | |
800 | terminal->pty = -1; | |
bc9724f7 | 801 | |
d926c261 CB |
802 | terminal->pty_nr = -1; |
803 | ||
2083d59d CB |
804 | if (terminal->log_fd >= 0) |
805 | close(terminal->log_fd); | |
806 | terminal->log_fd = -1; | |
63376d7d DL |
807 | } |
808 | ||
3b988b33 CB |
809 | /** |
810 | * Note that this function needs to run before the mainloop starts. Since we | |
36a94ce8 | 811 | * register a handler for the terminal's ptxfd when we create the mainloop |
2083d59d | 812 | * the terminal handler needs to see an allocated ringbuffer. |
3b988b33 | 813 | */ |
dcad02f8 | 814 | static int lxc_terminal_create_ringbuf(struct lxc_terminal *terminal) |
3b988b33 CB |
815 | { |
816 | int ret; | |
2083d59d CB |
817 | struct lxc_ringbuf *buf = &terminal->ringbuf; |
818 | uint64_t size = terminal->buffer_size; | |
3b988b33 CB |
819 | |
820 | /* no ringbuffer previously allocated and no ringbuffer requested */ | |
821 | if (!buf->addr && size <= 0) | |
822 | return 0; | |
823 | ||
824 | /* ringbuffer allocated but no new ringbuffer requested */ | |
825 | if (buf->addr && size <= 0) { | |
826 | lxc_ringbuf_release(buf); | |
827 | buf->addr = NULL; | |
828 | buf->r_off = 0; | |
829 | buf->w_off = 0; | |
830 | buf->size = 0; | |
2083d59d | 831 | TRACE("Deallocated terminal ringbuffer"); |
3b988b33 CB |
832 | return 0; |
833 | } | |
834 | ||
835 | if (size <= 0) | |
836 | return 0; | |
837 | ||
838 | /* check wether the requested size for the ringbuffer has changed */ | |
839 | if (buf->addr && buf->size != size) { | |
2083d59d CB |
840 | TRACE("Terminal ringbuffer size changed from %" PRIu64 |
841 | " to %" PRIu64 " bytes. Deallocating terminal ringbuffer", | |
3b988b33 CB |
842 | buf->size, size); |
843 | lxc_ringbuf_release(buf); | |
844 | } | |
845 | ||
846 | ret = lxc_ringbuf_create(buf, size); | |
847 | if (ret < 0) { | |
2083d59d | 848 | ERROR("Failed to setup %" PRIu64 " byte terminal ringbuffer", size); |
3b988b33 CB |
849 | return -1; |
850 | } | |
851 | ||
2083d59d | 852 | TRACE("Allocated %" PRIu64 " byte terminal ringbuffer", size); |
3b988b33 CB |
853 | return 0; |
854 | } | |
855 | ||
a0309168 | 856 | /** |
2083d59d CB |
857 | * This is the terminal log file. Please note that the terminal log file is |
858 | * (implementation wise not content wise) independent of the terminal ringbuffer. | |
a0309168 | 859 | */ |
dcad02f8 | 860 | int lxc_terminal_create_log_file(struct lxc_terminal *terminal) |
a0309168 | 861 | { |
2083d59d | 862 | if (!terminal->log_path) |
a0309168 CB |
863 | return 0; |
864 | ||
2083d59d CB |
865 | terminal->log_fd = lxc_unpriv(open(terminal->log_path, O_CLOEXEC | O_RDWR | O_CREAT | O_APPEND, 0600)); |
866 | if (terminal->log_fd < 0) { | |
867 | SYSERROR("Failed to open terminal log file \"%s\"", terminal->log_path); | |
a0309168 CB |
868 | return -1; |
869 | } | |
870 | ||
2083d59d | 871 | DEBUG("Using \"%s\" as terminal log file", terminal->log_path); |
a0309168 CB |
872 | return 0; |
873 | } | |
874 | ||
8ea93a0f CB |
875 | static int lxc_terminal_map_ids(struct lxc_conf *c, struct lxc_terminal *terminal) |
876 | { | |
877 | int ret; | |
878 | ||
879 | if (lxc_list_empty(&c->id_map)) | |
880 | return 0; | |
881 | ||
882 | if (is_empty_string(terminal->name) && terminal->pty < 0) | |
883 | return 0; | |
884 | ||
885 | if (terminal->pty >= 0) | |
886 | ret = userns_exec_mapped_root(NULL, terminal->pty, c); | |
887 | else | |
888 | ret = userns_exec_mapped_root(terminal->name, -EBADF, c); | |
889 | if (ret < 0) | |
890 | return log_error(-1, "Failed to chown terminal %d(%s)", terminal->pty, | |
891 | !is_empty_string(terminal->name) ? terminal->name : "(null)"); | |
892 | ||
893 | TRACE("Chowned terminal %d(%s)", terminal->pty, | |
894 | !is_empty_string(terminal->name) ? terminal->name : "(null)"); | |
895 | ||
896 | return 0; | |
897 | } | |
898 | ||
899 | static int lxc_terminal_create_foreign(struct lxc_conf *conf, struct lxc_terminal *terminal) | |
63376d7d | 900 | { |
8ded9244 | 901 | int ret; |
b5159817 | 902 | |
41808e20 | 903 | ret = openpty(&terminal->ptx, &terminal->pty, NULL, NULL, NULL); |
467c7ff3 | 904 | if (ret < 0) { |
8ded9244 | 905 | SYSERROR("Failed to open terminal"); |
b5159817 DE |
906 | return -1; |
907 | } | |
908 | ||
8ea93a0f CB |
909 | ret = lxc_terminal_map_ids(conf, terminal); |
910 | if (ret < 0) { | |
911 | SYSERROR("Failed to change ownership of terminal multiplexer device"); | |
912 | goto err; | |
913 | } | |
914 | ||
41808e20 | 915 | ret = ttyname_r(terminal->pty, terminal->name, sizeof(terminal->name)); |
3f15bdd9 | 916 | if (ret < 0) { |
41808e20 | 917 | SYSERROR("Failed to retrieve name of terminal pty"); |
3f15bdd9 CB |
918 | goto err; |
919 | } | |
920 | ||
36a94ce8 | 921 | ret = fd_cloexec(terminal->ptx, true); |
69629c82 | 922 | if (ret < 0) { |
36a94ce8 | 923 | SYSERROR("Failed to set FD_CLOEXEC flag on terminal ptx"); |
b5159817 DE |
924 | goto err; |
925 | } | |
926 | ||
41808e20 | 927 | ret = fd_cloexec(terminal->pty, true); |
69629c82 | 928 | if (ret < 0) { |
41808e20 | 929 | SYSERROR("Failed to set FD_CLOEXEC flag on terminal pty"); |
b5159817 DE |
930 | goto err; |
931 | } | |
932 | ||
2083d59d | 933 | ret = lxc_terminal_peer_default(terminal); |
467c7ff3 | 934 | if (ret < 0) { |
8ded9244 | 935 | ERROR("Failed to allocate proxy terminal"); |
467c7ff3 CB |
936 | goto err; |
937 | } | |
b5159817 | 938 | |
5777fe90 CB |
939 | return 0; |
940 | ||
941 | err: | |
2083d59d | 942 | lxc_terminal_delete(terminal); |
5777fe90 CB |
943 | return -ENODEV; |
944 | } | |
945 | ||
18129d94 CB |
946 | int lxc_devpts_terminal(int devpts_fd, int *ret_ptx, int *ret_pty, |
947 | int *ret_pty_nr, bool require_tiocgptpeer) | |
f797f05e | 948 | { |
4655bdd1 | 949 | __do_close int fd_ptx = -EBADF, fd_opath_pty = -EBADF, fd_pty = -EBADF; |
cb01e311 | 950 | int pty_nr = -1; |
f797f05e CB |
951 | int ret; |
952 | ||
9f77617b CB |
953 | if (devpts_fd < 0) |
954 | return ret_errno(EBADF); | |
955 | ||
4655bdd1 CB |
956 | fd_ptx = open_beneath(devpts_fd, "ptmx", O_RDWR | O_NOCTTY | O_CLOEXEC); |
957 | if (fd_ptx < 0) { | |
7a316559 CB |
958 | if (errno == ENOSPC) |
959 | return systrace("Exceeded number of allocatable terminals"); | |
960 | ||
961 | return syserror("Failed to open terminal multiplexer device"); | |
962 | } | |
f797f05e | 963 | |
4655bdd1 CB |
964 | ret = unlockpt(fd_ptx); |
965 | if (ret < 0) | |
966 | return syswarn_set(-ENODEV, "Failed to unlock multiplexer device device"); | |
f797f05e | 967 | |
4655bdd1 CB |
968 | fd_pty = ioctl(fd_ptx, TIOCGPTPEER, O_RDWR | O_NOCTTY | O_CLOEXEC); |
969 | if (fd_pty < 0) { | |
f382bcc6 CB |
970 | switch (errno) { |
971 | case ENOTTY: | |
972 | SYSTRACE("Pure fd-based terminal allocation not possible"); | |
973 | break; | |
974 | case ENOSPC: | |
7a316559 | 975 | SYSTRACE("Exceeded number of allocatable terminals"); |
f382bcc6 CB |
976 | break; |
977 | default: | |
978 | SYSWARN("Failed to allocate new pty device"); | |
64ac925f | 979 | return -errno; |
f382bcc6 | 980 | } |
f797f05e | 981 | |
64ac925f | 982 | /* The caller tells us that they trust the devpts instance. */ |
18129d94 CB |
983 | if (require_tiocgptpeer) |
984 | return ret_errno(ENODEV); | |
8ea93a0f | 985 | } |
f797f05e | 986 | |
4655bdd1 CB |
987 | ret = ioctl(fd_ptx, TIOCGPTN, &pty_nr); |
988 | if (ret) | |
989 | return syswarn_set(-ENODEV, "Failed to retrieve name of terminal pty"); | |
cb01e311 | 990 | |
18129d94 CB |
991 | if (fd_pty < 0) { |
992 | /* | |
993 | * If we end up it means that TIOCGPTPEER isn't supported but | |
994 | * the caller told us they trust the devpts instance so we use | |
995 | * the pty nr to open the pty side. | |
996 | */ | |
997 | fd_pty = open_at(devpts_fd, fdstr(pty_nr), PROTECT_OPEN_RW, | |
998 | PROTECT_LOOKUP_ABSOLUTE_XDEV, 0); | |
999 | if (fd_pty < 0) | |
1000 | return syswarn_set(-ENODEV, "Failed to open terminal pty fd by path %d/%d", | |
1001 | devpts_fd, pty_nr); | |
1002 | } else { | |
1003 | fd_opath_pty = open_at(devpts_fd, fdstr(pty_nr), PROTECT_OPATH_FILE, | |
1004 | PROTECT_LOOKUP_ABSOLUTE_XDEV, 0); | |
1005 | if (fd_opath_pty < 0) | |
1006 | return syswarn_set(-ENODEV, "Failed to open terminal pty fd by path %d/%d", | |
1007 | devpts_fd, pty_nr); | |
1008 | ||
1009 | if (!same_file_lax(fd_pty, fd_opath_pty)) | |
1010 | return syswarn_set(-ENODEV, "Terminal file descriptor changed"); | |
1011 | } | |
4655bdd1 | 1012 | |
4655bdd1 CB |
1013 | *ret_ptx = move_fd(fd_ptx); |
1014 | *ret_pty = move_fd(fd_pty); | |
d926c261 | 1015 | *ret_pty_nr = pty_nr; |
4655bdd1 CB |
1016 | return 0; |
1017 | } | |
1018 | ||
9f77617b CB |
1019 | int lxc_terminal_parent(struct lxc_conf *conf) |
1020 | { | |
1021 | __do_close int fd_devpts = -EBADF; | |
1022 | struct lxc_terminal *console = &conf->console; | |
1023 | int ret; | |
1024 | ||
1025 | if (!wants_console(&conf->console)) | |
1026 | return 0; | |
1027 | ||
1028 | /* Allocate console from the container's devpts. */ | |
1029 | if (conf->pty_max > 1) | |
1030 | return 0; | |
1031 | ||
1032 | /* Allocate console for the container from the host's devpts. */ | |
1033 | fd_devpts = open_at(-EBADF, | |
1034 | "/dev/pts", | |
1035 | PROTECT_OPATH_DIRECTORY, | |
1036 | PROTECT_LOOKUP_ABSOLUTE_XDEV, | |
1037 | 0); | |
1038 | if (fd_devpts < 0) | |
1039 | return syserror("Failed to open devpts instance"); | |
1040 | ||
18129d94 CB |
1041 | ret = lxc_devpts_terminal(fd_devpts, &console->ptx, &console->pty, |
1042 | &console->pty_nr, false); | |
9f77617b CB |
1043 | if (ret < 0) |
1044 | return syserror("Failed to allocate console"); | |
1045 | ||
1046 | ret = strnprintf(console->name, sizeof(console->name), | |
1047 | "/dev/pts/%d", console->pty_nr); | |
1048 | if (ret < 0) | |
1049 | return syserror("Failed to create console path"); | |
1050 | ||
1051 | return lxc_terminal_map_ids(conf, &conf->console); | |
1052 | } | |
1053 | ||
4655bdd1 | 1054 | static int lxc_terminal_create_native(const char *name, const char *lxcpath, |
4655bdd1 CB |
1055 | struct lxc_terminal *terminal) |
1056 | { | |
1057 | __do_close int devpts_fd = -EBADF; | |
1058 | int ret; | |
1059 | ||
1060 | devpts_fd = lxc_cmd_get_devpts_fd(name, lxcpath); | |
1061 | if (devpts_fd < 0) | |
23cc33cd | 1062 | return sysinfo("Failed to receive devpts fd"); |
4655bdd1 | 1063 | |
4dcf0c43 | 1064 | ret = lxc_devpts_terminal(devpts_fd, &terminal->ptx, &terminal->pty, |
18129d94 | 1065 | &terminal->pty_nr, true); |
4655bdd1 CB |
1066 | if (ret < 0) |
1067 | return ret; | |
cb01e311 | 1068 | |
d926c261 CB |
1069 | ret = strnprintf(terminal->name, sizeof(terminal->name), |
1070 | "/dev/pts/%d", terminal->pty_nr); | |
1071 | if (ret < 0) | |
1072 | return syserror("Failed to create path"); | |
1073 | ||
f797f05e CB |
1074 | ret = lxc_terminal_peer_default(terminal); |
1075 | if (ret < 0) { | |
4655bdd1 CB |
1076 | lxc_terminal_delete(terminal); |
1077 | return syswarn_set(-ENODEV, "Failed to allocate proxy terminal"); | |
f797f05e CB |
1078 | } |
1079 | ||
1080 | return 0; | |
f797f05e CB |
1081 | } |
1082 | ||
550ede0a CB |
1083 | int lxc_terminal_create(const char *name, const char *lxcpath, |
1084 | struct lxc_conf *conf, struct lxc_terminal *terminal) | |
f797f05e | 1085 | { |
4dcf0c43 | 1086 | if (!lxc_terminal_create_native(name, lxcpath, terminal)) |
f797f05e CB |
1087 | return 0; |
1088 | ||
8ea93a0f | 1089 | return lxc_terminal_create_foreign(conf, terminal); |
f797f05e CB |
1090 | } |
1091 | ||
564e31c4 | 1092 | int lxc_terminal_setup(struct lxc_conf *conf) |
5777fe90 CB |
1093 | { |
1094 | int ret; | |
dcad02f8 | 1095 | struct lxc_terminal *terminal = &conf->console; |
5777fe90 | 1096 | |
5ef86378 CB |
1097 | if (terminal->path && strequal(terminal->path, "none")) |
1098 | return log_info(0, "No terminal requested"); | |
5777fe90 | 1099 | |
9f77617b | 1100 | ret = lxc_terminal_peer_default(terminal); |
5777fe90 | 1101 | if (ret < 0) |
9f77617b | 1102 | goto err; |
5777fe90 | 1103 | |
2083d59d | 1104 | ret = lxc_terminal_create_log_file(terminal); |
a0309168 CB |
1105 | if (ret < 0) |
1106 | goto err; | |
1107 | ||
2083d59d | 1108 | ret = lxc_terminal_create_ringbuf(terminal); |
a0309168 CB |
1109 | if (ret < 0) |
1110 | goto err; | |
b5159817 DE |
1111 | |
1112 | return 0; | |
1113 | ||
1114 | err: | |
2083d59d | 1115 | lxc_terminal_delete(terminal); |
69629c82 | 1116 | return -ENODEV; |
b5159817 DE |
1117 | } |
1118 | ||
8ca7b374 CB |
1119 | static bool __terminal_dup2(int duplicate, int original) |
1120 | { | |
1121 | int ret; | |
1122 | ||
1123 | if (!isatty(original)) | |
1124 | return true; | |
1125 | ||
1126 | ret = dup2(duplicate, original); | |
1127 | if (ret < 0) { | |
1128 | SYSERROR("Failed to dup2(%d, %d)", duplicate, original); | |
1129 | return false; | |
1130 | } | |
1131 | ||
1132 | return true; | |
1133 | } | |
1134 | ||
ae6d3913 | 1135 | int lxc_terminal_set_stdfds(int fd) |
0d9acb99 | 1136 | { |
8ca7b374 CB |
1137 | int i; |
1138 | ||
39a78bbe | 1139 | if (fd < 0) |
0d9acb99 | 1140 | return 0; |
b5159817 | 1141 | |
8ca7b374 CB |
1142 | for (i = 0; i < 3; i++) |
1143 | if (!__terminal_dup2(fd, (int[]){STDIN_FILENO, STDOUT_FILENO, | |
1144 | STDERR_FILENO}[i])) | |
39a78bbe | 1145 | return -1; |
39a78bbe | 1146 | |
0d9acb99 DE |
1147 | return 0; |
1148 | } | |
b5159817 | 1149 | |
52f9292f | 1150 | int lxc_terminal_stdin_cb(int fd, uint32_t events, void *cbdata, |
3298b37d | 1151 | struct lxc_async_descr *descr) |
b5159817 | 1152 | { |
15085292 | 1153 | int ret; |
b5159817 | 1154 | char c; |
15085292 | 1155 | struct lxc_terminal_state *ts = cbdata; |
b5159817 | 1156 | |
97bc2422 | 1157 | if (fd != ts->stdinfd) |
a529bc25 | 1158 | return LXC_MAINLOOP_CLOSE; |
97bc2422 | 1159 | |
15085292 CB |
1160 | ret = lxc_read_nointr(ts->stdinfd, &c, 1); |
1161 | if (ret <= 0) | |
a529bc25 | 1162 | return LXC_MAINLOOP_CLOSE; |
63376d7d | 1163 | |
525e2117 | 1164 | if (ts->escape >= 1) { |
2083d59d | 1165 | /* we want to exit the terminal with Ctrl+a q */ |
014d5e1e CB |
1166 | if (c == ts->escape && !ts->saw_escape) { |
1167 | ts->saw_escape = 1; | |
15085292 | 1168 | return LXC_MAINLOOP_CONTINUE; |
014d5e1e | 1169 | } |
5c294060 | 1170 | |
014d5e1e | 1171 | if (c == 'q' && ts->saw_escape) |
a529bc25 | 1172 | return LXC_MAINLOOP_CLOSE; |
014d5e1e CB |
1173 | |
1174 | ts->saw_escape = 0; | |
1175 | } | |
63376d7d | 1176 | |
36a94ce8 | 1177 | ret = lxc_write_nointr(ts->ptxfd, &c, 1); |
15085292 | 1178 | if (ret <= 0) |
a529bc25 | 1179 | return LXC_MAINLOOP_CLOSE; |
b5159817 | 1180 | |
15085292 | 1181 | return LXC_MAINLOOP_CONTINUE; |
63376d7d DL |
1182 | } |
1183 | ||
36a94ce8 | 1184 | int lxc_terminal_ptx_cb(int fd, uint32_t events, void *cbdata, |
3298b37d | 1185 | struct lxc_async_descr *descr) |
63376d7d | 1186 | { |
0d4137cc | 1187 | int r, w; |
5bd171bd CB |
1188 | char buf[LXC_TERMINAL_BUFFER_SIZE]; |
1189 | struct lxc_terminal_state *ts = cbdata; | |
63376d7d | 1190 | |
36a94ce8 | 1191 | if (fd != ts->ptxfd) |
a529bc25 | 1192 | return LXC_MAINLOOP_CLOSE; |
97bc2422 | 1193 | |
e66b6c96 CB |
1194 | r = lxc_read_nointr(fd, buf, sizeof(buf)); |
1195 | if (r <= 0) | |
a529bc25 | 1196 | return LXC_MAINLOOP_CLOSE; |
1560f6c9 | 1197 | |
e66b6c96 | 1198 | w = lxc_write_nointr(ts->stdoutfd, buf, r); |
5bd171bd | 1199 | if (w <= 0 || w != r) |
a529bc25 | 1200 | return LXC_MAINLOOP_CLOSE; |
f78a1f32 | 1201 | |
5bd171bd | 1202 | return LXC_MAINLOOP_CONTINUE; |
b5159817 DE |
1203 | } |
1204 | ||
36a94ce8 | 1205 | int lxc_terminal_getfd(struct lxc_container *c, int *ttynum, int *ptxfd) |
b5159817 | 1206 | { |
7e85a2c4 | 1207 | return lxc_cmd_get_tty_fd(c->name, ttynum, ptxfd, c->config_path); |
b5159817 DE |
1208 | } |
1209 | ||
1210 | int lxc_console(struct lxc_container *c, int ttynum, | |
1211 | int stdinfd, int stdoutfd, int stderrfd, | |
1212 | int escape) | |
1213 | { | |
36a94ce8 | 1214 | int ptxfd, ret, ttyfd; |
3298b37d | 1215 | struct lxc_async_descr descr; |
b5159817 | 1216 | struct termios oldtios; |
5b55021f | 1217 | struct lxc_terminal_state *ts; |
28327a43 CB |
1218 | struct lxc_terminal terminal = { |
1219 | .tty_state = NULL, | |
1220 | }; | |
25964232 | 1221 | int istty = 0; |
b5159817 | 1222 | |
7e85a2c4 | 1223 | ttyfd = lxc_cmd_get_tty_fd(c->name, &ttynum, &ptxfd, c->config_path); |
6834f805 CB |
1224 | if (ttyfd < 0) |
1225 | return -1; | |
b5159817 DE |
1226 | |
1227 | ret = setsid(); | |
33b4b411 CB |
1228 | if (ret < 0) |
1229 | TRACE("Process is already group leader"); | |
b5159817 | 1230 | |
36a94ce8 | 1231 | ts = lxc_terminal_signal_init(stdinfd, ptxfd); |
b5159817 DE |
1232 | if (!ts) { |
1233 | ret = -1; | |
33b4b411 | 1234 | goto close_fds; |
b5159817 | 1235 | } |
28327a43 | 1236 | terminal.tty_state = ts; |
b5159817 | 1237 | ts->escape = escape; |
3b975060 | 1238 | ts->stdoutfd = stdoutfd; |
b5159817 | 1239 | |
6834f805 | 1240 | istty = isatty(stdinfd); |
25964232 | 1241 | if (istty) { |
36a94ce8 CB |
1242 | lxc_terminal_winsz(stdinfd, ptxfd); |
1243 | lxc_terminal_winsz(ts->stdinfd, ts->ptxfd); | |
6834f805 | 1244 | } else { |
71ac3f07 | 1245 | INFO("File descriptor %d does not refer to a terminal", stdinfd); |
25964232 | 1246 | } |
b5159817 DE |
1247 | |
1248 | ret = lxc_mainloop_open(&descr); | |
1249 | if (ret) { | |
33b4b411 CB |
1250 | ERROR("Failed to create mainloop"); |
1251 | goto sigwinch_fini; | |
b5159817 DE |
1252 | } |
1253 | ||
341c2aed CB |
1254 | if (ts->sigfd != -1) { |
1255 | ret = lxc_mainloop_add_handler(&descr, ts->sigfd, | |
543d2f83 CB |
1256 | lxc_terminal_signalfd_cb, |
1257 | default_cleanup_handler, | |
1258 | ts, "lxc_terminal_signalfd_cb"); | |
33b4b411 | 1259 | if (ret < 0) { |
0519b5cc | 1260 | ERROR("Failed to add signal handler to mainloop"); |
33b4b411 | 1261 | goto close_mainloop; |
341c2aed | 1262 | } |
b5159817 DE |
1263 | } |
1264 | ||
1265 | ret = lxc_mainloop_add_handler(&descr, ts->stdinfd, | |
543d2f83 CB |
1266 | lxc_terminal_stdin_cb, |
1267 | default_cleanup_handler, | |
1268 | ts, "lxc_terminal_stdin_cb"); | |
33b4b411 CB |
1269 | if (ret < 0) { |
1270 | ERROR("Failed to add stdin handler"); | |
1271 | goto close_mainloop; | |
b5159817 DE |
1272 | } |
1273 | ||
36a94ce8 | 1274 | ret = lxc_mainloop_add_handler(&descr, ts->ptxfd, |
543d2f83 CB |
1275 | lxc_terminal_ptx_cb, |
1276 | default_cleanup_handler, | |
1277 | ts, "lxc_terminal_ptx_cb"); | |
33b4b411 | 1278 | if (ret < 0) { |
36a94ce8 | 1279 | ERROR("Failed to add ptx handler"); |
33b4b411 | 1280 | goto close_mainloop; |
b5159817 DE |
1281 | } |
1282 | ||
686df166 CB |
1283 | if (ts->escape >= 1) { |
1284 | fprintf(stderr, | |
1285 | "\n" | |
6834f805 CB |
1286 | "Connected to tty %1$d\n" |
1287 | "Type <Ctrl+%2$c q> to exit the console, " | |
1288 | "<Ctrl+%2$c Ctrl+%2$c> to enter Ctrl+%2$c itself\n", | |
1289 | ttynum, 'a' + escape - 1); | |
686df166 | 1290 | } |
6834f805 CB |
1291 | |
1292 | if (istty) { | |
1293 | ret = lxc_setup_tios(stdinfd, &oldtios); | |
1294 | if (ret < 0) | |
1295 | goto close_mainloop; | |
1296 | } | |
1297 | ||
025ed0f3 | 1298 | ret = lxc_mainloop(&descr, -1); |
33b4b411 CB |
1299 | if (ret < 0) { |
1300 | ERROR("The mainloop returned an error"); | |
6834f805 | 1301 | goto restore_tios; |
b5159817 DE |
1302 | } |
1303 | ||
1304 | ret = 0; | |
1305 | ||
6834f805 CB |
1306 | restore_tios: |
1307 | if (istty) { | |
1308 | istty = tcsetattr(stdinfd, TCSAFLUSH, &oldtios); | |
1309 | if (istty < 0) | |
a24c5678 | 1310 | SYSWARN("Failed to restore terminal properties"); |
6834f805 CB |
1311 | } |
1312 | ||
33b4b411 | 1313 | close_mainloop: |
b5159817 | 1314 | lxc_mainloop_close(&descr); |
33b4b411 CB |
1315 | |
1316 | sigwinch_fini: | |
28327a43 | 1317 | lxc_terminal_signal_fini(&terminal); |
33b4b411 CB |
1318 | |
1319 | close_fds: | |
36a94ce8 | 1320 | close(ptxfd); |
b5159817 | 1321 | close(ttyfd); |
33b4b411 | 1322 | |
b5159817 | 1323 | return ret; |
63376d7d | 1324 | } |
e98affda | 1325 | |
cd0a2b2f | 1326 | int lxc_make_controlling_terminal(int fd) |
e98affda CB |
1327 | { |
1328 | int ret; | |
1329 | ||
1330 | setsid(); | |
1331 | ||
1332 | ret = ioctl(fd, TIOCSCTTY, (char *)NULL); | |
1333 | if (ret < 0) | |
1334 | return -1; | |
1335 | ||
1336 | return 0; | |
1337 | } | |
1338 | ||
d049f0e9 | 1339 | int lxc_terminal_prepare_login(int fd) |
e98affda CB |
1340 | { |
1341 | int ret; | |
1342 | ||
cd0a2b2f | 1343 | ret = lxc_make_controlling_terminal(fd); |
e98affda CB |
1344 | if (ret < 0) |
1345 | return -1; | |
1346 | ||
ae6d3913 | 1347 | ret = lxc_terminal_set_stdfds(fd); |
e98affda CB |
1348 | if (ret < 0) |
1349 | return -1; | |
1350 | ||
1351 | if (fd > STDERR_FILENO) | |
1352 | close(fd); | |
1353 | ||
1354 | return 0; | |
1355 | } | |
1356 | ||
e9a55b51 | 1357 | void lxc_terminal_info_init(struct lxc_terminal_info *terminal) |
e98affda | 1358 | { |
e9a55b51 | 1359 | terminal->name[0] = '\0'; |
36a94ce8 | 1360 | terminal->ptx = -EBADF; |
41808e20 | 1361 | terminal->pty = -EBADF; |
e9a55b51 | 1362 | terminal->busy = -1; |
e98affda CB |
1363 | } |
1364 | ||
e9a55b51 | 1365 | void lxc_terminal_init(struct lxc_terminal *terminal) |
e98affda | 1366 | { |
e9a55b51 | 1367 | memset(terminal, 0, sizeof(*terminal)); |
d926c261 | 1368 | terminal->pty_nr = -1; |
41808e20 | 1369 | terminal->pty = -EBADF; |
36a94ce8 | 1370 | terminal->ptx = -EBADF; |
e9a55b51 CB |
1371 | terminal->peer = -EBADF; |
1372 | terminal->log_fd = -EBADF; | |
1373 | lxc_terminal_info_init(&terminal->proxy); | |
e98affda CB |
1374 | } |
1375 | ||
dcad02f8 | 1376 | void lxc_terminal_conf_free(struct lxc_terminal *terminal) |
e98affda | 1377 | { |
2083d59d CB |
1378 | free(terminal->log_path); |
1379 | free(terminal->path); | |
1380 | if (terminal->buffer_size > 0 && terminal->ringbuf.addr) | |
1381 | lxc_ringbuf_release(&terminal->ringbuf); | |
28327a43 | 1382 | lxc_terminal_signal_fini(terminal); |
e98affda | 1383 | } |