Back to index

glibc  2.9
tst-fchmodat.c
Go to the documentation of this file.
00001 /* Test for fchmodat function.  */
00002 
00003 #include <dirent.h>
00004 #include <fcntl.h>
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <string.h>
00008 #include <unistd.h>
00009 
00010 
00011 static void prepare (void);
00012 #define PREPARE(argc, argv) prepare ()
00013 
00014 static int do_test (void);
00015 #define TEST_FUNCTION do_test ()
00016 
00017 #include "../test-skeleton.c"
00018 
00019 static int dir_fd;
00020 
00021 static void
00022 prepare (void)
00023 {
00024   size_t test_dir_len = strlen (test_dir);
00025   static const char dir_name[] = "/tst-fchmodat.XXXXXX";
00026 
00027   size_t dirbuflen = test_dir_len + sizeof (dir_name);
00028   char *dirbuf = malloc (dirbuflen);
00029   if (dirbuf == NULL)
00030     {
00031       puts ("out of memory");
00032       exit (1);
00033     }
00034 
00035   snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
00036   if (mkdtemp (dirbuf) == NULL)
00037     {
00038       puts ("cannot create temporary directory");
00039       exit (1);
00040     }
00041 
00042   add_temp_file (dirbuf);
00043 
00044   dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
00045   if (dir_fd == -1)
00046     {
00047       puts ("cannot open directory");
00048       exit (1);
00049     }
00050 }
00051 
00052 
00053 static int
00054 do_test (void)
00055 {
00056   /* fdopendir takes over the descriptor, make a copy.  */
00057   int dupfd = dup (dir_fd);
00058   if (dupfd == -1)
00059     {
00060       puts ("dup failed");
00061       return 1;
00062     }
00063   if (lseek (dupfd, 0, SEEK_SET) != 0)
00064     {
00065       puts ("1st lseek failed");
00066       return 1;
00067     }
00068 
00069   /* The directory should be empty save the . and .. files.  */
00070   DIR *dir = fdopendir (dupfd);
00071   if (dir == NULL)
00072     {
00073       puts ("fdopendir failed");
00074       return 1;
00075     }
00076   struct dirent64 *d;
00077   while ((d = readdir64 (dir)) != NULL)
00078     if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
00079       {
00080        printf ("temp directory contains file \"%s\"\n", d->d_name);
00081        return 1;
00082       }
00083   closedir (dir);
00084 
00085   umask (022);
00086 
00087   /* Try to create a file.  */
00088   int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
00089   if (fd == -1)
00090     {
00091       if (errno == ENOSYS)
00092        {
00093          puts ("*at functions not supported");
00094          return 0;
00095        }
00096 
00097       puts ("file creation failed");
00098       return 1;
00099     }
00100   write (fd, "hello", 5);
00101   puts ("file created");
00102 
00103   struct stat64 st1;
00104   if (fstat64 (fd, &st1) != 0)
00105     {
00106       puts ("fstat64 failed");
00107       return 1;
00108     }
00109 
00110   /* Before closing the file, try using this file descriptor to open
00111      another file.  This must fail.  */
00112   if (fchmodat (fd, "some-file", 0400, 0) != -1)
00113     {
00114       puts ("fchmodat using descriptor for normal file worked");
00115       return 1;
00116     }
00117   if (errno != ENOTDIR)
00118     {
00119       puts ("\
00120 error for fchmodat using descriptor for normal file not ENOTDIR ");
00121       return 1;
00122     }
00123 
00124   close (fd);
00125 
00126   if ((st1.st_mode & 0777) != 0644)
00127     {
00128       printf ("openat created mode %04o, not 0644\n", (st1.st_mode & 0777));
00129       return 1;
00130     }
00131 
00132   if (fchmodat (dir_fd, "some-file", 0400, 0) != 0)
00133     {
00134       puts ("fchownat failed");
00135       return 1;
00136     }
00137 
00138   struct stat64 st2;
00139   if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0)
00140     {
00141       puts ("fstatat64 failed");
00142       return 1;
00143     }
00144 
00145   if ((st2.st_mode & 0777) != 0400)
00146     {
00147       puts ("mode change failed");
00148       return 1;
00149     }
00150 
00151   if (unlinkat (dir_fd, "some-file", 0) != 0)
00152     {
00153       puts ("unlinkat failed");
00154       return 1;
00155     }
00156 
00157   /* Create a file descriptor which is closed again right away.  */
00158   int dir_fd2 = dup (dir_fd);
00159   if (dir_fd2 == -1)
00160     {
00161       puts ("dup failed");
00162       return 1;
00163     }
00164   close (dir_fd2);
00165 
00166   if (fchmodat (dir_fd2, "some-file", 0400, 0) != -1)
00167     {
00168       puts ("fchmodat using closed descriptor worked");
00169       return 1;
00170     }
00171   if (errno != EBADF)
00172     {
00173       puts ("error for fchmodat using closed descriptor not EBADF ");
00174       return 1;
00175     }
00176 
00177   close (dir_fd);
00178 
00179   if (fchmodat (-1, "some-file", 0400, 0) != -1)
00180     {
00181       puts ("fchmodat using invalid descriptor worked");
00182       return 1;
00183     }
00184   if (errno != EBADF)
00185     {
00186       puts ("error for fchmodat using invalid descriptor not EBADF ");
00187       return 1;
00188     }
00189 
00190   return 0;
00191 }