ses: SUN plugin doesn't actually need libnvfru
[unleashed.git] / usr / src / lib / libfsmgt / common / fs_mount_defaults.c
blob04d684bef23b344bf1d1abdf3941258a9f71a25b
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Traverses /etc/vfstab in order to find default mount information about
31 * file systems on the current host.
33 #include <errno.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <sys/vfstab.h>
37 #include <sys/types.h>
38 #include <strings.h>
39 #include <thread.h>
40 #include <synch.h>
41 #include "libfsmgt.h"
44 * Private constants
47 static const char sepstr[] = "\t\n";
50 * Private variables
52 static mutex_t vfstab_lock = DEFAULTMUTEX;
56 * Private method declarations
58 static int cmp_fields(char *, char *, int);
59 static fs_mntdefaults_t *create_mntdefaults_entry(struct vfstab vfstab_entry,
60 int *errp);
61 static struct vfstab *create_vfstab_filter(fs_mntdefaults_t *filter,
62 int *errp);
63 static void free_vfstab_entry(struct vfstab *vfstab_entry);
64 static char *create_vfstab_entry_line(struct vfstab *, int *);
65 static int vfstab_line_cmp(fs_mntdefaults_t *, struct vfstab *);
68 * Public methods
71 void fs_free_mntdefaults_list(fs_mntdefaults_t *headp) {
72 fs_mntdefaults_t *tmp;
74 while (headp != NULL) {
75 tmp = headp->next;
76 free(headp->resource);
77 free(headp->fsckdevice);
78 free(headp->mountp);
79 free(headp->fstype);
80 free(headp->fsckpass);
81 free(headp->mountatboot);
82 free(headp->mntopts);
83 headp->next = NULL;
84 free(headp);
86 headp = tmp;
88 } /* fs_free_mntdefaults_list */
91 * Filter by the fields that are filled in on the filter parameter.
92 * Fields that aren't used in filtering the defaults will be NULL.
94 fs_mntdefaults_t *fs_get_filtered_mount_defaults(fs_mntdefaults_t *filter,
95 int *errp) {
97 fs_mntdefaults_t *newp;
98 fs_mntdefaults_t *headp;
99 fs_mntdefaults_t *tailp;
100 FILE *fp;
102 headp = NULL;
103 tailp = NULL;
106 if ((fp = fopen(VFSTAB, "r")) != NULL) {
107 struct vfstab vfstab_entry;
108 struct vfstab *search_entry;
109 (void) mutex_lock(&vfstab_lock);
110 search_entry = create_vfstab_filter(filter, errp);
111 if (search_entry == NULL) {
113 * Out of memory, the error pointer (errp) gets
114 * set in create_vfstab_filter.
116 fs_free_mntdefaults_list(headp);
117 (void) mutex_unlock(&vfstab_lock);
118 (void) fclose(fp);
119 return (NULL);
122 while (getvfsany(fp, &vfstab_entry, search_entry) == 0) {
124 * Add to list to be returned
126 newp = create_mntdefaults_entry(vfstab_entry, errp);
127 if (newp == NULL) {
129 * Out of memory, the error pointer (errp)
130 * gets set in create_mntdefaults_entry.
132 fs_free_mntdefaults_list(headp);
133 (void) mutex_unlock(&vfstab_lock);
134 (void) fclose(fp);
135 return (NULL);
138 if (headp == NULL) {
139 headp = newp;
140 tailp = newp;
141 } else {
142 tailp->next = newp;
143 tailp = newp;
146 free_vfstab_entry(search_entry);
147 (void) mutex_unlock(&vfstab_lock);
148 (void) fclose(fp);
150 } else {
151 *errp = errno;
152 } /* if ((fp = fopen(VFSTAB, "r")) != NULL) */
154 return (headp);
155 } /* fs_get_filtered_mount_defaults */
158 fs_mntdefaults_t *
159 fs_get_mount_defaults(int *errp)
161 fs_mntdefaults_t *newp;
162 fs_mntdefaults_t *headp;
163 fs_mntdefaults_t *tailp;
164 FILE *fp;
166 headp = NULL;
167 tailp = NULL;
169 if ((fp = fopen(VFSTAB, "r")) != NULL) {
170 struct vfstab vfstab_entry;
171 (void) mutex_lock(&vfstab_lock);
172 while (getvfsent(fp, &vfstab_entry) == 0) {
174 * Add entry to list
176 newp = create_mntdefaults_entry(vfstab_entry, errp);
178 if (newp == NULL) {
180 * Out of memory, the error pointer (errp)
181 * gets set in create_mntdefaults_entry.
183 (void) fclose(fp);
184 (void) mutex_unlock(&vfstab_lock);
185 fs_free_mntdefaults_list(headp);
186 return (NULL);
189 if (headp == NULL) {
190 headp = newp;
191 tailp = newp;
192 } else {
193 tailp->next = newp;
194 tailp = newp;
197 (void) fclose(fp);
198 (void) mutex_unlock(&vfstab_lock);
199 } else {
200 *errp = errno;
201 } /* if ((fp = fopen(VFSTAB, "r")) != NULL) */
204 * Caller must free the returned list
206 return (headp);
208 } /* fs_get_mount_defaults */
210 fs_mntdefaults_t *
211 fs_add_mount_default(fs_mntdefaults_t *newp, int *errp) {
213 FILE *fp;
214 struct vfstab *new_entry;
215 fs_mntdefaults_t *ret_val;
217 new_entry = create_vfstab_filter(newp, errp);
218 if (new_entry != NULL) {
219 if ((fp = fopen(VFSTAB, "a")) != NULL) {
220 (void) mutex_lock(&vfstab_lock);
221 putvfsent(fp, new_entry);
222 free_vfstab_entry(new_entry);
223 (void) fclose(fp);
224 (void) mutex_unlock(&vfstab_lock);
225 ret_val = fs_get_mount_defaults(errp);
226 } else {
227 *errp = errno;
228 free_vfstab_entry(new_entry);
229 ret_val = NULL;
231 } else {
232 ret_val = NULL;
234 return (ret_val);
235 } /* fs_add_mount_default */
238 fs_mntdefaults_t *
239 fs_edit_mount_defaults(
240 fs_mntdefaults_t *old_vfstab_ent,
241 fs_mntdefaults_t *new_vfstab_ent,
242 int *errp)
244 FILE *fp;
245 fs_mntdefaults_t *ret_val;
246 char vfstab_line[VFS_LINE_MAX];
247 char **temp_vfstab = NULL;
248 char *new_line;
249 struct vfstab vfstabp, *new_vfstab;
250 int line_found = 0;
252 if ((fp = fopen(VFSTAB, "r")) != NULL) {
253 char *tmp;
254 int count = 0;
255 (void) mutex_lock(&vfstab_lock);
256 while (fgets(vfstab_line, VFS_LINE_MAX, fp) != NULL) {
257 char *charp;
258 struct vfstab *vp;
259 char *orig_line = strdup(vfstab_line);
260 if (orig_line == NULL) {
261 *errp = ENOMEM;
262 (void) fclose(fp);
263 (void) mutex_unlock(&vfstab_lock);
264 return (NULL);
266 vp = &vfstabp;
267 for (charp = vfstab_line;
268 *charp == ' ' || *charp == '\t'; charp++);
269 if (*charp == '#' || *charp == '\n') {
271 * Write comments out to temp vfstab
272 * image
274 if (!fileutil_add_string_to_array(
275 &temp_vfstab, vfstab_line, &count, errp)) {
276 ret_val = NULL;
277 line_found = 0;
278 break;
280 continue;
282 vp->vfs_special = (char *)strtok_r(
283 vfstab_line, sepstr, &tmp);
284 vp->vfs_fsckdev = (char *)strtok_r(
285 NULL, sepstr, &tmp);
286 vp->vfs_mountp = (char *)strtok_r(
287 NULL, sepstr, &tmp);
288 vp->vfs_fstype = (char *)strtok_r(
289 NULL, sepstr, &tmp);
290 vp->vfs_fsckpass = (char *)strtok_r(
291 NULL, sepstr, &tmp);
292 vp->vfs_automnt = (char *)strtok_r(
293 NULL, sepstr, &tmp);
294 vp->vfs_mntopts = (char *)strtok_r(
295 NULL, sepstr, &tmp);
296 if (strtok_r(NULL, sepstr, &tmp) != NULL) {
298 * Invalid vfstab line.
300 *errp = EINVAL;
301 (void) mutex_unlock(&vfstab_lock);
302 (void) fclose(fp);
303 return (NULL);
306 if (vfstab_line_cmp(old_vfstab_ent, vp)) {
307 line_found = 1;
308 new_vfstab =
309 create_vfstab_filter(
310 new_vfstab_ent, errp);
311 new_line =
312 create_vfstab_entry_line(new_vfstab, errp);
313 if (!fileutil_add_string_to_array(
314 &temp_vfstab, new_line, &count, errp)) {
315 ret_val = NULL;
316 line_found = 0;
317 free(new_line);
318 break;
320 free(new_line);
321 } else {
322 if (!fileutil_add_string_to_array(
323 &temp_vfstab, orig_line, &count, errp)) {
324 ret_val = NULL;
325 line_found = 0;
326 break;
329 free(orig_line);
331 (void) fclose(fp);
333 if (line_found && temp_vfstab != NULL) {
334 if ((fp = fopen(VFSTAB, "w")) != NULL) {
335 int i;
336 for (i = 0; i < count; i++) {
337 fprintf(fp, "%s", temp_vfstab[i]);
339 (void) fclose(fp);
340 (void) mutex_unlock(&vfstab_lock);
341 ret_val = fs_get_mount_defaults(errp);
342 fileutil_free_string_array(temp_vfstab, count);
343 } else {
344 *errp = errno;
345 (void) mutex_unlock(&vfstab_lock);
346 ret_val = NULL;
348 } else {
349 *errp = errno;
350 (void) mutex_unlock(&vfstab_lock);
351 ret_val = NULL;
353 } else {
354 *errp = errno;
355 ret_val = NULL;
357 return (ret_val);
358 } /* fs_edit_mount_defaults */
360 fs_mntdefaults_t *
361 fs_del_mount_default_ent(fs_mntdefaults_t *old_vfstab_ent, int *errp)
363 FILE *fp;
364 fs_mntdefaults_t *ret_val;
365 char vfstab_line[VFS_LINE_MAX];
366 struct vfstab vfstabp;
367 int line_found = 0;
369 if ((fp = fopen(VFSTAB, "r")) != NULL) {
370 struct vfstab *vp;
371 char *tmp;
372 char *charp;
373 char *orig_line = NULL;
374 char **temp_vfstab = NULL;
375 int count = 0;
376 vp = &vfstabp;
377 (void) mutex_lock(&vfstab_lock);
378 while (fgets(vfstab_line, VFS_LINE_MAX, fp) != NULL) {
380 orig_line = strdup(vfstab_line);
381 if (orig_line == NULL) {
382 *errp = ENOMEM;
383 (void) fclose(fp);
384 (void) mutex_unlock(&vfstab_lock);
385 return (NULL);
388 for (charp = vfstab_line;
389 *charp == ' ' || *charp == '\t'; charp++);
391 if (*charp == '#' || *charp == '\n') {
393 * Write comments out to temp vfstab
394 * image
396 if (!fileutil_add_string_to_array(
397 &temp_vfstab, vfstab_line, &count, errp)) {
398 ret_val = NULL;
399 line_found = 0;
400 free(orig_line);
401 break;
403 continue;
406 vp->vfs_special = (char *)strtok_r(
407 vfstab_line, sepstr, &tmp);
408 vp->vfs_fsckdev = (char *)strtok_r(
409 NULL, sepstr, &tmp);
410 vp->vfs_mountp = (char *)strtok_r(
411 NULL, sepstr, &tmp);
412 vp->vfs_fstype = (char *)strtok_r(
413 NULL, sepstr, &tmp);
414 vp->vfs_fsckpass = (char *)strtok_r(
415 NULL, sepstr, &tmp);
416 vp->vfs_automnt = (char *)strtok_r(
417 NULL, sepstr, &tmp);
418 vp->vfs_mntopts = (char *)strtok_r(
419 NULL, sepstr, &tmp);
421 if (strtok_r(NULL, sepstr, &tmp) != NULL) {
423 * Invalid vfstab line.
425 *errp = EINVAL;
426 free(orig_line);
427 (void) fclose(fp);
428 (void) mutex_unlock(&vfstab_lock);
429 return (NULL);
432 if (vfstab_line_cmp(old_vfstab_ent, vp)) {
433 line_found = 1;
434 } else {
435 if (!fileutil_add_string_to_array(
436 &temp_vfstab, orig_line, &count, errp)) {
437 ret_val = NULL;
438 line_found = 0;
439 free(orig_line);
440 break;
443 free(orig_line);
446 (void) fclose(fp);
448 if (line_found && temp_vfstab != NULL) {
449 if ((fp = fopen(VFSTAB, "w")) != NULL) {
450 int i;
451 for (i = 0; i < count; i++) {
452 fprintf(fp, "%s", temp_vfstab[i]);
454 (void) fclose(fp);
455 (void) mutex_unlock(&vfstab_lock);
456 ret_val = fs_get_mount_defaults(errp);
457 fileutil_free_string_array(temp_vfstab, count);
458 } else {
459 *errp = errno;
460 (void) mutex_unlock(&vfstab_lock);
461 fileutil_free_string_array(temp_vfstab, count);
462 ret_val = NULL;
464 } else {
465 (void) mutex_unlock(&vfstab_lock);
466 if (temp_vfstab != NULL) {
467 fileutil_free_string_array(temp_vfstab, count);
469 ret_val = NULL;
471 } else {
472 *errp = errno;
473 ret_val = NULL;
475 return (ret_val);
479 * Private methods
482 static fs_mntdefaults_t *
483 create_mntdefaults_entry(struct vfstab vfstab_entry, int *errp) {
484 fs_mntdefaults_t *newp;
486 newp = (fs_mntdefaults_t *)calloc((size_t)1,
487 (size_t)sizeof (fs_mntdefaults_t));
489 if (newp == NULL) {
491 * Out of memory
493 *errp = errno;
494 return (NULL);
498 if (vfstab_entry.vfs_special != NULL) {
499 newp->resource = strdup(vfstab_entry.vfs_special);
500 if (newp->resource == NULL) {
502 * Out of memory
504 *errp = errno;
505 fs_free_mntdefaults_list(newp);
506 return (NULL);
511 if (vfstab_entry.vfs_fsckdev != NULL) {
512 newp->fsckdevice = strdup(vfstab_entry.vfs_fsckdev);
513 if (newp->fsckdevice == NULL) {
515 * Out of memory
517 *errp = errno;
518 fs_free_mntdefaults_list(newp);
519 return (NULL);
523 if (vfstab_entry.vfs_mountp != NULL) {
524 newp->mountp = strdup(vfstab_entry.vfs_mountp);
525 if (newp->mountp == NULL) {
527 * Out of memory
529 *errp = errno;
530 fs_free_mntdefaults_list(newp);
531 return (NULL);
535 if (vfstab_entry.vfs_fstype != NULL) {
536 newp->fstype = strdup(vfstab_entry.vfs_fstype);
537 if (newp->fstype == NULL) {
539 * Out of memory
541 *errp = errno;
542 fs_free_mntdefaults_list(newp);
543 return (NULL);
547 if (vfstab_entry.vfs_fsckpass != NULL) {
548 newp->fsckpass = strdup(vfstab_entry.vfs_fsckpass);
549 if (newp->fsckpass == NULL) {
551 * Out of memory
553 *errp = errno;
554 fs_free_mntdefaults_list(newp);
555 return (NULL);
559 if (vfstab_entry.vfs_automnt != NULL) {
560 newp->mountatboot = strdup(vfstab_entry.vfs_automnt);
561 if (newp->mountatboot == NULL) {
563 * Out of memory
565 *errp = errno;
566 fs_free_mntdefaults_list(newp);
567 return (NULL);
571 if (vfstab_entry.vfs_mntopts != NULL) {
572 newp->mntopts = strdup(vfstab_entry.vfs_mntopts);
573 if (newp->mntopts == NULL) {
575 * Out of memory
577 *errp = errno;
578 fs_free_mntdefaults_list(newp);
579 return (NULL);
582 newp->next = NULL;
584 return (newp);
586 } /* create_mntdefaults_entry */
588 static struct vfstab *
589 create_vfstab_filter(fs_mntdefaults_t *filter, int *errp) {
590 struct vfstab *search_entry;
592 search_entry = (struct vfstab *)calloc((size_t)1,
593 (size_t)sizeof (struct vfstab));
594 if (search_entry == NULL) {
596 * Out of memory
598 *errp = errno;
599 return (NULL);
603 * Populate the filter criteria
605 if (filter->resource != NULL) {
606 search_entry->vfs_special = strdup(filter->resource);
607 if (search_entry->vfs_special == NULL) {
609 * Out of memory
611 *errp = errno;
612 free_vfstab_entry(search_entry);
613 return (NULL);
618 if (filter->fsckdevice != NULL) {
619 search_entry->vfs_fsckdev = strdup(filter->fsckdevice);
620 if (search_entry->vfs_fsckdev == NULL) {
622 * Out of memory
624 *errp = errno;
625 free_vfstab_entry(search_entry);
626 return (NULL);
630 if (filter->mountp != NULL) {
631 search_entry->vfs_mountp = strdup(filter->mountp);
632 if (search_entry->vfs_mountp == NULL) {
634 * Out of memory
636 *errp = errno;
637 free_vfstab_entry(search_entry);
638 return (NULL);
642 if (filter->fstype != NULL) {
643 search_entry->vfs_fstype = strdup(filter->fstype);
644 if (search_entry->vfs_fstype == NULL) {
646 * Out of memory
648 *errp = errno;
649 free_vfstab_entry(search_entry);
650 return (NULL);
654 if (filter->fsckpass != NULL) {
655 search_entry->vfs_fsckpass = strdup(filter->fsckpass);
656 if (search_entry->vfs_fsckpass == NULL) {
658 * Out of memory
660 *errp = errno;
661 free_vfstab_entry(search_entry);
662 return (NULL);
666 if (filter->mountatboot != NULL) {
667 search_entry->vfs_automnt = strdup(filter->mountatboot);
668 if (search_entry->vfs_automnt == NULL) {
670 * Out of memory
672 *errp = errno;
673 free_vfstab_entry(search_entry);
674 return (NULL);
678 if (filter->mntopts != NULL) {
679 search_entry->vfs_mntopts = strdup(filter->mntopts);
680 if (search_entry->vfs_mntopts == NULL) {
682 * Out of memory
684 *errp = errno;
685 free_vfstab_entry(search_entry);
686 return (NULL);
690 return (search_entry);
691 } /* create_vfstab_filter */
693 static void free_vfstab_entry(struct vfstab *vfstab_entry) {
695 free(vfstab_entry->vfs_special);
696 free(vfstab_entry->vfs_fsckdev);
697 free(vfstab_entry->vfs_mountp);
698 free(vfstab_entry->vfs_fstype);
699 free(vfstab_entry->vfs_fsckpass);
700 free(vfstab_entry->vfs_automnt);
701 free(vfstab_entry->vfs_mntopts);
703 free(vfstab_entry);
704 } /* free_vfstab_entry */
706 static int
707 vfstab_line_cmp(fs_mntdefaults_t *mntdftp, struct vfstab *vp) {
709 int ret_val = 1;
711 ret_val = cmp_fields(mntdftp->resource, vp->vfs_special, ret_val);
712 ret_val = cmp_fields(mntdftp->mountp, vp->vfs_mountp, ret_val);
714 return (ret_val);
715 } /* vfstab_line_cmp */
718 * Helper function for comparing fields in a fs_mntdefaults_t to a
719 * vfstab structure. Used in vfstab_line_cmp().
721 static int
722 cmp_fields(char *mntdflt_str, char *vfstab_str, int ret_val) {
723 if (ret_val != 0) {
724 if (mntdflt_str != NULL && vfstab_str != NULL) {
725 if (strcmp(mntdflt_str, vfstab_str) != 0) {
726 ret_val = 0;
728 } else if (mntdflt_str == NULL || vfstab_str == NULL) {
729 ret_val = 0;
732 return (ret_val);
733 } /* cmp_fields */
736 * Helper fuction used by del_vfstab_ent() and edit_vfstab_ent() to
737 * create a vfstab line for writing out to the vfstab file.
739 char *
740 create_vfstab_entry_line(struct vfstab *vfstab_ent, int *errp) {
741 char *line;
742 int line_length;
743 line_length = (
744 (vfstab_ent->vfs_special ?
745 (strlen(vfstab_ent->vfs_special) +1) : 2) +
746 (vfstab_ent->vfs_fsckdev ?
747 (strlen(vfstab_ent->vfs_fsckdev) +1) : 2) +
748 (vfstab_ent->vfs_mountp ?
749 (strlen(vfstab_ent->vfs_mountp) +1) : 2) +
750 (vfstab_ent->vfs_fstype ?
751 (strlen(vfstab_ent->vfs_fstype) +1) : 2) +
752 (vfstab_ent->vfs_fsckpass ?
753 (strlen(vfstab_ent->vfs_fsckpass) +1) : 2) +
754 (vfstab_ent->vfs_automnt ?
755 (strlen(vfstab_ent->vfs_automnt) +1) : 2) +
756 (vfstab_ent->vfs_mntopts ?
757 (strlen(vfstab_ent->vfs_mntopts) +1) : 2));
758 line = (char *)malloc(line_length + 1);
759 if (line != NULL) {
760 sprintf(line, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
761 vfstab_ent->vfs_special ? vfstab_ent->vfs_special : "-",
762 vfstab_ent->vfs_fsckdev ? vfstab_ent->vfs_fsckdev : "-",
763 vfstab_ent->vfs_mountp ? vfstab_ent->vfs_mountp : "-",
764 vfstab_ent->vfs_fstype ? vfstab_ent->vfs_fstype : "-",
765 vfstab_ent->vfs_fsckpass ? vfstab_ent->vfs_fsckpass : "-",
766 vfstab_ent->vfs_automnt ? vfstab_ent->vfs_automnt : "-",
767 vfstab_ent->vfs_mntopts ? vfstab_ent->vfs_mntopts : "-");
768 } else {
769 *errp = errno;
771 return (line);
772 } /* create_vfstab_entry_line */