summary refs log tree commit diff
path: root/src/runit-init.c
blob: f075d98bf7dd17c893021f9572cbb970cdad8dcf (plain) (blame)
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
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <unistd.h>
#include "runit.h"
#include "strerr.h"
#include "sig.h"
#include "open.h"
#include "error.h"

#define USAGE " 0|6"
#define FATAL "init: fatal: "
/* #define WARNING "init: warning: " */

const char *progname;

void usage(void) { strerr_die4x(0, "usage: ", progname, USAGE, "\n"); }

void runit_halt () {
  if (open_trunc(STOPIT) == -1)
    strerr_die4sys(111, FATAL, "unable to create ", STOPIT, ": ");
  if (chmod(STOPIT, 0100) == -1)
    strerr_die4sys(111, FATAL, "unable to chmod ", STOPIT, ": ");
  if (chmod(REBOOT, 0) == -1)
    if (errno != error_noent)
      strerr_die4sys(111, FATAL, "unable to chmod ", REBOOT, ": ");
  kill(1, sig_cont);
  _exit(0);
}

void runit_reboot () {
  if (open_trunc(STOPIT) == -1)
    strerr_die4sys(111, FATAL, "unable to create ", STOPIT, ": ");
  if (chmod(STOPIT, 0100) == -1)
    strerr_die4sys(111, FATAL, "unable to chmod ", STOPIT, ": ");
  if (open_trunc(REBOOT) == -1)
    strerr_die4sys(111, FATAL, "unable to create ", REBOOT, ": ");
  if (chmod(REBOOT, 0100) == -1)
    strerr_die4sys(111, FATAL, "unable to chmod ", REBOOT, ": ");
  kill(1, sig_cont);
  _exit(0);
}

int main (int argc, const char * const *argv, char * const *envp) {
  const char *prog[2];

  progname =*argv++;

  if (getpid() == 1) {
    prog[1] =0;
    prog[0] ="runit";

    /* kernel is starting init, runit does the job. */
    execve(RUNIT, (char *const *)prog, envp);

    /* serious error */
    strerr_die4sys(111, FATAL, "unable to start ", prog[0], ": ");
  }

  if (! *argv || ! **argv) usage();
  switch (**argv) {
  case '0':
    runit_halt();
    break;
  case '6':
    runit_reboot();
    break;
  case '-':
    if ((*argv)[1] == 'V')
      strerr_warn1("$Id$\n", 0);
  default:
    usage();
  }
  /* not reached */
  _exit(0);
}