LCOV - code coverage report
Current view: top level - login - logind-action.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 1 63 1.6 %
Date: 2019-08-23 13:36:53 Functions: 2 5 40.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 4 94 4.3 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : 
       3                 :            : #include <unistd.h>
       4                 :            : 
       5                 :            : #include "alloc-util.h"
       6                 :            : #include "bus-error.h"
       7                 :            : #include "bus-util.h"
       8                 :            : #include "conf-parser.h"
       9                 :            : #include "format-util.h"
      10                 :            : #include "logind-action.h"
      11                 :            : #include "logind-dbus.h"
      12                 :            : #include "logind-session-dbus.h"
      13                 :            : #include "process-util.h"
      14                 :            : #include "sleep-config.h"
      15                 :            : #include "special.h"
      16                 :            : #include "string-table.h"
      17                 :            : #include "terminal-util.h"
      18                 :            : #include "user-util.h"
      19                 :            : 
      20                 :          0 : const char* manager_target_for_action(HandleAction handle) {
      21                 :            :         static const char * const target_table[_HANDLE_ACTION_MAX] = {
      22                 :            :                 [HANDLE_POWEROFF] = SPECIAL_POWEROFF_TARGET,
      23                 :            :                 [HANDLE_REBOOT] = SPECIAL_REBOOT_TARGET,
      24                 :            :                 [HANDLE_HALT] = SPECIAL_HALT_TARGET,
      25                 :            :                 [HANDLE_KEXEC] = SPECIAL_KEXEC_TARGET,
      26                 :            :                 [HANDLE_SUSPEND] = SPECIAL_SUSPEND_TARGET,
      27                 :            :                 [HANDLE_HIBERNATE] = SPECIAL_HIBERNATE_TARGET,
      28                 :            :                 [HANDLE_HYBRID_SLEEP] = SPECIAL_HYBRID_SLEEP_TARGET,
      29                 :            :                 [HANDLE_SUSPEND_THEN_HIBERNATE] = SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET,
      30                 :            :         };
      31                 :            : 
      32         [ #  # ]:          0 :         assert(handle >= 0);
      33         [ #  # ]:          0 :         if (handle < (ssize_t) ELEMENTSOF(target_table))
      34                 :          0 :                 return target_table[handle];
      35                 :          0 :         return NULL;
      36                 :            : }
      37                 :            : 
      38                 :          0 : int manager_handle_action(
      39                 :            :                 Manager *m,
      40                 :            :                 InhibitWhat inhibit_key,
      41                 :            :                 HandleAction handle,
      42                 :            :                 bool ignore_inhibited,
      43                 :            :                 bool is_edge) {
      44                 :            : 
      45                 :            :         static const char * const message_table[_HANDLE_ACTION_MAX] = {
      46                 :            :                 [HANDLE_POWEROFF] = "Powering Off...",
      47                 :            :                 [HANDLE_REBOOT] = "Rebooting...",
      48                 :            :                 [HANDLE_HALT] = "Halting...",
      49                 :            :                 [HANDLE_KEXEC] = "Rebooting via kexec...",
      50                 :            :                 [HANDLE_SUSPEND] = "Suspending...",
      51                 :            :                 [HANDLE_HIBERNATE] = "Hibernating...",
      52                 :            :                 [HANDLE_HYBRID_SLEEP] = "Hibernating and suspending...",
      53                 :            :                 [HANDLE_SUSPEND_THEN_HIBERNATE] = "Suspending, then hibernating...",
      54                 :            :         };
      55                 :            : 
      56                 :          0 :         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
      57                 :            :         InhibitWhat inhibit_operation;
      58                 :          0 :         Inhibitor *offending = NULL;
      59                 :            :         bool supported;
      60                 :            :         const char *target;
      61                 :            :         int r;
      62                 :            : 
      63         [ #  # ]:          0 :         assert(m);
      64                 :            : 
      65                 :            :         /* If the key handling is turned off, don't do anything */
      66         [ #  # ]:          0 :         if (handle == HANDLE_IGNORE) {
      67         [ #  # ]:          0 :                 log_debug("Refusing operation, as it is turned off.");
      68                 :          0 :                 return 0;
      69                 :            :         }
      70                 :            : 
      71         [ #  # ]:          0 :         if (inhibit_key == INHIBIT_HANDLE_LID_SWITCH) {
      72                 :            :                 /* If the last system suspend or startup is too close,
      73                 :            :                  * let's not suspend for now, to give USB docking
      74                 :            :                  * stations some time to settle so that we can
      75                 :            :                  * properly watch its displays. */
      76         [ #  # ]:          0 :                 if (m->lid_switch_ignore_event_source) {
      77         [ #  # ]:          0 :                         log_debug("Ignoring lid switch request, system startup or resume too close.");
      78                 :          0 :                         return 0;
      79                 :            :                 }
      80                 :            :         }
      81                 :            : 
      82                 :            :         /* If the key handling is inhibited, don't do anything */
      83         [ #  # ]:          0 :         if (inhibit_key > 0) {
      84         [ #  # ]:          0 :                 if (manager_is_inhibited(m, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0, NULL)) {
      85         [ #  # ]:          0 :                         log_debug("Refusing %s operation, %s is inhibited.",
      86                 :            :                                   handle_action_to_string(handle),
      87                 :            :                                   inhibit_what_to_string(inhibit_key));
      88                 :          0 :                         return 0;
      89                 :            :                 }
      90                 :            :         }
      91                 :            : 
      92                 :            :         /* Locking is handled differently from the rest. */
      93         [ #  # ]:          0 :         if (handle == HANDLE_LOCK) {
      94         [ #  # ]:          0 :                 if (!is_edge)
      95                 :          0 :                         return 0;
      96                 :            : 
      97         [ #  # ]:          0 :                 log_info("Locking sessions...");
      98                 :          0 :                 session_send_lock_all(m, true);
      99                 :          0 :                 return 1;
     100                 :            :         }
     101                 :            : 
     102         [ #  # ]:          0 :         if (handle == HANDLE_SUSPEND)
     103                 :          0 :                 supported = can_sleep("suspend") > 0;
     104         [ #  # ]:          0 :         else if (handle == HANDLE_HIBERNATE)
     105                 :          0 :                 supported = can_sleep("hibernate") > 0;
     106         [ #  # ]:          0 :         else if (handle == HANDLE_HYBRID_SLEEP)
     107                 :          0 :                 supported = can_sleep("hybrid-sleep") > 0;
     108         [ #  # ]:          0 :         else if (handle == HANDLE_SUSPEND_THEN_HIBERNATE)
     109                 :          0 :                 supported = can_sleep("suspend-then-hibernate") > 0;
     110         [ #  # ]:          0 :         else if (handle == HANDLE_KEXEC)
     111                 :          0 :                 supported = access(KEXEC, X_OK) >= 0;
     112                 :            :         else
     113                 :          0 :                 supported = true;
     114                 :            : 
     115   [ #  #  #  #  :          0 :         if (!supported && IN_SET(handle, HANDLE_HIBERNATE, HANDLE_HYBRID_SLEEP, HANDLE_SUSPEND_THEN_HIBERNATE)) {
                   #  # ]
     116                 :          0 :                 supported = can_sleep("suspend") > 0;
     117         [ #  # ]:          0 :                 if (supported) {
     118         [ #  # ]:          0 :                         log_notice("Requested %s operation is not supported, using regular suspend instead.",
     119                 :            :                                    handle_action_to_string(handle));
     120                 :          0 :                         handle = HANDLE_SUSPEND;
     121                 :            :                 }
     122                 :            :         }
     123                 :            : 
     124         [ #  # ]:          0 :         if (!supported)
     125         [ #  # ]:          0 :                 return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
     126                 :            :                                          "Requested %s operation not supported, ignoring.", handle_action_to_string(handle));
     127                 :            : 
     128         [ #  # ]:          0 :         if (m->action_what > 0)
     129         [ #  # ]:          0 :                 return log_debug_errno(SYNTHETIC_ERRNO(EALREADY),
     130                 :            :                                        "Action already in progress (%s), ignoring requested %s operation.",
     131                 :            :                                        inhibit_what_to_string(m->action_what),
     132                 :            :                                        handle_action_to_string(handle));
     133                 :            : 
     134         [ #  # ]:          0 :         assert_se(target = manager_target_for_action(handle));
     135                 :            : 
     136         [ #  # ]:          0 :         inhibit_operation = IN_SET(handle, HANDLE_SUSPEND, HANDLE_HIBERNATE,
     137                 :            :                                            HANDLE_HYBRID_SLEEP,
     138         [ #  # ]:          0 :                                            HANDLE_SUSPEND_THEN_HIBERNATE) ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN;
     139                 :            : 
     140                 :            :         /* If the actual operation is inhibited, warn and fail */
     141   [ #  #  #  # ]:          0 :         if (!ignore_inhibited &&
     142                 :          0 :             manager_is_inhibited(m, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0, &offending)) {
     143                 :          0 :                 _cleanup_free_ char *comm = NULL, *u = NULL;
     144                 :            : 
     145                 :          0 :                 (void) get_process_comm(offending->pid, &comm);
     146                 :          0 :                 u = uid_to_name(offending->uid);
     147                 :            : 
     148                 :            :                 /* If this is just a recheck of the lid switch then don't warn about anything */
     149   [ #  #  #  # ]:          0 :                 log_full(is_edge ? LOG_ERR : LOG_DEBUG,
     150                 :            :                          "Refusing %s operation, %s is inhibited by UID "UID_FMT"/%s, PID "PID_FMT"/%s.",
     151                 :            :                          handle_action_to_string(handle),
     152                 :            :                          inhibit_what_to_string(inhibit_operation),
     153                 :            :                          offending->uid, strna(u),
     154                 :            :                          offending->pid, strna(comm));
     155                 :            : 
     156         [ #  # ]:          0 :                 return is_edge ? -EPERM : 0;
     157                 :            :         }
     158                 :            : 
     159         [ #  # ]:          0 :         log_info("%s", message_table[handle]);
     160                 :            : 
     161                 :          0 :         r = bus_manager_shutdown_or_sleep_now_or_later(m, target, inhibit_operation, &error);
     162         [ #  # ]:          0 :         if (r < 0)
     163         [ #  # ]:          0 :                 return log_error_errno(r, "Failed to execute %s operation: %s",
     164                 :            :                                        handle_action_to_string(handle),
     165                 :            :                                        bus_error_message(&error, r));
     166                 :            : 
     167                 :          0 :         return 1;
     168                 :            : }
     169                 :            : 
     170                 :            : static const char* const handle_action_table[_HANDLE_ACTION_MAX] = {
     171                 :            :         [HANDLE_IGNORE] = "ignore",
     172                 :            :         [HANDLE_POWEROFF] = "poweroff",
     173                 :            :         [HANDLE_REBOOT] = "reboot",
     174                 :            :         [HANDLE_HALT] = "halt",
     175                 :            :         [HANDLE_KEXEC] = "kexec",
     176                 :            :         [HANDLE_SUSPEND] = "suspend",
     177                 :            :         [HANDLE_HIBERNATE] = "hibernate",
     178                 :            :         [HANDLE_HYBRID_SLEEP] = "hybrid-sleep",
     179                 :            :         [HANDLE_SUSPEND_THEN_HIBERNATE] = "suspend-then-hibernate",
     180                 :            :         [HANDLE_LOCK] = "lock",
     181                 :            : };
     182                 :            : 
     183   [ +  +  +  + ]:         96 : DEFINE_STRING_TABLE_LOOKUP(handle_action, HandleAction);
     184   [ #  #  #  #  :          0 : DEFINE_CONFIG_PARSE_ENUM(config_parse_handle_action, handle_action, HandleAction, "Failed to parse handle action setting");
          #  #  #  #  #  
                #  #  # ]

Generated by: LCOV version 1.14