Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / unix / sysv / linux / pathconf.c
blob578b73654f1e4142cfcb97e3f6808241aa212740
1 /* Get file-specific information about a file. Linux version.
2 Copyright (C) 1991-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <mntent.h>
21 #include <stdio_ext.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/sysmacros.h>
26 #include "pathconf.h"
27 #include "linux_fsinfo.h"
28 #include <not-cancel.h>
30 static long int posix_pathconf (const char *file, int name);
32 /* Define this first, so it can be inlined. */
33 #define __pathconf static posix_pathconf
34 #include <sysdeps/posix/pathconf.c>
37 /* Get file-specific information about FILE. */
38 long int
39 __pathconf (const char *file, int name)
41 struct statfs fsbuf;
43 switch (name)
45 case _PC_LINK_MAX:
46 return __statfs_link_max (__statfs (file, &fsbuf), &fsbuf, file, -1);
48 case _PC_FILESIZEBITS:
49 return __statfs_filesize_max (__statfs (file, &fsbuf), &fsbuf);
51 case _PC_2_SYMLINKS:
52 return __statfs_symlinks (__statfs (file, &fsbuf), &fsbuf);
54 case _PC_CHOWN_RESTRICTED:
55 return __statfs_chown_restricted (__statfs (file, &fsbuf), &fsbuf);
57 default:
58 return posix_pathconf (file, name);
63 static long int
64 distinguish_extX (const struct statfs *fsbuf, const char *file, int fd)
66 char buf[64];
67 char path[PATH_MAX];
68 struct stat64 st;
70 if ((file == NULL ? fstat64 (fd, &st) : stat64 (file, &st)) != 0)
71 /* Strange. The statfd call worked, but stat fails. Default to
72 the more pessimistic value. */
73 return EXT2_LINK_MAX;
75 __snprintf (buf, sizeof (buf), "/sys/dev/block/%u:%u",
76 gnu_dev_major (st.st_dev), gnu_dev_minor (st.st_dev));
78 ssize_t n = __readlink (buf, path, sizeof (path));
79 if (n != -1 && n < sizeof (path))
81 path[n] = '\0';
82 char *base = strdupa (basename (path));
83 __snprintf (path, sizeof (path), "/sys/fs/ext4/%s", base);
85 return __access (path, F_OK) == 0 ? EXT4_LINK_MAX : EXT2_LINK_MAX;
88 /* XXX Is there a better way to distinguish ext2/3 from ext4 than
89 iterating over the mounted filesystems and compare the device
90 numbers? */
91 FILE *mtab = __setmntent ("/proc/mounts", "r");
92 if (mtab == NULL)
93 mtab = __setmntent (_PATH_MOUNTED, "r");
95 /* By default be conservative. */
96 long int result = EXT2_LINK_MAX;
97 if (mtab != NULL)
99 struct mntent mntbuf;
100 char tmpbuf[1024];
102 /* No locking needed. */
103 (void) __fsetlocking (mtab, FSETLOCKING_BYCALLER);
105 while (__getmntent_r (mtab, &mntbuf, tmpbuf, sizeof (tmpbuf)))
107 if (strcmp (mntbuf.mnt_type, "ext2") != 0
108 && strcmp (mntbuf.mnt_type, "ext3") != 0
109 && strcmp (mntbuf.mnt_type, "ext4") != 0)
110 continue;
112 struct stat64 fsst;
113 if (stat64 (mntbuf.mnt_dir, &fsst) >= 0
114 && st.st_dev == fsst.st_dev)
116 if (strcmp (mntbuf.mnt_type, "ext4") == 0)
117 result = EXT4_LINK_MAX;
118 break;
122 /* Close the file. */
123 __endmntent (mtab);
126 return result;
130 /* Used like: return statfs_link_max (__statfs (name, &buf), &buf); */
131 long int
132 __statfs_link_max (int result, const struct statfs *fsbuf, const char *file,
133 int fd)
135 if (result < 0)
137 if (errno == ENOSYS)
138 /* Not possible, return the default value. */
139 return LINUX_LINK_MAX;
141 /* Some error occured. */
142 return -1;
145 switch (fsbuf->f_type)
147 case EXT2_SUPER_MAGIC:
148 /* Unfortunately the kernel does not return a different magic number
149 for ext4. This would be necessary to easily detect etx4 since it
150 has a different LINK_MAX value. Therefore we have to find it out
151 the hard way. */
152 return distinguish_extX (fsbuf, file, fd);
154 case F2FS_SUPER_MAGIC:
155 return F2FS_LINK_MAX;
157 case MINIX_SUPER_MAGIC:
158 case MINIX_SUPER_MAGIC2:
159 return MINIX_LINK_MAX;
161 case MINIX2_SUPER_MAGIC:
162 case MINIX2_SUPER_MAGIC2:
163 return MINIX2_LINK_MAX;
165 case XENIX_SUPER_MAGIC:
166 return XENIX_LINK_MAX;
168 case SYSV4_SUPER_MAGIC:
169 case SYSV2_SUPER_MAGIC:
170 return SYSV_LINK_MAX;
172 case COH_SUPER_MAGIC:
173 return COH_LINK_MAX;
175 case UFS_MAGIC:
176 case UFS_CIGAM:
177 return UFS_LINK_MAX;
179 case REISERFS_SUPER_MAGIC:
180 return REISERFS_LINK_MAX;
182 case XFS_SUPER_MAGIC:
183 return XFS_LINK_MAX;
185 case LUSTRE_SUPER_MAGIC:
186 return LUSTRE_LINK_MAX;
188 default:
189 return LINUX_LINK_MAX;
194 /* Used like: return statfs_filesize_max (__statfs (name, &buf), &buf); */
195 long int
196 __statfs_filesize_max (int result, const struct statfs *fsbuf)
198 if (result < 0)
200 if (errno == ENOSYS)
201 /* Not possible, return the default value. */
202 return 32;
204 /* Some error occured. */
205 return -1;
208 switch (fsbuf->f_type)
210 case F2FS_SUPER_MAGIC:
211 return 256;
213 case BTRFS_SUPER_MAGIC:
214 return 255;
216 case EXT2_SUPER_MAGIC:
217 case UFS_MAGIC:
218 case UFS_CIGAM:
219 case REISERFS_SUPER_MAGIC:
220 case XFS_SUPER_MAGIC:
221 case SMB_SUPER_MAGIC:
222 case NTFS_SUPER_MAGIC:
223 case UDF_SUPER_MAGIC:
224 case JFS_SUPER_MAGIC:
225 case VXFS_SUPER_MAGIC:
226 case CGROUP_SUPER_MAGIC:
227 case LUSTRE_SUPER_MAGIC:
228 return 64;
230 case MSDOS_SUPER_MAGIC:
231 case JFFS_SUPER_MAGIC:
232 case JFFS2_SUPER_MAGIC:
233 case NCP_SUPER_MAGIC:
234 case ROMFS_SUPER_MAGIC:
235 return 32;
237 default:
238 return 32;
243 /* Used like: return statfs_link_max (__statfs (name, &buf), &buf); */
244 long int
245 __statfs_symlinks (int result, const struct statfs *fsbuf)
247 if (result < 0)
249 if (errno == ENOSYS)
250 /* Not possible, return the default value. */
251 return 1;
253 /* Some error occured. */
254 return -1;
257 switch (fsbuf->f_type)
259 case ADFS_SUPER_MAGIC:
260 case BFS_MAGIC:
261 case CRAMFS_MAGIC:
262 case DEVPTS_SUPER_MAGIC:
263 case EFS_SUPER_MAGIC:
264 case EFS_MAGIC:
265 case MSDOS_SUPER_MAGIC:
266 case NTFS_SUPER_MAGIC:
267 case QNX4_SUPER_MAGIC:
268 case ROMFS_SUPER_MAGIC:
269 /* No symlink support. */
270 return 0;
272 default:
273 return 1;
278 /* Used like: return __statfs_chown_restricted (__statfs (name, &buf), &buf);*/
279 long int
280 __statfs_chown_restricted (int result, const struct statfs *fsbuf)
282 if (result < 0)
284 if (errno == ENOSYS)
285 /* Not possible, return the default value. */
286 return 1;
288 /* Some error occured. */
289 return -1;
292 #if __ASSUME_XFS_RESTRICTED_CHOWN
293 return 1;
294 #else
295 int fd;
296 int save_errno;
297 long int retval = 1;
298 switch (fsbuf->f_type)
300 case XFS_SUPER_MAGIC:
301 save_errno = errno;
302 /* Read the value from /proc/sys/fs/xfs/restrict_chown. If we cannot
303 read it default to assume the restriction is in place. */
304 fd = open_not_cancel_2 ("/proc/sys/fs/xfs/restrict_chown", O_RDONLY);
305 if (fd != -1)
307 char buf[2];
308 if (TEMP_FAILURE_RETRY (read_not_cancel (fd, buf, 2)) == 2
309 && buf[0] >= '0' && buf[0] <= '1')
310 retval = buf[0] - '0';
312 close_not_cancel_no_status (fd);
314 __set_errno (save_errno);
315 break;
317 default:
318 break;
321 return retval;
322 #endif