summaryrefslogtreecommitdiff
path: root/src/init/s6-linux-init.c
blob: 5223d54964879b7d123f715ce993507432d49b9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* ISC license. */

#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/mount.h>

#include <skalibs/types.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/sgetopt.h>
#include <skalibs/strerr2.h>
#include <skalibs/stralloc.h>
#include <skalibs/env.h>
#include <skalibs/djbunix.h>

#include <s6/config.h>

#include <s6-linux-init/config.h>

#include "defaults.h"
#include "initctl.h"

#define USAGE "s6-linux-init [ -c basedir ] [ -p initpath ] [ -s envdumpdir ] [ -m umask ] [ -d devtmpfs ] [ -D initdefault ]"
#define dieusage() strerr_dieusage(100, USAGE)

#define BANNER "\n  s6-linux-init version " S6_LINUX_INIT_VERSION "\n\n"

static inline char const *scan_cmdline (char const *initdefault, char const *const *argv, unsigned int argc)
{
  static char const *valid[] = { "default", "2", "3", "4", "5", 0 } ;
  for (unsigned int i = 0 ; i < argc ; i++)
    for (char const *const *p = valid ; *p ; p++)
      if (!strcmp(argv[i], *p)) return argv[i] ;
  return initdefault ;
}

static inline void run_stage2 (char const *basedir, char const **argv, unsigned int argc, char const *const *envp, size_t envlen, char const *modifs, size_t modiflen, char const *initdefault)
{
  size_t dirlen = strlen(basedir) ;
  char const *childargv[argc + 3] ;
  char fn[dirlen + 1 + sizeof(STAGE2)] ;
  PROG = "s6-linux-init (child)" ;
  argv[0] = PROG ;
  memcpy(fn, basedir, dirlen) ;
  fn[dirlen] = '/' ;
  memcpy(fn + dirlen + 1, STAGE2, sizeof(STAGE2)) ;
  childargv[0] = fn ;
  childargv[1] = scan_cmdline(initdefault, argv + 1, argc - 1) ;
  for (unsigned int i = 0 ; i < argc ; i++)
    childargv[i+2] = argv[i] ;
  childargv[argc + 2] = 0 ;
  setsid() ;
  fd_close(1) ;
  if (open(LOGFIFO, O_WRONLY) != 1)  /* blocks until catch-all logger is up */
    strerr_diefu1sys(111, "open " LOGFIFO " for writing") ;
  if (fd_copy(2, 1) == -1)
    strerr_diefu1sys(111, "fd_copy stdout to stderr") ;
  xpathexec_r(childargv, envp, envlen, modifs, modiflen) ;
}

int main (int argc, char const **argv, char const *const *envp)
{
  mode_t mask = 0022 ;
  char const *basedir = BASEDIR ;
  char const *path = INITPATH ;
  char const *slashdev = 0 ;
  char const *envdumpdir = 0 ;
  char const *initdefault = "default" ;
  stralloc envmodifs = STRALLOC_ZERO ;
  PROG = "s6-linux-init" ;

  if (getpid() != 1)
  {
    argv[0] = S6_LINUX_INIT_BINPREFIX "/s6-linux-init-telinit" ;
    pathexec_run(argv[0], argv, envp) ;
    strerr_dieexec(111, argv[0]) ;
  }

  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      int opt = subgetopt_r(argc, argv, "c:p:s:m:d:D:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'c' : basedir = l.arg ; break ;
        case 'p' : path = l.arg ; break ;
        case 's' : envdumpdir = l.arg ; break ;
        case 'm' : if (!uint0_oscan(l.arg, &mask)) dieusage() ; break ;
        case 'd' : slashdev = l.arg ; break ;
        case 'D' : initdefault = l.arg ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }

  allwrite(1, BANNER, sizeof(BANNER) - 1) ;
  if (chdir("/") == -1) strerr_diefu1sys(111, "chdir to /") ;
  umask(mask) ;
  setpgid(0, 0) ;
  fd_close(0) ;
  if (slashdev)
  {
    fd_close(1) ;
    fd_close(2) ;
    if (mount("dev", slashdev, "devtmpfs", MS_NOSUID | MS_NOEXEC, "") == -1)
    {
      int e = errno ;
      open("/dev/null", O_RDONLY) ;
      open("/dev/console", O_WRONLY) ;
      fd_copy(2, 1) ;
      errno = e ;
      strerr_diefu2sys(111, "mount ", slashdev) ;
    }
    if (open("/dev/console", O_WRONLY)
     || fd_copy(1, 0) == -1
     || fd_move(2, 0) == -1) return 111 ;
  }
  if (open("/dev/null", O_RDONLY)) strerr_diefu1sys(111, "open /dev/null") ;
  
  if (umount(S6_LINUX_INIT_TMPFS) == -1)
  {
    if (errno != EINVAL)
      strerr_warnwu1sys("umount " S6_LINUX_INIT_TMPFS) ;
  }
  if (mount("tmpfs", S6_LINUX_INIT_TMPFS, "tmpfs", MS_NODEV | MS_NOSUID, "mode=0755") == -1)
    strerr_diefu1sys(111, "mount tmpfs on " S6_LINUX_INIT_TMPFS) ;
  {
    size_t dirlen = strlen(basedir) ;
    char fn[dirlen + 1 + (sizeof(RUNIMAGE) > sizeof(ENVSTAGE1) ? sizeof(RUNIMAGE) : sizeof(ENVSTAGE1))] ;
    memcpy(fn, basedir, dirlen) ;
    fn[dirlen] = '/' ;
    memcpy(fn + dirlen + 1, RUNIMAGE, sizeof(RUNIMAGE)) ;
    if (!hiercopy(fn, S6_LINUX_INIT_TMPFS))
      strerr_diefu3sys(111, "copy ", fn, " to " S6_LINUX_INIT_TMPFS) ;
    memcpy(fn + dirlen + 1, ENVSTAGE1, sizeof(ENVSTAGE1)) ;
    if (envdir(fn, &envmodifs) == -1)
      strerr_warnwu2sys("envdir ", fn) ;
  }
  if (envdumpdir && !env_dump(envdumpdir, 0700, envp))
    strerr_warnwu2sys("dump kernel environment to ", envdumpdir) ;
  {
    int fdr = open_read(LOGFIFO) ;
    if (fdr == -1) strerr_diefu1sys(111, "open " LOGFIFO) ;
    fd_close(1) ;
    if (open(LOGFIFO, O_WRONLY) != 1) strerr_diefu1sys(111, "open " LOGFIFO) ;
    fd_close(fdr) ;
  }
  {
    static char const *const newargv[5] = { S6_EXTBINPREFIX "s6-svscan", "-st0", "--", S6_LINUX_INIT_TMPFS "/" SCANDIR, 0 } ;
    char const *newenvp[2] = { 0, 0 } ;
    pid_t pid ;
    size_t pathlen = path ? strlen(path) : 0 ;
    char pathvar[6 + pathlen] ;
    if (path)
    {
      if (setenv("PATH", path, 1) == -1)
        strerr_diefu1sys(111, "set initial PATH") ;
      memcpy(pathvar, "PATH=", 5) ;
      memcpy(pathvar + 5, path, pathlen + 1) ;
      newenvp[0] = pathvar ;
    }
    pid = fork() ;
    if (pid == -1) strerr_diefu1sys(111, "fork") ;
    if (!pid) run_stage2(basedir, argv, argc, newenvp, !!path, envmodifs.s, envmodifs.len, initdefault) ;
    if (fd_copy(2, 1) == -1)
      strerr_diefu1sys(111, "redirect output file descriptor") ;
    xpathexec_r(newargv, newenvp, !!path, envmodifs.s, envmodifs.len) ;
  }
}