17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*181c2f42Smmusante * Common Development and Distribution License (the "License").
6*181c2f42Smmusante * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*181c2f42Smmusante * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * Creates and maintains a cache of mount points.
307c478bd9Sstevel@tonic-gate */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <synch.h>
367c478bd9Sstevel@tonic-gate #include <thread.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <fcntl.h>
417c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
427c478bd9Sstevel@tonic-gate #include <sys/swap.h>
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate #include "libdiskmgt.h"
457c478bd9Sstevel@tonic-gate #include "disks_private.h"
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate * The list of mount point entries in /etc/mnttab
497c478bd9Sstevel@tonic-gate */
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate struct mntpnt_list {
527c478bd9Sstevel@tonic-gate struct mntpnt_list *next;
537c478bd9Sstevel@tonic-gate char *special;
547c478bd9Sstevel@tonic-gate char *mountp;
557c478bd9Sstevel@tonic-gate };
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate static struct mntpnt_list *mntpoint_listp = NULL;
587c478bd9Sstevel@tonic-gate static rwlock_t mntpoint_lock = DEFAULTRWLOCK;
597c478bd9Sstevel@tonic-gate static int initialized = 0;
607c478bd9Sstevel@tonic-gate static mutex_t init_lock = DEFAULTMUTEX;
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate static boolean_t diff_mnttab(int send_event, struct mntpnt_list *firstp,
637c478bd9Sstevel@tonic-gate struct mntpnt_list *secondp);
647c478bd9Sstevel@tonic-gate static void free_mnttab(struct mntpnt_list *listp);
657c478bd9Sstevel@tonic-gate static boolean_t in_list(struct mntpnt_list *elementp,
667c478bd9Sstevel@tonic-gate struct mntpnt_list *listp);
677c478bd9Sstevel@tonic-gate static int load_mnttab(int send_event);
687c478bd9Sstevel@tonic-gate static void watch_mnttab();
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate * Search the list of devices from /etc/mnttab to find the mount point
727c478bd9Sstevel@tonic-gate * for the specified device.
737c478bd9Sstevel@tonic-gate */
747c478bd9Sstevel@tonic-gate int
inuse_mnt(char * slice,nvlist_t * attrs,int * errp)757c478bd9Sstevel@tonic-gate inuse_mnt(char *slice, nvlist_t *attrs, int *errp)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate struct mntpnt_list *listp;
787c478bd9Sstevel@tonic-gate int found = 0;
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate *errp = 0;
817c478bd9Sstevel@tonic-gate if (slice == NULL) {
827c478bd9Sstevel@tonic-gate return (found);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate (void) mutex_lock(&init_lock);
867c478bd9Sstevel@tonic-gate if (!initialized) {
877c478bd9Sstevel@tonic-gate thread_t mnttab_thread;
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate /* load the mntpnt cache */
907c478bd9Sstevel@tonic-gate *errp = load_mnttab(B_FALSE);
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate if (*errp == 0) {
937c478bd9Sstevel@tonic-gate /* start a thread to monitor the mnttab */
947c478bd9Sstevel@tonic-gate *errp = thr_create(NULL, NULL, (void *(*)(void *))watch_mnttab,
957c478bd9Sstevel@tonic-gate NULL, THR_NEW_LWP | THR_DAEMON, &mnttab_thread);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate if (*errp == 0) {
997c478bd9Sstevel@tonic-gate initialized = 1;
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate (void) mutex_unlock(&init_lock);
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate (void) rw_rdlock(&mntpoint_lock);
1057c478bd9Sstevel@tonic-gate listp = mntpoint_listp;
1067c478bd9Sstevel@tonic-gate while (listp != NULL) {
1077c478bd9Sstevel@tonic-gate if (libdiskmgt_str_eq(slice, listp->special)) {
1087c478bd9Sstevel@tonic-gate libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_MOUNT, errp);
1097c478bd9Sstevel@tonic-gate libdiskmgt_add_str(attrs, DM_USED_NAME, listp->mountp, errp);
1107c478bd9Sstevel@tonic-gate found = 1;
1117c478bd9Sstevel@tonic-gate break;
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate listp = listp->next;
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate (void) rw_unlock(&mntpoint_lock);
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate return (found);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate * Return true if the lists are different. Send an event for each different
1227c478bd9Sstevel@tonic-gate * device.
1237c478bd9Sstevel@tonic-gate */
1247c478bd9Sstevel@tonic-gate static boolean_t
diff_mnttab(int send_event,struct mntpnt_list * firstp,struct mntpnt_list * secondp)1257c478bd9Sstevel@tonic-gate diff_mnttab(int send_event, struct mntpnt_list *firstp,
1267c478bd9Sstevel@tonic-gate struct mntpnt_list *secondp)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate boolean_t different = B_FALSE;
1297c478bd9Sstevel@tonic-gate struct mntpnt_list *listp;
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate listp = firstp;
1327c478bd9Sstevel@tonic-gate while (listp != NULL) {
1337c478bd9Sstevel@tonic-gate if (! in_list(listp, secondp)) {
1347c478bd9Sstevel@tonic-gate /* not in new list, so was mounted and now unmounted */
1357c478bd9Sstevel@tonic-gate if (send_event) {
1367c478bd9Sstevel@tonic-gate events_new_slice_event(listp->special, DM_EV_TCHANGE);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate different = B_TRUE;
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate listp = listp->next;
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate listp = secondp;
1447c478bd9Sstevel@tonic-gate while (listp != NULL) {
1457c478bd9Sstevel@tonic-gate if (! in_list(listp, firstp)) {
1467c478bd9Sstevel@tonic-gate /* not in orig list, so this is a new mount */
1477c478bd9Sstevel@tonic-gate if (send_event) {
1487c478bd9Sstevel@tonic-gate events_new_slice_event(listp->special, DM_EV_TCHANGE);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate different = B_TRUE;
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate listp = listp->next;
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate return (different);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate * free_mnttab()
1607c478bd9Sstevel@tonic-gate *
1617c478bd9Sstevel@tonic-gate * Free the list of metadevices from /etc/mnttab.
1627c478bd9Sstevel@tonic-gate */
1637c478bd9Sstevel@tonic-gate static void
free_mnttab(struct mntpnt_list * listp)1647c478bd9Sstevel@tonic-gate free_mnttab(struct mntpnt_list *listp) {
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate struct mntpnt_list *nextp;
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate while (listp != NULL) {
1697c478bd9Sstevel@tonic-gate nextp = listp->next;
1707c478bd9Sstevel@tonic-gate free((void *)listp->special);
1717c478bd9Sstevel@tonic-gate free((void *)listp->mountp);
1727c478bd9Sstevel@tonic-gate free((void *)listp);
1737c478bd9Sstevel@tonic-gate listp = nextp;
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate * Return true if the element is in the list.
1797c478bd9Sstevel@tonic-gate */
1807c478bd9Sstevel@tonic-gate static boolean_t
in_list(struct mntpnt_list * elementp,struct mntpnt_list * listp)1817c478bd9Sstevel@tonic-gate in_list(struct mntpnt_list *elementp, struct mntpnt_list *listp)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate while (listp != NULL) {
1847c478bd9Sstevel@tonic-gate if (libdiskmgt_str_eq(elementp->special, listp->special) &&
1857c478bd9Sstevel@tonic-gate libdiskmgt_str_eq(elementp->mountp, listp->mountp)) {
1867c478bd9Sstevel@tonic-gate return (B_TRUE);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate listp = listp->next;
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate return (B_FALSE);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate * load_mnttab()
1967c478bd9Sstevel@tonic-gate *
1977c478bd9Sstevel@tonic-gate * Create a list of devices from /etc/mnttab and swap.
1987c478bd9Sstevel@tonic-gate * return 1 if the list has changed, 0 if the list is still the same
1997c478bd9Sstevel@tonic-gate */
2007c478bd9Sstevel@tonic-gate static int
load_mnttab(int send_event)2017c478bd9Sstevel@tonic-gate load_mnttab(int send_event)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate struct mntpnt_list *currp;
2057c478bd9Sstevel@tonic-gate FILE *fp;
2067c478bd9Sstevel@tonic-gate struct mntpnt_list *headp;
2077c478bd9Sstevel@tonic-gate int num;
2087c478bd9Sstevel@tonic-gate struct mntpnt_list *prevp;
209*181c2f42Smmusante struct swaptable *st;
210*181c2f42Smmusante struct swapent *swapent;
211*181c2f42Smmusante int err;
212*181c2f42Smmusante int i;
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate headp = NULL;
2157c478bd9Sstevel@tonic-gate prevp = NULL;
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate /* get the mnttab entries */
2187c478bd9Sstevel@tonic-gate if ((fp = fopen("/etc/mnttab", "r")) != NULL) {
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate struct mnttab entry;
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate while (getmntent(fp, &entry) == 0) {
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate * Ignore entries that are incomplete or that are not
2267c478bd9Sstevel@tonic-gate * devices (skips network mounts, automounter entries,
2277c478bd9Sstevel@tonic-gate * /proc, etc.).
2287c478bd9Sstevel@tonic-gate */
2297c478bd9Sstevel@tonic-gate if (entry.mnt_special == NULL ||
2307c478bd9Sstevel@tonic-gate entry.mnt_mountp == NULL ||
2317c478bd9Sstevel@tonic-gate strncmp(entry.mnt_special, "/dev", 4) != 0) {
2327c478bd9Sstevel@tonic-gate continue;
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate currp = (struct mntpnt_list *)calloc((size_t)1,
2367c478bd9Sstevel@tonic-gate (size_t)sizeof (struct mntpnt_list));
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate if (currp == NULL) {
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate * out of memory, free what we have and return
2417c478bd9Sstevel@tonic-gate */
2427c478bd9Sstevel@tonic-gate free_mnttab(headp);
2437c478bd9Sstevel@tonic-gate (void) fclose(fp);
2447c478bd9Sstevel@tonic-gate return (ENOMEM);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate if (headp == NULL) {
2487c478bd9Sstevel@tonic-gate headp = currp;
2497c478bd9Sstevel@tonic-gate } else {
2507c478bd9Sstevel@tonic-gate prevp->next = currp;
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate currp->next = NULL;
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate currp->special = strdup(entry.mnt_special);
2567c478bd9Sstevel@tonic-gate if (currp->special == NULL) {
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate * out of memory, free what we have and return
2597c478bd9Sstevel@tonic-gate */
2607c478bd9Sstevel@tonic-gate free_mnttab(headp);
2617c478bd9Sstevel@tonic-gate (void) fclose(fp);
2627c478bd9Sstevel@tonic-gate return (ENOMEM);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate currp->mountp = strdup(entry.mnt_mountp);
2667c478bd9Sstevel@tonic-gate if (currp->mountp == NULL) {
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate * out of memory, free what we have and return
2697c478bd9Sstevel@tonic-gate */
2707c478bd9Sstevel@tonic-gate free_mnttab(headp);
2717c478bd9Sstevel@tonic-gate (void) fclose(fp);
2727c478bd9Sstevel@tonic-gate return (ENOMEM);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate prevp = currp;
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate (void) fclose(fp);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate /* get the swap entries */
282*181c2f42Smmusante num = dm_get_swapentries(&st, &err);
283*181c2f42Smmusante if (num < 0) {
284*181c2f42Smmusante free_mnttab(headp);
285*181c2f42Smmusante return (ENOMEM);
286*181c2f42Smmusante }
2877c478bd9Sstevel@tonic-gate
288*181c2f42Smmusante for (i = 0, swapent = st->swt_ent; i < num; i++, swapent++) {
2897c478bd9Sstevel@tonic-gate char fullpath[MAXPATHLEN+1];
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate currp = (struct mntpnt_list *)
2927c478bd9Sstevel@tonic-gate calloc((size_t)1, (size_t)sizeof (struct mntpnt_list));
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate if (currp == NULL) {
2957c478bd9Sstevel@tonic-gate /* out of memory, free what we have and return */
296*181c2f42Smmusante dm_free_swapentries(st);
2977c478bd9Sstevel@tonic-gate free_mnttab(headp);
2987c478bd9Sstevel@tonic-gate return (ENOMEM);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate if (headp == NULL) {
3027c478bd9Sstevel@tonic-gate headp = currp;
3037c478bd9Sstevel@tonic-gate } else {
3047c478bd9Sstevel@tonic-gate prevp->next = currp;
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate
3077c478bd9Sstevel@tonic-gate currp->next = NULL;
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate if (*swapent->ste_path != '/') {
3107c478bd9Sstevel@tonic-gate (void) snprintf(fullpath, sizeof (fullpath), "/dev/%s",
3117c478bd9Sstevel@tonic-gate swapent->ste_path);
3127c478bd9Sstevel@tonic-gate } else {
3137c478bd9Sstevel@tonic-gate (void) strlcpy(fullpath, swapent->ste_path,
3147c478bd9Sstevel@tonic-gate sizeof (fullpath));
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate currp->special = strdup(fullpath);
3187c478bd9Sstevel@tonic-gate if (currp->special == NULL) {
3197c478bd9Sstevel@tonic-gate /* out of memory, free what we have and return */
320*181c2f42Smmusante dm_free_swapentries(st);
3217c478bd9Sstevel@tonic-gate free_mnttab(headp);
3227c478bd9Sstevel@tonic-gate return (ENOMEM);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate currp->mountp = strdup("swap");
3267c478bd9Sstevel@tonic-gate if (currp->mountp == NULL) {
3277c478bd9Sstevel@tonic-gate /* out of memory, free what we have and return */
328*181c2f42Smmusante dm_free_swapentries(st);
3297c478bd9Sstevel@tonic-gate free_mnttab(headp);
3307c478bd9Sstevel@tonic-gate return (ENOMEM);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate prevp = currp;
3347c478bd9Sstevel@tonic-gate }
335*181c2f42Smmusante if (num)
336*181c2f42Smmusante dm_free_swapentries(st);
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate /* note that we unlock the mutex in both paths of this if statement */
3397c478bd9Sstevel@tonic-gate (void) rw_wrlock(&mntpoint_lock);
3407c478bd9Sstevel@tonic-gate if (diff_mnttab(send_event, mntpoint_listp, headp) == B_TRUE) {
3417c478bd9Sstevel@tonic-gate struct mntpnt_list *tmpp;
3427c478bd9Sstevel@tonic-gate
3437c478bd9Sstevel@tonic-gate tmpp = mntpoint_listp;
3447c478bd9Sstevel@tonic-gate mntpoint_listp = headp;
3457c478bd9Sstevel@tonic-gate (void) rw_unlock(&mntpoint_lock);
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate /* free the old list */
3487c478bd9Sstevel@tonic-gate free_mnttab(tmpp);
3497c478bd9Sstevel@tonic-gate } else {
3507c478bd9Sstevel@tonic-gate (void) rw_unlock(&mntpoint_lock);
3517c478bd9Sstevel@tonic-gate /* no change that we care about, so keep the current list */
3527c478bd9Sstevel@tonic-gate free_mnttab(headp);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate return (0);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate * This is a thread that runs forever, watching for changes in the mnttab
3597c478bd9Sstevel@tonic-gate * that would cause us to flush and reload the cache of mnt entries. Only
3607c478bd9Sstevel@tonic-gate * changes to /dev devices will cause the cache to be flushed and reloaded.
3617c478bd9Sstevel@tonic-gate */
3627c478bd9Sstevel@tonic-gate static void
watch_mnttab()3637c478bd9Sstevel@tonic-gate watch_mnttab()
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate struct pollfd fds[1];
3667c478bd9Sstevel@tonic-gate int res;
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate if ((fds[0].fd = open("/etc/mnttab", O_RDONLY)) != -1) {
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate char buf[81];
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate /* do the initial read so we don't get the event right away */
3737c478bd9Sstevel@tonic-gate (void) read(fds[0].fd, buf, (size_t)(sizeof (buf) - 1));
3747c478bd9Sstevel@tonic-gate (void) lseek(fds[0].fd, 0, SEEK_SET);
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate fds[0].events = POLLRDBAND;
3777c478bd9Sstevel@tonic-gate while (res = poll(fds, (nfds_t)1, -1)) {
3787c478bd9Sstevel@tonic-gate if (res <= 0)
3797c478bd9Sstevel@tonic-gate continue;
3807c478bd9Sstevel@tonic-gate
3817c478bd9Sstevel@tonic-gate (void) load_mnttab(B_TRUE);
3827c478bd9Sstevel@tonic-gate
3837c478bd9Sstevel@tonic-gate (void) read(fds[0].fd, buf, (size_t)(sizeof (buf) - 1));
3847c478bd9Sstevel@tonic-gate (void) lseek(fds[0].fd, 0, SEEK_SET);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate }
388