]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/terminal.c
terminal: lxc_terminal_stdin_cb()
[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
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 55lxc_log_define(terminal, lxc);
36eb9bde 56
b5159817 57static struct lxc_list lxc_ttys;
724e753c 58
b5159817 59typedef 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 66void 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 90static 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 98void 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
109int 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 133struct 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
191on_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 204void 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 219static 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 228static 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 257static 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 363int 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 422static 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 448int 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 474int lxc_setup_tios(int fd, struct termios *oldtios)
b5159817 475{
e4953e62 476 int ret;
b5159817 477 struct termios newtios;
e0dc0de7 478
b5159817 479 if (!isatty(fd)) {
e4953e62 480 ERROR("File descriptor %d does not refert to a terminal", fd);
b5159817 481 return -1;
e0dc0de7
DL
482 }
483
e4953e62
CB
484 /* Get current termios. */
485 ret = tcgetattr(fd, oldtios);
486 if (ret < 0) {
487 SYSERROR("Failed to get current terminal settings");
b5159817 488 return -1;
e0dc0de7
DL
489 }
490
4dc96430
TJ
491 /* ensure we don't end up in an endless loop:
492 * The kernel might fire SIGTTOU while an
493 * ioctl() in tcsetattr() is executed. When the ioctl()
494 * is resumed and retries, the signal handler interrupts it again.
495 */
496 signal (SIGTTIN, SIG_IGN);
497 signal (SIGTTOU, SIG_IGN);
498
b5159817 499 newtios = *oldtios;
e0dc0de7 500
a7c97a40
CB
501 /* We use the same settings that ssh does. */
502 newtios.c_iflag |= IGNPAR;
503 newtios.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
504#ifdef IUCLC
505 newtios.c_iflag &= ~IUCLC;
506#endif
4dc96430 507 newtios.c_lflag &= ~(TOSTOP | ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
a7c97a40
CB
508#ifdef IEXTEN
509 newtios.c_lflag &= ~IEXTEN;
510#endif
d3893399 511 newtios.c_oflag &= ~OPOST;
b5159817
DE
512 newtios.c_cc[VMIN] = 1;
513 newtios.c_cc[VTIME] = 0;
e0dc0de7 514
a7c97a40 515 /* Set new attributes. */
e4953e62
CB
516 ret = tcsetattr(fd, TCSAFLUSH, &newtios);
517 if (ret < 0) {
518 ERROR("Failed to set new terminal settings");
b5159817 519 return -1;
e0dc0de7
DL
520 }
521
63376d7d 522 return 0;
b5159817 523}
e0dc0de7 524
dcad02f8 525static void lxc_terminal_peer_proxy_free(struct lxc_terminal *terminal)
b5159817 526{
2083d59d
CB
527 if (terminal->tty_state) {
528 lxc_terminal_signal_fini(terminal->tty_state);
529 terminal->tty_state = NULL;
b5159817 530 }
e788f4ac 531
fb87aa6a 532 close(terminal->proxy.master);
fb87aa6a 533 terminal->proxy.master = -1;
e788f4ac
CB
534
535 close(terminal->proxy.slave);
fb87aa6a 536 terminal->proxy.slave = -1;
e788f4ac 537
fb87aa6a 538 terminal->proxy.busy = -1;
e788f4ac 539
fb87aa6a 540 terminal->proxy.name[0] = '\0';
e788f4ac 541
2083d59d 542 terminal->peer = -1;
b5159817 543}
596a818d 544
60dd8ef4
CB
545static int lxc_terminal_peer_proxy_alloc(struct lxc_terminal *terminal,
546 int sockfd)
b5159817 547{
60dd8ef4 548 int ret;
b5159817 549 struct termios oldtermio;
5b55021f 550 struct lxc_terminal_state *ts;
b5159817 551
2083d59d
CB
552 if (terminal->master < 0) {
553 ERROR("Terminal not set up");
b5159817
DE
554 return -1;
555 }
60dd8ef4 556
fb87aa6a 557 if (terminal->proxy.busy != -1 || terminal->peer != -1) {
2083d59d 558 NOTICE("Terminal already in use");
b5159817
DE
559 return -1;
560 }
60dd8ef4 561
2083d59d 562 if (terminal->tty_state) {
60dd8ef4 563 ERROR("Terminal has already been initialized");
b5159817 564 return -1;
596a818d
DE
565 }
566
60dd8ef4
CB
567 /* This is the proxy pty that will be given to the client, and that
568 * the real pty master will send to / recv from.
b5159817 569 */
fb87aa6a 570 ret = openpty(&terminal->proxy.master, &terminal->proxy.slave,
60dd8ef4
CB
571 terminal->proxy.name, NULL, NULL);
572 if (ret < 0) {
573 SYSERROR("Failed to open proxy terminal");
b5159817
DE
574 return -1;
575 }
596a818d 576
60dd8ef4
CB
577 ret = lxc_setup_tios(terminal->proxy.slave, &oldtermio);
578 if (ret < 0)
579 goto on_error;
b5159817 580
fb87aa6a 581 ts = lxc_terminal_signal_init(terminal->proxy.master, terminal->master);
b5159817 582 if (!ts)
60dd8ef4 583 goto on_error;
b5159817 584
2083d59d 585 terminal->tty_state = ts;
fb87aa6a
CB
586 terminal->peer = terminal->proxy.slave;
587 terminal->proxy.busy = sockfd;
2083d59d 588 ret = lxc_terminal_mainloop_add_peer(terminal);
a529bc25 589 if (ret < 0)
60dd8ef4 590 goto on_error;
b5159817 591
60dd8ef4
CB
592 NOTICE("Opened proxy terminal with master fd %d and slave fd %d",
593 terminal->proxy.master, terminal->proxy.slave);
b5159817
DE
594 return 0;
595
60dd8ef4 596on_error:
2083d59d 597 lxc_terminal_peer_proxy_free(terminal);
63376d7d
DL
598 return -1;
599}
600
c1ee47cd 601int lxc_terminal_allocate(struct lxc_conf *conf, int sockfd, int *ttyreq)
b5159817 602{
12c2eaaa
CB
603 int ttynum;
604 int masterfd = -1;
0e4be3cf 605 struct lxc_tty_info *ttys = &conf->ttys;
dcad02f8 606 struct lxc_terminal *terminal = &conf->console;
b5159817 607
b5159817 608 if (*ttyreq == 0) {
12c2eaaa
CB
609 int ret;
610
611 ret = lxc_terminal_peer_proxy_alloc(terminal, sockfd);
612 if (ret < 0)
b5159817 613 goto out;
12c2eaaa 614
fb87aa6a 615 masterfd = terminal->proxy.master;
b5159817
DE
616 goto out;
617 }
618
619 if (*ttyreq > 0) {
0e4be3cf 620 if (*ttyreq > ttys->nbtty)
b5159817
DE
621 goto out;
622
0e4be3cf 623 if (ttys->tty[*ttyreq - 1].busy)
b5159817
DE
624 goto out;
625
12c2eaaa 626 /* The requested tty is available. */
b5159817
DE
627 ttynum = *ttyreq;
628 goto out_tty;
629 }
630
12c2eaaa
CB
631 /* Search for next available tty, fixup index tty1 => [0]. */
632 for (ttynum = 1; ttynum <= ttys->nbtty && ttys->tty[ttynum - 1].busy; ttynum++) {
0d4137cc 633 ;
12c2eaaa 634 }
b5159817 635
12c2eaaa 636 /* We didn't find any available slot for tty. */
0e4be3cf 637 if (ttynum > ttys->nbtty)
b5159817
DE
638 goto out;
639
640 *ttyreq = ttynum;
641
642out_tty:
0e4be3cf
CB
643 ttys->tty[ttynum - 1].busy = sockfd;
644 masterfd = ttys->tty[ttynum - 1].master;
12c2eaaa 645
b5159817 646out:
b5159817
DE
647 return masterfd;
648}
649
3dfe6f8d 650void lxc_terminal_free(struct lxc_conf *conf, int fd)
63376d7d 651{
b5159817 652 int i;
0e4be3cf 653 struct lxc_tty_info *ttys = &conf->ttys;
dcad02f8 654 struct lxc_terminal *terminal = &conf->console;
b5159817 655
1b5e93c4 656 for (i = 0; i < ttys->nbtty; i++)
0e4be3cf
CB
657 if (ttys->tty[i].busy == fd)
658 ttys->tty[i].busy = 0;
b5159817 659
1b5e93c4
CB
660 if (terminal->proxy.busy != fd)
661 return;
662
663 lxc_mainloop_del_handler(terminal->descr, terminal->proxy.slave);
664 lxc_terminal_peer_proxy_free(terminal);
b5159817
DE
665}
666
dcad02f8 667static int lxc_terminal_peer_default(struct lxc_terminal *terminal)
b5159817 668{
5b55021f 669 struct lxc_terminal_state *ts;
2083d59d 670 const char *path = terminal->path;
467c7ff3
CB
671 int fd;
672 int ret = 0;
b5159817 673
49cd0656
CB
674 if (!path) {
675 ret = access("/dev/tty", F_OK);
676 if (ret == 0) {
677 /* If no terminal was given, try current controlling
678 * terminal, there won't be one if we were started as a
679 * daemon (-d).
680 */
681 fd = open("/dev/tty", O_RDWR);
682 if (fd >= 0) {
683 close(fd);
684 path = "/dev/tty";
685 }
b5159817
DE
686 }
687 }
688
467c7ff3
CB
689 if (!path) {
690 errno = ENOTTY;
49cd0656
CB
691 DEBUG("Theh process does not have a controlling terminal");
692 goto on_succes;
467c7ff3 693 }
b5159817 694
2083d59d
CB
695 terminal->peer = lxc_unpriv(open(path, O_RDWR | O_CLOEXEC));
696 if (terminal->peer < 0) {
49cd0656
CB
697 ERROR("%s - Failed to open proxy terminal \"%s\"",
698 strerror(errno), path);
467c7ff3
CB
699 return -ENOTTY;
700 }
49cd0656 701 DEBUG("Using terminal \"%s\" as proxy", path);
b5159817 702
2083d59d 703 if (!isatty(terminal->peer)) {
49cd0656
CB
704 ERROR("File descriptor for \"%s\" does not refer to a terminal", path);
705 goto on_error_free_tios;
467c7ff3 706 }
b5159817 707
2083d59d
CB
708 ts = lxc_terminal_signal_init(terminal->peer, terminal->master);
709 terminal->tty_state = ts;
341c2aed 710 if (!ts) {
0519b5cc 711 WARN("Failed to install signal handler");
49cd0656 712 goto on_error_free_tios;
341c2aed 713 }
b5159817 714
2083d59d 715 lxc_terminal_winsz(terminal->peer, terminal->master);
b5159817 716
2083d59d 717 terminal->tios = malloc(sizeof(*terminal->tios));
49cd0656
CB
718 if (!terminal->tios)
719 goto on_error_free_tios;
b5159817 720
49cd0656
CB
721 ret = lxc_setup_tios(terminal->peer, terminal->tios);
722 if (ret < 0)
723 goto on_error_close_peer;
467c7ff3 724 else
49cd0656 725 goto on_succes;
b5159817 726
49cd0656 727on_error_free_tios:
2083d59d
CB
728 free(terminal->tios);
729 terminal->tios = NULL;
467c7ff3 730
49cd0656 731on_error_close_peer:
2083d59d
CB
732 close(terminal->peer);
733 terminal->peer = -1;
467c7ff3
CB
734 ret = -ENOTTY;
735
49cd0656 736on_succes:
467c7ff3 737 return ret;
b5159817
DE
738}
739
dcad02f8 740int lxc_terminal_write_ringbuffer(struct lxc_terminal *terminal)
39c6cdb7
CB
741{
742 char *r_addr;
743 ssize_t ret;
744 uint64_t used;
2083d59d 745 struct lxc_ringbuf *buf = &terminal->ringbuf;
39c6cdb7
CB
746
747 /* There's not log file where we can dump the ringbuffer to. */
2083d59d 748 if (terminal->log_fd < 0)
39c6cdb7
CB
749 return 0;
750
39c6cdb7
CB
751 used = lxc_ringbuf_used(buf);
752 if (used == 0)
753 return 0;
754
2083d59d 755 ret = lxc_terminal_truncate_log_file(terminal);
39c6cdb7
CB
756 if (ret < 0)
757 return ret;
758
759 /* Write as much as we can without exceeding the limit. */
2083d59d
CB
760 if (terminal->log_size < used)
761 used = terminal->log_size;
39c6cdb7
CB
762
763 r_addr = lxc_ringbuf_get_read_addr(buf);
2083d59d 764 ret = lxc_write_nointr(terminal->log_fd, r_addr, used);
39c6cdb7
CB
765 if (ret < 0)
766 return -EIO;
767
768 return 0;
769}
770
dcad02f8 771void lxc_terminal_delete(struct lxc_terminal *terminal)
b5159817 772{
69629c82
CB
773 int ret;
774
2083d59d 775 ret = lxc_terminal_write_ringbuffer(terminal);
39c6cdb7 776 if (ret < 0)
2083d59d 777 WARN("Failed to write terminal log to disk");
39c6cdb7 778
2083d59d
CB
779 if (terminal->tios && terminal->peer >= 0) {
780 ret = tcsetattr(terminal->peer, TCSAFLUSH, terminal->tios);
69629c82
CB
781 if (ret < 0)
782 WARN("%s - Failed to set old terminal settings", strerror(errno));
783 }
2083d59d
CB
784 free(terminal->tios);
785 terminal->tios = NULL;
596a818d 786
2083d59d
CB
787 if (terminal->peer >= 0)
788 close(terminal->peer);
789 terminal->peer = -1;
bc9724f7 790
2083d59d
CB
791 if (terminal->master >= 0)
792 close(terminal->master);
793 terminal->master = -1;
bc9724f7 794
2083d59d
CB
795 if (terminal->slave >= 0)
796 close(terminal->slave);
797 terminal->slave = -1;
bc9724f7 798
2083d59d
CB
799 if (terminal->log_fd >= 0)
800 close(terminal->log_fd);
801 terminal->log_fd = -1;
63376d7d
DL
802}
803
3b988b33
CB
804/**
805 * Note that this function needs to run before the mainloop starts. Since we
2083d59d
CB
806 * register a handler for the terminal's masterfd when we create the mainloop
807 * the terminal handler needs to see an allocated ringbuffer.
3b988b33 808 */
dcad02f8 809static int lxc_terminal_create_ringbuf(struct lxc_terminal *terminal)
3b988b33
CB
810{
811 int ret;
2083d59d
CB
812 struct lxc_ringbuf *buf = &terminal->ringbuf;
813 uint64_t size = terminal->buffer_size;
3b988b33
CB
814
815 /* no ringbuffer previously allocated and no ringbuffer requested */
816 if (!buf->addr && size <= 0)
817 return 0;
818
819 /* ringbuffer allocated but no new ringbuffer requested */
820 if (buf->addr && size <= 0) {
821 lxc_ringbuf_release(buf);
822 buf->addr = NULL;
823 buf->r_off = 0;
824 buf->w_off = 0;
825 buf->size = 0;
2083d59d 826 TRACE("Deallocated terminal ringbuffer");
3b988b33
CB
827 return 0;
828 }
829
830 if (size <= 0)
831 return 0;
832
833 /* check wether the requested size for the ringbuffer has changed */
834 if (buf->addr && buf->size != size) {
2083d59d
CB
835 TRACE("Terminal ringbuffer size changed from %" PRIu64
836 " to %" PRIu64 " bytes. Deallocating terminal ringbuffer",
3b988b33
CB
837 buf->size, size);
838 lxc_ringbuf_release(buf);
839 }
840
841 ret = lxc_ringbuf_create(buf, size);
842 if (ret < 0) {
2083d59d 843 ERROR("Failed to setup %" PRIu64 " byte terminal ringbuffer", size);
3b988b33
CB
844 return -1;
845 }
846
2083d59d 847 TRACE("Allocated %" PRIu64 " byte terminal ringbuffer", size);
3b988b33
CB
848 return 0;
849}
850
a0309168 851/**
2083d59d
CB
852 * This is the terminal log file. Please note that the terminal log file is
853 * (implementation wise not content wise) independent of the terminal ringbuffer.
a0309168 854 */
dcad02f8 855int lxc_terminal_create_log_file(struct lxc_terminal *terminal)
a0309168 856{
2083d59d 857 if (!terminal->log_path)
a0309168
CB
858 return 0;
859
2083d59d
CB
860 terminal->log_fd = lxc_unpriv(open(terminal->log_path, O_CLOEXEC | O_RDWR | O_CREAT | O_APPEND, 0600));
861 if (terminal->log_fd < 0) {
862 SYSERROR("Failed to open terminal log file \"%s\"", terminal->log_path);
a0309168
CB
863 return -1;
864 }
865
2083d59d 866 DEBUG("Using \"%s\" as terminal log file", terminal->log_path);
a0309168
CB
867 return 0;
868}
869
dcad02f8 870int lxc_terminal_create(struct lxc_terminal *terminal)
63376d7d 871{
8ded9244 872 int ret;
b5159817 873
8ded9244 874 ret = openpty(&terminal->master, &terminal->slave, terminal->name, NULL, NULL);
467c7ff3 875 if (ret < 0) {
8ded9244 876 SYSERROR("Failed to open terminal");
b5159817
DE
877 return -1;
878 }
879
2083d59d 880 ret = fcntl(terminal->master, F_SETFD, FD_CLOEXEC);
69629c82 881 if (ret < 0) {
2083d59d 882 SYSERROR("Failed to set FD_CLOEXEC flag on terminal master");
b5159817
DE
883 goto err;
884 }
885
2083d59d 886 ret = fcntl(terminal->slave, F_SETFD, FD_CLOEXEC);
69629c82 887 if (ret < 0) {
2083d59d 888 SYSERROR("Failed to set FD_CLOEXEC flag on terminal slave");
b5159817
DE
889 goto err;
890 }
891
2083d59d 892 ret = lxc_terminal_peer_default(terminal);
467c7ff3 893 if (ret < 0) {
8ded9244 894 ERROR("Failed to allocate proxy terminal");
467c7ff3
CB
895 goto err;
896 }
b5159817 897
5777fe90
CB
898 return 0;
899
900err:
2083d59d 901 lxc_terminal_delete(terminal);
5777fe90
CB
902 return -ENODEV;
903}
904
564e31c4 905int lxc_terminal_setup(struct lxc_conf *conf)
5777fe90
CB
906{
907 int ret;
dcad02f8 908 struct lxc_terminal *terminal = &conf->console;
5777fe90 909
1a443ac1
CB
910 if (terminal->path && strcmp(terminal->path, "none") == 0) {
911 INFO("No terminal requested");
5777fe90
CB
912 return 0;
913 }
914
96eee564 915 ret = lxc_terminal_create(terminal);
5777fe90
CB
916 if (ret < 0)
917 return -1;
918
2083d59d 919 ret = lxc_terminal_create_log_file(terminal);
a0309168
CB
920 if (ret < 0)
921 goto err;
922
2083d59d 923 ret = lxc_terminal_create_ringbuf(terminal);
a0309168
CB
924 if (ret < 0)
925 goto err;
b5159817
DE
926
927 return 0;
928
929err:
2083d59d 930 lxc_terminal_delete(terminal);
69629c82 931 return -ENODEV;
b5159817
DE
932}
933
8ca7b374
CB
934static bool __terminal_dup2(int duplicate, int original)
935{
936 int ret;
937
938 if (!isatty(original))
939 return true;
940
941 ret = dup2(duplicate, original);
942 if (ret < 0) {
943 SYSERROR("Failed to dup2(%d, %d)", duplicate, original);
944 return false;
945 }
946
947 return true;
948}
949
ae6d3913 950int lxc_terminal_set_stdfds(int fd)
0d9acb99 951{
8ca7b374
CB
952 int i;
953
39a78bbe 954 if (fd < 0)
0d9acb99 955 return 0;
b5159817 956
8ca7b374
CB
957 for (i = 0; i < 3; i++)
958 if (!__terminal_dup2(fd, (int[]){STDIN_FILENO, STDOUT_FILENO,
959 STDERR_FILENO}[i]))
39a78bbe 960 return -1;
39a78bbe 961
0d9acb99
DE
962 return 0;
963}
b5159817 964
52f9292f 965int lxc_terminal_stdin_cb(int fd, uint32_t events, void *cbdata,
15085292 966 struct lxc_epoll_descr *descr)
b5159817 967{
15085292 968 int ret;
b5159817 969 char c;
15085292 970 struct lxc_terminal_state *ts = cbdata;
b5159817 971
97bc2422 972 if (fd != ts->stdinfd)
a529bc25 973 return LXC_MAINLOOP_CLOSE;
97bc2422 974
15085292
CB
975 ret = lxc_read_nointr(ts->stdinfd, &c, 1);
976 if (ret <= 0)
a529bc25 977 return LXC_MAINLOOP_CLOSE;
63376d7d 978
525e2117 979 if (ts->escape >= 1) {
2083d59d 980 /* we want to exit the terminal with Ctrl+a q */
014d5e1e
CB
981 if (c == ts->escape && !ts->saw_escape) {
982 ts->saw_escape = 1;
15085292 983 return LXC_MAINLOOP_CONTINUE;
014d5e1e 984 }
5c294060 985
014d5e1e 986 if (c == 'q' && ts->saw_escape)
a529bc25 987 return LXC_MAINLOOP_CLOSE;
014d5e1e
CB
988
989 ts->saw_escape = 0;
990 }
63376d7d 991
15085292
CB
992 ret = lxc_write_nointr(ts->masterfd, &c, 1);
993 if (ret <= 0)
a529bc25 994 return LXC_MAINLOOP_CLOSE;
b5159817 995
15085292 996 return LXC_MAINLOOP_CONTINUE;
63376d7d
DL
997}
998
ee9102ff
CB
999int lxc_terminal_master_cb(int fd, uint32_t events, void *cbdata,
1000 struct lxc_epoll_descr *descr)
63376d7d 1001{
5b55021f 1002 struct lxc_terminal_state *ts = cbdata;
de708fb7 1003 char buf[LXC_TERMINAL_BUFFER_SIZE];
0d4137cc 1004 int r, w;
63376d7d 1005
97bc2422 1006 if (fd != ts->masterfd)
a529bc25 1007 return LXC_MAINLOOP_CLOSE;
97bc2422 1008
e66b6c96
CB
1009 r = lxc_read_nointr(fd, buf, sizeof(buf));
1010 if (r <= 0)
a529bc25 1011 return LXC_MAINLOOP_CLOSE;
1560f6c9 1012
e66b6c96
CB
1013 w = lxc_write_nointr(ts->stdoutfd, buf, r);
1014 if (w <= 0) {
a529bc25 1015 return LXC_MAINLOOP_CLOSE;
e66b6c96 1016 } else if (w != r) {
a529bc25 1017 SYSERROR("Failed to write");
b5159817 1018 return 1;
f78a1f32
DL
1019 }
1020
b5159817
DE
1021 return 0;
1022}
1023
c86e2584 1024int lxc_terminal_getfd(struct lxc_container *c, int *ttynum, int *masterfd)
b5159817
DE
1025{
1026 return lxc_cmd_console(c->name, ttynum, masterfd, c->config_path);
1027}
1028
1029int lxc_console(struct lxc_container *c, int ttynum,
1030 int stdinfd, int stdoutfd, int stderrfd,
1031 int escape)
1032{
1033 int ret, ttyfd, masterfd;
1034 struct lxc_epoll_descr descr;
1035 struct termios oldtios;
5b55021f 1036 struct lxc_terminal_state *ts;
25964232 1037 int istty = 0;
b5159817 1038
b5159817 1039 ttyfd = lxc_cmd_console(c->name, &ttynum, &masterfd, c->config_path);
6834f805
CB
1040 if (ttyfd < 0)
1041 return -1;
b5159817
DE
1042
1043 ret = setsid();
33b4b411
CB
1044 if (ret < 0)
1045 TRACE("Process is already group leader");
b5159817 1046
dc8c7883 1047 ts = lxc_terminal_signal_init(stdinfd, masterfd);
b5159817
DE
1048 if (!ts) {
1049 ret = -1;
33b4b411 1050 goto close_fds;
b5159817
DE
1051 }
1052 ts->escape = escape;
1053 ts->winch_proxy = c->name;
1054 ts->winch_proxy_lxcpath = c->config_path;
3b975060 1055 ts->stdoutfd = stdoutfd;
b5159817 1056
6834f805 1057 istty = isatty(stdinfd);
25964232 1058 if (istty) {
4e9c0330 1059 lxc_terminal_winsz(stdinfd, masterfd);
8ccbbf94 1060 lxc_cmd_terminal_winch(ts->winch_proxy, ts->winch_proxy_lxcpath);
6834f805
CB
1061 } else {
1062 INFO("File descriptor %d does not refer to a tty device", stdinfd);
25964232 1063 }
b5159817
DE
1064
1065 ret = lxc_mainloop_open(&descr);
1066 if (ret) {
33b4b411
CB
1067 ERROR("Failed to create mainloop");
1068 goto sigwinch_fini;
b5159817
DE
1069 }
1070
341c2aed
CB
1071 if (ts->sigfd != -1) {
1072 ret = lxc_mainloop_add_handler(&descr, ts->sigfd,
9bafc8cb 1073 lxc_terminal_signalfd_cb, ts);
33b4b411 1074 if (ret < 0) {
0519b5cc 1075 ERROR("Failed to add signal handler to mainloop");
33b4b411 1076 goto close_mainloop;
341c2aed 1077 }
b5159817
DE
1078 }
1079
1080 ret = lxc_mainloop_add_handler(&descr, ts->stdinfd,
52f9292f 1081 lxc_terminal_stdin_cb, ts);
33b4b411
CB
1082 if (ret < 0) {
1083 ERROR("Failed to add stdin handler");
1084 goto close_mainloop;
b5159817
DE
1085 }
1086
1087 ret = lxc_mainloop_add_handler(&descr, ts->masterfd,
ee9102ff 1088 lxc_terminal_master_cb, ts);
33b4b411
CB
1089 if (ret < 0) {
1090 ERROR("Failed to add master handler");
1091 goto close_mainloop;
b5159817
DE
1092 }
1093
686df166
CB
1094 if (ts->escape >= 1) {
1095 fprintf(stderr,
1096 "\n"
6834f805
CB
1097 "Connected to tty %1$d\n"
1098 "Type <Ctrl+%2$c q> to exit the console, "
1099 "<Ctrl+%2$c Ctrl+%2$c> to enter Ctrl+%2$c itself\n",
1100 ttynum, 'a' + escape - 1);
686df166 1101 }
6834f805
CB
1102
1103 if (istty) {
1104 ret = lxc_setup_tios(stdinfd, &oldtios);
1105 if (ret < 0)
1106 goto close_mainloop;
1107 }
1108
025ed0f3 1109 ret = lxc_mainloop(&descr, -1);
33b4b411
CB
1110 if (ret < 0) {
1111 ERROR("The mainloop returned an error");
6834f805 1112 goto restore_tios;
b5159817
DE
1113 }
1114
1115 ret = 0;
1116
6834f805
CB
1117restore_tios:
1118 if (istty) {
1119 istty = tcsetattr(stdinfd, TCSAFLUSH, &oldtios);
1120 if (istty < 0)
1121 WARN("%s - Failed to restore terminal properties",
1122 strerror(errno));
1123 }
1124
33b4b411 1125close_mainloop:
b5159817 1126 lxc_mainloop_close(&descr);
33b4b411
CB
1127
1128sigwinch_fini:
a8867251 1129 lxc_terminal_signal_fini(ts);
33b4b411
CB
1130
1131close_fds:
b5159817
DE
1132 close(masterfd);
1133 close(ttyfd);
33b4b411 1134
b5159817 1135 return ret;
63376d7d 1136}
e98affda
CB
1137
1138int lxc_make_controlling_pty(int fd)
1139{
1140 int ret;
1141
1142 setsid();
1143
1144 ret = ioctl(fd, TIOCSCTTY, (char *)NULL);
1145 if (ret < 0)
1146 return -1;
1147
1148 return 0;
1149}
1150
1151int lxc_login_pty(int fd)
1152{
1153 int ret;
1154
1155 ret = lxc_make_controlling_pty(fd);
1156 if (ret < 0)
1157 return -1;
1158
ae6d3913 1159 ret = lxc_terminal_set_stdfds(fd);
e98affda
CB
1160 if (ret < 0)
1161 return -1;
1162
1163 if (fd > STDERR_FILENO)
1164 close(fd);
1165
1166 return 0;
1167}
1168
99a04585 1169void lxc_terminal_info_init(struct lxc_terminal_info *pty)
e98affda
CB
1170{
1171 pty->name[0] = '\0';
1172 pty->master = -EBADF;
1173 pty->slave = -EBADF;
1174 pty->busy = -1;
1175}
1176
dcad02f8 1177void lxc_terminal_init(struct lxc_terminal *pty)
e98affda
CB
1178{
1179 memset(pty, 0, sizeof(*pty));
1180 pty->slave = -EBADF;
1181 pty->master = -EBADF;
1182 pty->peer = -EBADF;
1183 pty->log_fd = -EBADF;
fb87aa6a 1184 lxc_terminal_info_init(&pty->proxy);
e98affda
CB
1185}
1186
dcad02f8 1187void lxc_terminal_conf_free(struct lxc_terminal *terminal)
e98affda 1188{
2083d59d
CB
1189 free(terminal->log_path);
1190 free(terminal->path);
1191 if (terminal->buffer_size > 0 && terminal->ringbuf.addr)
1192 lxc_ringbuf_release(&terminal->ringbuf);
e98affda 1193}
7cfeddd7 1194
dcad02f8 1195int lxc_terminal_map_ids(struct lxc_conf *c, struct lxc_terminal *pty)
7cfeddd7
CB
1196{
1197 int ret;
1198
1199 if (lxc_list_empty(&c->id_map))
1200 return 0;
1201
1202 ret = strcmp(pty->name, "");
1203 if (ret == 0)
1204 return 0;
1205
1206 ret = chown_mapped_root(pty->name, c);
1207 if (ret < 0) {
1208 ERROR("Failed to chown \"%s\"", pty->name);
1209 return -1;
1210 }
1211
1212 TRACE("Chowned \"%s\"", pty->name);
1213
1214 return 0;
1215}