]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - lib/libc/gen/fstab.c
MFC r363988:
[FreeBSD/stable/9.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 <vis.h>
50 #include "un-namespace.h"
51
52 static FILE *_fs_fp;
53 static struct fstab _fs_fstab;
54 static int LineNo = 0;
55 static char *path_fstab;
56 static char fstab_path[PATH_MAX];
57 static int fsp_set = 0;
58
59 static void error(int);
60 static void fixfsfile(void);
61 static int fstabscan(void);
62
63 void
64 setfstab(const char *file)
65 {
66
67         if (file == NULL) {
68                 path_fstab = _PATH_FSTAB;
69         } else {
70                 strncpy(fstab_path, file, PATH_MAX);
71                 fstab_path[PATH_MAX - 1] = '\0';
72                 path_fstab = fstab_path;
73         }
74         fsp_set = 1;
75
76         return;
77 }
78
79 const char *
80 getfstab (void)
81 {
82
83         if (fsp_set)
84                 return (path_fstab);
85         else
86                 return (_PATH_FSTAB);
87 }
88
89 static void
90 fixfsfile()
91 {
92         static char buf[sizeof(_PATH_DEV) + MNAMELEN];
93         struct stat sb;
94         struct statfs sf;
95
96         if (_fs_fstab.fs_file != NULL && strcmp(_fs_fstab.fs_file, "/") != 0)
97                 return;
98         if (statfs("/", &sf) != 0)
99                 return;
100         if (sf.f_mntfromname[0] == '/')
101                 buf[0] = '\0';
102         else
103                 strcpy(buf, _PATH_DEV);
104         strcat(buf, sf.f_mntfromname);
105         if (stat(buf, &sb) != 0 ||
106             (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode)))
107                 return;
108         _fs_fstab.fs_spec = buf;
109 }
110
111 static int
112 fstabscan()
113 {
114         char *cp, *p;
115 #define MAXLINELENGTH   1024
116         static char line[MAXLINELENGTH];
117         char subline[MAXLINELENGTH];
118         int typexx;
119
120         for (;;) {
121
122                 if (!(p = fgets(line, sizeof(line), _fs_fp)))
123                         return(0);
124 /* OLD_STYLE_FSTAB */
125                 ++LineNo;
126                 if (*line == '#' || *line == '\n')
127                         continue;
128                 if (!strpbrk(p, " \t")) {
129                         _fs_fstab.fs_spec = strsep(&p, ":\n");
130                         _fs_fstab.fs_file = strsep(&p, ":\n");
131                         fixfsfile();
132                         _fs_fstab.fs_type = strsep(&p, ":\n");
133                         if (_fs_fstab.fs_type) {
134                                 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
135                                         continue;
136                                 _fs_fstab.fs_mntops = _fs_fstab.fs_type;
137                                 _fs_fstab.fs_vfstype =
138                                     strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
139                                     "ufs" : "swap";
140                                 if ((cp = strsep(&p, ":\n")) != NULL) {
141                                         _fs_fstab.fs_freq = atoi(cp);
142                                         if ((cp = strsep(&p, ":\n")) != NULL) {
143                                                 _fs_fstab.fs_passno = atoi(cp);
144                                                 return(1);
145                                         }
146                                 }
147                         }
148                         goto bad;
149                 }
150 /* OLD_STYLE_FSTAB */
151                 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
152                         ;
153                 _fs_fstab.fs_spec = cp;
154                 if (_fs_fstab.fs_spec == NULL || *_fs_fstab.fs_spec == '#')
155                         continue;
156                 if (strunvis(_fs_fstab.fs_spec, _fs_fstab.fs_spec) < 0)
157                         goto bad;
158                 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
159                         ;
160                 _fs_fstab.fs_file = cp;
161                 if (_fs_fstab.fs_file == NULL)
162                         goto bad;
163                 if (strunvis(_fs_fstab.fs_file, _fs_fstab.fs_file) < 0)
164                         goto bad;
165                 fixfsfile();
166                 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
167                         ;
168                 _fs_fstab.fs_vfstype = cp;
169                 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
170                         ;
171                 _fs_fstab.fs_mntops = cp;
172                 if (_fs_fstab.fs_mntops == NULL)
173                         goto bad;
174                 _fs_fstab.fs_freq = 0;
175                 _fs_fstab.fs_passno = 0;
176                 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
177                         ;
178                 if (cp != NULL) {
179                         _fs_fstab.fs_freq = atoi(cp);
180                         while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
181                                 ;
182                         if (cp != NULL)
183                                 _fs_fstab.fs_passno = atoi(cp);
184                 }
185                 strcpy(subline, _fs_fstab.fs_mntops);
186                 p = subline;
187                 for (typexx = 0, cp = strsep(&p, ","); cp;
188                      cp = strsep(&p, ",")) {
189                         if (strlen(cp) != 2)
190                                 continue;
191                         if (!strcmp(cp, FSTAB_RW)) {
192                                 _fs_fstab.fs_type = FSTAB_RW;
193                                 break;
194                         }
195                         if (!strcmp(cp, FSTAB_RQ)) {
196                                 _fs_fstab.fs_type = FSTAB_RQ;
197                                 break;
198                         }
199                         if (!strcmp(cp, FSTAB_RO)) {
200                                 _fs_fstab.fs_type = FSTAB_RO;
201                                 break;
202                         }
203                         if (!strcmp(cp, FSTAB_SW)) {
204                                 _fs_fstab.fs_type = FSTAB_SW;
205                                 break;
206                         }
207                         if (!strcmp(cp, FSTAB_XX)) {
208                                 _fs_fstab.fs_type = FSTAB_XX;
209                                 typexx++;
210                                 break;
211                         }
212                 }
213                 if (typexx)
214                         continue;
215                 if (cp != NULL)
216                         return(1);
217
218 bad:            /* no way to distinguish between EOF and syntax error */
219                 error(EFTYPE);
220         }
221         /* NOTREACHED */
222 }
223
224 struct fstab *
225 getfsent()
226 {
227         if ((!_fs_fp && !setfsent()) || !fstabscan())
228                 return((struct fstab *)NULL);
229         return(&_fs_fstab);
230 }
231
232 struct fstab *
233 getfsspec(name)
234         const char *name;
235 {
236         if (setfsent())
237                 while (fstabscan())
238                         if (!strcmp(_fs_fstab.fs_spec, name))
239                                 return(&_fs_fstab);
240         return((struct fstab *)NULL);
241 }
242
243 struct fstab *
244 getfsfile(name)
245         const char *name;
246 {
247         if (setfsent())
248                 while (fstabscan())
249                         if (!strcmp(_fs_fstab.fs_file, name))
250                                 return(&_fs_fstab);
251         return((struct fstab *)NULL);
252 }
253
254 int 
255 setfsent()
256 {
257         int fd;
258
259         if (_fs_fp) {
260                 rewind(_fs_fp);
261                 LineNo = 0;
262                 return(1);
263         }
264         if (fsp_set == 0) {
265                 if (issetugid())
266                         setfstab(NULL);
267                 else
268                         setfstab(getenv("PATH_FSTAB"));
269         }
270         fd = _open(path_fstab, O_RDONLY | O_CLOEXEC);
271         if (fd == -1) {
272                 error(errno);
273                 return (0);
274         }
275         _fs_fp = fdopen(fd, "r");
276         if (_fs_fp  != NULL) {
277                 LineNo = 0;
278                 return(1);
279         }
280         error(errno);
281         _close(fd);
282         return(0);
283 }
284
285 void
286 endfsent()
287 {
288         if (_fs_fp) {
289                 (void)fclose(_fs_fp);
290                 _fs_fp = NULL;
291         }
292
293         fsp_set = 0;
294 }
295
296 static void
297 error(err)
298         int err;
299 {
300         char *p;
301         char num[30];
302
303         (void)_write(STDERR_FILENO, "fstab: ", 7);
304         (void)_write(STDERR_FILENO, path_fstab, strlen(path_fstab));
305         (void)_write(STDERR_FILENO, ":", 1);
306         sprintf(num, "%d: ", LineNo);
307         (void)_write(STDERR_FILENO, num, strlen(num));
308         p = strerror(err);
309         (void)_write(STDERR_FILENO, p, strlen(p));
310         (void)_write(STDERR_FILENO, "\n", 1);
311 }