aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/kill_everyone_and_reboot.cc101
1 files changed, 49 insertions, 52 deletions
diff --git a/src/kill_everyone_and_reboot.cc b/src/kill_everyone_and_reboot.cc
index 8b67f89..a849c25 100644
--- a/src/kill_everyone_and_reboot.cc
+++ b/src/kill_everyone_and_reboot.cc
@@ -1,79 +1,76 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+#include <iostream>
+#include <cstdlib>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/reboot.h>
#include <unistd.h>
#include <dirent.h>
-#include <stdlib.h>
-#include <stdio.h>
#include <errno.h>
-#include <string.h>
#include <signal.h>
-#include <stdbool.h>
-#include <signal.h>
-
-bool is_pid(const char * const c)
-{
- for (const char * q = c ; *q ; ++q)
- if (*q < '0' || *q > '9')
- return false;
-
- return true;
-}
-void kill_everybody(const int sig)
+namespace
{
- DIR * const dirh = opendir("/proc");
- if (! dirh)
+ bool is_pid(const std::string & s)
{
- perror("Couldn't opendir /proc");
- exit(EXIT_FAILURE);
+ return std::string::npos == s.find_first_not_of("0123456789");
}
- fprintf(stderr, "Sending signal %d to: ", sig);
- const struct dirent * dp;
- while (((dp = readdir(dirh))))
+ void kill_everybody(const int sig)
{
- if (! is_pid(dp->d_name))
- continue;
+ 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 = atoi(dp->d_name);
- if (1 == pid || getpid() == pid)
- continue;
+ const pid_t pid = std::atoi(dp->d_name);
+ if (1 == pid || getpid() == pid)
+ continue;
- fprintf(stderr, "%d ", pid);
- kill(pid, sig);
+ std::cerr << pid << " ";
+ kill(pid, sig);
+ }
+ closedir(dirh);
+ fprintf(stderr, "\n");
}
- closedir(dirh);
- fprintf(stderr, "\n");
-}
-void remount_root_ro()
-{
- fprintf(stderr, "Remounting / read-only");
- if (0 != mount("none", "/", "none", MS_RDONLY | MS_REMOUNT, ""))
+ void remount_root_ro()
{
- perror("Couldn't remount / read-only");
- exit(EXIT_FAILURE);
+ 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()
-{
- fprintf(stderr, "Syncing filesystems");
- sync();
- sleep(1);
- sync();
-}
+ void sync_filesystems()
+ {
+ std::cerr << "Syncing filesystems" << std::endl;
+ sync();
+ sleep(1);
+ std::cerr << "Syncing filesystems again" << std::endl;
+ sync();
+ }
-void power_off()
-{
- fprintf(stderr, "Rebooting");
- if (0 != reboot(RB_POWER_OFF))
+ void power_off()
{
- perror("Couldn't call reboot syscall");
- exit(EXIT_FAILURE);
+ 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);
+ }
}
}