LCOV - code coverage report
Current view: top level - login - logind-utmp.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 0 78 0.0 %
Date: 2019-08-23 13:36:53 Functions: 0 5 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 66 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : 
       3                 :            : #include <errno.h>
       4                 :            : #include <pwd.h>
       5                 :            : #include <string.h>
       6                 :            : #include <unistd.h>
       7                 :            : 
       8                 :            : #include "sd-messages.h"
       9                 :            : 
      10                 :            : #include "alloc-util.h"
      11                 :            : #include "audit-util.h"
      12                 :            : #include "bus-common-errors.h"
      13                 :            : #include "bus-error.h"
      14                 :            : #include "bus-util.h"
      15                 :            : #include "format-util.h"
      16                 :            : #include "logind.h"
      17                 :            : #include "path-util.h"
      18                 :            : #include "special.h"
      19                 :            : #include "strv.h"
      20                 :            : #include "unit-name.h"
      21                 :            : #include "user-util.h"
      22                 :            : #include "utmp-wtmp.h"
      23                 :            : 
      24                 :          0 : _const_ static usec_t when_wall(usec_t n, usec_t elapse) {
      25                 :            : 
      26                 :            :         usec_t left;
      27                 :            :         unsigned i;
      28                 :            :         static const int wall_timers[] = {
      29                 :            :                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
      30                 :            :                 25, 40, 55, 70, 100, 130, 150, 180,
      31                 :            :         };
      32                 :            : 
      33                 :            :         /* If the time is already passed, then don't announce */
      34         [ #  # ]:          0 :         if (n >= elapse)
      35                 :          0 :                 return 0;
      36                 :            : 
      37                 :          0 :         left = elapse - n;
      38                 :            : 
      39         [ #  # ]:          0 :         for (i = 1; i < ELEMENTSOF(wall_timers); i++)
      40         [ #  # ]:          0 :                 if (wall_timers[i] * USEC_PER_MINUTE >= left)
      41                 :          0 :                         return left - wall_timers[i-1] * USEC_PER_MINUTE;
      42                 :            : 
      43                 :          0 :         return left % USEC_PER_HOUR;
      44                 :            : }
      45                 :            : 
      46                 :          0 : bool logind_wall_tty_filter(const char *tty, void *userdata) {
      47                 :          0 :         Manager *m = userdata;
      48                 :            :         const char *p;
      49                 :            : 
      50         [ #  # ]:          0 :         assert(m);
      51                 :            : 
      52         [ #  # ]:          0 :         if (!m->scheduled_shutdown_tty)
      53                 :          0 :                 return true;
      54                 :            : 
      55                 :          0 :         p = path_startswith(tty, "/dev/");
      56         [ #  # ]:          0 :         if (!p)
      57                 :          0 :                 return true;
      58                 :            : 
      59                 :          0 :         return !streq(p, m->scheduled_shutdown_tty);
      60                 :            : }
      61                 :            : 
      62                 :          0 : static int warn_wall(Manager *m, usec_t n) {
      63                 :          0 :         char date[FORMAT_TIMESTAMP_MAX] = {};
      64                 :          0 :         _cleanup_free_ char *l = NULL, *username = NULL;
      65                 :            :         usec_t left;
      66                 :            :         int r;
      67                 :            : 
      68         [ #  # ]:          0 :         assert(m);
      69                 :            : 
      70         [ #  # ]:          0 :         if (!m->enable_wall_messages)
      71                 :          0 :                 return 0;
      72                 :            : 
      73                 :          0 :         left = m->scheduled_shutdown_timeout > n;
      74                 :            : 
      75   [ #  #  #  #  :          0 :         r = asprintf(&l, "%s%sThe system is going down for %s %s%s!",
                   #  # ]
      76                 :          0 :                      strempty(m->wall_message),
      77                 :          0 :                      isempty(m->wall_message) ? "" : "\n",
      78                 :            :                      m->scheduled_shutdown_type,
      79                 :            :                      left ? "at " : "NOW",
      80                 :          0 :                      left ? format_timestamp(date, sizeof(date), m->scheduled_shutdown_timeout) : "");
      81         [ #  # ]:          0 :         if (r < 0) {
      82                 :          0 :                 log_oom();
      83                 :          0 :                 return 0;
      84                 :            :         }
      85                 :            : 
      86                 :          0 :         username = uid_to_name(m->scheduled_shutdown_uid);
      87                 :          0 :         utmp_wall(l, username, m->scheduled_shutdown_tty, logind_wall_tty_filter, m);
      88                 :            : 
      89                 :          0 :         return 1;
      90                 :            : }
      91                 :            : 
      92                 :          0 : static int wall_message_timeout_handler(
      93                 :            :                         sd_event_source *s,
      94                 :            :                         uint64_t usec,
      95                 :            :                         void *userdata) {
      96                 :            : 
      97                 :          0 :         Manager *m = userdata;
      98                 :            :         usec_t n, next;
      99                 :            :         int r;
     100                 :            : 
     101         [ #  # ]:          0 :         assert(m);
     102         [ #  # ]:          0 :         assert(s == m->wall_message_timeout_source);
     103                 :            : 
     104                 :          0 :         n = now(CLOCK_REALTIME);
     105                 :            : 
     106                 :          0 :         r = warn_wall(m, n);
     107         [ #  # ]:          0 :         if (r == 0)
     108                 :          0 :                 return 0;
     109                 :            : 
     110                 :          0 :         next = when_wall(n, m->scheduled_shutdown_timeout);
     111         [ #  # ]:          0 :         if (next > 0) {
     112                 :          0 :                 r = sd_event_source_set_time(s, n + next);
     113         [ #  # ]:          0 :                 if (r < 0)
     114         [ #  # ]:          0 :                         return log_error_errno(r, "sd_event_source_set_time() failed. %m");
     115                 :            : 
     116                 :          0 :                 r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT);
     117         [ #  # ]:          0 :                 if (r < 0)
     118         [ #  # ]:          0 :                         return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
     119                 :            :         }
     120                 :            : 
     121                 :          0 :         return 0;
     122                 :            : }
     123                 :            : 
     124                 :          0 : int manager_setup_wall_message_timer(Manager *m) {
     125                 :            : 
     126                 :            :         usec_t n, elapse;
     127                 :            :         int r;
     128                 :            : 
     129         [ #  # ]:          0 :         assert(m);
     130                 :            : 
     131                 :          0 :         n = now(CLOCK_REALTIME);
     132                 :          0 :         elapse = m->scheduled_shutdown_timeout;
     133                 :            : 
     134                 :            :         /* wall message handling */
     135                 :            : 
     136         [ #  # ]:          0 :         if (isempty(m->scheduled_shutdown_type)) {
     137                 :          0 :                 warn_wall(m, n);
     138                 :          0 :                 return 0;
     139                 :            :         }
     140                 :            : 
     141         [ #  # ]:          0 :         if (elapse < n)
     142                 :          0 :                 return 0;
     143                 :            : 
     144                 :            :         /* Warn immediately if less than 15 minutes are left */
     145         [ #  # ]:          0 :         if (elapse - n < 15 * USEC_PER_MINUTE) {
     146                 :          0 :                 r = warn_wall(m, n);
     147         [ #  # ]:          0 :                 if (r == 0)
     148                 :          0 :                         return 0;
     149                 :            :         }
     150                 :            : 
     151                 :          0 :         elapse = when_wall(n, elapse);
     152         [ #  # ]:          0 :         if (elapse == 0)
     153                 :          0 :                 return 0;
     154                 :            : 
     155         [ #  # ]:          0 :         if (m->wall_message_timeout_source) {
     156                 :          0 :                 r = sd_event_source_set_time(m->wall_message_timeout_source, n + elapse);
     157         [ #  # ]:          0 :                 if (r < 0)
     158         [ #  # ]:          0 :                         return log_error_errno(r, "sd_event_source_set_time() failed. %m");
     159                 :            : 
     160                 :          0 :                 r = sd_event_source_set_enabled(m->wall_message_timeout_source, SD_EVENT_ONESHOT);
     161         [ #  # ]:          0 :                 if (r < 0)
     162         [ #  # ]:          0 :                         return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
     163                 :            :         } else {
     164                 :          0 :                 r = sd_event_add_time(m->event, &m->wall_message_timeout_source,
     165                 :            :                                       CLOCK_REALTIME, n + elapse, 0, wall_message_timeout_handler, m);
     166         [ #  # ]:          0 :                 if (r < 0)
     167         [ #  # ]:          0 :                         return log_error_errno(r, "sd_event_add_time() failed. %m");
     168                 :            :         }
     169                 :            : 
     170                 :          0 :         return 0;
     171                 :            : }

Generated by: LCOV version 1.14