LCOV - code coverage report
Current view: top level - nspawn - nspawn-stub-pid1.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 0 82 0.0 %
Date: 2019-08-22 15:41:25 Functions: 0 2 0.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <sys/reboot.h>
       4             : #include <sys/wait.h>
       5             : #include <sys/prctl.h>
       6             : #include <unistd.h>
       7             : 
       8             : #include "def.h"
       9             : #include "exit-status.h"
      10             : #include "fd-util.h"
      11             : #include "log.h"
      12             : #include "missing.h"
      13             : #include "nspawn-stub-pid1.h"
      14             : #include "process-util.h"
      15             : #include "signal-util.h"
      16             : #include "time-util.h"
      17             : 
      18           0 : static int reset_environ(const char *new_environment, size_t length) {
      19             :         unsigned long start, end;
      20             : 
      21           0 :         start = (unsigned long) new_environment;
      22           0 :         end = start + length;
      23             : 
      24           0 :         if (prctl(PR_SET_MM, PR_SET_MM_ENV_START, start, 0, 0) < 0)
      25           0 :                 return -errno;
      26             : 
      27           0 :         if (prctl(PR_SET_MM, PR_SET_MM_ENV_END, end, 0, 0) < 0)
      28           0 :                 return -errno;
      29             : 
      30           0 :         return 0;
      31             : }
      32             : 
      33           0 : int stub_pid1(sd_id128_t uuid) {
      34             :         enum {
      35             :                 STATE_RUNNING,
      36             :                 STATE_REBOOT,
      37             :                 STATE_POWEROFF,
      38           0 :         } state = STATE_RUNNING;
      39             : 
      40             :         sigset_t fullmask, oldmask, waitmask;
      41           0 :         usec_t quit_usec = USEC_INFINITY;
      42             :         pid_t pid;
      43             :         int r;
      44             : 
      45             :         /* The new environment we set up, on the stack. */
      46           0 :         char new_environment[] =
      47             :                 "container=systemd-nspawn\0"
      48             :                 "container_uuid=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
      49             : 
      50             :         /* Implements a stub PID 1, that reaps all processes and processes a couple of standard signals. This is useful
      51             :          * for allowing arbitrary processes run in a container, and still have all zombies reaped. */
      52             : 
      53           0 :         assert_se(sigfillset(&fullmask) >= 0);
      54           0 :         assert_se(sigprocmask(SIG_BLOCK, &fullmask, &oldmask) >= 0);
      55             : 
      56           0 :         pid = fork();
      57           0 :         if (pid < 0)
      58           0 :                 return log_error_errno(errno, "Failed to fork child pid: %m");
      59             : 
      60           0 :         if (pid == 0) {
      61             :                 /* Return in the child */
      62           0 :                 assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) >= 0);
      63           0 :                 setsid();
      64           0 :                 return 0;
      65             :         }
      66             : 
      67           0 :         reset_all_signal_handlers();
      68             : 
      69           0 :         log_close();
      70           0 :         (void) close_all_fds(NULL, 0);
      71           0 :         log_open();
      72             : 
      73             :         /* Flush out /proc/self/environ, so that we don't leak the environment from the host into the container. Also,
      74             :          * set $container= and $container_uuid= so that clients in the container that query it from /proc/1/environ
      75             :          * find them set. */
      76           0 :         sd_id128_to_string(uuid, new_environment + sizeof(new_environment) - SD_ID128_STRING_MAX);
      77           0 :         reset_environ(new_environment, sizeof(new_environment));
      78             : 
      79           0 :         (void) rename_process("(sd-stubinit)");
      80             : 
      81           0 :         assert_se(sigemptyset(&waitmask) >= 0);
      82           0 :         assert_se(sigset_add_many(&waitmask,
      83             :                                   SIGCHLD,          /* posix: process died */
      84             :                                   SIGINT,           /* sysv: ctrl-alt-del */
      85             :                                   SIGRTMIN+3,       /* systemd: halt */
      86             :                                   SIGRTMIN+4,       /* systemd: poweroff */
      87             :                                   SIGRTMIN+5,       /* systemd: reboot */
      88             :                                   SIGRTMIN+6,       /* systemd: kexec */
      89             :                                   SIGRTMIN+13,      /* systemd: halt */
      90             :                                   SIGRTMIN+14,      /* systemd: poweroff */
      91             :                                   SIGRTMIN+15,      /* systemd: reboot */
      92             :                                   SIGRTMIN+16,      /* systemd: kexec */
      93             :                                   -1) >= 0);
      94             : 
      95             :         /* Note that we ignore SIGTERM (sysv's reexec), SIGHUP (reload), and all other signals here, since we don't
      96             :          * support reexec/reloading in this stub process. */
      97             : 
      98           0 :         for (;;) {
      99             :                 siginfo_t si;
     100             :                 usec_t current_usec;
     101             : 
     102           0 :                 si.si_pid = 0;
     103           0 :                 r = waitid(P_ALL, 0, &si, WEXITED|WNOHANG);
     104           0 :                 if (r < 0) {
     105           0 :                         r = log_error_errno(errno, "Failed to reap children: %m");
     106           0 :                         goto finish;
     107             :                 }
     108             : 
     109           0 :                 current_usec = now(CLOCK_MONOTONIC);
     110             : 
     111           0 :                 if (si.si_pid == pid || current_usec >= quit_usec) {
     112             : 
     113             :                         /* The child we started ourselves died or we reached a timeout. */
     114             : 
     115           0 :                         if (state == STATE_REBOOT) { /* dispatch a queued reboot */
     116           0 :                                 (void) reboot(RB_AUTOBOOT);
     117           0 :                                 r = log_error_errno(errno, "Failed to reboot: %m");
     118           0 :                                 goto finish;
     119             : 
     120           0 :                         } else if (state == STATE_POWEROFF)
     121           0 :                                 (void) reboot(RB_POWER_OFF); /* if this fails, fall back to normal exit. */
     122             : 
     123           0 :                         if (si.si_pid == pid && si.si_code == CLD_EXITED)
     124           0 :                                 r = si.si_status; /* pass on exit code */
     125             :                         else
     126           0 :                                 r = EXIT_EXCEPTION; /* signal, coredump, timeout, … */
     127             : 
     128           0 :                         goto finish;
     129             :                 }
     130           0 :                 if (si.si_pid != 0)
     131             :                         /* We reaped something. Retry until there's nothing more to reap. */
     132           0 :                         continue;
     133             : 
     134           0 :                 if (quit_usec == USEC_INFINITY)
     135           0 :                         r = sigwaitinfo(&waitmask, &si);
     136             :                 else {
     137             :                         struct timespec ts;
     138           0 :                         r = sigtimedwait(&waitmask, &si, timespec_store(&ts, quit_usec - current_usec));
     139             :                 }
     140           0 :                 if (r < 0) {
     141           0 :                         if (errno == EINTR) /* strace -p attach can result in EINTR, let's handle this nicely. */
     142           0 :                                 continue;
     143           0 :                         if (errno == EAGAIN) /* timeout reached */
     144           0 :                                 continue;
     145             : 
     146           0 :                         r = log_error_errno(errno, "Failed to wait for signal: %m");
     147           0 :                         goto finish;
     148             :                 }
     149             : 
     150           0 :                 if (si.si_signo == SIGCHLD)
     151           0 :                         continue; /* Let's reap this */
     152             : 
     153           0 :                 if (state != STATE_RUNNING)
     154           0 :                         continue;
     155             : 
     156             :                 /* Would love to use a switch() statement here, but SIGRTMIN is actually a function call, not a
     157             :                  * constant… */
     158             : 
     159           0 :                 if (si.si_signo == SIGRTMIN+3 ||
     160           0 :                     si.si_signo == SIGRTMIN+4 ||
     161           0 :                     si.si_signo == SIGRTMIN+13 ||
     162           0 :                     si.si_signo == SIGRTMIN+14)
     163             : 
     164           0 :                         state = STATE_POWEROFF;
     165             : 
     166           0 :                 else if (si.si_signo == SIGINT ||
     167           0 :                          si.si_signo == SIGRTMIN+5 ||
     168           0 :                          si.si_signo == SIGRTMIN+6 ||
     169           0 :                          si.si_signo == SIGRTMIN+15 ||
     170           0 :                          si.si_signo == SIGRTMIN+16)
     171             : 
     172           0 :                         state = STATE_REBOOT;
     173             :                 else
     174           0 :                         assert_not_reached("Got unexpected signal");
     175             : 
     176           0 :                 r = kill_and_sigcont(pid, SIGTERM);
     177             : 
     178             :                 /* Let's send a SIGHUP after the SIGTERM, as shells tend to ignore SIGTERM but do react to SIGHUP. We
     179             :                  * do it strictly in this order, so that the SIGTERM is dispatched first, and SIGHUP second for those
     180             :                  * processes which handle both. That's because services tend to bind configuration reload or something
     181             :                  * else to SIGHUP. */
     182             : 
     183           0 :                 if (r != -ESRCH)
     184           0 :                         (void) kill(pid, SIGHUP);
     185             : 
     186           0 :                 quit_usec = now(CLOCK_MONOTONIC) + DEFAULT_TIMEOUT_USEC;
     187             :         }
     188             : 
     189           0 : finish:
     190           0 :         _exit(r < 0 ? EXIT_FAILURE : r);
     191             : }

Generated by: LCOV version 1.14