Back to index

glibc  2.9
tst-fcntl.c
Go to the documentation of this file.
00001 /* Tests for fcntl.
00002    Copyright (C) 2000 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <fcntl.h>
00023 #include <paths.h>
00024 #include <string.h>
00025 #include <unistd.h>
00026 #include <sys/stat.h>
00027 
00028 
00029 /* Prototype for our test function.  */
00030 extern void do_prepare (int argc, char *argv[]);
00031 extern int do_test (int argc, char *argv[]);
00032 
00033 /* We have a preparation function.  */
00034 #define PREPARE do_prepare
00035 
00036 #include "../test-skeleton.c"
00037 
00038 
00039 /* Name of the temporary files.  */
00040 static char *name;
00041 
00042 void
00043 do_prepare (int argc, char *argv[])
00044 {
00045    size_t name_len;
00046 
00047    name_len = strlen (test_dir);
00048    name = malloc (name_len + sizeof ("/fcntlXXXXXX"));
00049    mempcpy (mempcpy (name, test_dir, name_len),
00050            "/fcntlXXXXXX", sizeof ("/fcntlXXXXXX"));
00051    add_temp_file (name);
00052 }
00053 
00054 
00055 int
00056 do_test (int argc, char *argv[])
00057 {
00058   int fd;
00059   int fd2;
00060   int fd3;
00061   struct stat64 st;
00062   int val;
00063   int result = 0;
00064 
00065   /* Create the temporary file.  */
00066   fd = mkstemp (name);
00067   if (fd == -1)
00068     {
00069       printf ("cannot open temporary file: %m\n");
00070       return 1;
00071     }
00072   if (fstat64 (fd, &st) != 0)
00073     {
00074       printf ("cannot stat test file: %m\n");
00075       return 1;
00076     }
00077   if (! S_ISREG (st.st_mode) || st.st_size != 0)
00078     {
00079       puts ("file not created correctly");
00080       return 1;
00081     }
00082 
00083   /* Get the flags with fcntl().  */
00084   val = fcntl (fd, F_GETFL);
00085   if (val == -1)
00086     {
00087       printf ("fcntl(fd, F_GETFL) failed: %m\n");
00088       result = 1;
00089     }
00090   else if ((val & O_ACCMODE) != O_RDWR)
00091     {
00092       puts ("temporary file not opened for read and write");
00093       result = 1;
00094     }
00095 
00096   /* Set the flags to something else.  */
00097   if (fcntl (fd, F_SETFL, O_RDONLY) == -1)
00098     {
00099       printf ("fcntl(fd, F_SETFL, O_RDONLY) failed: %m\n");
00100       result = 1;
00101     }
00102 
00103   val = fcntl (fd, F_GETFL);
00104   if (val == -1)
00105     {
00106       printf ("fcntl(fd, F_GETFL) after F_SETFL failed: %m\n");
00107       result = 1;
00108     }
00109   else if ((val & O_ACCMODE) != O_RDWR)
00110     {
00111       puts ("temporary file access mode changed");
00112       result = 1;
00113     }
00114 
00115   /* Set the flags to something else.  */
00116   if (fcntl (fd, F_SETFL, O_APPEND) == -1)
00117     {
00118       printf ("fcntl(fd, F_SETFL, O_APPEND) failed: %m\n");
00119       result = 1;
00120     }
00121 
00122   val = fcntl (fd, F_GETFL);
00123   if (val == -1)
00124     {
00125       printf ("fcntl(fd, F_GETFL) after second F_SETFL failed: %m\n");
00126       result = 1;
00127     }
00128   else if ((val & O_APPEND) == 0)
00129     {
00130       puts ("O_APPEND not set");
00131       result = 1;
00132     }
00133 
00134   val = fcntl (fd, F_GETFD);
00135   if (val == -1)
00136     {
00137       printf ("fcntl(fd, F_GETFD) failed: %m\n");
00138       result = 1;
00139     }
00140   else if (fcntl (fd, F_SETFD, val | FD_CLOEXEC) == -1)
00141     {
00142       printf ("fcntl(fd, F_SETFD, FD_CLOEXEC) failed: %m\n");
00143       result = 1;
00144     }
00145   else
00146     {
00147       val = fcntl (fd, F_GETFD);
00148       if (val == -1)
00149        {
00150          printf ("fcntl(fd, F_GETFD) after F_SETFD failed: %m\n");
00151          result = 1;
00152        }
00153       else if ((val & FD_CLOEXEC) == 0)
00154        {
00155          puts ("FD_CLOEXEC not set");
00156          result = 1;
00157        }
00158     }
00159 
00160   /* Get a number of a free descriptor.  If /dev/null is not available
00161      don't continue testing.  */
00162   fd2 = open (_PATH_DEVNULL, O_RDWR);
00163   if (fd2 == -1)
00164     return result;
00165   close (fd2);
00166 
00167   fd3 = fcntl (fd, F_DUPFD, fd2 + 1);
00168   if (fd3 == -1)
00169     {
00170       printf ("fcntl(fd, F_DUPFD, %d) failed: %m\n", fd2 + 1);
00171       result = 1;
00172     }
00173   else if (fd3 <= fd2)
00174     {
00175       printf ("F_DUPFD returned %d which is not larger than %d\n", fd3, fd2);
00176       result = 1;
00177     }
00178 
00179   if (fd3 != -1)
00180     {
00181       val = fcntl (fd3, F_GETFD);
00182       if (val == -1)
00183        {
00184          printf ("fcntl(fd3, F_GETFD) after F_DUPFD failed: %m\n");
00185          result = 1;
00186        }
00187       else if ((val & FD_CLOEXEC) != 0)
00188        {
00189          puts ("FD_CLOEXEC still set");
00190          result = 1;
00191        }
00192 
00193       close (fd3);
00194     }
00195 
00196   return result;
00197 }