Back to index

glibc  2.9
tst-fdopendir.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <fcntl.h>
00003 #include <stdlib.h>
00004 #include <unistd.h>
00005 #include <dirent.h>
00006 #include <stdbool.h>
00007 #include <string.h>
00008 
00009 
00010 static int
00011 do_test (void)
00012 {
00013   char fname[] = "/tmp/jXXXXXX";
00014   int fd = mkstemp (fname);
00015   if (fd == -1)
00016     {
00017       puts ("mkstemp failed");
00018       return 1;
00019     }
00020 
00021   write (fd, "hello", 5);
00022   close (fd);
00023 
00024   struct stat64 st;
00025   if (stat64 (fname, &st) == -1)
00026     {
00027       puts ("first stat failed");
00028       return 0;
00029     }
00030 
00031   /* Make sure there is enough time between the creation and the access.  */
00032   sleep (2);
00033 
00034   fd = open (fname, O_RDONLY | O_NOATIME);
00035   if (fd == -1)
00036     {
00037       puts ("first open failed");
00038       return 1;
00039     }
00040 
00041   char buf[5];
00042   read(fd, buf, sizeof (buf));
00043   close(fd);
00044 
00045   struct stat64 st2;
00046   if (stat64 (fname, &st2) == -1)
00047     {
00048       puts ("second stat failed");
00049       return 0;
00050     }
00051 
00052   bool no_noatime = false;
00053 #ifdef _STATBUF_ST_NSEC
00054   if (st.st_atim.tv_sec != st2.st_atim.tv_sec
00055       || st.st_atim.tv_nsec != st2.st_atim.tv_nsec)
00056 #else
00057   if (st.st_atime != st2.st_atime)
00058 #endif
00059     {
00060       puts ("file atime changed");
00061       no_noatime = true;
00062     }
00063 
00064   unlink(fname);
00065 
00066   strcpy(fname, "/tmp/dXXXXXX");
00067   char *d = mkdtemp (fname);
00068   if (d == NULL)
00069     {
00070       puts ("mkdtemp failed");
00071       return 1;
00072     }
00073 
00074   if (stat64 (d, &st) == -1)
00075     {
00076       puts ("third stat failed");
00077       return 0;
00078     }
00079   sleep (2);
00080 
00081   fd = open64 (d, O_RDONLY|O_NDELAY|O_DIRECTORY|O_NOATIME);
00082   if (fd == -1)
00083     {
00084       puts ("second open failed");
00085       return 1;
00086     }
00087   DIR *dir = fdopendir (fd);
00088   if (dir == NULL)
00089     {
00090       puts ("fdopendir failed");
00091       return 1;
00092     }
00093 
00094   struct dirent *de;
00095   while ((de = readdir (dir)) != NULL)
00096     ;
00097 
00098   closedir (dir);
00099 
00100   if (stat64 (d, &st2) == -1)
00101     {
00102       puts ("fourth stat failed");
00103       return 0;
00104     }
00105 #ifdef _STATBUF_ST_NSEC
00106   if (!no_noatime
00107       && (st.st_atim.tv_sec != st2.st_atim.tv_sec
00108         || st.st_atim.tv_nsec != st2.st_atim.tv_nsec))
00109 #else
00110   if (!no_noatime && st.st_atime != st2.st_atime)
00111 #endif
00112     {
00113       puts ("directory atime changed");
00114       return 1;
00115     }
00116 
00117   rmdir(fname);
00118 
00119   return 0;
00120 }
00121 
00122 #define TIMEOUT 6
00123 #define TEST_FUNCTION do_test ()
00124 #include "../test-skeleton.c"