chiark / gitweb /
nspawn: make tty code more robust against closed/reopened /dev/console
[elogind.git] / src / nspawn.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <signal.h>
23 #include <sched.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/syscall.h>
27 #include <sys/mount.h>
28 #include <sys/wait.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <errno.h>
33 #include <sys/prctl.h>
34 #include <sys/capability.h>
35 #include <getopt.h>
36 #include <sys/epoll.h>
37 #include <termios.h>
38 #include <sys/signalfd.h>
39
40 #include "log.h"
41 #include "util.h"
42 #include "missing.h"
43 #include "cgroup-util.h"
44 #include "sd-daemon.h"
45 #include "strv.h"
46
47 static char *arg_directory = NULL;
48
49 static int help(void) {
50
51         printf("%s [OPTIONS...] [PATH] [ARGUMENTS...]\n\n"
52                "Spawn a minimal namespace container for debugging, testing and building.\n\n"
53                "  -h --help            Show this help\n"
54                "  -D --directory=NAME  Root directory for the container\n",
55                program_invocation_short_name);
56
57         return 0;
58 }
59
60 static int parse_argv(int argc, char *argv[]) {
61
62         static const struct option options[] = {
63                 { "help",      no_argument,       NULL, 'h' },
64                 { "directory", required_argument, NULL, 'D' },
65                 { NULL,        0,                 NULL, 0   }
66         };
67
68         int c;
69
70         assert(argc >= 0);
71         assert(argv);
72
73         while ((c = getopt_long(argc, argv, "+hD:", options, NULL)) >= 0) {
74
75                 switch (c) {
76
77                 case 'h':
78                         help();
79                         return 0;
80
81                 case 'D':
82                         free(arg_directory);
83                         if (!(arg_directory = strdup(optarg))) {
84                                 log_error("Failed to duplicate root directory.");
85                                 return -ENOMEM;
86                         }
87
88                         break;
89
90                 case '?':
91                         return -EINVAL;
92
93                 default:
94                         log_error("Unknown option code %c", c);
95                         return -EINVAL;
96                 }
97         }
98
99         return 1;
100 }
101
102 static int mount_all(const char *dest) {
103
104         typedef struct MountPoint {
105                 const char *what;
106                 const char *where;
107                 const char *type;
108                 const char *options;
109                 unsigned long flags;
110                 bool fatal;
111         } MountPoint;
112
113         static const MountPoint mount_table[] = {
114                 { "proc",      "/proc",     "proc",      NULL,        MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
115                 { "/proc/sys", "/proc/sys", "bind",      NULL,        MS_BIND, true },                      /* Bind mount first */
116                 { "/proc/sys", "/proc/sys", "bind",      NULL,        MS_BIND|MS_RDONLY|MS_REMOUNT, true }, /* Then, make it r/o */
117                 { "sysfs",     "/sys",      "sysfs",     NULL,        MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, true },
118                 { "tmpfs",     "/dev",      "tmpfs",     "mode=755",  MS_NOSUID, true },
119                 { "/dev/pts",  "/dev/pts",  "bind",      NULL,        MS_BIND, true },
120                 { "tmpfs",     "/dev/.run", "tmpfs",     "mode=755",  MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
121 #ifdef HAVE_SELINUX
122                 { "selinux",   "/selinux",  "selinuxfs", NULL,        MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, false },
123 #endif
124         };
125
126         unsigned k;
127         int r = 0;
128
129         for (k = 0; k < ELEMENTSOF(mount_table); k++) {
130                 char *where;
131                 int t;
132
133                 if (asprintf(&where, "%s/%s", dest, mount_table[k].where) < 0) {
134                         log_error("Out of memory");
135
136                         if (r == 0)
137                                 r = -ENOMEM;
138
139                         break;
140                 }
141
142                 if ((t = path_is_mount_point(where)) < 0) {
143                         log_error("Failed to detect whether %s is a mount point: %s", where, strerror(-t));
144                         free(where);
145
146                         if (r == 0)
147                                 r = t;
148
149                         continue;
150                 }
151
152                 mkdir_p(where, 0755);
153
154                 if (mount(mount_table[k].what,
155                           where,
156                           mount_table[k].type,
157                           mount_table[k].flags,
158                           mount_table[k].options) < 0 &&
159                     mount_table[k].fatal) {
160
161                         log_error("mount(%s) failed: %m", where);
162
163                         if (r == 0)
164                                 r = -errno;
165                 }
166
167                 free(where);
168         }
169
170         return r;
171 }
172
173 static int copy_devnodes(const char *dest, const char *console) {
174
175         static const char devnodes[] =
176                 "null\0"
177                 "zero\0"
178                 "full\0"
179                 "random\0"
180                 "urandom\0"
181                 "tty\0"
182                 "ptmx\0"
183                 "kmsg\0"
184                 "rtc0\0";
185
186         const char *d;
187         int r = 0, k;
188         mode_t u;
189         struct stat st;
190         char *from = NULL, *to = NULL;
191
192         assert(dest);
193         assert(console);
194
195         u = umask(0000);
196
197         NULSTR_FOREACH(d, devnodes) {
198                 from = to = NULL;
199
200                 asprintf(&from, "/dev/%s", d);
201                 asprintf(&to, "%s/dev/%s", dest, d);
202
203                 if (!from || !to) {
204                         log_error("Failed to allocate devnode path");
205
206                         free(from);
207                         free(to);
208
209                         from = to = NULL;
210
211                         if (r == 0)
212                                 r = -ENOMEM;
213
214                         break;
215                 }
216
217                 if (stat(from, &st) < 0) {
218
219                         if (errno != ENOENT) {
220                                 log_error("Failed to stat %s: %m", from);
221                                 if (r == 0)
222                                         r = -errno;
223                         }
224
225                 } else if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) {
226
227                         log_error("%s is not a char or block device, cannot copy.", from);
228                         if (r == 0)
229                                 r = -EIO;
230
231                 } else if (mknod(to, st.st_mode, st.st_rdev) < 0) {
232
233                         log_error("mknod(%s) failed: %m", dest);
234                         if (r == 0)
235                                 r = -errno;
236                 }
237
238                 free(from);
239                 free(to);
240         }
241
242         if (stat(console, &st) < 0) {
243
244                 log_error("Failed to stat %s: %m", console);
245                 if (r == 0)
246                         r = -errno;
247
248                 goto finish;
249
250         } else if (!S_ISCHR(st.st_mode)) {
251
252                 log_error("/dev/console is not a char device.");
253                 if (r == 0)
254                         r = -EIO;
255
256                 goto finish;
257         }
258
259         if (asprintf(&to, "%s/dev/console", dest) < 0) {
260
261                 log_error("Out of memory");
262                 if (r == 0)
263                         r = -ENOMEM;
264
265                  goto finish;
266         }
267
268         /* We need to bind mount the right tty to /dev/console since
269          * ptys can only exist on pts file systems. To have something
270          * to bind mount things on we create a device node first, that
271          * has the right major/minor (note that the major minor
272          * doesn't actually matter here, since we mount it over
273          * anyway). */
274
275         if (mknod(to, (st.st_mode & ~07777) | 0600, st.st_rdev) < 0)
276                 log_error("mknod for /dev/console failed: %m");
277
278         if (mount(console, to, "bind", MS_BIND, NULL) < 0) {
279                 log_error("bind mount for /dev/console failed: %m");
280
281                 if (r == 0)
282                         r = -errno;
283         }
284
285         free(to);
286
287         if ((k = chmod_and_chown(console, 0600, 0, 0)) < 0) {
288                 log_error("Failed to correct access mode for TTY: %s", strerror(-k));
289
290                 if (r == 0)
291                         r = k;
292         }
293
294 finish:
295
296         umask(u);
297
298         return r;
299 }
300
301 static int drop_capabilities(void) {
302         static const unsigned long retain[] = {
303                 CAP_CHOWN,
304                 CAP_DAC_OVERRIDE,
305                 CAP_DAC_READ_SEARCH,
306                 CAP_FOWNER,
307                 CAP_FSETID,
308                 CAP_IPC_OWNER,
309                 CAP_KILL,
310                 CAP_LEASE,
311                 CAP_LINUX_IMMUTABLE,
312                 CAP_NET_BIND_SERVICE,
313                 CAP_NET_BROADCAST,
314                 CAP_NET_RAW,
315                 CAP_SETGID,
316                 CAP_SETFCAP,
317                 CAP_SETPCAP,
318                 CAP_SETUID,
319                 CAP_SYS_ADMIN,
320                 CAP_SYS_CHROOT,
321                 CAP_SYS_NICE,
322                 CAP_SYS_PTRACE,
323                 CAP_SYS_TTY_CONFIG
324         };
325
326         unsigned long l;
327
328         for (l = 0; l <= MAX(63LU, (unsigned long) CAP_LAST_CAP); l ++) {
329                 unsigned i;
330
331                 for (i = 0; i < ELEMENTSOF(retain); i++)
332                         if (retain[i] == l)
333                                 break;
334
335                 if (i < ELEMENTSOF(retain))
336                         continue;
337
338                 if (prctl(PR_CAPBSET_DROP, l) < 0) {
339
340                         /* If this capability is not known, EINVAL
341                          * will be returned, let's ignore this. */
342                         if (errno == EINVAL)
343                                 continue;
344
345                         log_error("PR_CAPBSET_DROP failed: %m");
346                         return -errno;
347                 }
348         }
349
350         return 0;
351 }
352
353 static int is_os_tree(const char *path) {
354         int r;
355         char *p;
356         /* We use /bin/sh as flag file if something is an OS */
357
358         if (asprintf(&p, "%s/bin/sh", path) < 0)
359                 return -ENOMEM;
360
361         r = access(p, F_OK);
362         free(p);
363
364         return r < 0 ? 0 : 1;
365 }
366
367 #define BUFFER_SIZE 1024
368
369 static int process_pty(int master, sigset_t *mask) {
370         char in_buffer[BUFFER_SIZE], out_buffer[BUFFER_SIZE];
371         size_t in_buffer_full = 0, out_buffer_full = 0;
372         struct epoll_event stdin_ev, stdout_ev, master_ev, signal_ev;
373         bool stdin_readable = false, stdout_writable = false, master_readable = false, master_writable = false;
374         int ep = -1, signal_fd = -1, r;
375
376         fd_nonblock(STDIN_FILENO, 1);
377         fd_nonblock(STDOUT_FILENO, 1);
378         fd_nonblock(master, 1);
379
380         if ((signal_fd = signalfd(-1, mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
381                 log_error("signalfd(): %m");
382                 r = -errno;
383                 goto finish;
384         }
385
386         if ((ep = epoll_create1(EPOLL_CLOEXEC)) < 0) {
387                 log_error("Failed to create epoll: %m");
388                 r = -errno;
389                 goto finish;
390         }
391
392         zero(stdin_ev);
393         stdin_ev.events = EPOLLIN|EPOLLET;
394         stdin_ev.data.fd = STDIN_FILENO;
395
396         zero(stdout_ev);
397         stdout_ev.events = EPOLLOUT|EPOLLET;
398         stdout_ev.data.fd = STDOUT_FILENO;
399
400         zero(master_ev);
401         master_ev.events = EPOLLIN|EPOLLOUT|EPOLLET;
402         master_ev.data.fd = master;
403
404         zero(signal_ev);
405         signal_ev.events = EPOLLIN;
406         signal_ev.data.fd = signal_fd;
407
408         if (epoll_ctl(ep, EPOLL_CTL_ADD, STDIN_FILENO, &stdin_ev) < 0 ||
409             epoll_ctl(ep, EPOLL_CTL_ADD, STDOUT_FILENO, &stdout_ev) < 0 ||
410             epoll_ctl(ep, EPOLL_CTL_ADD, master, &master_ev) < 0 ||
411             epoll_ctl(ep, EPOLL_CTL_ADD, signal_fd, &signal_ev) < 0) {
412                 log_error("Failed to regiser fds in epoll: %m");
413                 r = -errno;
414                 goto finish;
415         }
416
417         for (;;) {
418                 struct epoll_event ev[16];
419                 ssize_t k;
420                 int i, nfds;
421
422                 if ((nfds = epoll_wait(ep, ev, ELEMENTSOF(ev), -1)) < 0) {
423
424                         if (errno == EINTR || errno == EAGAIN)
425                                 continue;
426
427                         log_error("epoll_wait(): %m");
428                         r = -errno;
429                         goto finish;
430                 }
431
432                 assert(nfds >= 1);
433
434                 for (i = 0; i < nfds; i++) {
435                         if (ev[i].data.fd == STDIN_FILENO) {
436
437                                 if (ev[i].events & (EPOLLIN|EPOLLHUP))
438                                         stdin_readable = true;
439
440                         } else if (ev[i].data.fd == STDOUT_FILENO) {
441
442                                 if (ev[i].events & (EPOLLOUT|EPOLLHUP))
443                                         stdout_writable = true;
444
445                         } else if (ev[i].data.fd == master) {
446
447                                 if (ev[i].events & (EPOLLIN|EPOLLHUP))
448                                         master_readable = true;
449
450                                 if (ev[i].events & (EPOLLOUT|EPOLLHUP))
451                                         master_writable = true;
452
453                         } else if (ev[i].data.fd == signal_fd) {
454                                 struct signalfd_siginfo sfsi;
455                                 ssize_t n;
456
457                                 if ((n = read(signal_fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
458
459                                         if (n >= 0) {
460                                                 r = -EIO;
461                                                 goto finish;
462                                         }
463
464                                         if (errno != EINTR && errno != EAGAIN) {
465                                                 r = -errno;
466                                                 goto finish;
467                                         }
468                                 } else {
469
470                                         if (sfsi.ssi_signo == SIGWINCH) {
471                                                 struct winsize ws;
472
473                                                 /* The window size changed, let's forward that. */
474                                                 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
475                                                         ioctl(master, TIOCSWINSZ, &ws);
476                                         } else {
477                                                 r = -EINTR;
478                                                 goto finish;
479                                         }
480                                 }
481                         }
482                 }
483
484                 while ((stdin_readable && in_buffer_full <= 0) ||
485                        (master_writable && in_buffer_full > 0) ||
486                        (master_readable && out_buffer_full <= 0) ||
487                        (stdout_writable && out_buffer_full > 0)) {
488
489                         if (stdin_readable && in_buffer_full < BUFFER_SIZE) {
490
491                                 if ((k = read(STDIN_FILENO, in_buffer + in_buffer_full, BUFFER_SIZE - in_buffer_full)) < 0) {
492
493                                         if (errno == EAGAIN || errno == EPIPE || errno == ECONNRESET || errno == EIO)
494                                                 stdin_readable = false;
495                                         else {
496                                                 log_error("read(): %m");
497                                                 goto finish;
498                                         }
499                                 } else
500                                         in_buffer_full += (size_t) k;
501                         }
502
503                         if (master_writable && in_buffer_full > 0) {
504
505                                 if ((k = write(master, in_buffer, in_buffer_full)) < 0) {
506
507                                         if (errno == EAGAIN || errno == EPIPE || errno == ECONNRESET || errno == EIO)
508                                                 master_writable = false;
509                                         else {
510                                                 log_error("write(): %m");
511                                                 goto finish;
512                                         }
513
514                                 } else {
515                                         assert(in_buffer_full >= (size_t) k);
516                                         memmove(in_buffer, in_buffer + k, in_buffer_full - k);
517                                         in_buffer_full -= k;
518                                 }
519                         }
520
521                         if (master_readable && out_buffer_full < BUFFER_SIZE) {
522
523                                 if ((k = read(master, out_buffer + out_buffer_full, BUFFER_SIZE - out_buffer_full)) < 0) {
524
525                                         if (errno == EAGAIN || errno == EPIPE || errno == ECONNRESET || errno == EIO)
526                                                 master_readable = false;
527                                         else {
528                                                 log_error("read(): %m");
529                                                 goto finish;
530                                         }
531                                 }  else
532                                         out_buffer_full += (size_t) k;
533                         }
534
535                         if (stdout_writable && out_buffer_full > 0) {
536
537                                 if ((k = write(STDOUT_FILENO, out_buffer, out_buffer_full)) < 0) {
538
539                                         if (errno == EAGAIN || errno == EPIPE || errno == ECONNRESET || errno == EIO)
540                                                 stdout_writable = false;
541                                         else {
542                                                 log_error("write(): %m");
543                                                 goto finish;
544                                         }
545
546                                 } else {
547                                         assert(out_buffer_full >= (size_t) k);
548                                         memmove(out_buffer, out_buffer + k, out_buffer_full - k);
549                                         out_buffer_full -= k;
550                                 }
551                         }
552                 }
553         }
554
555 finish:
556         if (ep >= 0)
557                 close_nointr_nofail(ep);
558
559         if (signal_fd >= 0)
560                 close_nointr_nofail(signal_fd);
561
562         return r;
563 }
564
565 int main(int argc, char *argv[]) {
566         pid_t pid = 0;
567         int r = EXIT_FAILURE, k;
568         char *oldcg = NULL, *newcg = NULL;
569         int master = -1;
570         const char *console = NULL;
571         struct termios saved_attr, raw_attr;
572         sigset_t mask;
573         bool saved_attr_valid = false;
574         struct winsize ws;
575
576         log_parse_environment();
577         log_open();
578
579         if ((r = parse_argv(argc, argv)) <= 0)
580                 goto finish;
581
582         if (arg_directory) {
583                 char *p;
584
585                 p = path_make_absolute_cwd(arg_directory);
586                 free(arg_directory);
587                 arg_directory = p;
588         } else
589                 arg_directory = get_current_dir_name();
590
591         if (!arg_directory) {
592                 log_error("Failed to determine path");
593                 goto finish;
594         }
595
596         path_kill_slashes(arg_directory);
597
598         if (geteuid() != 0) {
599                 log_error("Need to be root.");
600                 goto finish;
601         }
602
603         if (sd_booted() <= 0) {
604                 log_error("Not running on a systemd system.");
605                 goto finish;
606         }
607
608         if (path_equal(arg_directory, "/")) {
609                 log_error("Spawning container on root directory not supported.");
610                 goto finish;
611         }
612
613         if (is_os_tree(arg_directory) <= 0) {
614                 log_error("Directory %s doesn't look like an OS root directory. Refusing.", arg_directory);
615                 goto finish;
616         }
617
618         if ((k = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 0, &oldcg)) < 0) {
619                 log_error("Failed to determine current cgroup: %s", strerror(-k));
620                 goto finish;
621         }
622
623         if (asprintf(&newcg, "%s/nspawn-%lu", oldcg, (unsigned long) getpid()) < 0) {
624                 log_error("Failed to allocate cgroup path.");
625                 goto finish;
626         }
627
628         if ((k = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, newcg, 0)) < 0)  {
629                 log_error("Failed to create cgroup: %s", strerror(-k));
630                 goto finish;
631         }
632
633         if ((master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC|O_NDELAY)) < 0) {
634                 log_error("Failed to acquire pseudo tty: %m");
635                 goto finish;
636         }
637
638         if (!(console = ptsname(master))) {
639                 log_error("Failed to determine tty name: %m");
640                 goto finish;
641         }
642
643         log_info("Spawning namespace container on %s (console is %s).", arg_directory, console);
644
645         if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
646                 ioctl(master, TIOCSWINSZ, &ws);
647
648         if (unlockpt(master) < 0) {
649                 log_error("Failed to unlock tty: %m");
650                 goto finish;
651         }
652
653         if (tcgetattr(STDIN_FILENO, &saved_attr) < 0) {
654                 log_error("Failed to get terminal attributes: %m");
655                 goto finish;
656         }
657
658         saved_attr_valid = true;
659
660         raw_attr = saved_attr;
661         cfmakeraw(&raw_attr);
662         raw_attr.c_lflag &= ~ECHO;
663
664         if (tcsetattr(STDIN_FILENO, TCSANOW, &raw_attr) < 0) {
665                 log_error("Failed to set terminal attributes: %m");
666                 goto finish;
667         }
668
669         assert_se(sigemptyset(&mask) == 0);
670         sigset_add_many(&mask, SIGCHLD, SIGWINCH, SIGTERM, SIGINT, -1);
671         assert_se(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);
672
673         if ((pid = syscall(__NR_clone, SIGCHLD|CLONE_NEWIPC|CLONE_NEWNS|CLONE_NEWPID|CLONE_NEWUTS, NULL)) < 0) {
674                 log_error("clone() failed: %m");
675                 goto finish;
676         }
677
678         if (pid == 0) {
679                 /* child */
680
681                 const char *hn;
682                 const char *envp[] = {
683                         "HOME=/root",
684                         "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
685                         NULL,
686                         NULL
687                 };
688
689                 envp[2] = strv_find_prefix(environ, "TERM=");
690
691                 close_nointr_nofail(master);
692
693                 close_nointr(STDIN_FILENO);
694                 close_nointr(STDOUT_FILENO);
695                 close_nointr(STDERR_FILENO);
696
697                 close_all_fds(NULL, 0);
698
699                 reset_all_signal_handlers();
700
701                 assert_se(sigemptyset(&mask) == 0);
702                 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
703
704                 if (setsid() < 0)
705                         goto child_fail;
706
707                 if (prctl(PR_SET_PDEATHSIG, SIGKILL) < 0)
708                         goto child_fail;
709
710                 if (mount_all(arg_directory) < 0)
711                         goto child_fail;
712
713                 if (copy_devnodes(arg_directory, console) < 0)
714                         goto child_fail;
715
716                 if (chdir(arg_directory) < 0) {
717                         log_error("chdir(%s) failed: %m", arg_directory);
718                         goto child_fail;
719                 }
720
721                 if (open_terminal("dev/console", O_RDWR) != STDIN_FILENO ||
722                     dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO ||
723                     dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO)
724                         goto child_fail;
725
726                 if (mount(arg_directory, "/", "bind", MS_BIND|MS_MOVE, NULL) < 0) {
727                         log_error("mount(MS_MOVE) failed: %m");
728                         goto child_fail;
729                 }
730
731                 if (chroot(".") < 0) {
732                         log_error("chroot() failed: %m");
733                         goto child_fail;
734                 }
735
736                 if (chdir("/") < 0) {
737                         log_error("chdir() failed: %m");
738                         goto child_fail;
739                 }
740
741                 umask(0002);
742
743                 if (drop_capabilities() < 0)
744                         goto child_fail;
745
746                 if ((hn = file_name_from_path(arg_directory)))
747                         sethostname(hn, strlen(hn));
748
749                 if (argc > optind)
750                         execvpe(argv[optind], argv + optind, (char**) envp);
751                 else {
752                         chdir("/root");
753                         execle("/bin/bash", "-bash", NULL, (char**) envp);
754                 }
755
756                 log_error("execv() failed: %m");
757
758         child_fail:
759                 _exit(EXIT_FAILURE);
760         }
761
762         if (process_pty(master, &mask) < 0)
763                 goto finish;
764
765         if (saved_attr_valid) {
766                 tcsetattr(STDIN_FILENO, TCSANOW, &saved_attr);
767                 saved_attr_valid = false;
768         }
769
770         r = wait_for_terminate_and_warn(argc > optind ? argv[optind] : "bash", pid);
771
772         if (r < 0)
773                 r = EXIT_FAILURE;
774
775 finish:
776         if (saved_attr_valid)
777                 tcsetattr(STDIN_FILENO, TCSANOW, &saved_attr);
778
779         if (master >= 0)
780                 close_nointr_nofail(master);
781
782         if (oldcg)
783                 cg_attach(SYSTEMD_CGROUP_CONTROLLER, oldcg, 0);
784
785         if (newcg)
786                 cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, newcg, true);
787
788         free(arg_directory);
789         free(oldcg);
790         free(newcg);
791
792         return r;
793 }