Merge commit 'ad3ad82ad2fb99c424a8482bd1908d08b990ccea'
[unleashed.git] / usr / src / cmd / fs.d / fslib.c
blobf985622ab9d5a7b8fcaeeb2c08499485f999c247
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <libintl.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <syslog.h>
35 #include <alloca.h>
36 #include <sys/vfstab.h>
37 #include <sys/mnttab.h>
38 #include <sys/mntent.h>
39 #include <sys/mount.h>
40 #include <sys/filio.h>
41 #include <sys/fs/ufs_filio.h>
42 #include <sys/stat.h>
43 #include <sys/param.h>
44 #include <zone.h>
45 #include <signal.h>
46 #include <strings.h>
47 #include "fslib.h"
49 /* LINTLIBRARY */
51 #define BUFLEN 256
53 #define TIME_MAX 16
56 * Reads all of the entries from the in-kernel mnttab, and returns the
57 * linked list of the entries.
59 mntlist_t *
60 fsgetmntlist(void)
62 FILE *mfp;
63 mntlist_t *mntl;
64 char buf[BUFLEN];
66 if ((mfp = fopen(MNTTAB, "r")) == NULL) {
67 (void) snprintf(buf, BUFLEN, "fsgetmntlist: fopen %s", MNTTAB);
68 perror(buf);
69 return (NULL);
72 mntl = fsmkmntlist(mfp);
74 (void) fclose(mfp);
75 return (mntl);
79 static struct extmnttab zmnttab = { 0 };
81 struct extmnttab *
82 fsdupmnttab(struct extmnttab *mnt)
84 struct extmnttab *new;
86 new = (struct extmnttab *)malloc(sizeof (*new));
87 if (new == NULL)
88 goto alloc_failed;
90 *new = zmnttab;
92 * Allocate an extra byte for the mountpoint
93 * name in case a space needs to be added.
95 new->mnt_mountp = (char *)malloc(strlen(mnt->mnt_mountp) + 2);
96 if (new->mnt_mountp == NULL)
97 goto alloc_failed;
98 (void) strcpy(new->mnt_mountp, mnt->mnt_mountp);
100 if ((new->mnt_special = strdup(mnt->mnt_special)) == NULL)
101 goto alloc_failed;
103 if ((new->mnt_fstype = strdup(mnt->mnt_fstype)) == NULL)
104 goto alloc_failed;
106 if (mnt->mnt_mntopts != NULL)
107 if ((new->mnt_mntopts = strdup(mnt->mnt_mntopts)) == NULL)
108 goto alloc_failed;
110 if (mnt->mnt_time != NULL)
111 if ((new->mnt_time = strdup(mnt->mnt_time)) == NULL)
112 goto alloc_failed;
114 new->mnt_major = mnt->mnt_major;
115 new->mnt_minor = mnt->mnt_minor;
116 return (new);
118 alloc_failed:
119 (void) fprintf(stderr, gettext("fsdupmnttab: Out of memory\n"));
120 fsfreemnttab(new);
121 return (NULL);
125 * Free a single mnttab structure
127 void
128 fsfreemnttab(struct extmnttab *mnt)
131 if (mnt) {
132 free(mnt->mnt_special);
133 free(mnt->mnt_mountp);
134 free(mnt->mnt_fstype);
135 free(mnt->mnt_mntopts);
136 free(mnt->mnt_time);
137 free(mnt);
141 void
142 fsfreemntlist(mntlist_t *mntl)
144 mntlist_t *mntl_tmp;
146 while (mntl) {
147 fsfreemnttab(mntl->mntl_mnt);
148 mntl_tmp = mntl;
149 mntl = mntl->mntl_next;
150 free(mntl_tmp);
155 * Read the mnttab file and return it as a list of mnttab structs.
156 * Returns NULL if there was a memory failure.
158 mntlist_t *
159 fsmkmntlist(FILE *mfp)
161 struct extmnttab mnt;
162 mntlist_t *mhead, *mtail;
163 int ret;
165 mhead = mtail = NULL;
167 resetmnttab(mfp);
168 while ((ret = getextmntent(mfp, &mnt, sizeof (struct extmnttab)))
169 != -1) {
170 mntlist_t *mp;
172 if (ret != 0) /* bad entry */
173 continue;
175 mp = (mntlist_t *)malloc(sizeof (*mp));
176 if (mp == NULL)
177 goto alloc_failed;
178 if (mhead == NULL)
179 mhead = mp;
180 else
181 mtail->mntl_next = mp;
182 mtail = mp;
183 mp->mntl_next = NULL;
184 mp->mntl_flags = 0;
185 if ((mp->mntl_mnt = fsdupmnttab(&mnt)) == NULL)
186 goto alloc_failed;
188 return (mhead);
190 alloc_failed:
191 fsfreemntlist(mhead);
192 return (NULL);
196 * Return the last entry that matches mntin's special
197 * device and/or mountpt.
198 * Helps to be robust here, so we check for NULL pointers.
200 mntlist_t *
201 fsgetmlast(mntlist_t *ml, struct mnttab *mntin)
203 mntlist_t *delete = NULL;
205 for (; ml; ml = ml->mntl_next) {
206 if (mntin->mnt_mountp && mntin->mnt_special) {
208 * match if and only if both are equal.
210 if ((strcmp(ml->mntl_mnt->mnt_mountp,
211 mntin->mnt_mountp) == 0) &&
212 (strcmp(ml->mntl_mnt->mnt_special,
213 mntin->mnt_special) == 0))
214 delete = ml;
215 } else if (mntin->mnt_mountp) {
216 if (strcmp(ml->mntl_mnt->mnt_mountp,
217 mntin->mnt_mountp) == 0)
218 delete = ml;
219 } else if (mntin->mnt_special) {
220 if (strcmp(ml->mntl_mnt->mnt_special,
221 mntin->mnt_special) == 0)
222 delete = ml;
225 return (delete);
230 * Returns the mountlevel of the pathname in cp. As examples,
231 * / => 1, /bin => 2, /bin/ => 2, ////bin////ls => 3, sdf => 0, etc...
234 fsgetmlevel(char *cp)
236 int mlevel;
237 char *cp1;
239 if (cp == NULL || *cp == '\0' || *cp != '/')
240 return (0); /* this should never happen */
242 mlevel = 1; /* root (/) is the minimal case */
244 for (cp1 = cp + 1; *cp1; cp++, cp1++)
245 if (*cp == '/' && *cp1 != '/') /* "///" counts as 1 */
246 mlevel++;
248 return (mlevel);
252 * Returns non-zero if string s is a member of the strings in ps.
255 fsstrinlist(const char *s, const char **ps)
257 const char *cp;
258 cp = *ps;
259 while (cp) {
260 if (strcmp(s, cp) == 0)
261 return (1);
262 ps++;
263 cp = *ps;
265 return (0);
268 static char *empty_opt_vector[] = {
269 NULL
272 * Compare the mount options that were requested by the caller to
273 * the options actually supported by the file system. If any requested
274 * options are not supported, print a warning message.
276 * WARNING: this function modifies the string pointed to by
277 * the requested_opts argument.
279 * Arguments:
280 * requested_opts - the string containing the requested options.
281 * actual_opts - the string returned by mount(2), which lists the
282 * options actually supported. It is normal for this
283 * string to contain more options than the requested options.
284 * (The actual options may contain the default options, which
285 * may not have been included in the requested options.)
286 * special - device being mounted (only used in error messages).
287 * mountp - mount point (only used in error messages).
289 void
290 cmp_requested_to_actual_options(char *requested_opts, char *actual_opts,
291 char *special, char *mountp)
293 char *option_ptr, *actopt, *equalptr;
294 int found;
295 char *actual_opt_hold, *bufp;
297 if (requested_opts == NULL)
298 return;
300 bufp = alloca(strlen(actual_opts) + 1);
302 while (*requested_opts != '\0') {
303 (void) getsubopt(&requested_opts, empty_opt_vector,
304 &option_ptr);
307 * Truncate any "=<value>" string from the end of
308 * the option.
310 if ((equalptr = strchr(option_ptr, '=')) != NULL)
311 *equalptr = '\0';
313 if (*option_ptr == '\0')
314 continue;
317 * Whilst we don't need this option to perform a lofi
318 * mount, let's not be mendacious enough to complain
319 * about it.
321 if (strcmp(option_ptr, "loop") == 0)
322 continue;
325 * Search for the requested option in the list of options
326 * actually supported.
328 found = 0;
331 * Need to use a copy of actual_opts because getsubopt
332 * is destructive and we need to scan the actual_opts
333 * string more than once.
335 * We also need to reset actual_opt_hold to the
336 * beginning of the buffer because getsubopt changes
337 * actual_opt_hold (the pointer).
339 actual_opt_hold = bufp;
340 if (actual_opts != NULL)
341 (void) strcpy(actual_opt_hold, actual_opts);
342 else
343 *actual_opt_hold = '\0';
345 while (*actual_opt_hold != '\0') {
346 (void) getsubopt(&actual_opt_hold, empty_opt_vector,
347 &actopt);
349 /* Truncate the "=<value>", if any. */
350 if ((equalptr = strchr(actopt, '=')) != NULL)
351 *equalptr = '\0';
353 if ((strcmp(option_ptr, actopt)) == 0) {
354 found = 1;
355 break;
359 if (found == 0) {
361 * That we're ignoring the option is always
362 * truthful; the old message that the option
363 * was unknown is often not correct.
365 (void) fprintf(stderr, gettext(
366 "mount: %s on %s - WARNING ignoring option "
367 "\"%s\"\n"), special, mountp, option_ptr);
372 * FUNCTION: fsgetmaxphys(int *, int *)
374 * INPUT: int *maxphys - a pointer to an integer that will hold
375 * the value for the system maxphys value.
376 * int *error - 0 means completed successfully
377 * otherwise this indicates the errno value.
379 * RETURNS: int - 0 if maxphys not found
380 * - 1 if maxphys is found
383 fsgetmaxphys(int *maxphys, int *error) {
385 int gotit = 0;
386 int fp = open("/", O_RDONLY);
388 *error = 0;
391 * For some reason cannot open root as read only. Need a valid file
392 * descriptor to call the ufs private ioctl. If this open failes,
393 * just assume we cannot get maxphys in this case.
395 if (fp == -1) {
396 return (gotit);
399 if (ioctl(fp, _FIOGETMAXPHYS, maxphys) == -1) {
400 *error = errno;
401 (void) close(fp);
402 return (gotit);
405 (void) close(fp);
406 gotit = 1;
407 return (gotit);
412 * The below is limited support for zone-aware commands.
414 struct zone_summary {
415 zoneid_t zoneid;
416 char rootpath[MAXPATHLEN];
417 size_t rootpathlen;
420 struct zone_summary *
421 fs_get_zone_summaries(void)
423 uint_t numzones = 0, oldnumzones = 0;
424 uint_t i, j;
425 zoneid_t *ids = NULL;
426 struct zone_summary *summaries;
427 zoneid_t myzoneid = getzoneid();
429 for (;;) {
430 if (zone_list(ids, &numzones) < 0) {
431 perror("unable to retrieve list of zones");
432 free(ids);
433 return (NULL);
435 if (numzones <= oldnumzones)
436 break;
437 free(ids);
438 ids = malloc(numzones * sizeof (*ids));
439 if (ids == NULL) {
440 perror("malloc failed");
441 return (NULL);
443 oldnumzones = numzones;
446 summaries = malloc((numzones + 1) * sizeof (*summaries));
447 if (summaries == NULL) {
448 free(ids);
449 perror("malloc failed");
450 return (NULL);
454 for (i = 0, j = 0; i < numzones; i++) {
455 ssize_t len;
457 if (ids[i] == myzoneid)
458 continue;
459 len = zone_getattr(ids[i], ZONE_ATTR_ROOT,
460 summaries[j].rootpath, sizeof (summaries[j].rootpath));
461 if (len < 0) {
463 * Zone must have gone away. Skip.
465 continue;
468 * Adding a trailing '/' to the zone's rootpath allows us to
469 * use strncmp() to see if a given path resides within that
470 * zone.
472 * As an example, if the zone's rootpath is "/foo/root",
473 * "/foo/root/usr" resides within the zone, while
474 * "/foo/rootpath" doesn't.
476 (void) strlcat(summaries[j].rootpath, "/",
477 sizeof (summaries[j].rootpath));
478 summaries[j].rootpathlen = len;
479 summaries[j].zoneid = ids[i];
480 j++;
482 summaries[j].zoneid = -1;
483 free(ids);
484 return (summaries);
487 static zoneid_t
488 fs_find_zone(const struct zone_summary *summaries, const char *mntpt)
490 uint_t i;
492 for (i = 0; summaries[i].zoneid != -1; i++) {
493 if (strncmp(mntpt, summaries[i].rootpath,
494 summaries[i].rootpathlen) == 0)
495 return (summaries[i].zoneid);
498 * (-1) is the special token we return to the caller if the mount
499 * wasn't found in any other mounts on the system. This means it's
500 * only visible to our zone.
502 * Odd choice of constant, I know, but it beats calling getzoneid() a
503 * million times.
505 return (-1);
508 boolean_t
509 fs_mount_in_other_zone(const struct zone_summary *summaries, const char *mntpt)
511 return (fs_find_zone(summaries, mntpt) != -1);
515 * List of standard options.
517 static const char *stdopts[] = {
518 MNTOPT_RO, MNTOPT_RW,
519 MNTOPT_SUID, MNTOPT_NOSUID,
520 MNTOPT_DEVICES, MNTOPT_NODEVICES,
521 MNTOPT_SETUID, MNTOPT_NOSETUID,
522 MNTOPT_NBMAND, MNTOPT_NONBMAND,
523 MNTOPT_EXEC, MNTOPT_NOEXEC,
526 #define NSTDOPT (sizeof (stdopts) / sizeof (stdopts[0]))
528 static int
529 optindx(const char *opt)
531 int i;
533 for (i = 0; i < NSTDOPT; i++) {
534 if (strcmp(opt, stdopts[i]) == 0)
535 return (i);
537 return (-1);
541 * INPUT: filesystem option not recognized by the fs specific option
542 * parsing code.
543 * OUTPUT: True if and only if the option is one of the standard VFS
544 * layer options.
546 boolean_t
547 fsisstdopt(const char *opt)
549 return (optindx(opt) != -1);