Blame io/tst-fchmodat.c

Packit 6c4009
/* Test for fchmodat 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-fchmodat.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
  umask (022);
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
  struct stat64 st1;
Packit 6c4009
  if (fstat64 (fd, &st1) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("fstat64 failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
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 (fchmodat (fd, "some-file", 0400, 0) != -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("fchmodat 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 fchmodat using descriptor for normal file not ENOTDIR ");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  close (fd);
Packit 6c4009
Packit 6c4009
  if ((st1.st_mode & 0777) != 0644)
Packit 6c4009
    {
Packit 6c4009
      printf ("openat created mode %04o, not 0644\n", (st1.st_mode & 0777));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (fchmodat (dir_fd, "some-file", 0400, 0) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("fchownat failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  struct stat64 st2;
Packit 6c4009
  if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("fstatat64 failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if ((st2.st_mode & 0777) != 0400)
Packit 6c4009
    {
Packit 6c4009
      puts ("mode change failed");
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
      return 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
  if (fchmodat (dir_fd2, "some-file", 0400, 0) != -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("fchmodat using closed descriptor worked");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (errno != EBADF)
Packit 6c4009
    {
Packit 6c4009
      puts ("error for fchmodat using closed descriptor not EBADF ");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  close (dir_fd);
Packit 6c4009
Packit 6c4009
  if (fchmodat (-1, "some-file", 0400, 0) != -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("fchmodat using invalid descriptor worked");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (errno != EBADF)
Packit 6c4009
    {
Packit 6c4009
      puts ("error for fchmodat using invalid descriptor not EBADF ");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}