Back to index

glibc  2.9
bug-ftw4.c
Go to the documentation of this file.
00001 /* Test if ftw function doesn't leak fds.
00002    Copyright (C) 2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
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 <fcntl.h>
00022 #include <ftw.h>
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <string.h>
00026 #include <sys/stat.h>
00027 #include <unistd.h>
00028 
00029 static int cb_called;
00030 
00031 static int
00032 cb (const char *name, const struct stat64 *st, int type)
00033 {
00034   return cb_called++ & 1;
00035 }
00036 
00037 int
00038 main (void)
00039 {
00040   char name[32] = "/tmp/ftwXXXXXX", *p;
00041   int ret, i, result = 0, fd, fd1, fd2;
00042 
00043   if (mkdtemp (name) == NULL)
00044     {
00045       printf ("Couldn't make temporary directory: %m\n");
00046       exit (EXIT_FAILURE);
00047     }
00048   p = strchr (name, '\0');
00049   strcpy (p, "/1");
00050   if (mkdir (name, 0755) < 0)
00051     {
00052       printf ("Couldn't make temporary subdirectory: %m\n");
00053       exit (EXIT_FAILURE);
00054     }
00055   *p = '\0';
00056 
00057   ret = ftw64 (name, cb, 20);
00058   if (ret != 1)
00059     {
00060       printf ("ftw64 returned %d instead of 1", ret);
00061       result = 1;
00062     }
00063 
00064   fd = open (name, O_RDONLY);
00065   if (fd < 0)
00066     {
00067       printf ("open failed: %m\n");
00068       result = 1;
00069     }
00070   fd1 = open (name, O_RDONLY);
00071   if (fd1 < 0)
00072     {
00073       printf ("open failed: %m\n");
00074       result = 1;
00075     }
00076   else
00077     close (fd1);
00078   if (fd >= 0)
00079     close (fd);
00080 
00081   for (i = 0; i < 128; ++i)
00082     {
00083       ret = ftw64 (name, cb, 20);
00084       if (ret != 1)
00085        {
00086          printf ("ftw64 returned %d instead of 1", ret);
00087          result = 1;
00088        }
00089     }
00090 
00091   fd = open (name, O_RDONLY);
00092   if (fd < 0)
00093     {
00094       printf ("open failed: %m\n");
00095       result = 1;
00096     }
00097   fd2 = open (name, O_RDONLY);
00098   if (fd2 < 0)
00099     {
00100       printf ("open failed: %m\n");
00101       result = 1;
00102     }
00103   else
00104     close (fd2);
00105   if (fd >= 0)
00106     close (fd);
00107 
00108   if (fd2 >= fd1 + 128)
00109     {
00110       printf ("ftw64 leaking fds: %d -> %d\n", fd1, fd2);
00111       result = 1;
00112     }
00113 
00114   if (cb_called != 129 * 2)
00115     {
00116       printf ("callback called %d times\n", cb_called);
00117       result = 1;
00118     }
00119 
00120   strcpy (p, "/1");
00121   rmdir (name);
00122   *p = '\0';
00123   rmdir (name);
00124   return result;
00125 }