]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libc/gen/fstab.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / lib / libc / gen / fstab.c
1 /*
2  * Copyright (c) 1980, 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)fstab.c     8.1 (Berkeley) 6/4/93";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include "namespace.h"
37 #include <sys/param.h>
38 #include <sys/mount.h>
39 #include <sys/stat.h>
40
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <fstab.h>
44 #include <paths.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include "un-namespace.h"
50
51 static FILE *_fs_fp;
52 static struct fstab _fs_fstab;
53 static int LineNo = 0;
54 static char *path_fstab;
55 static char fstab_path[PATH_MAX];
56 static int fsp_set = 0;
57
58 static void error(int);
59 static void fixfsfile(void);
60 static int fstabscan(void);
61
62 void
63 setfstab(const char *file)
64 {
65
66         if (file == NULL) {
67                 path_fstab = _PATH_FSTAB;
68         } else {
69                 strncpy(fstab_path, file, PATH_MAX);
70                 fstab_path[PATH_MAX - 1] = '\0';
71                 path_fstab = fstab_path;
72         }
73         fsp_set = 1;
74
75         return;
76 }
77
78 const char *
79 getfstab (void)
80 {
81
82         if (fsp_set)
83                 return (path_fstab);
84         else
85                 return (_PATH_FSTAB);
86 }
87
88 static void
89 fixfsfile()
90 {
91         static char buf[sizeof(_PATH_DEV) + MNAMELEN];
92         struct stat sb;
93         struct statfs sf;
94
95         if (_fs_fstab.fs_file != NULL && strcmp(_fs_fstab.fs_file, "/") != 0)
96                 return;
97         if (statfs("/", &sf) != 0)
98                 return;
99         if (sf.f_mntfromname[0] == '/')
100                 buf[0] = '\0';
101         else
102                 strcpy(buf, _PATH_DEV);
103         strcat(buf, sf.f_mntfromname);
104         if (stat(buf, &sb) != 0 ||
105             (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode)))
106                 return;
107         _fs_fstab.fs_spec = buf;
108 }
109
110 static int
111 fstabscan()
112 {
113         char *cp, *p;
114 #define MAXLINELENGTH   1024
115         static char line[MAXLINELENGTH];
116         char subline[MAXLINELENGTH];
117         int typexx;
118
119         for (;;) {
120
121                 if (!(p = fgets(line, sizeof(line), _fs_fp)))
122                         return(0);
123 /* OLD_STYLE_FSTAB */
124                 ++LineNo;
125                 if (*line == '#' || *line == '\n')
126                         continue;
127                 if (!strpbrk(p, " \t")) {
128                         _fs_fstab.fs_spec = strsep(&p, ":\n");
129                         _fs_fstab.fs_file = strsep(&p, ":\n");
130                         fixfsfile();
131                         _fs_fstab.fs_type = strsep(&p, ":\n");
132                         if (_fs_fstab.fs_type) {
133                                 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
134                                         continue;
135                                 _fs_fstab.fs_mntops = _fs_fstab.fs_type;
136                                 _fs_fstab.fs_vfstype =
137                                     strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
138                                     "ufs" : "swap";
139                                 if ((cp = strsep(&p, ":\n")) != NULL) {
140                                         _fs_fstab.fs_freq = atoi(cp);
141                                         if ((cp = strsep(&p, ":\n")) != NULL) {
142                                                 _fs_fstab.fs_passno = atoi(cp);
143                                                 return(1);
144                                         }
145                                 }
146                         }
147                         goto bad;
148                 }
149 /* OLD_STYLE_FSTAB */
150                 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
151                         ;
152                 _fs_fstab.fs_spec = cp;
153                 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
154                         continue;
155                 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
156                         ;
157                 _fs_fstab.fs_file = cp;
158                 fixfsfile();
159                 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
160                         ;
161                 _fs_fstab.fs_vfstype = cp;
162                 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
163                         ;
164                 _fs_fstab.fs_mntops = cp;
165                 if (_fs_fstab.fs_mntops == NULL)
166                         goto bad;
167                 _fs_fstab.fs_freq = 0;
168                 _fs_fstab.fs_passno = 0;
169                 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
170                         ;
171                 if (cp != NULL) {
172                         _fs_fstab.fs_freq = atoi(cp);
173                         while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
174                                 ;
175                         if (cp != NULL)
176                                 _fs_fstab.fs_passno = atoi(cp);
177                 }
178                 strcpy(subline, _fs_fstab.fs_mntops);
179                 p = subline;
180                 for (typexx = 0, cp = strsep(&p, ","); cp;
181                      cp = strsep(&p, ",")) {
182                         if (strlen(cp) != 2)
183                                 continue;
184                         if (!strcmp(cp, FSTAB_RW)) {
185                                 _fs_fstab.fs_type = FSTAB_RW;
186                                 break;
187                         }
188                         if (!strcmp(cp, FSTAB_RQ)) {
189                                 _fs_fstab.fs_type = FSTAB_RQ;
190                                 break;
191                         }
192                         if (!strcmp(cp, FSTAB_RO)) {
193                                 _fs_fstab.fs_type = FSTAB_RO;
194                                 break;
195                         }
196                         if (!strcmp(cp, FSTAB_SW)) {
197                                 _fs_fstab.fs_type = FSTAB_SW;
198                                 break;
199                         }
200                         if (!strcmp(cp, FSTAB_XX)) {
201                                 _fs_fstab.fs_type = FSTAB_XX;
202                                 typexx++;
203                                 break;
204                         }
205                 }
206                 if (typexx)
207                         continue;
208                 if (cp != NULL)
209                         return(1);
210
211 bad:            /* no way to distinguish between EOF and syntax error */
212                 error(EFTYPE);
213         }
214         /* NOTREACHED */
215 }
216
217 struct fstab *
218 getfsent()
219 {
220         if ((!_fs_fp && !setfsent()) || !fstabscan())
221                 return((struct fstab *)NULL);
222         return(&_fs_fstab);
223 }
224
225 struct fstab *
226 getfsspec(name)
227         const char *name;
228 {
229         if (setfsent())
230                 while (fstabscan())
231                         if (!strcmp(_fs_fstab.fs_spec, name))
232                                 return(&_fs_fstab);
233         return((struct fstab *)NULL);
234 }
235
236 struct fstab *
237 getfsfile(name)
238         const char *name;
239 {
240         if (setfsent())
241                 while (fstabscan())
242                         if (!strcmp(_fs_fstab.fs_file, name))
243                                 return(&_fs_fstab);
244         return((struct fstab *)NULL);
245 }
246
247 int 
248 setfsent()
249 {
250         int fd;
251
252         if (_fs_fp) {
253                 rewind(_fs_fp);
254                 LineNo = 0;
255                 return(1);
256         }
257         if (fsp_set == 0) {
258                 if (issetugid())
259                         setfstab(NULL);
260                 else
261                         setfstab(getenv("PATH_FSTAB"));
262         }
263         fd = _open(path_fstab, O_RDONLY | O_CLOEXEC);
264         if (fd == -1) {
265                 error(errno);
266                 return (0);
267         }
268         _fs_fp = fdopen(fd, "r");
269         if (_fs_fp  != NULL) {
270                 LineNo = 0;
271                 return(1);
272         }
273         error(errno);
274         _close(fd);
275         return(0);
276 }
277
278 void
279 endfsent()
280 {
281         if (_fs_fp) {
282                 (void)fclose(_fs_fp);
283                 _fs_fp = NULL;
284         }
285
286         fsp_set = 0;
287 }
288
289 static void
290 error(err)
291         int err;
292 {
293         char *p;
294         char num[30];
295
296         (void)_write(STDERR_FILENO, "fstab: ", 7);
297         (void)_write(STDERR_FILENO, path_fstab, strlen(path_fstab));
298         (void)_write(STDERR_FILENO, ":", 1);
299         sprintf(num, "%d: ", LineNo);
300         (void)_write(STDERR_FILENO, num, strlen(num));
301         p = strerror(err);
302         (void)_write(STDERR_FILENO, p, strlen(p));
303         (void)_write(STDERR_FILENO, "\n", 1);
304 }