Blame io/tst-faccessat.c

Packit 6c4009
/* Test for faccessat function.  */
Packit 6c4009
Packit 6c4009
#include <dirent.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void prepare (void);
Packit 6c4009
#define PREPARE(argc, argv) prepare ()
Packit 6c4009
Packit 6c4009
static int do_test (void);
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
Packit 6c4009
#include "../test-skeleton.c"
Packit 6c4009
Packit 6c4009
static int dir_fd;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
prepare (void)
Packit 6c4009
{
Packit 6c4009
  size_t test_dir_len = strlen (test_dir);
Packit 6c4009
  static const char dir_name[] = "/tst-faccessat.XXXXXX";
Packit 6c4009
Packit 6c4009
  size_t dirbuflen = test_dir_len + sizeof (dir_name);
Packit 6c4009
  char *dirbuf = malloc (dirbuflen);
Packit 6c4009
  if (dirbuf == NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("out of memory");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
Packit 6c4009
  if (mkdtemp (dirbuf) == NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("cannot create temporary directory");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  add_temp_file (dirbuf);
Packit 6c4009
Packit 6c4009
  dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
Packit 6c4009
  if (dir_fd == -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("cannot open directory");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  /* fdopendir takes over the descriptor, make a copy.  */
Packit 6c4009
  int dupfd = dup (dir_fd);
Packit 6c4009
  if (dupfd == -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("dup failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (lseek (dupfd, 0, SEEK_SET) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("1st lseek failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* The directory should be empty save the . and .. files.  */
Packit 6c4009
  DIR *dir = fdopendir (dupfd);
Packit 6c4009
  if (dir == NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("fdopendir failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  struct dirent64 *d;
Packit 6c4009
  while ((d = readdir64 (dir)) != NULL)
Packit 6c4009
    if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
Packit 6c4009
      {
Packit 6c4009
	printf ("temp directory contains file \"%s\"\n", d->d_name);
Packit 6c4009
	return 1;
Packit 6c4009
      }
Packit 6c4009
  closedir (dir);
Packit 6c4009
Packit 6c4009
  /* Try to create a file.  */
Packit 6c4009
  int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    {
Packit 6c4009
      if (errno == ENOSYS)
Packit 6c4009
	{
Packit 6c4009
	  puts ("*at functions not supported");
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      puts ("file creation failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  write (fd, "hello", 5);
Packit 6c4009
  puts ("file created");
Packit 6c4009
Packit 6c4009
  /* Before closing the file, try using this file descriptor to open
Packit 6c4009
     another file.  This must fail.  */
Packit 6c4009
  if (faccessat (fd, "should-not-work", F_OK, AT_EACCESS) != -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("faccessat using descriptor for normal file worked");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (errno != ENOTDIR)
Packit 6c4009
    {
Packit 6c4009
      puts ("\
Packit 6c4009
error for faccessat using descriptor for normal file not ENOTDIR ");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  close (fd);
Packit 6c4009
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  if (faccessat (dir_fd, "some-file", F_OK, AT_EACCESS))
Packit 6c4009
    {
Packit 6c4009
      printf ("faccessat F_OK: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  if (faccessat (dir_fd, "some-file", W_OK, AT_EACCESS))
Packit 6c4009
    {
Packit 6c4009
      printf ("faccessat W_OK: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  errno = 0;
Packit 6c4009
  if (faccessat (dir_fd, "some-file", X_OK, AT_EACCESS) == 0
Packit 6c4009
      || errno != EACCES)
Packit 6c4009
    {
Packit 6c4009
      printf ("faccessat X_OK on nonexecutable: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (fchmodat (dir_fd, "some-file", 0400, 0) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("fchownat failed: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (faccessat (dir_fd, "some-file", R_OK, AT_EACCESS))
Packit 6c4009
    {
Packit 6c4009
      printf ("faccessat R_OK: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  errno = 0;
Packit 6c4009
  if (faccessat (dir_fd, "some-file", W_OK, AT_EACCESS) == 0
Packit 6c4009
      ? (geteuid () != 0) : (errno != EACCES))
Packit 6c4009
    {
Packit 6c4009
      printf ("faccessat W_OK on unwritable file: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Create a file descriptor which is closed again right away.  */
Packit 6c4009
  int dir_fd2 = dup (dir_fd);
Packit 6c4009
  if (dir_fd2 == -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("dup failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  close (dir_fd2);
Packit 6c4009
Packit 6c4009
  /* With the file descriptor closed the next call must fail.  */
Packit 6c4009
  if (faccessat (dir_fd2, "some-file", F_OK, AT_EACCESS) != -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("faccessat using closed descriptor succeeded");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (errno != EBADF)
Packit 6c4009
    {
Packit 6c4009
      puts ("faccessat using closed descriptor did not set EBADF");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Same with a non-existing file.  */
Packit 6c4009
  if (faccessat (dir_fd2, "non-existing-file", F_OK, AT_EACCESS) != -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("2nd faccessat using closed descriptor succeeded");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (errno != EBADF)
Packit 6c4009
    {
Packit 6c4009
      puts ("2nd faccessat using closed descriptor did not set EBADF");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (unlinkat (dir_fd, "some-file", 0) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("unlinkat failed");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  close (dir_fd);
Packit 6c4009
Packit 6c4009
  fd = faccessat (-1, "some-file", F_OK, AT_EACCESS);
Packit 6c4009
  if (fd != -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("faccessat using -1 descriptor succeeded");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (errno != EBADF)
Packit 6c4009
    {
Packit 6c4009
      puts ("faccessat using -1 descriptor did not set EBADF");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}