aboutsummaryrefslogtreecommitdiff
path: root/toys/other/reboot.c
blob: 15eea285f3c657f693a445d5064d7c717ec05e1e (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
/* reboot.c - Restart, halt or powerdown the system.
 *
 * Copyright 2013 Elie De Brauwer <eliedebrauwer@gmail.com>

USE_REBOOT(NEWTOY(reboot, "fn", TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
USE_REBOOT(OLDTOY(halt, reboot, TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
USE_REBOOT(OLDTOY(poweroff, reboot, TOYFLAG_SBIN|TOYFLAG_NEEDROOT))

config REBOOT
  bool "reboot"
  default y
  help
    usage: reboot/halt/poweroff [-fn]

    Restart, halt or powerdown the system.

    -f	Don't signal init
    -n	Don't sync before stopping the system.
*/

#define FOR_reboot
#include "toys.h"
#include <sys/reboot.h>

void reboot_main(void)
{
  int types[] = {RB_AUTOBOOT, RB_HALT_SYSTEM, RB_POWER_OFF},
      sigs[] = {SIGTERM, SIGUSR1, SIGUSR2}, idx;

  if (!(toys.optflags & FLAG_n)) sync();

  idx = stridx("hp", *toys.which->name)+1;
  if (toys.optflags & FLAG_f) toys.exitval = reboot(types[idx]);
  else toys.exitval = kill(1, sigs[idx]);
}