Blame lib/opendir-safer.c

Packit 709fb3
/* Invoke opendir, but avoid some glitches.
Packit 709fb3
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.  */
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
Packit 709fb3
#include "dirent-safer.h"
Packit 709fb3
Packit 709fb3
#include <errno.h>
Packit 709fb3
#include <unistd.h>
Packit 709fb3
#include "unistd-safer.h"
Packit 709fb3
Packit 709fb3
/* Like opendir, but do not clobber stdin, stdout, or stderr.  */
Packit 709fb3
Packit 709fb3
DIR *
Packit 709fb3
opendir_safer (char const *name)
Packit 709fb3
{
Packit 709fb3
  DIR *dp = opendir (name);
Packit 709fb3
Packit 709fb3
  if (dp)
Packit 709fb3
    {
Packit 709fb3
      int fd = dirfd (dp);
Packit 709fb3
Packit 709fb3
      if (0 <= fd && fd <= STDERR_FILENO)
Packit 709fb3
        {
Packit 709fb3
          /* If fdopendir is native (as on Linux), then it is safe to
Packit 709fb3
             assume dirfd(fdopendir(n))==n.  If we are using the
Packit 709fb3
             gnulib module fdopendir, then this guarantee is not met,
Packit 709fb3
             but fdopendir recursively calls opendir_safer up to 3
Packit 709fb3
             times to at least get a safe fd.  If fdopendir is not
Packit 709fb3
             present but dirfd is accurate (as on cygwin 1.5.x), then
Packit 709fb3
             we recurse up to 3 times ourselves.  Finally, if dirfd
Packit 709fb3
             always fails (as on mingw), then we are already safe.  */
Packit 709fb3
          DIR *newdp;
Packit 709fb3
          int e;
Packit 709fb3
#if HAVE_FDOPENDIR || GNULIB_FDOPENDIR
Packit 709fb3
          int f = dup_safer (fd);
Packit 709fb3
          if (f < 0)
Packit 709fb3
            {
Packit 709fb3
              e = errno;
Packit 709fb3
              newdp = NULL;
Packit 709fb3
            }
Packit 709fb3
          else
Packit 709fb3
            {
Packit 709fb3
              newdp = fdopendir (f);
Packit 709fb3
              e = errno;
Packit 709fb3
              if (! newdp)
Packit 709fb3
                close (f);
Packit 709fb3
            }
Packit 709fb3
#else /* !FDOPENDIR */
Packit 709fb3
          newdp = opendir_safer (name);
Packit 709fb3
          e = errno;
Packit 709fb3
#endif
Packit 709fb3
          closedir (dp);
Packit 709fb3
          errno = e;
Packit 709fb3
          dp = newdp;
Packit 709fb3
        }
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  return dp;
Packit 709fb3
}