aboutsummaryrefslogtreecommitdiff
path: root/contrib/conditional-init.c
blob: 10c69cc032b8c5b1881dab75fc8a160bb0ade8ae (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
#include <unistd.h>
#include <time.h>
#include <unistd.h>
#include <sys/reboot.h>
#include "../ninitfeatures.h"
#include "../uid.h"

#define TM_OUT 1200

int main(int argc, char **argv) {
  unsigned long ul;
  char *stable, *test;

  if (argc < 4) {
    carp("usage: conditional-init now /stable/init /test/init [args...]\n"
	 "\tnow must be the output of: date +%s");
    return 1;
  }

  errmsg_argv0 = "conditional-init";
  stable=argv[2];
  test=argv[3];
  
  if (scan_ulong(argv[1], &ul)==0 || ul + TM_OUT < (unsigned long)time(0)) {
    carp("booting (stable): ", stable);
    argv += 3;
    argv[0] = stable;
    execve(stable, argv, environ);
    return 0;
  }

  if (fork()==0) {
    unsigned long deadline = (unsigned long)time(0) + TM_OUT;
    while ((unsigned long)time(0) < deadline ) nano_sleep(1,0);
    sync();
    set_reboot(RB_AUTOBOOT);
    return 2;
  }

  argv += 3;
  argv[0]=test;
  carp("booting (test): ", test);
  execve(test, argv, environ);
  nano_sleep(TM_OUT,0); /* only if test fails */
  set_reboot(RB_AUTOBOOT);
  return 1;
}