Blame gnulib-tests/test-dirent-safer.c

Packit 709fb3
/* Test that directory streams leave standard fds alone.
Packit 709fb3
   Copyright (C) 2009-2017 Free Software Foundation, Inc.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
/* Written by Eric Blake <ebb9@byu.net>, 2009.  */
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
Packit 709fb3
#include "dirent--.h"
Packit 709fb3
Packit 709fb3
#include <errno.h>
Packit 709fb3
#include <fcntl.h>
Packit 709fb3
#include <stdio.h>
Packit 709fb3
#include <unistd.h>
Packit 709fb3
Packit 709fb3
#include "unistd-safer.h"
Packit 709fb3
Packit 709fb3
/* This test intentionally closes stderr.  So, we arrange to have fd 10
Packit 709fb3
   (outside the range of interesting fd's during the test) set up to
Packit 709fb3
   duplicate the original stderr.  */
Packit 709fb3
Packit 709fb3
#define BACKUP_STDERR_FILENO 10
Packit 709fb3
#define ASSERT_STREAM myerr
Packit 709fb3
#include "macros.h"
Packit 709fb3
Packit 709fb3
static FILE *myerr;
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
main (void)
Packit 709fb3
{
Packit 709fb3
  int i;
Packit 709fb3
  DIR *dp;
Packit 709fb3
  /* The dirent-safer module works without the use of fdopendir (which
Packit 709fb3
     would also pull in fchdir and openat); but if those modules were
Packit 709fb3
     also used, we ensure that they are safe.  In particular, the
Packit 709fb3
     gnulib version of fdopendir is unable to guarantee that
Packit 709fb3
     dirfd(fdopendir(fd))==fd, but we can at least guarantee that if
Packit 709fb3
     they are not equal, the fd returned by dirfd is safe.  */
Packit 709fb3
#if HAVE_FDOPENDIR || GNULIB_TEST_FDOPENDIR
Packit 709fb3
  int dfd;
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
  /* We close fd 2 later, so save it in fd 10.  */
Packit 709fb3
  if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
Packit 709fb3
      || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
Packit 709fb3
    return 2;
Packit 709fb3
Packit 709fb3
#if HAVE_FDOPENDIR || GNULIB_TEST_FDOPENDIR
Packit 709fb3
  dfd = open (".", O_RDONLY);
Packit 709fb3
  ASSERT (STDERR_FILENO < dfd);
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
  /* Four iterations, with progressively more standard descriptors
Packit 709fb3
     closed.  */
Packit 709fb3
  for (i = -1; i <= STDERR_FILENO; i++)
Packit 709fb3
    {
Packit 709fb3
      if (0 <= i)
Packit 709fb3
        ASSERT (close (i) == 0);
Packit 709fb3
      dp = opendir (".");
Packit 709fb3
      ASSERT (dp);
Packit 709fb3
      ASSERT (dirfd (dp) == -1 || STDERR_FILENO < dirfd (dp));
Packit 709fb3
      ASSERT (closedir (dp) == 0);
Packit 709fb3
Packit 709fb3
#if HAVE_FDOPENDIR || GNULIB_TEST_FDOPENDIR
Packit 709fb3
      {
Packit 709fb3
        int fd = dup_safer (dfd);
Packit 709fb3
        ASSERT (STDERR_FILENO < fd);
Packit 709fb3
        dp = fdopendir (fd);
Packit 709fb3
        ASSERT (dp);
Packit 709fb3
        ASSERT (dirfd (dp) == -1 || STDERR_FILENO < dirfd (dp));
Packit 709fb3
        ASSERT (closedir (dp) == 0);
Packit 709fb3
        errno = 0;
Packit 709fb3
        ASSERT (close (fd) == -1);
Packit 709fb3
        ASSERT (errno == EBADF);
Packit 709fb3
      }
Packit 709fb3
#endif
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
#if HAVE_FDOPENDIR || GNULIB_TEST_FDOPENDIR
Packit 709fb3
  ASSERT (close (dfd) == 0);
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
  return 0;
Packit 709fb3
}