1 /* Linux specific extensions to fpathconf.
2    Copyright (C) 1991,95,96,98,99,2000,2001 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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.
9 
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.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19 
20 /* Modified for newlib July 19, 2002 by Jeff Johnston */
21 
22 #include <errno.h>
23 #include <stddef.h>
24 #include <unistd.h>
25 #include <limits.h>
26 #include <sys/stat.h>
27 #include <sys/statfs.h>
28 #include <sys/statvfs.h>
29 #include <machine/weakalias.h>
30 #include "linux_fsinfo.h"
31 
32 /* The Linux kernel header mentioned this as a kind of generic value.  */
33 #define LINUX_LINK_MAX	127
34 
35 static long int posix_fpathconf (int fd, int name);
36 
37 /* Get file-specific information about descriptor FD.  */
38 long int
__fpathconf(fd,name)39 __fpathconf (fd, name)
40      int fd;
41      int name;
42 {
43   if (name == _PC_LINK_MAX)
44     {
45       struct statfs fsbuf;
46 
47       /* Determine the filesystem type.  */
48       if (__fstatfs (fd, &fsbuf) < 0)
49 	{
50 	  if (errno == ENOSYS)
51 	    /* not possible, return the default value.  */
52 	    return LINUX_LINK_MAX;
53 
54 	  /* Some error occured.  */
55           return -1;
56 	}
57 
58       switch (fsbuf.f_type)
59 	{
60 	case EXT2_SUPER_MAGIC:
61 	  return EXT2_LINK_MAX;
62 
63 	case MINIX_SUPER_MAGIC:
64 	case MINIX_SUPER_MAGIC2:
65 	  return MINIX_LINK_MAX;
66 
67 	case MINIX2_SUPER_MAGIC:
68 	case MINIX2_SUPER_MAGIC2:
69 	  return MINIX2_LINK_MAX;
70 
71 	case XENIX_SUPER_MAGIC:
72 	  return XENIX_LINK_MAX;
73 
74 	case SYSV4_SUPER_MAGIC:
75 	case SYSV2_SUPER_MAGIC:
76 	  return SYSV_LINK_MAX;
77 
78 	case COH_SUPER_MAGIC:
79 	  return COH_LINK_MAX;
80 
81 	case UFS_MAGIC:
82 	case UFS_CIGAM:
83 	  return UFS_LINK_MAX;
84 
85 	case REISERFS_SUPER_MAGIC:
86 	  return REISERFS_LINK_MAX;
87 
88 	default:
89 	  return LINUX_LINK_MAX;
90 	}
91     }
92 
93   return posix_fpathconf (fd, name);
94 }
95 
96 /* Get file-specific information about descriptor FD.  */
97 static long int
posix_fpathconf(fd,name)98 posix_fpathconf (fd, name)
99      int fd;
100      int name;
101 {
102   if (fd < 0)
103     {
104       __set_errno (EBADF);
105       return -1;
106     }
107 
108   switch (name)
109     {
110     default:
111       __set_errno (EINVAL);
112       return -1;
113 
114     case _PC_LINK_MAX:
115 #ifdef	LINK_MAX
116       return LINK_MAX;
117 #else
118       return -1;
119 #endif
120 
121     case _PC_MAX_CANON:
122 #ifdef	MAX_CANON
123       return MAX_CANON;
124 #else
125       return -1;
126 #endif
127 
128     case _PC_MAX_INPUT:
129 #ifdef	MAX_INPUT
130       return MAX_INPUT;
131 #else
132       return -1;
133 #endif
134 
135     case _PC_NAME_MAX:
136 #ifdef	NAME_MAX
137       {
138 	struct statfs buf;
139 	int save_errno = errno;
140 
141 	if (__fstatfs (fd, &buf) < 0)
142 	  {
143 	    if (errno == ENOSYS)
144 	      {
145 		__set_errno (save_errno);
146 		return NAME_MAX;
147 	      }
148 	    else if (errno == ENODEV)
149 	      __set_errno (EINVAL);
150 
151 	    return -1;
152 	  }
153 	else
154 	  {
155 #ifdef _STATFS_F_NAMELEN
156 	    return buf.f_namelen;
157 #else
158 # ifdef _STATFS_F_NAME_MAX
159 	    return buf.f_name_max;
160 # else
161 	    return NAME_MAX;
162 # endif
163 #endif
164 	  }
165       }
166 #else
167       return -1;
168 #endif
169 
170     case _PC_PATH_MAX:
171 #ifdef	PATH_MAX
172       return PATH_MAX;
173 #else
174       return -1;
175 #endif
176 
177     case _PC_PIPE_BUF:
178 #ifdef	PIPE_BUF
179       return PIPE_BUF;
180 #else
181       return -1;
182 #endif
183 
184     case _PC_CHOWN_RESTRICTED:
185 #ifdef	_POSIX_CHOWN_RESTRICTED
186       return _POSIX_CHOWN_RESTRICTED;
187 #else
188       return -1;
189 #endif
190 
191     case _PC_NO_TRUNC:
192 #ifdef	_POSIX_NO_TRUNC
193       return _POSIX_NO_TRUNC;
194 #else
195       return -1;
196 #endif
197 
198     case _PC_VDISABLE:
199 #ifdef	_POSIX_VDISABLE
200       return _POSIX_VDISABLE;
201 #else
202       return -1;
203 #endif
204 
205     case _PC_SYNC_IO:
206 #ifdef	_POSIX_SYNC_IO
207       return _POSIX_SYNC_IO;
208 #else
209       return -1;
210 #endif
211 
212     case _PC_ASYNC_IO:
213 #ifdef	_POSIX_ASYNC_IO
214       {
215 	/* AIO is only allowed on regular files and block devices.  */
216 	struct stat64 st;
217 
218 	if (fstat64 (fd, &st) < 0
219 	    || (! S_ISREG (st.st_mode) && ! S_ISBLK (st.st_mode)))
220 	  return -1;
221 	else
222 	  return 1;
223       }
224 #else
225       return -1;
226 #endif
227 
228     case _PC_PRIO_IO:
229 #ifdef	_POSIX_PRIO_IO
230       return _POSIX_PRIO_IO;
231 #else
232       return -1;
233 #endif
234 
235     case _PC_SOCK_MAXBUF:
236 #ifdef	SOCK_MAXBUF
237       return SOCK_MAXBUF;
238 #else
239       return -1;
240 #endif
241 
242     case _PC_FILESIZEBITS:
243 #ifdef FILESIZEBITS
244       return FILESIZEBITS;
245 #else
246       /* We let platforms with larger file sizes overwrite this value.  */
247       return 32;
248 #endif
249 
250     case _PC_REC_INCR_XFER_SIZE:
251       /* XXX It is not entirely clear what the limit is supposed to do.
252 	 What is incremented?  */
253       return -1;
254 
255     case _PC_REC_MAX_XFER_SIZE:
256       /* XXX It is not entirely clear what the limit is supposed to do.
257 	 In general there is no top limit of the number of bytes which
258 	 case be transported at once.  */
259       return -1;
260 
261     case _PC_REC_MIN_XFER_SIZE:
262       {
263 	/* XXX It is not entirely clear what the limit is supposed to do.
264 	   I assume this is the block size of the filesystem.  */
265 	struct statvfs64 sv;
266 
267 	if (__fstatvfs64 (fd, &sv) < 0)
268 	  return -1;
269 	return sv.f_bsize;
270       }
271 
272     case _PC_REC_XFER_ALIGN:
273       {
274 	/* XXX It is not entirely clear what the limit is supposed to do.
275 	   I assume that the number should reflect the minimal block
276 	   alignment.  */
277 	struct statvfs64 sv;
278 
279 	if (__fstatvfs64 (fd, &sv) < 0)
280 	  return -1;
281 	return sv.f_frsize;
282       }
283 
284     case _PC_ALLOC_SIZE_MIN:
285       {
286 	/* XXX It is not entirely clear what the limit is supposed to do.
287 	   I assume that the number should reflect the minimal block
288 	   alignment.  */
289 	struct statvfs64 sv;
290 
291 	if (__fstatvfs64 (fd, &sv) < 0)
292 	  return -1;
293 	return sv.f_frsize;
294       }
295 
296     case _PC_SYMLINK_MAX:
297       /* In general there are no limits.  If a system has one it should
298 	 overwrite this case.  */
299       return -1;
300     }
301 }
302 
303 weak_alias (__fpathconf, fpathconf)
304