Back to index

glibc  2.9
tst-faccessat.c
Go to the documentation of this file.
00001 /* Test for faccessat 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-faccessat.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   /* Try to create a file.  */
00086   int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
00087   if (fd == -1)
00088     {
00089       if (errno == ENOSYS)
00090        {
00091          puts ("*at functions not supported");
00092          return 0;
00093        }
00094 
00095       puts ("file creation failed");
00096       return 1;
00097     }
00098   write (fd, "hello", 5);
00099   puts ("file created");
00100 
00101   /* Before closing the file, try using this file descriptor to open
00102      another file.  This must fail.  */
00103   if (faccessat (fd, "should-not-work", F_OK, AT_EACCESS) != -1)
00104     {
00105       puts ("faccessat using descriptor for normal file worked");
00106       return 1;
00107     }
00108   if (errno != ENOTDIR)
00109     {
00110       puts ("\
00111 error for faccessat using descriptor for normal file not ENOTDIR ");
00112       return 1;
00113     }
00114 
00115   close (fd);
00116 
00117   int result = 0;
00118 
00119   if (faccessat (dir_fd, "some-file", F_OK, AT_EACCESS))
00120     {
00121       printf ("faccessat F_OK: %m\n");
00122       result = 1;
00123     }
00124   if (faccessat (dir_fd, "some-file", W_OK, AT_EACCESS))
00125     {
00126       printf ("faccessat W_OK: %m\n");
00127       result = 1;
00128     }
00129 
00130   errno = 0;
00131   if (faccessat (dir_fd, "some-file", X_OK, AT_EACCESS) == 0
00132       || errno != EACCES)
00133     {
00134       printf ("faccessat X_OK on nonexecutable: %m\n");
00135       result = 1;
00136     }
00137 
00138   if (fchmodat (dir_fd, "some-file", 0400, 0) != 0)
00139     {
00140       printf ("fchownat failed: %m\n");
00141       return 1;
00142     }
00143 
00144   if (faccessat (dir_fd, "some-file", R_OK, AT_EACCESS))
00145     {
00146       printf ("faccessat R_OK: %m\n");
00147       result = 1;
00148     }
00149 
00150   errno = 0;
00151   if (faccessat (dir_fd, "some-file", W_OK, AT_EACCESS) == 0
00152       ? (geteuid () != 0) : (errno != EACCES))
00153     {
00154       printf ("faccessat W_OK on unwritable file: %m\n");
00155       result = 1;
00156     }
00157 
00158   /* Create a file descriptor which is closed again right away.  */
00159   int dir_fd2 = dup (dir_fd);
00160   if (dir_fd2 == -1)
00161     {
00162       puts ("dup failed");
00163       return 1;
00164     }
00165   close (dir_fd2);
00166 
00167   /* With the file descriptor closed the next call must fail.  */
00168   if (faccessat (dir_fd2, "some-file", F_OK, AT_EACCESS) != -1)
00169     {
00170       puts ("faccessat using closed descriptor succeeded");
00171       return 1;
00172     }
00173   if (errno != EBADF)
00174     {
00175       puts ("faccessat using closed descriptor did not set EBADF");
00176       return 1;
00177     }
00178 
00179   /* Same with a non-existing file.  */
00180   if (faccessat (dir_fd2, "non-existing-file", F_OK, AT_EACCESS) != -1)
00181     {
00182       puts ("2nd faccessat using closed descriptor succeeded");
00183       return 1;
00184     }
00185   if (errno != EBADF)
00186     {
00187       puts ("2nd faccessat using closed descriptor did not set EBADF");
00188       return 1;
00189     }
00190 
00191   if (unlinkat (dir_fd, "some-file", 0) != 0)
00192     {
00193       puts ("unlinkat failed");
00194       result = 1;
00195     }
00196 
00197   close (dir_fd);
00198 
00199   fd = faccessat (-1, "some-file", F_OK, AT_EACCESS);
00200   if (fd != -1)
00201     {
00202       puts ("faccessat using -1 descriptor succeeded");
00203       return 1;
00204     }
00205   if (errno != EBADF)
00206     {
00207       puts ("faccessat using -1 descriptor did not set EBADF");
00208       return 1;
00209     }
00210 
00211   return result;
00212 }