File: tst-faccessat.c

package info (click to toggle)
glibc 2.41-12
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 300,384 kB
  • sloc: ansic: 1,050,583; asm: 238,243; makefile: 20,379; python: 13,537; sh: 11,827; cpp: 5,197; awk: 1,795; perl: 317; yacc: 292; pascal: 182; sed: 19
file content (123 lines) | stat: -rw-r--r-- 3,781 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* Test for faccessat function.
   Copyright (C) 2006-2025 Free Software Foundation, Inc.
   Copyright The GNU Toolchain Authors.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>

#include <support/check.h>
#include <support/temp_file.h>
#include <support/test-driver.h>
#include <support/xdirent.h>
#include <support/xunistd.h>

static int dir_fd;

static void
prepare (int argc, char **argv)
{
  dir_fd = xopen (support_create_temp_directory ("tst-faccessat."),
		  O_RDONLY | O_DIRECTORY, 0);
}


static int
do_test (void)
{
  /* fdopendir takes over the descriptor, make a copy.  */
  int dupfd = xdup (dir_fd);
  xlseek (dupfd, 0, SEEK_SET);

  /* The directory should be empty save the . and .. files.  */
  DIR *dir = xfdopendir (dupfd);

  struct dirent64 *d;
  while ((d = xreaddir64 (dir)) != NULL)
    if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
      FAIL_EXIT1 ("temp directory contains file \"%s\"\n", d->d_name);
  xclosedir (dir);

  /* Try to create a file.  */
  int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
  if (fd == -1)
    {
      if (errno == ENOSYS)
	FAIL_UNSUPPORTED ("*at functions not supported");

      FAIL_EXIT1 ("file creation failed");
    }
  xwrite (fd, "hello", 5);
  puts ("file created");

  /* Before closing the file, try using this file descriptor to open
     another file.  This must fail.  */
  TEST_VERIFY_EXIT (faccessat (fd, "should-not-work", F_OK, AT_EACCESS) == -1);
  TEST_VERIFY_EXIT (errno == ENOTDIR);

  xclose (fd);

  TEST_VERIFY (faccessat (dir_fd, "some-file", F_OK, AT_EACCESS) == 0);
  TEST_VERIFY (faccessat (dir_fd, "some-file", W_OK, AT_EACCESS) == 0);

  errno = 0;
  if (faccessat (dir_fd, "some-file", X_OK, AT_EACCESS) != 0)
    TEST_COMPARE (errno, EACCES);
  else
    FAIL ("faccessat unexpectedly succeeded\n");

  if (fchmodat (dir_fd, "some-file", 0400, 0) != 0)
    FAIL_EXIT1 ("fchownat failed: %m\n");

  TEST_VERIFY (faccessat (dir_fd, "some-file", R_OK, AT_EACCESS) == 0);

  /* Write would succeed only for EUID root, otherwise this test should
     fail.  */
  errno = 0;
  TEST_VERIFY (faccessat (dir_fd, "some-file", W_OK, AT_EACCESS) == 0
	       ? (geteuid () == 0) : (errno == EACCES));

  /* Create a file descriptor which is closed again right away.  */
  int dir_fd2 = xdup (dir_fd);
  close (dir_fd2);

  /* With the file descriptor closed the next call must fail.  */
  TEST_VERIFY_EXIT (faccessat (dir_fd2, "some-file", F_OK, AT_EACCESS)
		    == -1);
  TEST_VERIFY_EXIT (errno == EBADF);

  /* Same with a non-existing file.  */
  TEST_VERIFY_EXIT (faccessat (dir_fd2, "non-existing-file", F_OK, AT_EACCESS)
		    == -1);
  TEST_VERIFY_EXIT (errno == EBADF);

  TEST_VERIFY (unlinkat (dir_fd, "some-file", 0) == 0);

  xclose (dir_fd);

  TEST_VERIFY_EXIT (faccessat (-1, "some-file", F_OK, AT_EACCESS) == -1);
  TEST_VERIFY_EXIT (errno == EBADF);

  return 0;
}
#define PREPARE prepare
#include <support/test-driver.c>