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