summaryrefslogtreecommitdiff
path: root/usr/src/cmd/stat/common/mnt.c
blob: 94700fcf1d8158872c916080c06e6bbe398f30ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/mnttab.h>
#include <sys/mntent.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <string.h>
#include <fcntl.h>

#include "statcommon.h"
#include "dsr.h"

static time_t mtime;
mnt_t *nfs;
static mnt_t *ufs;

static void build_mnt_list(FILE *);

mnt_t *
lookup_mntent_byname(char *nm)
{
	mnt_t *rv = 0;
	mnt_t *minfo;
	uint_t did_nfs;


	if (nm) {
		minfo = ufs;
		did_nfs = 0;
		while (minfo) {
			if (strcmp(nm, minfo->device_name)) {
				if (minfo->next != 0)
					minfo = minfo->next;
				else if (did_nfs == 0) {
					minfo = nfs;
					did_nfs = 1;
				}
				else
					minfo = 0;
			} else {
				rv = minfo;
				break;
			}
		}
	}
	return (rv);
}

void
do_mnttab(void)
{
	struct stat 	buf;
	FILE 		*mpt;
	struct flock    lb;

	if (stat(MNTTAB, &buf) == 0) {
		if (buf.st_mtime != mtime) {
			/*
			 * File has changed. Get the new file.
			 */
			if ((mpt = fopen(MNTTAB, "r"))) {
				lb.l_type = F_RDLCK;
				lb.l_whence = 0;
				lb.l_start = 0;
				lb.l_len = 0;
				(void) fcntl(fileno(mpt), F_SETLKW, &lb);
				build_mnt_list(mpt);
				mtime = buf.st_mtime;
				/*
				 * Lock goes away when we close the file.
				 */
				(void) fclose(mpt);
			}
		}
	}
}

static void
build_mnt_list(FILE *mpt)
{
	mnt_t *item;
	mnt_t **which;
	mnt_t *tmp;
	int  found;
	struct extmnttab mnt;

	if (mpt) {
		while (nfs) {
			free(nfs->device_name);
			free(nfs->mount_point);
			free(nfs->devinfo);
			tmp = nfs;
			nfs = nfs->next;
			free(tmp);
		}
		while (ufs) {
			free(ufs->device_name);
			free(ufs->mount_point);
			free(ufs->devinfo);
			tmp = ufs;
			ufs = ufs->next;
			free(tmp);
		}
		(void) memset(&mnt, 0, sizeof (struct extmnttab));

		resetmnttab(mpt);
		while ((found = getextmntent(mpt, &mnt,
			sizeof (struct extmnttab))) != -1) {
			if (found == 0) {
				if (strcmp(mnt.mnt_fstype, MNTTYPE_UFS) == 0)
					which = &ufs;
				else if (strcmp(mnt.mnt_fstype,
				    MNTTYPE_NFS) == 0)
					which = &nfs;
				else
					which = 0;
				if (which) {
					item = safe_alloc(sizeof (mnt_t));
					item->device_name =
						safe_strdup(mnt.mnt_special);
					item->mount_point =
						safe_strdup(mnt.mnt_mountp);
					item->devinfo =
						safe_strdup(mnt.mnt_mntopts);
					item->minor = mnt.mnt_minor;
					item->next = *which;
					*which = item;
				}
			}
		}
	}
}