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