Blame io/tst-openat.c

Packit 6c4009
#include <dirent.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.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-openat.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 safe 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
Packit 6c4009
  /* Before closing the file, try using this file descriptor to open
Packit 6c4009
     another file.  This must fail.  */
Packit 6c4009
  int fd2 = openat (fd, "should-not-work", O_CREAT|O_RDWR, 0666);
Packit 6c4009
  if (fd2 != -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("openat using descriptor for normal file worked");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (errno != ENOTDIR)
Packit 6c4009
    {
Packit 6c4009
      puts ("error for openat using descriptor for normal file not ENOTDIR ");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  close (fd);
Packit 6c4009
  puts ("file created");
Packit 6c4009
Packit 6c4009
  /* fdopendir takes over the descriptor, make a copy.  */
Packit 6c4009
  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 ("2nd lseek failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* The directory should be empty safe the . and .. files.  */
Packit 6c4009
  dir = fdopendir (dupfd);
Packit 6c4009
  if (dir == NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("fdopendir failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  bool seen_file = false;
Packit 6c4009
  while ((d = readdir64 (dir)) != NULL)
Packit 6c4009
    if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
Packit 6c4009
      {
Packit 6c4009
	if (strcmp (d->d_name, "some-file") != 0)
Packit 6c4009
	  {
Packit 6c4009
	    printf ("temp directory contains file \"%s\"\n", d->d_name);
Packit 6c4009
	    return 1;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
	seen_file = true;
Packit 6c4009
      }
Packit 6c4009
  closedir (dir);
Packit 6c4009
Packit 6c4009
  if (!seen_file)
Packit 6c4009
    {
Packit 6c4009
      puts ("file not created in correct directory");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int cwdfd = open (".", O_RDONLY | O_DIRECTORY);
Packit 6c4009
  if (cwdfd == -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("cannot get descriptor for cwd");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (fchdir (dir_fd) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("1st fchdir failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (unlink ("some-file") != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("unlink failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (fchdir (cwdfd) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("2nd fchdir failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  close (dir_fd);
Packit 6c4009
  close (cwdfd);
Packit 6c4009
Packit 6c4009
  /* With the file descriptor closed the next call must fail.  */
Packit 6c4009
  fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
Packit 6c4009
  if (fd != -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("openat using closed descriptor succeeded");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (errno != EBADF)
Packit 6c4009
    {
Packit 6c4009
      puts ("openat using closed descriptor did not set EBADF");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fd = openat (-1, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
Packit 6c4009
  if (fd != -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("openat using -1 descriptor succeeded");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (errno != EBADF)
Packit 6c4009
    {
Packit 6c4009
      puts ("openat using -1 descriptor did not set EBADF");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}