chiark / gitweb /
logind: check return value from lseek
[elogind.git] / src / login / eloginctl.c
index 0fc4fc04e68bd955618049c83a6d1a69eba40d56..dd0a89d383290e8dcab89e7c577e1c32adfc4fa4 100644 (file)
@@ -25,6 +25,7 @@
 #include "parse-util.h"
 #include "process-util.h"
 #include "sd-login.h"
+#include "sd-messages.h"
 #include "spawn-polkit-agent.h"
 #include "string-util.h"
 #include "strv.h"
@@ -175,6 +176,14 @@ int elogind_cancel_shutdown(sd_bus *bus) {
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
         int r;
 
+        r = elogind_set_wall_message(bus, NULL);
+
+        if (r < 0) {
+                log_warning_errno(r, "Failed to set wall message, ignoring: %s",
+                                  bus_error_message(&error, r));
+                sd_bus_error_free(&error);
+        }
+
         r = sd_bus_call_method(
                         bus,
                         "org.freedesktop.login1",
@@ -194,6 +203,63 @@ void elogind_cleanup(void) {
         strv_free(arg_wall);
 }
 
+static void elogind_log_special(enum elogind_action a) {
+#ifdef ENABLE_DEBUG_ELOGIND
+        switch (a) {
+        case ACTION_HALT:
+                log_struct(LOG_INFO,
+                           LOG_MESSAGE("Halt action called."),
+                           LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
+                           NULL);
+                break;
+        case ACTION_POWEROFF:
+                log_struct(LOG_INFO,
+                           LOG_MESSAGE("Poweroff action called."),
+                           LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
+                           NULL);
+                break;
+        case ACTION_REBOOT:
+                log_struct(LOG_INFO,
+                           LOG_MESSAGE("Reboot action called."),
+                           LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
+                           NULL);
+                break;
+        case ACTION_KEXEC:
+                log_struct(LOG_INFO,
+                           LOG_MESSAGE("KExec action called."),
+                           LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
+                           NULL);
+                break;
+        case ACTION_SUSPEND:
+                log_struct(LOG_INFO,
+                           LOG_MESSAGE("Suspend action called."),
+                           LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_START),
+                           NULL);
+                break;
+        case ACTION_HIBERNATE:
+                log_struct(LOG_INFO,
+                           LOG_MESSAGE("Hibernate action called."),
+                           LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_START),
+                           NULL);
+                break;
+        case ACTION_HYBRID_SLEEP:
+                log_struct(LOG_INFO,
+                           LOG_MESSAGE("Hybrid-Sleep action called."),
+                           LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_START),
+                           NULL);
+                break;
+        case ACTION_CANCEL_SHUTDOWN:
+                log_struct(LOG_INFO,
+                           LOG_MESSAGE("Cancel Shutdown called."),
+                           LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
+                           NULL);
+                break;
+        default:
+                break;
+        }
+#endif // ENABLE_DEBUG_ELOGIND
+}
+
 static int elogind_reboot(sd_bus *bus, enum elogind_action a) {
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
         const char *method  = NULL;
@@ -206,8 +272,6 @@ static int elogind_reboot(sd_bus *bus, enum elogind_action a) {
         if (!bus)
                 return -EIO;
 
-        polkit_agent_open_if_enabled();
-
         switch (a) {
 
         case ACTION_POWEROFF:
@@ -234,12 +298,16 @@ static int elogind_reboot(sd_bus *bus, enum elogind_action a) {
                 return -EINVAL;
         }
 
-        r = elogind_set_wall_message(bus, table[a]);
+        polkit_agent_open_if_enabled();
 
-        if (r < 0) {
-                log_warning_errno(r, "Failed to set wall message, ignoring: %s",
-                                  bus_error_message(&error, r));
-                sd_bus_error_free(&error);
+        if ( IN_SET(a, ACTION_POWEROFF, ACTION_REBOOT) ) {
+                r = elogind_set_wall_message(bus, table[a]);
+
+                if (r < 0) {
+                        log_warning_errno(r, "Failed to set wall message, ignoring: %s",
+                                          bus_error_message(&error, r));
+                        sd_bus_error_free(&error);
+                }
         }
 
         /* Now call elogind itself to request the operation */
@@ -251,8 +319,7 @@ static int elogind_reboot(sd_bus *bus, enum elogind_action a) {
                         method,
                         &error,
                         NULL,
-                        "b",
-                        arg_ask_password);
+                        "b", arg_ask_password);
 
         if (r < 0)
                 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
@@ -268,8 +335,6 @@ static int elogind_schedule_shutdown(sd_bus *bus, enum elogind_action a) {
         if (!bus)
                 return -EIO;
 
-        polkit_agent_open_if_enabled();
-
         switch (a) {
 
         case ACTION_POWEROFF:
@@ -412,6 +477,44 @@ int start_special(int argc, char *argv[], void *userdata) {
 
         a = verb_to_action(argv[0]);
 
+        elogind_log_special(a);
+
+        /* For poweroff and reboot, some extra checks are performed: */
+        if ( IN_SET(a, ACTION_POWEROFF, ACTION_REBOOT) ) {
+
+                /* No power off actions in chroot environments */
+                if ( running_in_chroot() > 0 ) {
+                        log_info("Running in chroot, ignoring request.");
+                        return 0;
+                }
+
+                /* Check time argument */
+                if ( (argc > 1) && (ACTION_CANCEL_SHUTDOWN != arg_action)) {
+                        r = parse_shutdown_time_spec(argv[1], &arg_when);
+                        if (r < 0) {
+                                log_error("Failed to parse time specification: %s", argv[optind]);
+                                return r;
+                        }
+                }
+
+                /* The optional user wall message must be set */
+                if ( (argc > 1)
+                  && ( (arg_action == ACTION_CANCEL_SHUTDOWN)
+                    || (0 == arg_when) ) )
+                        /* No time argument for shutdown cancel, or no
+                         * time argument given. */
+                        wall = argv + 1;
+                else if (argc > 2)
+                        /* We skip the time argument */
+                        wall = argv + 2;
+
+                if (wall) {
+                        arg_wall = strv_copy(wall);
+                        if (!arg_wall)
+                                return log_oom();
+                }
+        }
+
         /* Switch to cancel shutdown, if a shutdown action was requested,
            and the option to cancel it was set: */
         if ( IN_SET(a, ACTION_POWEROFF, ACTION_REBOOT)
@@ -422,40 +525,6 @@ int start_special(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return r;
 
-        /* No power off actions in chroot environments */
-        if ((a == ACTION_POWEROFF ||
-             a == ACTION_REBOOT) &&
-            (running_in_chroot() > 0) ) {
-                log_info("Running in chroot, ignoring request.");
-                return 0;
-        }
-
-        /* Check time arguments */
-        if ( IN_SET(a, ACTION_POWEROFF, ACTION_REBOOT)
-          && (argc > 1)
-          && (arg_action != ACTION_CANCEL_SHUTDOWN) ) {
-                r = parse_shutdown_time_spec(argv[1], &arg_when);
-                if (r < 0) {
-                        log_error("Failed to parse time specification: %s", argv[optind]);
-                        return r;
-                }
-        } else
-                arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
-
-        /* The optional user wall message must be set */
-        if ((argc > 1) && (arg_action == ACTION_CANCEL_SHUTDOWN) )
-                /* No time argument for shutdown cancel */
-                wall = argv + 1;
-        else if (argc > 2)
-                /* We skip the time argument */
-                wall = argv + 2;
-
-        if (wall) {
-                arg_wall = strv_copy(wall);
-                if (!arg_wall)
-                        return log_oom();
-        }
-
         /* Perform requested action */
         if (IN_SET(a,
                    ACTION_POWEROFF,