summaryrefslogtreecommitdiff
path: root/src/minutils/hpr.c
blob: d78470eaae06d65bedf928205c8ecba87ea12666 (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
/* ISC license. */

#include <unistd.h>
#include <signal.h>
#include <sys/reboot.h>
#include <skalibs/strerr2.h>
#include <skalibs/sgetopt.h>

#define USAGE PROGNAME " [ -h | -p | -r ] [ -f ] [ -w | -d ]"

int main (int argc, char const *const *argv)
{
  int what = WHATDEFAULT ;
  int force = 0 ;
  PROG = PROGNAME ;

  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "hprfwd", &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 'w' : what = 0 ; break ;
        case 'd' : break ;
        default : strerr_dieusage(100, USAGE) ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }

  if (what)
  {
    if (force)
    {
      sync() ;
      reboot(what == 3 ? RB_AUTOBOOT : what == 2 ? RB_POWER_OFF : RB_HALT_SYSTEM) ;
      strerr_diefu1sys(111, "reboot()") ;
    }
    else if (kill(1, what == 3 ? SIGINT : what == 2 ? SIGUSR1 : SIGUSR2) < 0)
      strerr_diefu1sys(111, "signal process 1") ;
  }
  return 0 ;
}