summaryrefslogtreecommitdiff
path: root/src/shutdown/s6-linux-init-hpr.c
blob: 14e184448cd2543420d3e42499e1c152c0e30aef (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
/* ISC license. */

#include <unistd.h>
#include <errno.h>

#include <skalibs/strerr2.h>
#include <skalibs/sgetopt.h>
#include <skalibs/tai.h>

#include "hpr.h"
#include "os.h"

#define USAGE "s6-linux-init-hpr [ -h | -p | -r ] [ -n ] [ -d | -w ] [ -W ] [ -f ] [ -i ]"

int main (int argc, char const *const *argv)
{
  int what = 0 ;
  int force = 0 ;
  int dowtmp = 1 ;
  int dowall = 1 ;
  int dosync = 1 ;
  int doconfirm = 0 ;
  PROG = "s6-linux-init-hpr" ;

  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      int opt = subgetopt_r(argc, argv, "hprfdwWni", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'h' : what = 1 ; break ;
        case 'p' : what = 2 ; break ;
        case 'r' : what = 3 ; break ;
        case 'f' : force = 1 ; break ;
        case 'd' : dowtmp = 0 ; break ;
        case 'w' : dowtmp = 2 ; break ;
        case 'W' : dowall = 0 ; break ;
        case 'n' : dosync = 0 ; break ;
        case 'i' : doconfirm = 1 ; break ;
        default : strerr_dieusage(100, USAGE) ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }

  if (!what)
    strerr_dief1x(100, "one of the -h, -p or -r options must be given") ;

  if (geteuid())
  {
    errno = EPERM ;
    strerr_dief1sys(100, "nice try, peon") ;
  }

  if (doconfirm) hpr_confirm_hostname() ;

  if (force)
  {
    if (dosync) sync() ;
    os_reboot(what) ;
    strerr_diefu1sys(111, "os_reboot") ;
  }

  if (!tain_now_g()) strerr_warnw1sys("get current time") ;
  if (dowtmp) os_final_wtmp(what) ;
  if (dowall) hpr_wall(HPR_WALL_BANNER) ;
  if (dowtmp < 2)
  {
    if (!hpr_shutdown(what, &tain_zero, 0))
      strerr_diefu1sys(111, "notify s6-linux-init-shutdownd") ;
  }
  return 0 ;
}