LCOV - code coverage report
Current view: top level - login - inhibit.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 24 144 16.7 %
Date: 2019-08-23 13:36:53 Functions: 4 6 66.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 9 113 8.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : 
       3                 :            : #include <fcntl.h>
       4                 :            : #include <getopt.h>
       5                 :            : #include <stdio.h>
       6                 :            : #include <stdlib.h>
       7                 :            : #include <unistd.h>
       8                 :            : 
       9                 :            : #include "sd-bus.h"
      10                 :            : 
      11                 :            : #include "alloc-util.h"
      12                 :            : #include "bus-error.h"
      13                 :            : #include "bus-util.h"
      14                 :            : #include "fd-util.h"
      15                 :            : #include "format-table.h"
      16                 :            : #include "format-util.h"
      17                 :            : #include "main-func.h"
      18                 :            : #include "pager.h"
      19                 :            : #include "pretty-print.h"
      20                 :            : #include "process-util.h"
      21                 :            : #include "signal-util.h"
      22                 :            : #include "strv.h"
      23                 :            : #include "user-util.h"
      24                 :            : #include "util.h"
      25                 :            : 
      26                 :            : static const char* arg_what = "idle:sleep:shutdown";
      27                 :            : static const char* arg_who = NULL;
      28                 :            : static const char* arg_why = "Unknown reason";
      29                 :            : static const char* arg_mode = NULL;
      30                 :            : static PagerFlags arg_pager_flags = 0;
      31                 :            : static bool arg_legend = true;
      32                 :            : 
      33                 :            : static enum {
      34                 :            :         ACTION_INHIBIT,
      35                 :            :         ACTION_LIST
      36                 :            : } arg_action = ACTION_INHIBIT;
      37                 :            : 
      38                 :          0 : static int inhibit(sd_bus *bus, sd_bus_error *error) {
      39                 :          0 :         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
      40                 :            :         int r;
      41                 :            :         int fd;
      42                 :            : 
      43                 :          0 :         r = sd_bus_call_method(
      44                 :            :                         bus,
      45                 :            :                         "org.freedesktop.login1",
      46                 :            :                         "/org/freedesktop/login1",
      47                 :            :                         "org.freedesktop.login1.Manager",
      48                 :            :                         "Inhibit",
      49                 :            :                         error,
      50                 :            :                         &reply,
      51                 :            :                         "ssss", arg_what, arg_who, arg_why, arg_mode);
      52         [ #  # ]:          0 :         if (r < 0)
      53                 :          0 :                 return r;
      54                 :            : 
      55                 :          0 :         r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
      56         [ #  # ]:          0 :         if (r < 0)
      57                 :          0 :                 return r;
      58                 :            : 
      59                 :          0 :         r = fcntl(fd, F_DUPFD_CLOEXEC, 3);
      60         [ #  # ]:          0 :         if (r < 0)
      61                 :          0 :                 return -errno;
      62                 :            : 
      63                 :          0 :         return r;
      64                 :            : }
      65                 :            : 
      66                 :          0 : static int print_inhibitors(sd_bus *bus) {
      67                 :          0 :         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
      68                 :          0 :         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
      69                 :          0 :         _cleanup_(table_unrefp) Table *table = NULL;
      70                 :            :         int r;
      71                 :            : 
      72                 :          0 :         (void) pager_open(arg_pager_flags);
      73                 :            : 
      74                 :          0 :         r = sd_bus_call_method(
      75                 :            :                         bus,
      76                 :            :                         "org.freedesktop.login1",
      77                 :            :                         "/org/freedesktop/login1",
      78                 :            :                         "org.freedesktop.login1.Manager",
      79                 :            :                         "ListInhibitors",
      80                 :            :                         &error,
      81                 :            :                         &reply,
      82                 :            :                         "");
      83         [ #  # ]:          0 :         if (r < 0)
      84         [ #  # ]:          0 :                 return log_error_errno(r, "Could not get active inhibitors: %s", bus_error_message(&error, r));
      85                 :            : 
      86                 :          0 :         table = table_new("who", "uid", "user", "pid", "comm", "what", "why", "mode");
      87         [ #  # ]:          0 :         if (!table)
      88                 :          0 :                 return log_oom();
      89                 :            : 
      90                 :            :         /* If there's not enough space, shorten the "WHY" column, as it's little more than an explaining comment. */
      91                 :          0 :         (void) table_set_weight(table, TABLE_HEADER_CELL(6), 20);
      92                 :            : 
      93                 :          0 :         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
      94         [ #  # ]:          0 :         if (r < 0)
      95         [ #  # ]:          0 :                 return bus_log_parse_error(r);
      96                 :            : 
      97                 :          0 :         for (;;) {
      98   [ #  #  #  #  :          0 :                 _cleanup_free_ char *comm = NULL, *u = NULL;
             #  #  #  # ]
      99                 :            :                 const char *what, *who, *why, *mode;
     100                 :            :                 uint32_t uid, pid;
     101                 :            : 
     102                 :          0 :                 r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid);
     103         [ #  # ]:          0 :                 if (r < 0)
     104         [ #  # ]:          0 :                         return bus_log_parse_error(r);
     105         [ #  # ]:          0 :                 if (r == 0)
     106                 :          0 :                         break;
     107                 :            : 
     108   [ #  #  #  # ]:          0 :                 if (arg_mode && !streq(mode, arg_mode))
     109                 :          0 :                         continue;
     110                 :            : 
     111                 :          0 :                 (void) get_process_comm(pid, &comm);
     112                 :          0 :                 u = uid_to_name(uid);
     113                 :            : 
     114                 :          0 :                 r = table_add_many(table,
     115                 :            :                                    TABLE_STRING, who,
     116                 :            :                                    TABLE_UINT32, uid,
     117                 :            :                                    TABLE_STRING, strna(u),
     118                 :            :                                    TABLE_UINT32, pid,
     119                 :            :                                    TABLE_STRING, strna(comm),
     120                 :            :                                    TABLE_STRING, what,
     121                 :            :                                    TABLE_STRING, why,
     122                 :            :                                    TABLE_STRING, mode);
     123         [ #  # ]:          0 :                 if (r < 0)
     124         [ #  # ]:          0 :                         return log_error_errno(r, "Failed to add table row: %m");
     125                 :            :         }
     126                 :            : 
     127                 :          0 :         r = sd_bus_message_exit_container(reply);
     128         [ #  # ]:          0 :         if (r < 0)
     129         [ #  # ]:          0 :                 return bus_log_parse_error(r);
     130                 :            : 
     131         [ #  # ]:          0 :         if (table_get_rows(table) > 1) {
     132                 :          0 :                 r = table_set_sort(table, (size_t) 1, (size_t) 0, (size_t) 5, (size_t) 6, (size_t) -1);
     133         [ #  # ]:          0 :                 if (r < 0)
     134         [ #  # ]:          0 :                         return log_error_errno(r, "Failed to sort table: %m");
     135                 :            : 
     136                 :          0 :                 table_set_header(table, arg_legend);
     137                 :            : 
     138                 :          0 :                 r = table_print(table, NULL);
     139         [ #  # ]:          0 :                 if (r < 0)
     140         [ #  # ]:          0 :                         return log_error_errno(r, "Failed to show table: %m");
     141                 :            :         }
     142                 :            : 
     143         [ #  # ]:          0 :         if (arg_legend) {
     144         [ #  # ]:          0 :                 if (table_get_rows(table) > 1)
     145                 :          0 :                         printf("\n%zu inhibitors listed.\n", table_get_rows(table) - 1);
     146                 :            :                 else
     147                 :          0 :                         printf("No inhibitors.\n");
     148                 :            :         }
     149                 :            : 
     150                 :          0 :         return 0;
     151                 :            : }
     152                 :            : 
     153                 :         12 : static int help(void) {
     154                 :         12 :         _cleanup_free_ char *link = NULL;
     155                 :            :         int r;
     156                 :            : 
     157                 :         12 :         r = terminal_urlify_man("systemd-inhibit", "1", &link);
     158         [ -  + ]:         12 :         if (r < 0)
     159                 :          0 :                 return log_oom();
     160                 :            : 
     161                 :         12 :         printf("%s [OPTIONS...] {COMMAND} ...\n\n"
     162                 :            :                "Execute a process while inhibiting shutdown/sleep/idle.\n\n"
     163                 :            :                "  -h --help               Show this help\n"
     164                 :            :                "     --version            Show package version\n"
     165                 :            :                "     --no-pager           Do not pipe output into a pager\n"
     166                 :            :                "     --no-legend          Do not show the headers and footers\n"
     167                 :            :                "     --what=WHAT          Operations to inhibit, colon separated list of:\n"
     168                 :            :                "                          shutdown, sleep, idle, handle-power-key,\n"
     169                 :            :                "                          handle-suspend-key, handle-hibernate-key,\n"
     170                 :            :                "                          handle-lid-switch\n"
     171                 :            :                "     --who=STRING         A descriptive string who is inhibiting\n"
     172                 :            :                "     --why=STRING         A descriptive string why is being inhibited\n"
     173                 :            :                "     --mode=MODE          One of block or delay\n"
     174                 :            :                "     --list               List active inhibitors\n"
     175                 :            :                "\nSee the %s for details.\n"
     176                 :            :                , program_invocation_short_name
     177                 :            :                , link
     178                 :            :         );
     179                 :            : 
     180                 :         12 :         return 0;
     181                 :            : }
     182                 :            : 
     183                 :         16 : static int parse_argv(int argc, char *argv[]) {
     184                 :            : 
     185                 :            :         enum {
     186                 :            :                 ARG_VERSION = 0x100,
     187                 :            :                 ARG_WHAT,
     188                 :            :                 ARG_WHO,
     189                 :            :                 ARG_WHY,
     190                 :            :                 ARG_MODE,
     191                 :            :                 ARG_LIST,
     192                 :            :                 ARG_NO_PAGER,
     193                 :            :                 ARG_NO_LEGEND,
     194                 :            :         };
     195                 :            : 
     196                 :            :         static const struct option options[] = {
     197                 :            :                 { "help",         no_argument,       NULL, 'h'              },
     198                 :            :                 { "version",      no_argument,       NULL, ARG_VERSION      },
     199                 :            :                 { "what",         required_argument, NULL, ARG_WHAT         },
     200                 :            :                 { "who",          required_argument, NULL, ARG_WHO          },
     201                 :            :                 { "why",          required_argument, NULL, ARG_WHY          },
     202                 :            :                 { "mode",         required_argument, NULL, ARG_MODE         },
     203                 :            :                 { "list",         no_argument,       NULL, ARG_LIST         },
     204                 :            :                 { "no-pager",     no_argument,       NULL, ARG_NO_PAGER     },
     205                 :            :                 { "no-legend",    no_argument,       NULL, ARG_NO_LEGEND       },
     206                 :            :                 {}
     207                 :            :         };
     208                 :            : 
     209                 :            :         int c;
     210                 :            : 
     211         [ -  + ]:         16 :         assert(argc >= 0);
     212         [ -  + ]:         16 :         assert(argv);
     213                 :            : 
     214         [ +  - ]:         16 :         while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
     215                 :            : 
     216   [ +  -  -  -  :         16 :                 switch (c) {
          -  -  -  -  -  
                   +  - ]
     217                 :            : 
     218                 :         12 :                 case 'h':
     219                 :         12 :                         return help();
     220                 :            : 
     221                 :          0 :                 case ARG_VERSION:
     222                 :          0 :                         return version();
     223                 :            : 
     224                 :          0 :                 case ARG_WHAT:
     225                 :          0 :                         arg_what = optarg;
     226                 :          0 :                         break;
     227                 :            : 
     228                 :          0 :                 case ARG_WHO:
     229                 :          0 :                         arg_who = optarg;
     230                 :          0 :                         break;
     231                 :            : 
     232                 :          0 :                 case ARG_WHY:
     233                 :          0 :                         arg_why = optarg;
     234                 :          0 :                         break;
     235                 :            : 
     236                 :          0 :                 case ARG_MODE:
     237                 :          0 :                         arg_mode = optarg;
     238                 :          0 :                         break;
     239                 :            : 
     240                 :          0 :                 case ARG_LIST:
     241                 :          0 :                         arg_action = ACTION_LIST;
     242                 :          0 :                         break;
     243                 :            : 
     244                 :          0 :                 case ARG_NO_PAGER:
     245                 :          0 :                         arg_pager_flags |= PAGER_DISABLE;
     246                 :          0 :                         break;
     247                 :            : 
     248                 :          0 :                 case ARG_NO_LEGEND:
     249                 :          0 :                         arg_legend = false;
     250                 :          0 :                         break;
     251                 :            : 
     252                 :          4 :                 case '?':
     253                 :          4 :                         return -EINVAL;
     254                 :            : 
     255                 :          0 :                 default:
     256                 :          0 :                         assert_not_reached("Unhandled option");
     257                 :            :                 }
     258                 :            : 
     259   [ #  #  #  # ]:          0 :         if (arg_action == ACTION_INHIBIT && optind == argc)
     260                 :          0 :                 arg_action = ACTION_LIST;
     261                 :            : 
     262   [ #  #  #  # ]:          0 :         else if (arg_action == ACTION_INHIBIT && optind >= argc)
     263         [ #  # ]:          0 :                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
     264                 :            :                                        "Missing command line to execute.");
     265                 :            : 
     266                 :          0 :         return 1;
     267                 :            : }
     268                 :            : 
     269                 :         16 : static int run(int argc, char *argv[]) {
     270                 :         16 :         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
     271                 :            :         int r;
     272                 :            : 
     273                 :         16 :         log_show_color(true);
     274                 :         16 :         log_parse_environment();
     275                 :         16 :         log_open();
     276                 :            : 
     277                 :         16 :         r = parse_argv(argc, argv);
     278         [ +  - ]:         16 :         if (r <= 0)
     279                 :         16 :                 return r;
     280                 :            : 
     281                 :          0 :         r = sd_bus_default_system(&bus);
     282         [ #  # ]:          0 :         if (r < 0)
     283         [ #  # ]:          0 :                 return log_error_errno(r, "Failed to connect to bus: %m");
     284                 :            : 
     285         [ #  # ]:          0 :         if (arg_action == ACTION_LIST)
     286                 :          0 :                 return print_inhibitors(bus);
     287                 :            :         else {
     288                 :          0 :                 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
     289                 :          0 :                 _cleanup_strv_free_ char **arguments = NULL;
     290                 :          0 :                 _cleanup_free_ char *w = NULL;
     291                 :          0 :                 _cleanup_close_ int fd = -1;
     292                 :            :                 pid_t pid;
     293                 :            : 
     294                 :            :                 /* Ignore SIGINT and allow the forked process to receive it */
     295                 :          0 :                 (void) ignore_signals(SIGINT, -1);
     296                 :            : 
     297         [ #  # ]:          0 :                 if (!arg_who) {
     298                 :          0 :                         w = strv_join(argv + optind, " ");
     299         [ #  # ]:          0 :                         if (!w)
     300                 :          0 :                                 return log_oom();
     301                 :            : 
     302                 :          0 :                         arg_who = w;
     303                 :            :                 }
     304                 :            : 
     305         [ #  # ]:          0 :                 if (!arg_mode)
     306                 :          0 :                         arg_mode = "block";
     307                 :            : 
     308                 :          0 :                 fd = inhibit(bus, &error);
     309         [ #  # ]:          0 :                 if (fd < 0)
     310         [ #  # ]:          0 :                         return log_error_errno(fd, "Failed to inhibit: %s", bus_error_message(&error, fd));
     311                 :            : 
     312                 :          0 :                 arguments = strv_copy(argv + optind);
     313         [ #  # ]:          0 :                 if (!arguments)
     314                 :          0 :                         return log_oom();
     315                 :            : 
     316                 :          0 :                 r = safe_fork("(inhibit)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
     317         [ #  # ]:          0 :                 if (r < 0)
     318                 :          0 :                         return r;
     319         [ #  # ]:          0 :                 if (r == 0) {
     320                 :            :                         /* Child */
     321                 :          0 :                         execvp(arguments[0], arguments);
     322                 :          0 :                         log_open();
     323         [ #  # ]:          0 :                         log_error_errno(errno, "Failed to execute %s: %m", argv[optind]);
     324                 :          0 :                         _exit(EXIT_FAILURE);
     325                 :            :                 }
     326                 :            : 
     327                 :          0 :                 return wait_for_terminate_and_check(argv[optind], pid, WAIT_LOG);
     328                 :            :         }
     329                 :            : }
     330                 :            : 
     331         [ +  + ]:         16 : DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);

Generated by: LCOV version 1.14