1*3034Sdougm /*
2*3034Sdougm * CDDL HEADER START
3*3034Sdougm *
4*3034Sdougm * The contents of this file are subject to the terms of the
5*3034Sdougm * Common Development and Distribution License, Version 1.0 only
6*3034Sdougm * (the "License"). You may not use this file except in compliance
7*3034Sdougm * with the License.
8*3034Sdougm *
9*3034Sdougm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*3034Sdougm * or http://www.opensolaris.org/os/licensing.
11*3034Sdougm * See the License for the specific language governing permissions
12*3034Sdougm * and limitations under the License.
13*3034Sdougm *
14*3034Sdougm * When distributing Covered Code, include this CDDL HEADER in each
15*3034Sdougm * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*3034Sdougm * If applicable, add the following below this CDDL HEADER, with the
17*3034Sdougm * fields enclosed by brackets "[]" replaced with your own identifying
18*3034Sdougm * information: Portions Copyright [yyyy] [name of copyright owner]
19*3034Sdougm *
20*3034Sdougm * CDDL HEADER END
21*3034Sdougm */
22*3034Sdougm /*
23*3034Sdougm * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24*3034Sdougm * Use is subject to license terms.
25*3034Sdougm */
26*3034Sdougm
27*3034Sdougm /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*3034Sdougm /* All Rights Reserved */
29*3034Sdougm
30*3034Sdougm /*
31*3034Sdougm * University Copyright- Copyright (c) 1982, 1986, 1988
32*3034Sdougm * The Regents of the University of California
33*3034Sdougm * All Rights Reserved
34*3034Sdougm *
35*3034Sdougm * University Acknowledgment- Portions of this document are derived from
36*3034Sdougm * software developed by the University of California, Berkeley, and its
37*3034Sdougm * contributors.
38*3034Sdougm */
39*3034Sdougm
40*3034Sdougm #pragma ident "%Z%%M% %I% %E% SMI"
41*3034Sdougm
42*3034Sdougm /*
43*3034Sdougm * Subdirectory detection: needed by exportfs and rpc.mountd.
44*3034Sdougm * The above programs call issubdir() frequently, so we make
45*3034Sdougm * it fast by caching the results of stat().
46*3034Sdougm */
47*3034Sdougm #include <sys/types.h>
48*3034Sdougm #include <sys/param.h>
49*3034Sdougm #include <sys/stat.h>
50*3034Sdougm #include <string.h>
51*3034Sdougm
52*3034Sdougm #define MAXSTATS MAXPATHLEN/2 /* maximum number of stat()'s to save */
53*3034Sdougm
54*3034Sdougm #define inoeq(ino1, ino2) ((ino1) == (ino2))
55*3034Sdougm #define deveq(dev1, dev2) ((dev1) == (dev2))
56*3034Sdougm
57*3034Sdougm /*
58*3034Sdougm * dir1 is a subdirectory of dir2 within the same filesystem if
59*3034Sdougm * (a) dir1 is identical to dir2
60*3034Sdougm * (b) dir1's parent is dir2
61*3034Sdougm */
62*3034Sdougm int
issubdir(dir1,dir2)63*3034Sdougm issubdir(dir1, dir2)
64*3034Sdougm char *dir1;
65*3034Sdougm char *dir2;
66*3034Sdougm {
67*3034Sdougm struct stat st;
68*3034Sdougm struct stat parent_st;
69*3034Sdougm char *p;
70*3034Sdougm int index;
71*3034Sdougm
72*3034Sdougm static dev_t child_dev;
73*3034Sdougm static dev_t child_rdev;
74*3034Sdougm static ino_t child_ino[MAXSTATS];
75*3034Sdougm static int valid;
76*3034Sdougm static char childdir[MAXPATHLEN];
77*3034Sdougm static char child_fstype[_ST_FSTYPSZ];
78*3034Sdougm
79*3034Sdougm /*
80*3034Sdougm * Get parent directory info
81*3034Sdougm */
82*3034Sdougm if (stat(dir2, &parent_st) < 0) {
83*3034Sdougm return (0);
84*3034Sdougm }
85*3034Sdougm
86*3034Sdougm if (strcmp(childdir, dir1) != 0) {
87*3034Sdougm /*
88*3034Sdougm * Not in cache: get child directory info
89*3034Sdougm */
90*3034Sdougm p = strcpy(childdir, dir1) + strlen(dir1);
91*3034Sdougm index = 0;
92*3034Sdougm valid = 0;
93*3034Sdougm for (;;) {
94*3034Sdougm if (stat(childdir, &st) < 0) {
95*3034Sdougm childdir[0] = 0; /* invalidate cache */
96*3034Sdougm return (0);
97*3034Sdougm }
98*3034Sdougm if (index == 0) {
99*3034Sdougm child_dev = st.st_dev;
100*3034Sdougm child_rdev = st.st_rdev;
101*3034Sdougm (void) strncpy(child_fstype, st.st_fstype,
102*3034Sdougm sizeof (child_fstype));
103*3034Sdougm }
104*3034Sdougm if (index > 0 &&
105*3034Sdougm (child_dev != st.st_dev ||
106*3034Sdougm inoeq(child_ino[index - 1], st.st_ino))) {
107*3034Sdougm /*
108*3034Sdougm * Hit root: done
109*3034Sdougm */
110*3034Sdougm break;
111*3034Sdougm }
112*3034Sdougm child_ino[index++] = st.st_ino;
113*3034Sdougm if (S_ISDIR(st.st_mode)) {
114*3034Sdougm p = strcpy(p, "/..") + 3;
115*3034Sdougm } else {
116*3034Sdougm p = strrchr(childdir, '/');
117*3034Sdougm if (p == NULL) {
118*3034Sdougm p = strcpy(childdir, ".") + 1;
119*3034Sdougm } else {
120*3034Sdougm while (((p - 1) > childdir) &&
121*3034Sdougm *(p - 1) == '/') {
122*3034Sdougm p--;
123*3034Sdougm }
124*3034Sdougm *p = '\0';
125*3034Sdougm }
126*3034Sdougm }
127*3034Sdougm }
128*3034Sdougm valid = index;
129*3034Sdougm (void) strcpy(childdir, dir1);
130*3034Sdougm }
131*3034Sdougm
132*3034Sdougm /*
133*3034Sdougm * Perform the test
134*3034Sdougm */
135*3034Sdougm if (!deveq(parent_st.st_dev, child_dev)) {
136*3034Sdougm return (0);
137*3034Sdougm }
138*3034Sdougm
139*3034Sdougm /*
140*3034Sdougm * Check rdev also in case of lofs
141*3034Sdougm */
142*3034Sdougm if (((strcmp(parent_st.st_fstype, "lofs") == 0)) &&
143*3034Sdougm (strcmp(child_fstype, "lofs") == 0)) {
144*3034Sdougm if (!deveq(parent_st.st_rdev, child_rdev)) {
145*3034Sdougm return (0);
146*3034Sdougm }
147*3034Sdougm }
148*3034Sdougm
149*3034Sdougm for (index = 0; index < valid; index++) {
150*3034Sdougm if (inoeq(child_ino[index], parent_st.st_ino)) {
151*3034Sdougm return (1);
152*3034Sdougm }
153*3034Sdougm }
154*3034Sdougm return (0);
155*3034Sdougm }
156