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 913829559b
commit cae071ed72
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 /* __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)

View 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)

View 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)