aboutsummaryrefslogtreecommitdiff
path: root/src/kill_everyone_and_reboot.cc
blob: 33cfc04f5319cc0082697beb8330ec56669e7d42 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/reboot.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <signal.h>

namespace
{
    bool is_pid(const std::string & s)
    {
        return std::string::npos == s.find_first_not_of("0123456789");
    }

    void kill_everybody(const int sig)
    {
        DIR * const dirh = opendir("/proc");
        if (! dirh)
        {
            std::cerr << "Couldn't opendir /proc: " << strerror(errno) << std::endl;
            exit(EXIT_FAILURE);
        }

        std::cerr << "Sending signal " << sig << " to: ";
        const dirent * dp;
        while (((dp = readdir(dirh))))
        {
            if (! is_pid(dp->d_name))
                continue;

            const pid_t pid = std::atoi(dp->d_name);
            if (1 == pid || getpid() == pid)
                continue;

            std::cerr << pid << " ";
            kill(pid, sig);
        }
        closedir(dirh);
        std::cerr << "\n";
    }

    void remount_root_ro()
    {
        std::cerr << "Remounting / read-only" << std::endl;
        if (0 != mount("none", "/", "none", MS_RDONLY | MS_REMOUNT, ""))
        {
            std::cerr << "Couldn't remount / read-only: " << strerror(errno) << std::endl;
            exit(EXIT_FAILURE);
        }
    }

    void sync_filesystems()
    {
        std::cerr << "Syncing filesystems" << std::endl;
        sync();
        sleep(1);
        std::cerr << "Syncing filesystems again" << std::endl;
        sync();
    }

    void do_reboot()
    {
        std::cerr << "Rebooting" << std::endl;
        if (0 != reboot(RB_AUTOBOOT))
        {
            std::cerr << "Couldn't call reboot syscall" << strerror(errno) << std::endl;
            exit(EXIT_FAILURE);
        }
    }

    void power_off()
    {
        std::cerr << "Power off" << std::endl;
        if (0 != reboot(RB_POWER_OFF))
        {
            std::cerr << "Couldn't call reboot syscall" << strerror(errno) << std::endl;
            exit(EXIT_FAILURE);
        }
    }
}

int main(int argc, char * argv[])
{
    bool reboot(false);
    if (argc != 2)
    {
        std::cerr << "Usage: " << argv[0] << " (--reboot or --poweroff)" << std::endl;
        exit(EXIT_FAILURE);
    }

    std::string cmd(argv[1]);
    if (cmd == "--reboot")
        reboot = true;
    else if (cmd == "--poweroff")
        ;
    else
    {
        std::cerr << "Usage: " << argv[0] << " (--reboot or --poweroff)" << std::endl;
        exit(EXIT_FAILURE);
    }

    kill_everybody(SIGTERM);
    sleep(1);
    kill_everybody(SIGKILL);
    sleep(1);
    remount_root_ro();
    sync_filesystems();
    if (reboot)
        do_reboot();
    else
        power_off();
    return EXIT_SUCCESS;
}