openat-safer: new module
[gnulib.git] / lib / openat.h
1 /* provide a replacement openat function
2    Copyright (C) 2004-2006, 2008-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 #ifndef _GL_HEADER_OPENAT
20 #define _GL_HEADER_OPENAT
21
22 #include <fcntl.h>
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <dirent.h>
27 #include <unistd.h>
28 #include <stdbool.h>
29
30 #ifndef __attribute__
31 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
32 #  define __attribute__(x) /* empty */
33 # endif
34 #endif
35
36 #ifndef ATTRIBUTE_NORETURN
37 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
38 #endif
39
40 /* Work around a bug in Solaris 9 and 10: AT_FDCWD is positive.  Its
41    value exceeds INT_MAX, so its use as an int doesn't conform to the
42    C standard, and GCC and Sun C complain in some cases.  If the bug
43    is present, undef AT_FDCWD here, so it can be redefined below.  */
44 #if 0 < AT_FDCWD && AT_FDCWD == 0xffd19553
45 # undef AT_FDCWD
46 #endif
47
48 /* Use the same bit pattern as Solaris 9, but with the proper
49    signedness.  The bit pattern is important, in case this actually is
50    Solaris with the above workaround.  */
51 #ifndef AT_FDCWD
52 # define AT_FDCWD (-3041965)
53 #endif
54
55 /* Use the same values as Solaris 9.  This shouldn't matter, but
56    there's no real reason to differ.  */
57 #ifndef AT_SYMLINK_NOFOLLOW
58 # define AT_SYMLINK_NOFOLLOW 4096
59 # define AT_REMOVEDIR 1
60 #endif
61
62 #ifdef __OPENAT_PREFIX
63
64 # undef openat
65 # define __OPENAT_CONCAT(x, y) x ## y
66 # define __OPENAT_XCONCAT(x, y) __OPENAT_CONCAT (x, y)
67 # define __OPENAT_ID(y) __OPENAT_XCONCAT (__OPENAT_PREFIX, y)
68 # define openat __OPENAT_ID (openat)
69 int openat (int fd, char const *file, int flags, /* mode_t mode */ ...);
70 int openat_permissive (int fd, char const *file, int flags, mode_t mode,
71                        int *cwd_errno);
72 # define fstatat __OPENAT_ID (fstatat)
73 int fstatat (int fd, char const *file, struct stat *st, int flag);
74 # define unlinkat __OPENAT_ID (unlinkat)
75 int unlinkat (int fd, char const *file, int flag);
76 bool openat_needs_fchdir (void);
77
78 #else
79
80 # define openat_permissive(Fd, File, Flags, Mode, Cwd_errno) \
81     openat (Fd, File, Flags, Mode)
82 # define openat_needs_fchdir() false
83
84 #endif
85
86 #if HAVE_OPENAT && ! LSTAT_FOLLOWS_SLASHED_SYMLINK
87 int rpl_fstatat (int fd, char const *file, struct stat *st, int flag);
88 # if !COMPILING_FSTATAT
89 #  undef fstatat
90 #  define fstatat rpl_fstatat
91 # endif
92 #endif
93
94 int mkdirat (int fd, char const *file, mode_t mode);
95 void openat_restore_fail (int) ATTRIBUTE_NORETURN;
96 void openat_save_fail (int) ATTRIBUTE_NORETURN;
97 int fchmodat (int fd, char const *file, mode_t mode, int flag);
98 int fchownat (int fd, char const *file, uid_t owner, gid_t group, int flag);
99
100 /* Using these function names makes application code
101    slightly more readable than it would be with
102    fchownat (..., 0) or fchownat (..., AT_SYMLINK_NOFOLLOW).  */
103 static inline int
104 chownat (int fd, char const *file, uid_t owner, gid_t group)
105 {
106   return fchownat (fd, file, owner, group, 0);
107 }
108
109 static inline int
110 lchownat (int fd, char const *file, uid_t owner, gid_t group)
111 {
112   return fchownat (fd, file, owner, group, AT_SYMLINK_NOFOLLOW);
113 }
114
115 static inline int
116 chmodat (int fd, char const *file, mode_t mode)
117 {
118   return fchmodat (fd, file, mode, 0);
119 }
120
121 static inline int
122 lchmodat (int fd, char const *file, mode_t mode)
123 {
124   return fchmodat (fd, file, mode, AT_SYMLINK_NOFOLLOW);
125 }
126
127 #endif /* _GL_HEADER_OPENAT */