xref: /illumos-gate/usr/src/cmd/stat/common/mnt.c (revision f17620a4f72a29025a22655ba8735ccd20ae174f)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <stdio.h>
30 #include <sys/mnttab.h>
31 #include <sys/mntent.h>
32 #include <sys/stat.h>
33 #include <sys/time.h>
34 #include <string.h>
35 #include <fcntl.h>
36 
37 #include "statcommon.h"
38 #include "dsr.h"
39 
40 static time_t mtime;
41 mnt_t *nfs;
42 static mnt_t *ufs;
43 
44 static void build_mnt_list(FILE *);
45 
46 mnt_t *
47 lookup_mntent_byname(char *nm)
48 {
49 	mnt_t *rv = 0;
50 	mnt_t *minfo;
51 	uint_t did_nfs;
52 
53 
54 	if (nm) {
55 		minfo = ufs;
56 		did_nfs = 0;
57 		while (minfo) {
58 			if (strcmp(nm, minfo->device_name)) {
59 				if (minfo->next != 0)
60 					minfo = minfo->next;
61 				else if (did_nfs == 0) {
62 					minfo = nfs;
63 					did_nfs = 1;
64 				}
65 				else
66 					minfo = 0;
67 			} else {
68 				rv = minfo;
69 				break;
70 			}
71 		}
72 	}
73 	return (rv);
74 }
75 
76 void
77 do_mnttab(void)
78 {
79 	struct stat 	buf;
80 	FILE 		*mpt;
81 	struct flock    lb;
82 
83 	if (stat(MNTTAB, &buf) == 0) {
84 		if (buf.st_mtime != mtime) {
85 			/*
86 			 * File has changed. Get the new file.
87 			 */
88 			if ((mpt = fopen(MNTTAB, "r"))) {
89 				lb.l_type = F_RDLCK;
90 				lb.l_whence = 0;
91 				lb.l_start = 0;
92 				lb.l_len = 0;
93 				(void) fcntl(fileno(mpt), F_SETLKW, &lb);
94 				build_mnt_list(mpt);
95 				mtime = buf.st_mtime;
96 				/*
97 				 * Lock goes away when we close the file.
98 				 */
99 				(void) fclose(mpt);
100 			}
101 		}
102 	}
103 }
104 
105 static void
106 build_mnt_list(FILE *mpt)
107 {
108 	mnt_t *item;
109 	mnt_t **which;
110 	mnt_t *tmp;
111 	int  found;
112 	struct extmnttab mnt;
113 
114 	if (mpt) {
115 		while (nfs) {
116 			free(nfs->device_name);
117 			free(nfs->mount_point);
118 			free(nfs->devinfo);
119 			tmp = nfs;
120 			nfs = nfs->next;
121 			free(tmp);
122 		}
123 		while (ufs) {
124 			free(ufs->device_name);
125 			free(ufs->mount_point);
126 			free(ufs->devinfo);
127 			tmp = ufs;
128 			ufs = ufs->next;
129 			free(tmp);
130 		}
131 		(void) memset(&mnt, 0, sizeof (struct extmnttab));
132 
133 		resetmnttab(mpt);
134 		while ((found = getextmntent(mpt, &mnt,
135 			sizeof (struct extmnttab))) != -1) {
136 			if (found == 0) {
137 				if (strcmp(mnt.mnt_fstype, MNTTYPE_UFS) == 0)
138 					which = &ufs;
139 				else if (strcmp(mnt.mnt_fstype,
140 				    MNTTYPE_NFS) == 0)
141 					which = &nfs;
142 				else
143 					which = 0;
144 				if (which) {
145 					item = safe_alloc(sizeof (mnt_t));
146 					item->device_name =
147 						safe_strdup(mnt.mnt_special);
148 					item->mount_point =
149 						safe_strdup(mnt.mnt_mountp);
150 					item->devinfo =
151 						safe_strdup(mnt.mnt_mntopts);
152 					item->minor = mnt.mnt_minor;
153 					item->next = *which;
154 					*which = item;
155 				}
156 			}
157 		}
158 	}
159 }
160