]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
login: add callback for close_range()
authorKarel Zak <kzak@redhat.com>
Thu, 15 Jul 2021 10:51:39 +0000 (12:51 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 15 Jul 2021 10:51:39 +0000 (12:51 +0200)
Let's make it more robust for old kernels where is no close_range() or
in cases when it's unsuccessful.

Reported-by: Florian Weimer <fweimer@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
include/fileutils.h
lib/fileutils.c
login-utils/login.c

index f86426b964ede5210b39cda7cf14c48d1afa3331..17ad42938549b19b741dcae974bd96a631cfdf8d 100644 (file)
@@ -91,9 +91,7 @@ static inline int close_range(unsigned int first, unsigned int last, int flags)
 # endif        /* SYS_close_range */
 #endif /* __linux__ */
 
-#ifndef HAVE_CLOSE_RANGE
 extern void ul_close_all_fds(unsigned int first, unsigned int last);
-#endif
 
 #define UL_COPY_READ_ERROR (-1)
 #define UL_COPY_WRITE_ERROR (-2)
index a979b03acbfd9d957f2ccecb276b48365aa56c8c..7a8fce26f0c700672337acbfdb3c74dcc76f56e0 100644 (file)
@@ -129,7 +129,6 @@ unsigned int get_fd_tabsize(void)
        return m;
 }
 
-#ifndef HAVE_CLOSE_RANGE
 void ul_close_all_fds(unsigned int first, unsigned int last)
 {
        struct dirent *d;
@@ -166,7 +165,6 @@ void ul_close_all_fds(unsigned int first, unsigned int last)
                }
        }
 }
-#endif
 
 #ifdef TEST_PROGRAM_FILEUTILS
 int main(int argc, char *argv[])
@@ -189,10 +187,10 @@ int main(int argc, char *argv[])
                ignore_result( dup(STDIN_FILENO) );
 
 # ifdef HAVE_CLOSE_RANGE
-               close_range(STDERR_FILENO + 1, ~0U, 0);
-# else
-               ul_close_all_fds(STDERR_FILENO + 1, ~0U);
+               if (close_range(STDERR_FILENO + 1, ~0U, 0) < 0)
 # endif
+                       ul_close_all_fds(STDERR_FILENO + 1, ~0U);
+
        } else if (strcmp(argv[1], "--copy-file") == 0) {
                int ret = ul_copy_file(STDIN_FILENO, STDOUT_FILENO);
                if (ret == UL_COPY_READ_ERROR)
index d926466d3ebc6ebe6412ec6cefc44a0cb2cc4350..c6cd340b69289db3d5ed0d2307c6e477a92c13a6 100644 (file)
@@ -1357,10 +1357,9 @@ static void initialize(int argc, char **argv, struct login_context *cxt)
 #endif
        }
 #ifdef HAVE_CLOSE_RANGE
-       close_range(STDERR_FILENO + 1, ~0U, 0);
-#else
-       ul_close_all_fds(STDERR_FILENO + 1, ~0U);
+       if (close_range(STDERR_FILENO + 1, ~0U, 0) < 0)
 #endif
+               ul_close_all_fds(STDERR_FILENO + 1, ~0U);
 }
 
 int main(int argc, char **argv)