]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/initctl/initctl.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / initctl / initctl.c
index 3e57afb9971c02e124270e0678adf033aff8db7e..260dc2eb618b7092d93d298ae76b851a740baf4c 100644 (file)
@@ -1,21 +1,4 @@
-/***
-  This file is part of systemd.
-
-  Copyright 2010 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
+/* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include <ctype.h>
 #include <errno.h>
 #include "bus-util.h"
 #include "def.h"
 #include "fd-util.h"
-#include "formats-util.h"
+#include "format-util.h"
 #include "initreq.h"
 #include "list.h"
 #include "log.h"
 #include "special.h"
 #include "util.h"
+#include "process-util.h"
 
 #define SERVER_FD_MAX 16
 #define TIMEOUT_MSEC ((int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC))
@@ -97,7 +81,7 @@ static const char *translate_runlevel(int runlevel, bool *isolate) {
         return NULL;
 }
 
-static void change_runlevel(Server *s, int runlevel) {
+static int change_runlevel(Server *s, int runlevel) {
         const char *target;
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
         const char *mode;
@@ -109,7 +93,7 @@ static void change_runlevel(Server *s, int runlevel) {
         target = translate_runlevel(runlevel, &isolate);
         if (!target) {
                 log_warning("Got request for unknown runlevel %c, ignoring.", runlevel);
-                return;
+                return 0;
         }
 
         if (isolate)
@@ -128,10 +112,10 @@ static void change_runlevel(Server *s, int runlevel) {
                         &error,
                         NULL,
                         "ss", target, mode);
-        if (r < 0) {
-                log_error("Failed to change runlevel: %s", bus_error_message(&error, -r));
-                return;
-        }
+        if (r < 0)
+                return log_error_errno(r, "Failed to change runlevel: %s", bus_error_message(&error, -r));
+
+        return 0;
 }
 
 static void request_process(Server *s, const struct init_request *req) {
@@ -172,7 +156,7 @@ static void request_process(Server *s, const struct init_request *req) {
                                 break;
 
                         default:
-                                change_runlevel(s, req->runlevel);
+                                (void) change_runlevel(s, req->runlevel);
                         }
                 return;
 
@@ -249,12 +233,8 @@ static void server_done(Server *s) {
         while (s->fifos)
                 fifo_free(s->fifos);
 
-        safe_close(s->epoll_fd);
-
-        if (s->bus) {
-                sd_bus_flush(s->bus);
-                sd_bus_unref(s->bus);
-        }
+        s->epoll_fd = safe_close(s->epoll_fd);
+        s->bus = sd_bus_flush_close_unref(s->bus);
 }
 
 static int server_init(Server *s, unsigned n_sockets) {
@@ -314,7 +294,7 @@ static int server_init(Server *s, unsigned n_sockets) {
                 f->fd = fd;
                 LIST_PREPEND(fifo, s->fifos, f);
                 f->server = s;
-                s->n_fifos ++;
+                s->n_fifos++;
         }
 
         r = bus_connect_system_systemd(&s->bus);
@@ -338,10 +318,9 @@ static int process_event(Server *s, struct epoll_event *ev) {
 
         assert(s);
 
-        if (!(ev->events & EPOLLIN)) {
-                log_info("Got invalid event from epoll. (3)");
-                return -EIO;
-        }
+        if (!(ev->events & EPOLLIN))
+                return log_info_errno(SYNTHETIC_ERRNO(EIO),
+                                      "Got invalid event from epoll. (3)");
 
         f = (Fifo*) ev->data.ptr;
         r = fifo_process(f);
@@ -368,9 +347,7 @@ int main(int argc, char *argv[]) {
                 return EXIT_FAILURE;
         }
 
-        log_set_target(LOG_TARGET_AUTO);
-        log_parse_environment();
-        log_open();
+        log_setup_service();
 
         umask(0022);
 
@@ -388,7 +365,7 @@ int main(int argc, char *argv[]) {
         if (server_init(&server, (unsigned) n) < 0)
                 return EXIT_FAILURE;
 
-        log_debug("systemd-initctl running as pid "PID_FMT, getpid());
+        log_debug("systemd-initctl running as pid "PID_FMT, getpid_cached());
 
         sd_notify(false,
                   "READY=1\n"
@@ -415,7 +392,7 @@ int main(int argc, char *argv[]) {
 
         r = EXIT_SUCCESS;
 
-        log_debug("systemd-initctl stopped as pid "PID_FMT, getpid());
+        log_debug("systemd-initctl stopped as pid "PID_FMT, getpid_cached());
 
 fail:
         sd_notify(false,