chiark / gitweb /
tree-wide: beautify remaining copyright statements
[elogind.git] / src / login / logind-utmp.c
index 9bbffe34c710a83a7c9200ac6f08cfc77cb74909..37ae05e8b75df636c079f5287ee4089563f904c4 100644 (file)
@@ -1,39 +1,27 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
-  This file is part of systemd.
-
-  Copyright 2015 Daniel Mack
-
-  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/>.
+  Copyright © 2015 Daniel Mack
 ***/
 
 #include <errno.h>
+#include <pwd.h>
 #include <string.h>
 #include <unistd.h>
-#include <pwd.h>
 
 #include "sd-messages.h"
-#include "strv.h"
-#include "special.h"
-#include "unit-name.h"
-#include "audit.h"
-#include "bus-util.h"
-#include "bus-error.h"
+
+#include "alloc-util.h"
+#include "audit-util.h"
 #include "bus-common-errors.h"
+#include "bus-error.h"
+#include "bus-util.h"
+#include "format-util.h"
 #include "logind.h"
-#include "formats-util.h"
+#include "path-util.h"
+//#include "special.h"
+#include "strv.h"
+#include "unit-name.h"
+#include "user-util.h"
 #include "utmp-wtmp.h"
 
 _const_ static usec_t when_wall(usec_t n, usec_t elapse) {
@@ -59,15 +47,19 @@ _const_ static usec_t when_wall(usec_t n, usec_t elapse) {
 }
 
 bool logind_wall_tty_filter(const char *tty, void *userdata) {
-
         Manager *m = userdata;
+        const char *p;
 
         assert(m);
 
-        if (!startswith(tty, "/dev/"))
+        if (!m->scheduled_shutdown_tty)
+                return true;
+
+        p = path_startswith(tty, "/dev/");
+        if (!p)
                 return true;
 
-        return !streq(tty + 5, m->scheduled_shutdown_tty);
+        return !streq(p, m->scheduled_shutdown_tty);
 }
 
 static int warn_wall(Manager *m, usec_t n) {
@@ -94,7 +86,7 @@ static int warn_wall(Manager *m, usec_t n) {
                 return 0;
         }
 
-        utmp_wall(l, lookup_uid(m->scheduled_shutdown_uid),
+        utmp_wall(l, uid_to_name(m->scheduled_shutdown_uid),
                   m->scheduled_shutdown_tty, logind_wall_tty_filter, m);
 
         return 1;
@@ -122,11 +114,11 @@ static int wall_message_timeout_handler(
         if (next > 0) {
                 r = sd_event_source_set_time(s, n + next);
                 if (r < 0)
-                        return log_error_errno(r, "sd_event_source_set_time() failed. %m\n");
+                        return log_error_errno(r, "sd_event_source_set_time() failed. %m");
 
                 r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT);
                 if (r < 0)
-                        return log_error_errno(r, "sd_event_source_set_enabled() failed. %m\n");
+                        return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
         }
 
         return 0;
@@ -135,10 +127,18 @@ static int wall_message_timeout_handler(
 int manager_setup_wall_message_timer(Manager *m) {
 
         usec_t n, elapse;
+#if 1 /// let's fix double wall messages in elogind - they suck.
+        usec_t left;
+#endif // 1
         int r;
 
         assert(m);
 
+#if 1 /// Make elogind more aware of the possibility to disable wall messages
+        /* Do not do anything if wall messages aren't enabled */
+        if (!m->enable_wall_messages)
+                return 0;
+#endif // 1
         n = now(CLOCK_REALTIME);
         elapse = m->scheduled_shutdown_timeout;
 
@@ -152,6 +152,7 @@ int manager_setup_wall_message_timer(Manager *m) {
         if (elapse < n)
                 return 0;
 
+#if 0 /// let's fix double wall messages in elogind - they suck.
         /* Warn immediately if less than 15 minutes are left */
         if (elapse - n < 15 * USEC_PER_MINUTE) {
                 r = warn_wall(m, n);
@@ -162,20 +163,37 @@ int manager_setup_wall_message_timer(Manager *m) {
         elapse = when_wall(n, elapse);
         if (elapse == 0)
                 return 0;
+#else
+        left = elapse - n;
+        elapse = when_wall(n, elapse);
+
+        /* Warn immediately if less than 15 minutes are left, but not if
+         * there aren't more than three seconds to the next timeout. */
+        if ( (left   < 15 * USEC_PER_MINUTE)
+          && (elapse >  3 * USEC_PER_SEC) ) {
+                r = warn_wall(m, n);
+                if (r == 0)
+                        return 0;
+        }
+
+        /* If the next timeout is within on second, delay it by 3 seconds */
+        if (USEC_PER_SEC > elapse)
+                elapse = 3 * USEC_PER_SEC;
+#endif // 0
 
         if (m->wall_message_timeout_source) {
                 r = sd_event_source_set_time(m->wall_message_timeout_source, n + elapse);
                 if (r < 0)
-                        return log_error_errno(r, "sd_event_source_set_time() failed. %m\n");
+                        return log_error_errno(r, "sd_event_source_set_time() failed. %m");
 
                 r = sd_event_source_set_enabled(m->wall_message_timeout_source, SD_EVENT_ONESHOT);
                 if (r < 0)
-                        return log_error_errno(r, "sd_event_source_set_enabled() failed. %m\n");
+                        return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
         } else {
                 r = sd_event_add_time(m->event, &m->wall_message_timeout_source,
                                       CLOCK_REALTIME, n + elapse, 0, wall_message_timeout_handler, m);
                 if (r < 0)
-                        return log_error_errno(r, "sd_event_add_time() failed. %m\n");
+                        return log_error_errno(r, "sd_event_add_time() failed. %m");
         }
 
         return 0;