io: Fix destructive nature of tst-fchmod-errors

We must not change the permissions of /dev/null if running
as root.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>
This commit is contained in:
Florian Weimer 2024-08-30 22:07:12 +02:00
parent 424d97be50
commit 3844cdc330

View File

@ -18,8 +18,10 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h>
#include <support/check.h> #include <support/check.h>
#include <support/xunistd.h> #include <support/xunistd.h>
#include <unistd.h>
static int static int
do_test (void) do_test (void)
@ -27,9 +29,14 @@ do_test (void)
{ {
/* Permissions on /dev/null (the opened descriptor) cannot be changed. */ /* Permissions on /dev/null (the opened descriptor) cannot be changed. */
int fd = xopen ("/dev/null", O_RDWR, 0); int fd = xopen ("/dev/null", O_RDWR, 0);
errno = 0; if (getuid () == 0)
TEST_COMPARE (fchmod (fd, 0), -1); puts ("info: /dev/null fchmod test skipped because of root privileges");
TEST_COMPARE (errno, EPERM); else
{
errno = 0;
TEST_COMPARE (fchmod (fd, 0), -1);
TEST_COMPARE (errno, EPERM);
}
xclose (fd); xclose (fd);
/* Now testing an invalid file descriptor. */ /* Now testing an invalid file descriptor. */