malloc-h: New module.
[gnulib.git] / lib / fchmodat.c
blobeee0a1c56e4f612522d3d87c742cff89cf565cd4
1 /* Change the protections of file relative to an open directory.
2 Copyright (C) 2006, 2009-2020 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* written by Jim Meyering and Paul Eggert */
19 /* If the user's config.h happens to include <sys/stat.h>, let it include only
20 the system's <sys/stat.h> here, so that orig_fchmodat doesn't recurse to
21 rpl_fchmodat. */
22 #define __need_system_sys_stat_h
23 #include <config.h>
25 /* Specification. */
26 #include <sys/stat.h>
27 #undef __need_system_sys_stat_h
29 #if HAVE_FCHMODAT
30 static int
31 orig_fchmodat (int dir, char const *file, mode_t mode, int flags)
33 return fchmodat (dir, file, mode, flags);
35 #endif
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
43 #ifdef __osf__
44 /* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
45 eliminates this include because of the preliminary #include <sys/stat.h>
46 above. */
47 # include "sys/stat.h"
48 #else
49 # include <sys/stat.h>
50 #endif
52 #include <intprops.h>
54 /* Invoke chmod or lchmod on FILE, using mode MODE, in the directory
55 open on descriptor FD. If possible, do it without changing the
56 working directory. Otherwise, resort to using save_cwd/fchdir,
57 then (chmod|lchmod)/restore_cwd. If either the save_cwd or the
58 restore_cwd fails, then give a diagnostic and exit nonzero.
59 Note that an attempt to use a FLAG value of AT_SYMLINK_NOFOLLOW
60 on a system without lchmod support causes this function to fail. */
62 #if HAVE_FCHMODAT
63 int
64 fchmodat (int dir, char const *file, mode_t mode, int flags)
66 # if NEED_FCHMODAT_NONSYMLINK_FIX
67 if (flags == AT_SYMLINK_NOFOLLOW)
69 struct stat st;
71 # if defined O_PATH && defined AT_EMPTY_PATH
72 /* Open a file descriptor with O_NOFOLLOW, to make sure we don't
73 follow symbolic links, if /proc is mounted. O_PATH is used to
74 avoid a failure if the file is not readable.
75 Cf. <https://sourceware.org/bugzilla/show_bug.cgi?id=14578> */
76 int fd = openat (dir, file, O_PATH | O_NOFOLLOW | O_CLOEXEC);
77 if (fd < 0)
78 return fd;
80 /* Up to Linux 5.3 at least, when FILE refers to a symbolic link, the
81 chmod call below will change the permissions of the symbolic link
82 - which is undesired - and on many file systems (ext4, btrfs, jfs,
83 xfs, ..., but not reiserfs) fail with error EOPNOTSUPP - which is
84 misleading. Therefore test for a symbolic link explicitly.
85 Use fstatat because fstat does not work on O_PATH descriptors
86 before Linux 3.6. */
87 if (fstatat (fd, "", &st, AT_EMPTY_PATH) != 0)
89 int stat_errno = errno;
90 close (fd);
91 errno = stat_errno;
92 return -1;
94 if (S_ISLNK (st.st_mode))
96 close (fd);
97 errno = EOPNOTSUPP;
98 return -1;
101 # if defined __linux__ || defined __ANDROID__ || defined __CYGWIN__
102 static char const fmt[] = "/proc/self/fd/%d";
103 char buf[sizeof fmt - sizeof "%d" + INT_BUFSIZE_BOUND (int)];
104 sprintf (buf, fmt, fd);
105 int chmod_result = chmod (buf, mode);
106 int chmod_errno = errno;
107 close (fd);
108 if (chmod_result == 0)
109 return chmod_result;
110 if (chmod_errno != ENOENT)
112 errno = chmod_errno;
113 return chmod_result;
115 # endif
116 /* /proc is not mounted or would not work as in GNU/Linux. */
118 # else
119 int fstatat_result = fstatat (dir, file, &st, AT_SYMLINK_NOFOLLOW);
120 if (fstatat_result != 0)
121 return fstatat_result;
122 if (S_ISLNK (st.st_mode))
124 errno = EOPNOTSUPP;
125 return -1;
127 # endif
129 /* Fall back on orig_fchmodat with no flags, despite a possible race. */
130 flags = 0;
132 # endif
134 return orig_fchmodat (dir, file, mode, flags);
136 #else
137 # define AT_FUNC_NAME fchmodat
138 # define AT_FUNC_F1 lchmod
139 # define AT_FUNC_F2 chmod
140 # define AT_FUNC_USE_F1_COND AT_SYMLINK_NOFOLLOW
141 # define AT_FUNC_POST_FILE_PARAM_DECLS , mode_t mode, int flag
142 # define AT_FUNC_POST_FILE_ARGS , mode
143 # include "at-func.c"
144 #endif