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
5181c2f42Smmusante  * Common Development and Distribution License (the "License").
6181c2f42Smmusante  * 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 /*
22181c2f42Smmusante  * 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 /*
277c478bd9Sstevel@tonic-gate  * Creates and maintains a cache of mount points.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <synch.h>
347c478bd9Sstevel@tonic-gate #include <thread.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <fcntl.h>
397c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
407c478bd9Sstevel@tonic-gate #include <sys/swap.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "libdiskmgt.h"
437c478bd9Sstevel@tonic-gate #include "disks_private.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * The list of mount point entries in /etc/mnttab
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate struct mntpnt_list {
507c478bd9Sstevel@tonic-gate 	struct mntpnt_list	*next;
517c478bd9Sstevel@tonic-gate 	char			*special;
527c478bd9Sstevel@tonic-gate 	char			*mountp;
537c478bd9Sstevel@tonic-gate };
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static struct mntpnt_list	*mntpoint_listp = NULL;
567c478bd9Sstevel@tonic-gate static rwlock_t			mntpoint_lock = DEFAULTRWLOCK;
577c478bd9Sstevel@tonic-gate static int			initialized = 0;
587c478bd9Sstevel@tonic-gate static mutex_t			init_lock = DEFAULTMUTEX;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static boolean_t	diff_mnttab(int send_event, struct mntpnt_list *firstp,
617c478bd9Sstevel@tonic-gate 			    struct mntpnt_list *secondp);
627c478bd9Sstevel@tonic-gate static void		free_mnttab(struct mntpnt_list *listp);
637c478bd9Sstevel@tonic-gate static boolean_t	in_list(struct mntpnt_list *elementp,
647c478bd9Sstevel@tonic-gate 			    struct mntpnt_list *listp);
657c478bd9Sstevel@tonic-gate static int		load_mnttab(int send_event);
66*6aa760d1SToomas Soome static void		*watch_mnttab(void *);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * Search the list of devices from /etc/mnttab to find the mount point
707c478bd9Sstevel@tonic-gate  * for the specified device.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate int
inuse_mnt(char * slice,nvlist_t * attrs,int * errp)737c478bd9Sstevel@tonic-gate inuse_mnt(char *slice, nvlist_t *attrs, int *errp)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	struct mntpnt_list	*listp;
767c478bd9Sstevel@tonic-gate 	int			found = 0;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	*errp = 0;
797c478bd9Sstevel@tonic-gate 	if (slice == NULL) {
807c478bd9Sstevel@tonic-gate 	    return (found);
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&init_lock);
847c478bd9Sstevel@tonic-gate 	if (!initialized) {
857c478bd9Sstevel@tonic-gate 	    thread_t	mnttab_thread;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	    /* load the mntpnt cache */
887c478bd9Sstevel@tonic-gate 	    *errp = load_mnttab(B_FALSE);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	    if (*errp == 0) {
917c478bd9Sstevel@tonic-gate 		/* start a thread to monitor the mnttab */
92*6aa760d1SToomas Soome 		*errp = thr_create(NULL, 0, watch_mnttab,
937c478bd9Sstevel@tonic-gate 		    NULL, THR_NEW_LWP | THR_DAEMON, &mnttab_thread);
947c478bd9Sstevel@tonic-gate 	    }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	    if (*errp == 0) {
977c478bd9Sstevel@tonic-gate 		initialized = 1;
987c478bd9Sstevel@tonic-gate 	    }
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&init_lock);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	(void) rw_rdlock(&mntpoint_lock);
1037c478bd9Sstevel@tonic-gate 	listp = mntpoint_listp;
1047c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
1057c478bd9Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(slice, listp->special)) {
1067c478bd9Sstevel@tonic-gate 		libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_MOUNT, errp);
1077c478bd9Sstevel@tonic-gate 		libdiskmgt_add_str(attrs, DM_USED_NAME, listp->mountp, errp);
1087c478bd9Sstevel@tonic-gate 		found = 1;
1097c478bd9Sstevel@tonic-gate 		break;
1107c478bd9Sstevel@tonic-gate 	    }
1117c478bd9Sstevel@tonic-gate 	    listp = listp->next;
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&mntpoint_lock);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	return (found);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  * Return true if the lists are different.  Send an event for each different
1207c478bd9Sstevel@tonic-gate  * device.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate static boolean_t
diff_mnttab(int send_event,struct mntpnt_list * firstp,struct mntpnt_list * secondp)1237c478bd9Sstevel@tonic-gate diff_mnttab(int send_event, struct mntpnt_list *firstp,
1247c478bd9Sstevel@tonic-gate     struct mntpnt_list *secondp)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	boolean_t		different = B_FALSE;
1277c478bd9Sstevel@tonic-gate 	struct mntpnt_list	*listp;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	listp = firstp;
1307c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
1317c478bd9Sstevel@tonic-gate 	    if (! in_list(listp, secondp)) {
1327c478bd9Sstevel@tonic-gate 		/* not in new list, so was mounted and now unmounted */
1337c478bd9Sstevel@tonic-gate 		if (send_event) {
1347c478bd9Sstevel@tonic-gate 		    events_new_slice_event(listp->special, DM_EV_TCHANGE);
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 		different = B_TRUE;
1377c478bd9Sstevel@tonic-gate 	    }
1387c478bd9Sstevel@tonic-gate 	    listp = listp->next;
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	listp = secondp;
1427c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
1437c478bd9Sstevel@tonic-gate 	    if (! in_list(listp, firstp)) {
1447c478bd9Sstevel@tonic-gate 		/* not in orig list, so this is a new mount */
1457c478bd9Sstevel@tonic-gate 		if (send_event) {
1467c478bd9Sstevel@tonic-gate 		    events_new_slice_event(listp->special, DM_EV_TCHANGE);
1477c478bd9Sstevel@tonic-gate 		}
1487c478bd9Sstevel@tonic-gate 		different = B_TRUE;
1497c478bd9Sstevel@tonic-gate 	    }
1507c478bd9Sstevel@tonic-gate 	    listp = listp->next;
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	return (different);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  * free_mnttab()
1587c478bd9Sstevel@tonic-gate  *
1597c478bd9Sstevel@tonic-gate  * Free the list of metadevices from /etc/mnttab.
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate static void
free_mnttab(struct mntpnt_list * listp)1627c478bd9Sstevel@tonic-gate free_mnttab(struct mntpnt_list	*listp) {
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	struct mntpnt_list	*nextp;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
1677c478bd9Sstevel@tonic-gate 		nextp = listp->next;
1687c478bd9Sstevel@tonic-gate 		free((void *)listp->special);
1697c478bd9Sstevel@tonic-gate 		free((void *)listp->mountp);
1707c478bd9Sstevel@tonic-gate 		free((void *)listp);
1717c478bd9Sstevel@tonic-gate 		listp = nextp;
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * Return true if the element is in the list.
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate static boolean_t
in_list(struct mntpnt_list * elementp,struct mntpnt_list * listp)1797c478bd9Sstevel@tonic-gate in_list(struct mntpnt_list *elementp, struct mntpnt_list *listp)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
1827c478bd9Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(elementp->special, listp->special) &&
1837c478bd9Sstevel@tonic-gate 		libdiskmgt_str_eq(elementp->mountp, listp->mountp)) {
1847c478bd9Sstevel@tonic-gate 		return (B_TRUE);
1857c478bd9Sstevel@tonic-gate 	    }
1867c478bd9Sstevel@tonic-gate 	    listp = listp->next;
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	return (B_FALSE);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate  * load_mnttab()
1947c478bd9Sstevel@tonic-gate  *
1957c478bd9Sstevel@tonic-gate  * Create a list of devices from /etc/mnttab and swap.
1967c478bd9Sstevel@tonic-gate  * return 1 if the list has changed, 0 if the list is still the same
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate static int
load_mnttab(int send_event)1997c478bd9Sstevel@tonic-gate load_mnttab(int send_event)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	struct mntpnt_list	*currp;
2037c478bd9Sstevel@tonic-gate 	FILE			*fp;
2047c478bd9Sstevel@tonic-gate 	struct mntpnt_list	*headp;
2057c478bd9Sstevel@tonic-gate 	int			num;
2067c478bd9Sstevel@tonic-gate 	struct mntpnt_list	*prevp;
207181c2f42Smmusante 	struct swaptable	*st;
208181c2f42Smmusante 	struct swapent		*swapent;
209181c2f42Smmusante 	int			err;
210181c2f42Smmusante 	int			i;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	headp = NULL;
2137c478bd9Sstevel@tonic-gate 	prevp = NULL;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/* get the mnttab entries */
2167c478bd9Sstevel@tonic-gate 	if ((fp = fopen("/etc/mnttab", "r")) != NULL) {
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		struct mnttab	entry;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		while (getmntent(fp, &entry) == 0) {
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 			/*
2237c478bd9Sstevel@tonic-gate 			 * Ignore entries that are incomplete or that are not
2247c478bd9Sstevel@tonic-gate 			 * devices (skips network mounts, automounter entries,
2257c478bd9Sstevel@tonic-gate 			 * /proc, etc.).
2267c478bd9Sstevel@tonic-gate 			 */
2277c478bd9Sstevel@tonic-gate 			if (entry.mnt_special == NULL ||
2287c478bd9Sstevel@tonic-gate 				entry.mnt_mountp == NULL ||
2297c478bd9Sstevel@tonic-gate 				strncmp(entry.mnt_special, "/dev", 4) != 0) {
2307c478bd9Sstevel@tonic-gate 				continue;
2317c478bd9Sstevel@tonic-gate 			}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 			currp = (struct mntpnt_list *)calloc((size_t)1,
2347c478bd9Sstevel@tonic-gate 				(size_t)sizeof (struct mntpnt_list));
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 			if (currp == NULL) {
2377c478bd9Sstevel@tonic-gate 				/*
2387c478bd9Sstevel@tonic-gate 				 * out of memory, free what we have and return
2397c478bd9Sstevel@tonic-gate 				 */
2407c478bd9Sstevel@tonic-gate 				free_mnttab(headp);
2417c478bd9Sstevel@tonic-gate 				(void) fclose(fp);
2427c478bd9Sstevel@tonic-gate 				return (ENOMEM);
2437c478bd9Sstevel@tonic-gate 			}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 			if (headp == NULL) {
2467c478bd9Sstevel@tonic-gate 				headp = currp;
2477c478bd9Sstevel@tonic-gate 			} else {
2487c478bd9Sstevel@tonic-gate 				prevp->next = currp;
2497c478bd9Sstevel@tonic-gate 			}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 			currp->next = NULL;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 			currp->special = strdup(entry.mnt_special);
2547c478bd9Sstevel@tonic-gate 			if (currp->special == NULL) {
2557c478bd9Sstevel@tonic-gate 				/*
2567c478bd9Sstevel@tonic-gate 				 * out of memory, free what we have and return
2577c478bd9Sstevel@tonic-gate 				 */
2587c478bd9Sstevel@tonic-gate 				free_mnttab(headp);
2597c478bd9Sstevel@tonic-gate 				(void) fclose(fp);
2607c478bd9Sstevel@tonic-gate 				return (ENOMEM);
2617c478bd9Sstevel@tonic-gate 			}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 			currp->mountp = strdup(entry.mnt_mountp);
2647c478bd9Sstevel@tonic-gate 			if (currp->mountp == NULL) {
2657c478bd9Sstevel@tonic-gate 				/*
2667c478bd9Sstevel@tonic-gate 				 * out of memory, free what we have and return
2677c478bd9Sstevel@tonic-gate 				 */
2687c478bd9Sstevel@tonic-gate 				free_mnttab(headp);
2697c478bd9Sstevel@tonic-gate 				(void) fclose(fp);
2707c478bd9Sstevel@tonic-gate 				return (ENOMEM);
2717c478bd9Sstevel@tonic-gate 			}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 			prevp = currp;
2747c478bd9Sstevel@tonic-gate 		}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	/* get the swap entries */
280181c2f42Smmusante 	num = dm_get_swapentries(&st, &err);
281181c2f42Smmusante 	if (num < 0) {
2827c478bd9Sstevel@tonic-gate 		free_mnttab(headp);
2837c478bd9Sstevel@tonic-gate 		return (ENOMEM);
284181c2f42Smmusante 	}
2857c478bd9Sstevel@tonic-gate 
286181c2f42Smmusante 	for (i = 0, swapent = st->swt_ent; i < num; i++, swapent++) {
287181c2f42Smmusante 		char		fullpath[MAXPATHLEN+1];
2887c478bd9Sstevel@tonic-gate 
289181c2f42Smmusante 		currp = (struct mntpnt_list *)
290181c2f42Smmusante 		    calloc((size_t)1, (size_t)sizeof (struct mntpnt_list));
2917c478bd9Sstevel@tonic-gate 
292181c2f42Smmusante 		if (currp == NULL) {
2937c478bd9Sstevel@tonic-gate 			/* out of memory, free what we have and return */
294181c2f42Smmusante 			dm_free_swapentries(st);
2957c478bd9Sstevel@tonic-gate 			free_mnttab(headp);
2967c478bd9Sstevel@tonic-gate 			return (ENOMEM);
297181c2f42Smmusante 		}
2987c478bd9Sstevel@tonic-gate 
299181c2f42Smmusante 		if (headp == NULL) {
3007c478bd9Sstevel@tonic-gate 			headp = currp;
301181c2f42Smmusante 		} else {
3027c478bd9Sstevel@tonic-gate 			prevp->next = currp;
303181c2f42Smmusante 		}
3047c478bd9Sstevel@tonic-gate 
305181c2f42Smmusante 		currp->next = NULL;
3067c478bd9Sstevel@tonic-gate 
307181c2f42Smmusante 		if (*swapent->ste_path != '/') {
3087c478bd9Sstevel@tonic-gate 			(void) snprintf(fullpath, sizeof (fullpath), "/dev/%s",
3097c478bd9Sstevel@tonic-gate 			    swapent->ste_path);
310181c2f42Smmusante 		} else {
3117c478bd9Sstevel@tonic-gate 			(void) strlcpy(fullpath, swapent->ste_path,
3127c478bd9Sstevel@tonic-gate 			    sizeof (fullpath));
313181c2f42Smmusante 		}
3147c478bd9Sstevel@tonic-gate 
315181c2f42Smmusante 		currp->special = strdup(fullpath);
316181c2f42Smmusante 		if (currp->special == NULL) {
3177c478bd9Sstevel@tonic-gate 			/* out of memory, free what we have and return */
318181c2f42Smmusante 			dm_free_swapentries(st);
3197c478bd9Sstevel@tonic-gate 			free_mnttab(headp);
3207c478bd9Sstevel@tonic-gate 			return (ENOMEM);
321181c2f42Smmusante 		}
3227c478bd9Sstevel@tonic-gate 
323181c2f42Smmusante 		currp->mountp = strdup("swap");
324181c2f42Smmusante 		if (currp->mountp == NULL) {
3257c478bd9Sstevel@tonic-gate 			/* out of memory, free what we have and return */
326181c2f42Smmusante 			dm_free_swapentries(st);
3277c478bd9Sstevel@tonic-gate 			free_mnttab(headp);
3287c478bd9Sstevel@tonic-gate 			return (ENOMEM);
3297c478bd9Sstevel@tonic-gate 		}
3307c478bd9Sstevel@tonic-gate 
331181c2f42Smmusante 		prevp = currp;
3327c478bd9Sstevel@tonic-gate 	}
333181c2f42Smmusante 	if (num)
334181c2f42Smmusante 		dm_free_swapentries(st);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	/* note that we unlock the mutex in both paths of this if statement */
3377c478bd9Sstevel@tonic-gate 	(void) rw_wrlock(&mntpoint_lock);
3387c478bd9Sstevel@tonic-gate 	if (diff_mnttab(send_event, mntpoint_listp, headp) == B_TRUE) {
3397c478bd9Sstevel@tonic-gate 		struct mntpnt_list	*tmpp;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		tmpp = mntpoint_listp;
3427c478bd9Sstevel@tonic-gate 		mntpoint_listp = headp;
3437c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&mntpoint_lock);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 		/* free the old list */
3467c478bd9Sstevel@tonic-gate 		free_mnttab(tmpp);
3477c478bd9Sstevel@tonic-gate 	} else {
3487c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&mntpoint_lock);
3497c478bd9Sstevel@tonic-gate 		/* no change that we care about, so keep the current list */
3507c478bd9Sstevel@tonic-gate 		free_mnttab(headp);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 	return (0);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate /*
3567c478bd9Sstevel@tonic-gate  * This is a thread that runs forever, watching for changes in the mnttab
3577c478bd9Sstevel@tonic-gate  * that would cause us to flush and reload the cache of mnt entries.  Only
3587c478bd9Sstevel@tonic-gate  * changes to /dev devices will cause the cache to be flushed and reloaded.
3597c478bd9Sstevel@tonic-gate  */
360*6aa760d1SToomas Soome static void *
watch_mnttab(void * arg __unused)361*6aa760d1SToomas Soome watch_mnttab(void *arg __unused)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	struct pollfd fds[1];
3647c478bd9Sstevel@tonic-gate 	int res;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	if ((fds[0].fd = open("/etc/mnttab", O_RDONLY)) != -1) {
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	    char buf[81];
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	    /* do the initial read so we don't get the event right away */
3717c478bd9Sstevel@tonic-gate 	    (void) read(fds[0].fd, buf, (size_t)(sizeof (buf) - 1));
3727c478bd9Sstevel@tonic-gate 	    (void) lseek(fds[0].fd, 0, SEEK_SET);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	    fds[0].events = POLLRDBAND;
3757c478bd9Sstevel@tonic-gate 	    while (res = poll(fds, (nfds_t)1, -1)) {
3767c478bd9Sstevel@tonic-gate 		if (res <= 0)
3777c478bd9Sstevel@tonic-gate 		    continue;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 		(void) load_mnttab(B_TRUE);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		(void) read(fds[0].fd, buf, (size_t)(sizeof (buf) - 1));
3827c478bd9Sstevel@tonic-gate 		(void) lseek(fds[0].fd, 0, SEEK_SET);
3837c478bd9Sstevel@tonic-gate 	    }
3847c478bd9Sstevel@tonic-gate 	}
385*6aa760d1SToomas Soome 	return (NULL);
3867c478bd9Sstevel@tonic-gate }
387