Blame io/bug-ftw4.c

Packit 6c4009
/* Test if ftw function doesn't leak fds.
Packit 6c4009
   Copyright (C) 2003-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <ftw.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
static int cb_called;
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
cb (const char *name, const struct stat64 *st, int type)
Packit 6c4009
{
Packit 6c4009
  return cb_called++ & 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (void)
Packit 6c4009
{
Packit 6c4009
  char name[32] = "/tmp/ftwXXXXXX", *p;
Packit 6c4009
  int ret, i, result = 0, fd, fd1, fd2;
Packit 6c4009
Packit 6c4009
  if (mkdtemp (name) == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("Couldn't make temporary directory: %m\n");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
  p = strchr (name, '\0');
Packit 6c4009
  strcpy (p, "/1");
Packit 6c4009
  if (mkdir (name, 0755) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("Couldn't make temporary subdirectory: %m\n");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
  *p = '\0';
Packit 6c4009
Packit 6c4009
  ret = ftw64 (name, cb, 20);
Packit 6c4009
  if (ret != 1)
Packit 6c4009
    {
Packit 6c4009
      printf ("ftw64 returned %d instead of 1", ret);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fd = open (name, O_RDONLY);
Packit 6c4009
  if (fd < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("open failed: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  fd1 = open (name, O_RDONLY);
Packit 6c4009
  if (fd1 < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("open failed: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    close (fd1);
Packit 6c4009
  if (fd >= 0)
Packit 6c4009
    close (fd);
Packit 6c4009
Packit 6c4009
  for (i = 0; i < 128; ++i)
Packit 6c4009
    {
Packit 6c4009
      ret = ftw64 (name, cb, 20);
Packit 6c4009
      if (ret != 1)
Packit 6c4009
	{
Packit 6c4009
	  printf ("ftw64 returned %d instead of 1", ret);
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fd = open (name, O_RDONLY);
Packit 6c4009
  if (fd < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("open failed: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  fd2 = open (name, O_RDONLY);
Packit 6c4009
  if (fd2 < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("open failed: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    close (fd2);
Packit 6c4009
  if (fd >= 0)
Packit 6c4009
    close (fd);
Packit 6c4009
Packit 6c4009
  if (fd2 >= fd1 + 128)
Packit 6c4009
    {
Packit 6c4009
      printf ("ftw64 leaking fds: %d -> %d\n", fd1, fd2);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (cb_called != 129 * 2)
Packit 6c4009
    {
Packit 6c4009
      printf ("callback called %d times\n", cb_called);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  strcpy (p, "/1");
Packit 6c4009
  rmdir (name);
Packit 6c4009
  *p = '\0';
Packit 6c4009
  rmdir (name);
Packit 6c4009
  return result;
Packit 6c4009
}