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