dirent-safer: new module
[gnulib.git] / lib / fdopendir.c
1 /* provide a replacement fdopendir function
2    Copyright (C) 2004-2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* written by Jim Meyering */
18
19 #include <config.h>
20
21 #include <dirent.h>
22
23 #include <stdlib.h>
24 #include <unistd.h>
25
26 #include "openat.h"
27 #include "openat-priv.h"
28 #include "save-cwd.h"
29
30 #if GNULIB_DIRENT_SAFER
31 # include "dirent--.h"
32 #endif
33
34 /* Replacement for Solaris' function by the same name.
35    <http://www.google.com/search?q=fdopendir+site:docs.sun.com>
36    First, try to simulate it via opendir ("/proc/self/fd/FD").  Failing
37    that, simulate it by using fchdir metadata, or by doing
38    save_cwd/fchdir/opendir(".")/restore_cwd.
39    If either the save_cwd or the restore_cwd fails (relatively unlikely),
40    then give a diagnostic and exit nonzero.
41    Otherwise, this function works just like Solaris' fdopendir.
42
43    W A R N I N G:
44    Unlike other fd-related functions, this one effectively consumes
45    its FD parameter.  The caller should not close or otherwise
46    manipulate FD if this function returns successfully.  Also, this
47    implementation does not guarantee that dirfd(fdopendir(n))==n;
48    the open directory stream may use a clone of FD, or have no
49    associated fd at all.  */
50 DIR *
51 fdopendir (int fd)
52 {
53   int saved_errno;
54   DIR *dir;
55
56   char buf[OPENAT_BUFFER_SIZE];
57   char *proc_file = openat_proc_name (buf, fd, ".");
58   if (proc_file)
59     {
60       dir = opendir (proc_file);
61       saved_errno = errno;
62     }
63   else
64     {
65       dir = NULL;
66       saved_errno = EOPNOTSUPP;
67     }
68
69   /* If the syscall fails with an expected errno value, resort to
70      save_cwd/restore_cwd.  */
71   if (! dir && EXPECTED_ERRNO (saved_errno))
72     {
73 #if REPLACE_FCHDIR
74       const char *name = _gl_directory_name (fd);
75       if (name)
76         dir = opendir (name);
77       saved_errno = errno;
78 #else /* !REPLACE_FCHDIR */
79       struct saved_cwd saved_cwd;
80       if (save_cwd (&saved_cwd) != 0)
81         openat_save_fail (errno);
82
83       if (fchdir (fd) != 0)
84         {
85           dir = NULL;
86           saved_errno = errno;
87         }
88       else
89         {
90           dir = opendir (".");
91           saved_errno = errno;
92
93           if (restore_cwd (&saved_cwd) != 0)
94             openat_restore_fail (errno);
95         }
96
97       free_cwd (&saved_cwd);
98 #endif /* !REPLACE_FCHDIR */
99     }
100
101   if (dir)
102     close (fd);
103   if (proc_file != buf)
104     free (proc_file);
105   errno = saved_errno;
106   return dir;
107 }