login: add callback for close_range()

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>
This commit is contained in:
Karel Zak 2021-07-15 12:51:39 +02:00
parent 650fca1a06
commit a3d73c246f
3 changed files with 5 additions and 10 deletions

View File

@ -91,9 +91,7 @@ static inline int close_range(unsigned int first, unsigned int last, int flags)
# endif /* SYS_close_range */ # endif /* SYS_close_range */
#endif /* __linux__ */ #endif /* __linux__ */
#ifndef HAVE_CLOSE_RANGE
extern void ul_close_all_fds(unsigned int first, unsigned int last); extern void ul_close_all_fds(unsigned int first, unsigned int last);
#endif
#define UL_COPY_READ_ERROR (-1) #define UL_COPY_READ_ERROR (-1)
#define UL_COPY_WRITE_ERROR (-2) #define UL_COPY_WRITE_ERROR (-2)

View File

@ -129,7 +129,6 @@ unsigned int get_fd_tabsize(void)
return m; return m;
} }
#ifndef HAVE_CLOSE_RANGE
void ul_close_all_fds(unsigned int first, unsigned int last) void ul_close_all_fds(unsigned int first, unsigned int last)
{ {
struct dirent *d; struct dirent *d;
@ -166,7 +165,6 @@ void ul_close_all_fds(unsigned int first, unsigned int last)
} }
} }
} }
#endif
#ifdef TEST_PROGRAM_FILEUTILS #ifdef TEST_PROGRAM_FILEUTILS
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -189,10 +187,10 @@ int main(int argc, char *argv[])
ignore_result( dup(STDIN_FILENO) ); ignore_result( dup(STDIN_FILENO) );
# ifdef HAVE_CLOSE_RANGE # ifdef HAVE_CLOSE_RANGE
close_range(STDERR_FILENO + 1, ~0U, 0); if (close_range(STDERR_FILENO + 1, ~0U, 0) < 0)
# else
ul_close_all_fds(STDERR_FILENO + 1, ~0U);
# endif # endif
ul_close_all_fds(STDERR_FILENO + 1, ~0U);
} else if (strcmp(argv[1], "--copy-file") == 0) { } else if (strcmp(argv[1], "--copy-file") == 0) {
int ret = ul_copy_file(STDIN_FILENO, STDOUT_FILENO); int ret = ul_copy_file(STDIN_FILENO, STDOUT_FILENO);
if (ret == UL_COPY_READ_ERROR) if (ret == UL_COPY_READ_ERROR)

View File

@ -1357,10 +1357,9 @@ static void initialize(int argc, char **argv, struct login_context *cxt)
#endif #endif
} }
#ifdef HAVE_CLOSE_RANGE #ifdef HAVE_CLOSE_RANGE
close_range(STDERR_FILENO + 1, ~0U, 0); if (close_range(STDERR_FILENO + 1, ~0U, 0) < 0)
#else
ul_close_all_fds(STDERR_FILENO + 1, ~0U);
#endif #endif
ul_close_all_fds(STDERR_FILENO + 1, ~0U);
} }
int main(int argc, char **argv) int main(int argc, char **argv)