]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
login,user-sessions: always warn when we fail to remove nologin file
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 16 Feb 2018 05:58:33 +0000 (06:58 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 16 Feb 2018 09:44:22 +0000 (10:44 +0100)
This usually is very annoying to users who then cannot log in, so
make sure we always warn if that happens (selinux, or whatever other reason).

This reverts a790812cb349c5cef95d1b4a20fc80ca08d3a145.

src/basic/fs-util.c
src/basic/fs-util.h
src/login/logind-dbus.c
src/login/logind.c
src/user-sessions/user-sessions.c

index c96c7d0d25c99ea8200df1661221d0c6140f7368..47edcbb04f1556fd5619352095390c2fa8eef264 100644 (file)
@@ -554,6 +554,17 @@ int tmp_dir(const char **ret) {
         return tmp_dir_internal("/tmp", ret);
 }
 
+int unlink_or_warn(const char *filename) {
+        if (unlink(filename) < 0 && errno != ENOENT)
+                /* If the file doesn't exist and the fs simply was read-only (in which
+                 * case unlink() returns EROFS even if the file doesn't exist), don't
+                 * complain */
+                if (errno != EROFS || access(filename, F_OK) >= 0)
+                        return log_error_errno(errno, "Failed to remove \"%s\": %m", filename);
+
+        return 0;
+}
+
 int inotify_add_watch_fd(int fd, int what, uint32_t mask) {
         char path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int) + 1];
         int r;
index ae40d6d37fdff66b848f916c2a8ad0378a47bfd3..acb83dfd83a030ab030643cc3484d6140cb3452e 100644 (file)
@@ -64,6 +64,8 @@ int get_files_in_directory(const char *path, char ***list);
 int tmp_dir(const char **ret);
 int var_tmp_dir(const char **ret);
 
+int unlink_or_warn(const char *filename);
+
 #define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
 
 #define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
index ed9eba1ee05156404931f72ac805b3a065377d22..aa4bbf773977d79288ac8c7a3daad2e01a400a47 100644 (file)
@@ -2010,7 +2010,7 @@ static void reset_scheduled_shutdown(Manager *m) {
         m->shutdown_dry_run = false;
 
         if (m->unlink_nologin) {
-                (void) unlink("/run/nologin");
+                (void) unlink_or_warn("/run/nologin");
                 m->unlink_nologin = false;
         }
 
index d9b5f026cff8a09fefd2abcca51ae50a9feeadb8..0a869ba447d58cf18db269ac4f06ed11563a0b04 100644 (file)
@@ -35,6 +35,7 @@
 #include "dirent-util.h"
 #include "fd-util.h"
 #include "format-util.h"
+#include "fs-util.h"
 #include "logind.h"
 #include "process-util.h"
 #include "selinux-util.h"
@@ -183,7 +184,7 @@ static void manager_free(Manager *m) {
         udev_unref(m->udev);
 
         if (m->unlink_nologin)
-                (void) unlink("/run/nologin");
+                (void) unlink_or_warn("/run/nologin");
 
         bus_verify_polkit_async_registry_free(m->polkit_registry);
 
index 795766a6573a80a0894acfdd91a4fe8bb574710b..1aed9adae732534cc7170e5fe86eb4621fb533cc 100644 (file)
 
 #include "fileio.h"
 #include "fileio-label.h"
+#include "fs-util.h"
 #include "log.h"
 #include "selinux-util.h"
 #include "string-util.h"
 #include "util.h"
 
 int main(int argc, char*argv[]) {
+        int r, k;
 
         if (argc != 2) {
                 log_error("This program requires one argument.");
@@ -44,30 +46,12 @@ int main(int argc, char*argv[]) {
         mac_selinux_init();
 
         if (streq(argv[1], "start")) {
-                int r = 0;
-
-                if (unlink("/run/nologin") < 0 && errno != ENOENT)
-                        r = log_error_errno(errno,
-                                            "Failed to remove /run/nologin file: %m");
-
-                if (unlink("/etc/nologin") < 0 && errno != ENOENT) {
-                        /* If the file doesn't exist and /etc simply
-                         * was read-only (in which case unlink()
-                         * returns EROFS even if the file doesn't
-                         * exist), don't complain */
-
-                        if (errno != EROFS || access("/etc/nologin", F_OK) >= 0) {
-                                log_error_errno(errno, "Failed to remove /etc/nologin file: %m");
-                                return EXIT_FAILURE;
-                        }
-                }
-
-                if (r < 0)
+                r = unlink_or_warn("/run/nologin");
+                k = unlink_or_warn("/etc/nologin");
+                if (r < 0 || k < 0)
                         return EXIT_FAILURE;
 
         } else if (streq(argv[1], "stop")) {
-                int r;
-
                 r = write_string_file_atomic_label("/run/nologin", "System is going down.");
                 if (r < 0) {
                         log_error_errno(r, "Failed to create /run/nologin: %m");