1 /* Implement lchmod on platforms where it does not work correctly.
3 Copyright 2020-2025 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* written by Paul Eggert */
33 /* Work like chmod, except when FILE is a symbolic link.
34 In that case, on systems where permissions on symbolic links are unsupported
35 (such as Linux), set errno to EOPNOTSUPP and return -1. */
38 lchmod (char const *file
, mode_t mode
)
43 /* Open a file descriptor with O_NOFOLLOW, to make sure we don't
44 follow symbolic links, if /proc is mounted. O_PATH is used to
45 avoid a failure if the file is not readable.
46 Cf. <https://sourceware.org/bugzilla/show_bug.cgi?id=14578> */
47 int fd
= open (file
, O_PATH
| O_NOFOLLOW
| O_CLOEXEC
);
52 if (0 <= readlinkat (fd
, "", readlink_buf
, sizeof readlink_buf
))
54 else if (errno
== EINVAL
)
56 static char const fmt
[] = "/proc/self/fd/%d";
57 char buf
[sizeof fmt
- sizeof "%d" + INT_BUFSIZE_BOUND (int)];
58 sprintf (buf
, fmt
, fd
);
59 err
= chmod (buf
, mode
) == 0 ? 0 : errno
== ENOENT
? -1 : errno
;
62 err
= errno
== ENOENT
? -1 : errno
;
68 return err
== 0 ? 0 : -1;
71 size_t len
= strlen (file
);
72 if (len
&& file
[len
- 1] == '/')
75 if (lstat (file
, &st
) < 0)
77 if (!S_ISDIR (st
.st_mode
))
84 /* O_PATH + /proc is not supported. */
86 if (0 <= readlink (file
, readlink_buf
, sizeof readlink_buf
))
92 /* Fall back on chmod, despite a possible race. */
93 return chmod (file
, mode
);