Back to index

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