[systemd-devel] [PATCH 5/8] power: these binaries are internal APIs

Shawn Landden shawn at churchofgit.com
Fri Feb 20 14:31:02 PST 2015


They are not executed by a user (they all check how they were executed)
so we can use assert() in main() just like we would anywhere else.
---
 src/power/shutdown.c  | 20 ++++++++++----------
 src/power/shutdownd.c | 22 ++++------------------
 src/power/sleep.c     | 14 +++++++++++---
 3 files changed, 25 insertions(+), 31 deletions(-)

diff --git a/src/power/shutdown.c b/src/power/shutdown.c
index 71f001a..ffd12b8 100644
--- a/src/power/shutdown.c
+++ b/src/power/shutdown.c
@@ -162,24 +162,24 @@ int main(int argc, char *argv[]) {
         int cmd, r;
         static const char* const dirs[] = {SYSTEM_SHUTDOWN_PATH, NULL};
 
-        log_parse_environment();
-        r = parse_argv(argc, argv);
-        if (r < 0)
-                goto error;
+        /* we are executed by execve() (without fork())
+         * pid 1's main() at the bottom of src/core/main.c
+         */
+        assert(getpid() == 1);
 
+        log_set_target(LOG_TARGET_AUTO);
+        log_parse_environment();
         /* journald will die if not gone yet. The log target defaults
          * to console, but may have been changed by command line options. */
-
         log_close_console(); /* force reopen of /dev/console */
         log_open();
 
+        r = parse_argv(argc, argv);
+        assert(r >= 0);
+
         umask(0022);
 
-        if (getpid() != 1) {
-                log_error("Not executed by init (PID 1).");
-                r = -EPERM;
-                goto error;
-        }
+        in_container = detect_container(NULL) > 0;
 
         if (streq(arg_verb, "reboot"))
                 cmd = RB_AUTOBOOT;
diff --git a/src/power/shutdownd.c b/src/power/shutdownd.c
index 60a6468..742e1d5 100644
--- a/src/power/shutdownd.c
+++ b/src/power/shutdownd.c
@@ -264,15 +264,9 @@ int main(int argc, char *argv[]) {
         bool exec_shutdown = false, unlink_nologin = false;
         unsigned i;
 
-        if (getppid() != 1) {
-                log_error("This program should be invoked by init only.");
-                return EXIT_FAILURE;
-        }
-
-        if (argc > 1) {
-                log_error("This program does not take arguments.");
-                return EXIT_FAILURE;
-        }
+        /* we are executed through systemd-shutdownd.socket->systemd-shutdownd.service */
+        assert(getppid() == 1);
+        assert(argc == 0);
 
         log_set_target(LOG_TARGET_AUTO);
         log_parse_environment();
@@ -281,15 +275,7 @@ int main(int argc, char *argv[]) {
         umask(0022);
 
         n_fds = sd_listen_fds(true);
-        if (n_fds < 0) {
-                log_error_errno(r, "Failed to read listening file descriptors from environment: %m");
-                return EXIT_FAILURE;
-        }
-
-        if (n_fds != 1) {
-                log_error("Need exactly one file descriptor.");
-                return EXIT_FAILURE;
-        }
+        assert(n_fds == 1);
 
         pollfd[FD_SOCKET].fd = SD_LISTEN_FDS_START;
         pollfd[FD_SOCKET].events = POLLIN;
diff --git a/src/power/sleep.c b/src/power/sleep.c
index cc1ffa6..2462082 100644
--- a/src/power/sleep.c
+++ b/src/power/sleep.c
@@ -200,17 +200,25 @@ int main(int argc, char *argv[]) {
         _cleanup_strv_free_ char **modes = NULL, **states = NULL;
         int r;
 
+        /* we are executed through
+         *   - systemd-suspend.service
+         *   - systemd-hibernate.service
+         *   - systemd-hybrid-sleep.service
+         */
+        assert(getppid() == 1);
+
         log_set_target(LOG_TARGET_AUTO);
         log_parse_environment();
         log_open();
 
         r = parse_argv(argc, argv);
-        if (r <= 0)
-                goto finish;
+        assert(r > 0);
 
         r = parse_sleep_config(arg_verb, &modes, &states);
-        if (r < 0)
+        if (r < 0) {
+                log_error_errno(r, "Failed to parse " PKGSYSCONFDIR "/sleep.conf: %m");
                 goto finish;
+        }
 
         r = execute(modes, states);
 
-- 
2.2.1.209.g41e5f3a



More information about the systemd-devel mailing list