1186f7fbEdward Pilatowicz/*
2186f7fbEdward Pilatowicz * CDDL HEADER START
3186f7fbEdward Pilatowicz *
4186f7fbEdward Pilatowicz * The contents of this file are subject to the terms of the
5186f7fbEdward Pilatowicz * Common Development and Distribution License (the "License").
6186f7fbEdward Pilatowicz * You may not use this file except in compliance with the License.
7186f7fbEdward Pilatowicz *
8186f7fbEdward Pilatowicz * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9186f7fbEdward Pilatowicz * or http://www.opensolaris.org/os/licensing.
10186f7fbEdward Pilatowicz * See the License for the specific language governing permissions
11186f7fbEdward Pilatowicz * and limitations under the License.
12186f7fbEdward Pilatowicz *
13186f7fbEdward Pilatowicz * When distributing Covered Code, include this CDDL HEADER in each
14186f7fbEdward Pilatowicz * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15186f7fbEdward Pilatowicz * If applicable, add the following below this CDDL HEADER, with the
16186f7fbEdward Pilatowicz * fields enclosed by brackets "[]" replaced with your own identifying
17186f7fbEdward Pilatowicz * information: Portions Copyright [yyyy] [name of copyright owner]
18186f7fbEdward Pilatowicz *
19186f7fbEdward Pilatowicz * CDDL HEADER END
20186f7fbEdward Pilatowicz */
21186f7fbEdward Pilatowicz
22186f7fbEdward Pilatowicz/*
23a7e1d0dRoger A. Faulkner * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24186f7fbEdward Pilatowicz * Use is subject to license terms.
25186f7fbEdward Pilatowicz */
268f68126Bryan Cantrill/*
278f68126Bryan Cantrill * Copyright (c) 2013, Joyent, Inc. All rights reserved.
282a12f85Jeremy Jones * Copyright (c) 2013 by Delphix. All rights reserved.
298f68126Bryan Cantrill */
30186f7fbEdward Pilatowicz
31186f7fbEdward Pilatowicz#include <assert.h>
32186f7fbEdward Pilatowicz#include <dlfcn.h>
33186f7fbEdward Pilatowicz#include <errno.h>
34186f7fbEdward Pilatowicz#include <libzonecfg.h>
35186f7fbEdward Pilatowicz#include <link.h>
36186f7fbEdward Pilatowicz#include <string.h>
37186f7fbEdward Pilatowicz#include <strings.h>
38186f7fbEdward Pilatowicz#include <sys/list.h>
39186f7fbEdward Pilatowicz#include <sys/types.h>
40186f7fbEdward Pilatowicz#include <sys/mkdev.h>
41186f7fbEdward Pilatowicz#include <sys/mman.h>
42186f7fbEdward Pilatowicz#include <sys/mnttab.h>
43186f7fbEdward Pilatowicz
44186f7fbEdward Pilatowicz#include "Pcontrol.h"
45186f7fbEdward Pilatowicz
46186f7fbEdward Pilatowiczstruct path_node {
47186f7fbEdward Pilatowicz	struct path_node	*pn_next;
48186f7fbEdward Pilatowicz	char			*pn_path;
49186f7fbEdward Pilatowicz};
50186f7fbEdward Pilatowicztypedef struct path_node path_node_t;
51186f7fbEdward Pilatowicz
52a7e1d0dRoger A. Faulkner/*
53a7e1d0dRoger A. Faulkner * Parameters of the lofs lookup cache.
54a7e1d0dRoger A. Faulkner */
55a7e1d0dRoger A. Faulknerstatic struct stat64 lofs_mstat; /* last stat() of MNTTAB */
56a7e1d0dRoger A. Faulknerstatic struct lofs_mnttab {	/* linked list of all lofs mount points */
57a7e1d0dRoger A. Faulkner	struct lofs_mnttab *l_next;
58a7e1d0dRoger A. Faulkner	char	*l_special;	/* extracted from MNTTAB */
59a7e1d0dRoger A. Faulkner	char	*l_mountp;	/* ditto */
60a7e1d0dRoger A. Faulkner} *lofs_mnttab = NULL;
61a7e1d0dRoger A. Faulknerstatic mutex_t lofs_lock = DEFAULTMUTEX;	/* protects the lofs cache */
62a7e1d0dRoger A. Faulkner
63a7e1d0dRoger A. Faulknerstatic void
64a7e1d0dRoger A. Faulknerrebuild_lofs_cache(void)
65a7e1d0dRoger A. Faulkner{
66a7e1d0dRoger A. Faulkner	struct mnttab mt;
67a7e1d0dRoger A. Faulkner	struct mnttab mt_find;
68a7e1d0dRoger A. Faulkner	struct lofs_mnttab *lmt;
69a7e1d0dRoger A. Faulkner	struct lofs_mnttab *next;
70a7e1d0dRoger A. Faulkner	FILE *fp;
71a7e1d0dRoger A. Faulkner
72a7e1d0dRoger A. Faulkner	assert(MUTEX_HELD(&lofs_lock));
73a7e1d0dRoger A. Faulkner
74a7e1d0dRoger A. Faulkner	/* destroy the old cache */
75a7e1d0dRoger A. Faulkner	for (lmt = lofs_mnttab; lmt != NULL; lmt = next) {
76a7e1d0dRoger A. Faulkner		next = lmt->l_next;
77a7e1d0dRoger A. Faulkner		free(lmt->l_special);
78a7e1d0dRoger A. Faulkner		free(lmt->l_mountp);
79a7e1d0dRoger A. Faulkner		free(lmt);
80a7e1d0dRoger A. Faulkner	}
81a7e1d0dRoger A. Faulkner	lofs_mnttab = NULL;
82a7e1d0dRoger A. Faulkner
83a7e1d0dRoger A. Faulkner	/* prepare to create the new cache */
84a7e1d0dRoger A. Faulkner	if ((fp = fopen(MNTTAB, "r")) == NULL)
85a7e1d0dRoger A. Faulkner		return;
86a7e1d0dRoger A. Faulkner
87a7e1d0dRoger A. Faulkner	/*
88a7e1d0dRoger A. Faulkner	 * We only care about lofs mount points.  But we need to
89a7e1d0dRoger A. Faulkner	 * ignore lofs mounts where the source path is the same
90a7e1d0dRoger A. Faulkner	 * as the target path.  (This can happen when a non-global
91a7e1d0dRoger A. Faulkner	 * zone has a lofs mount of a global zone filesystem, since
92a7e1d0dRoger A. Faulkner	 * the source path can't expose information about global
93a7e1d0dRoger A. Faulkner	 * zone paths to the non-global zone.)
94a7e1d0dRoger A. Faulkner	 */
95a7e1d0dRoger A. Faulkner	bzero(&mt_find, sizeof (mt_find));
96a7e1d0dRoger A. Faulkner	mt_find.mnt_fstype = "lofs";
97a7e1d0dRoger A. Faulkner	while (getmntany(fp, &mt, &mt_find) == 0 &&
98a7e1d0dRoger A. Faulkner	    (strcmp(mt.mnt_fstype, "lofs") == 0) &&
99a7e1d0dRoger A. Faulkner	    (strcmp(mt.mnt_special, mt.mnt_mountp) != 0)) {
100a7e1d0dRoger A. Faulkner		if ((lmt = malloc(sizeof (struct lofs_mnttab))) == NULL)
101a7e1d0dRoger A. Faulkner			break;
102a7e1d0dRoger A. Faulkner		lmt->l_special = strdup(mt.mnt_special);
103a7e1d0dRoger A. Faulkner		lmt->l_mountp = strdup(mt.mnt_mountp);
104a7e1d0dRoger A. Faulkner		lmt->l_next = lofs_mnttab;
105a7e1d0dRoger A. Faulkner		lofs_mnttab = lmt;
106a7e1d0dRoger A. Faulkner	}
107a7e1d0dRoger A. Faulkner
108a7e1d0dRoger A. Faulkner	(void) fclose(fp);
109a7e1d0dRoger A. Faulkner}
110a7e1d0dRoger A. Faulkner
111a7e1d0dRoger A. Faulknerstatic const char *
112a7e1d0dRoger A. Faulknerlookup_lofs_mount_point(const char *mountp)
113a7e1d0dRoger A. Faulkner{
114a7e1d0dRoger A. Faulkner	struct lofs_mnttab *lmt;
115a7e1d0dRoger A. Faulkner
116a7e1d0dRoger A. Faulkner	assert(MUTEX_HELD(&lofs_lock));
117a7e1d0dRoger A. Faulkner
118a7e1d0dRoger A. Faulkner	for (lmt = lofs_mnttab; lmt != NULL; lmt = lmt->l_next) {
119a7e1d0dRoger A. Faulkner		if (strcmp(lmt->l_mountp, mountp) == 0)
120a7e1d0dRoger A. Faulkner			return (lmt->l_special);
121a7e1d0dRoger A. Faulkner	}
122a7e1d0dRoger A. Faulkner	return (NULL);
123a7e1d0dRoger A. Faulkner}
124a7e1d0dRoger A. Faulkner
125186f7fbEdward Pilatowiczstatic path_node_t *
126186f7fbEdward Pilatowiczpn_push(path_node_t **pnp, char *path)
127186f7fbEdward Pilatowicz{
128186f7fbEdward Pilatowicz	path_node_t *pn;
129186f7fbEdward Pilatowicz
130186f7fbEdward Pilatowicz	if ((pn = calloc(sizeof (path_node_t), 1)) == NULL)
131186f7fbEdward Pilatowicz		return (NULL);
132186f7fbEdward Pilatowicz
133186f7fbEdward Pilatowicz	if ((pn->pn_path = strdup(path)) == NULL) {
134186f7fbEdward Pilatowicz		free(pn);
135186f7fbEdward Pilatowicz		return (NULL);
136186f7fbEdward Pilatowicz	}
137186f7fbEdward Pilatowicz	pn->pn_next = *pnp;
138186f7fbEdward Pilatowicz	return (*pnp = pn);
139186f7fbEdward Pilatowicz}
140186f7fbEdward Pilatowicz
141186f7fbEdward Pilatowiczstatic void
142186f7fbEdward Pilatowiczpn_free(path_node_t **pnp)
143186f7fbEdward Pilatowicz{
144186f7fbEdward Pilatowicz	path_node_t *pn;
145186f7fbEdward Pilatowicz
146186f7fbEdward Pilatowicz	while (*pnp != NULL) {
147186f7fbEdward Pilatowicz		pn = *pnp;
148186f7fbEdward Pilatowicz		*pnp = pn->pn_next;
149186f7fbEdward Pilatowicz		free(pn->pn_path);
150186f7fbEdward Pilatowicz		free(pn);
151186f7fbEdward Pilatowicz	}
152186f7fbEdward Pilatowicz}
153186f7fbEdward Pilatowicz
154186f7fbEdward Pilatowiczstatic void
155186f7fbEdward Pilatowiczpn_free2(path_node_t **pn1, path_node_t **pn2)
156186f7fbEdward Pilatowicz{
157186f7fbEdward Pilatowicz	pn_free(pn1);
158186f7fbEdward Pilatowicz	pn_free(pn2);
159186f7fbEdward Pilatowicz}
160186f7fbEdward Pilatowicz
161186f7fbEdward Pilatowiczstatic char *
162186f7fbEdward Pilatowiczpn_pop(path_node_t **pnp, char *path)
163186f7fbEdward Pilatowicz{
164186f7fbEdward Pilatowicz	path_node_t *pn;
165186f7fbEdward Pilatowicz
166186f7fbEdward Pilatowicz	if (*pnp == NULL)
167186f7fbEdward Pilatowicz		return (NULL);
168186f7fbEdward Pilatowicz
169186f7fbEdward Pilatowicz	pn = *pnp;
170186f7fbEdward Pilatowicz	*pnp = pn->pn_next;
171186f7fbEdward Pilatowicz	pn->pn_next = NULL;
172186f7fbEdward Pilatowicz
173186f7fbEdward Pilatowicz	if (path == NULL) {
174186f7fbEdward Pilatowicz		pn_free(&pn);
175186f7fbEdward Pilatowicz		return (NULL);
176186f7fbEdward Pilatowicz	}
177186f7fbEdward Pilatowicz	(void) strlcpy(path, pn->pn_path, PATH_MAX);
178186f7fbEdward Pilatowicz	pn_free(&pn);
179186f7fbEdward Pilatowicz	return (path);
180186f7fbEdward Pilatowicz}
181186f7fbEdward Pilatowicz
182186f7fbEdward Pilatowicz
183186f7fbEdward Pilatowicz/*
184186f7fbEdward Pilatowicz * Libzonecfg.so links against libproc, so libproc can't link against
185186f7fbEdward Pilatowicz * libzonecfg.so.  Also, libzonecfg.so is optional and might not be
186186f7fbEdward Pilatowicz * installed.  Hence instead of relying on linking to access libzonecfg.so,
187186f7fbEdward Pilatowicz * we'll try dlopening it here.  This trick is borrowed from
188186f7fbEdward Pilatowicz * libc`zone_get_id(), see that function for more detailed comments.
189186f7fbEdward Pilatowicz */
190186f7fbEdward Pilatowiczstatic int
191186f7fbEdward Pilatowiczi_zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz)
192186f7fbEdward Pilatowicz{
193186f7fbEdward Pilatowicz	typedef	int (*zone_get_zonepath_t)(char *, char *, size_t);
194186f7fbEdward Pilatowicz	static zone_get_zonepath_t zone_get_zonepath_fp = NULL;
195186f7fbEdward Pilatowicz
196186f7fbEdward Pilatowicz	if (zone_get_zonepath_fp == NULL) {
197186f7fbEdward Pilatowicz		/* There's no harm in doing this multiple times. */
198186f7fbEdward Pilatowicz		void *dlhandle = dlopen("libzonecfg.so.1", RTLD_LAZY);
199186f7fbEdward Pilatowicz		void *sym = (void *)(-1);
200186f7fbEdward Pilatowicz		if (dlhandle != NULL &&
201186f7fbEdward Pilatowicz		    (sym = dlsym(dlhandle, "zone_get_zonepath")) == NULL) {
202186f7fbEdward Pilatowicz			sym = (void *)(-1);
203186f7fbEdward Pilatowicz			(void) dlclose(dlhandle);
204186f7fbEdward Pilatowicz		}
205186f7fbEdward Pilatowicz		zone_get_zonepath_fp = (zone_get_zonepath_t)sym;
206186f7fbEdward Pilatowicz	}
207186f7fbEdward Pilatowicz
208186f7fbEdward Pilatowicz	/* If we've successfully loaded it, call the real function */
209186f7fbEdward Pilatowicz	if (zone_get_zonepath_fp != (zone_get_zonepath_t)(-1))
210186f7fbEdward Pilatowicz		return (zone_get_zonepath_fp(zone_name, zonepath, rp_sz));
211186f7fbEdward Pilatowicz	return (Z_NO_ZONE);
212186f7fbEdward Pilatowicz}
213186f7fbEdward Pilatowicz
214186f7fbEdward Pilatowiczchar *
215186f7fbEdward PilatowiczPbrandname(struct ps_prochandle *P, char *buf, size_t buflen)
216186f7fbEdward Pilatowicz{
217186f7fbEdward Pilatowicz	long	addr;
218186f7fbEdward Pilatowicz
219186f7fbEdward Pilatowicz	if ((addr = Pgetauxval(P, AT_SUN_BRANDNAME)) == -1)
220186f7fbEdward Pilatowicz		return (NULL);
221186f7fbEdward Pilatowicz
222186f7fbEdward Pilatowicz	if (Pread_string(P, buf, buflen, addr) == -1)
223186f7fbEdward Pilatowicz		return (NULL);
224186f7fbEdward Pilatowicz
225186f7fbEdward Pilatowicz	return (buf);
226186f7fbEdward Pilatowicz}
227186f7fbEdward Pilatowicz
228186f7fbEdward Pilatowicz/*
229186f7fbEdward Pilatowicz * Get the zone name from the core file if we have it; look up the
230186f7fbEdward Pilatowicz * name based on the zone id if this is a live process.
231186f7fbEdward Pilatowicz */
232186f7fbEdward Pilatowiczchar *
233186f7fbEdward PilatowiczPzonename(struct ps_prochandle *P, char *s, size_t n)
234186f7fbEdward Pilatowicz{
2352a12f85Jeremy Jones	return (P->ops.pop_zonename(P, s, n, P->data));
236186f7fbEdward Pilatowicz}
237186f7fbEdward Pilatowicz
238186f7fbEdward Pilatowiczchar *
239186f7fbEdward PilatowiczPzoneroot(struct ps_prochandle *P, char *s, size_t n)
240186f7fbEdward Pilatowicz{
241186f7fbEdward Pilatowicz	char zname[ZONENAME_MAX], zpath[PATH_MAX], tmp[PATH_MAX];
242186f7fbEdward Pilatowicz	int rv;
243186f7fbEdward Pilatowicz
244186f7fbEdward Pilatowicz	if (P->zoneroot != NULL) {
245186f7fbEdward Pilatowicz		(void) strlcpy(s, P->zoneroot, n);
246186f7fbEdward Pilatowicz		return (s);
247186f7fbEdward Pilatowicz	}
248186f7fbEdward Pilatowicz
249186f7fbEdward Pilatowicz	if ((Pzonename(P, zname, sizeof (zname)) == NULL) ||
250186f7fbEdward Pilatowicz	    (strcmp(zname, GLOBAL_ZONENAME) == 0)) {
251186f7fbEdward Pilatowicz		if ((P->zoneroot = strdup("")) == NULL) {
252186f7fbEdward Pilatowicz			errno = ENOMEM;
253186f7fbEdward Pilatowicz			return (NULL);
254186f7fbEdward Pilatowicz		}
255186f7fbEdward Pilatowicz		dprintf("Pzoneroot defaulting to '%s'\n", GLOBAL_ZONENAME);
256186f7fbEdward Pilatowicz		(void) strlcpy(s, P->zoneroot, n);
257186f7fbEdward Pilatowicz		return (s);
258186f7fbEdward Pilatowicz	}
259186f7fbEdward Pilatowicz
260186f7fbEdward Pilatowicz	if (i_zone_get_zonepath(zname, zpath, sizeof (zpath)) != Z_OK) {
261186f7fbEdward Pilatowicz		if ((P->zoneroot = strdup("")) == NULL) {
262186f7fbEdward Pilatowicz			errno = ENOMEM;
263186f7fbEdward Pilatowicz			return (NULL);
264186f7fbEdward Pilatowicz		}
265186f7fbEdward Pilatowicz		dprintf(
266186f7fbEdward Pilatowicz		    "Pzoneroot zone not found '%s', defaulting to '%s'\n",
267186f7fbEdward Pilatowicz		    zname, GLOBAL_ZONENAME);
268186f7fbEdward Pilatowicz		(void) strlcpy(s, P->zoneroot, n);
269186f7fbEdward Pilatowicz		return (s);
270186f7fbEdward Pilatowicz	}
271186f7fbEdward Pilatowicz	(void) strlcat(zpath, "/root", sizeof (zpath));
272186f7fbEdward Pilatowicz
273186f7fbEdward Pilatowicz	if ((rv = resolvepath(zpath, tmp, sizeof (tmp) - 1)) < 0) {
274186f7fbEdward Pilatowicz		if ((P->zoneroot = strdup("")) == NULL) {
275186f7fbEdward Pilatowicz			errno = ENOMEM;
276186f7fbEdward Pilatowicz			return (NULL);
277186f7fbEdward Pilatowicz		}
278186f7fbEdward Pilatowicz		dprintf(
279186f7fbEdward Pilatowicz		    "Pzoneroot can't access '%s:%s', defaulting to '%s'\n",
280186f7fbEdward Pilatowicz		    zname, zpath, GLOBAL_ZONENAME);
281186f7fbEdward Pilatowicz		(void) strlcpy(s, P->zoneroot, n);
282186f7fbEdward Pilatowicz		return (s);
283186f7fbEdward Pilatowicz	}
284186f7fbEdward Pilatowicz	tmp[rv] = '\0';
285186f7fbEdward Pilatowicz	(void) strlcpy(zpath, tmp, sizeof (zpath));
286186f7fbEdward Pilatowicz
287186f7fbEdward Pilatowicz	if ((P->zoneroot = strdup(zpath)) == NULL) {
288186f7fbEdward Pilatowicz		errno = ENOMEM;
289186f7fbEdward Pilatowicz		return (NULL);
290186f7fbEdward Pilatowicz	}
291186f7fbEdward Pilatowicz	dprintf("Pzoneroot found zone root '%s:%s'\n", zname, zpath);
292186f7fbEdward Pilatowicz	(void) strlcpy(s, P->zoneroot, n);
293186f7fbEdward Pilatowicz	return (s);
294186f7fbEdward Pilatowicz}
295186f7fbEdward Pilatowicz
296186f7fbEdward Pilatowicz/*
297186f7fbEdward Pilatowicz * Plofspath() takes a path, "path",  and removes any lofs components from
298186f7fbEdward Pilatowicz * that path.  The resultant path (if different from the starting path)
299186f7fbEdward Pilatowicz * is placed in "s", which is limited to "n" characters, and the return
300186f7fbEdward Pilatowicz * value is the pointer s.  If there are no lofs components in the path
301186f7fbEdward Pilatowicz * the NULL is returned and s is not modified.  It's ok for "path" and
302186f7fbEdward Pilatowicz * "s" to be the same pointer.  (ie, the results can be stored directly
303186f7fbEdward Pilatowicz * in the input buffer.)  The path that is passed in must be an absolute
304186f7fbEdward Pilatowicz * path.
305186f7fbEdward Pilatowicz *
306186f7fbEdward Pilatowicz * Example:
307186f7fbEdward Pilatowicz *	if "path" == "/foo/bar", and "/candy/" is lofs mounted on "/foo/"
308186f7fbEdward Pilatowicz *	then "/candy/bar/" will be written into "s" and "s" will be returned.
309186f7fbEdward Pilatowicz */
310186f7fbEdward Pilatowiczchar *
311186f7fbEdward PilatowiczPlofspath(const char *path, char *s, size_t n)
312186f7fbEdward Pilatowicz{
313186f7fbEdward Pilatowicz	char tmp[PATH_MAX + 1];
314a7e1d0dRoger A. Faulkner	struct stat64 statb;
315a7e1d0dRoger A. Faulkner	const char *special;
316186f7fbEdward Pilatowicz	char *p, *p2;
317186f7fbEdward Pilatowicz	int rv;
318186f7fbEdward Pilatowicz
319186f7fbEdward Pilatowicz	dprintf("Plofspath path '%s'\n", path);
320186f7fbEdward Pilatowicz
321186f7fbEdward Pilatowicz	/* We only deal with absolute paths */
322186f7fbEdward Pilatowicz	if (path[0] != '/')
323186f7fbEdward Pilatowicz		return (NULL);
324186f7fbEdward Pilatowicz
325186f7fbEdward Pilatowicz	/* Make a copy of the path so that we can muck with it */
326186f7fbEdward Pilatowicz	(void) strlcpy(tmp, path, sizeof (tmp) - 1);
327186f7fbEdward Pilatowicz
328186f7fbEdward Pilatowicz	/*
329186f7fbEdward Pilatowicz	 * Use resolvepath() to make sure there are no consecutive or
330186f7fbEdward Pilatowicz	 * trailing '/'s in the path.
331186f7fbEdward Pilatowicz	 */
332186f7fbEdward Pilatowicz	if ((rv = resolvepath(tmp, tmp, sizeof (tmp) - 1)) >= 0)
333186f7fbEdward Pilatowicz		tmp[rv] = '\0';
334186f7fbEdward Pilatowicz
335a7e1d0dRoger A. Faulkner	(void) mutex_lock(&lofs_lock);
336a7e1d0dRoger A. Faulkner
337a7e1d0dRoger A. Faulkner	/*
338a7e1d0dRoger A. Faulkner	 * If /etc/mnttab has been modified since the last time
339a7e1d0dRoger A. Faulkner	 * we looked, then rebuild the lofs lookup cache.
340a7e1d0dRoger A. Faulkner	 */
341a7e1d0dRoger A. Faulkner	if (stat64(MNTTAB, &statb) == 0 &&
342a7e1d0dRoger A. Faulkner	    (statb.st_mtim.tv_sec != lofs_mstat.st_mtim.tv_sec ||
343a7e1d0dRoger A. Faulkner	    statb.st_mtim.tv_nsec != lofs_mstat.st_mtim.tv_nsec ||
344a7e1d0dRoger A. Faulkner	    statb.st_ctim.tv_sec != lofs_mstat.st_ctim.tv_sec ||
345a7e1d0dRoger A. Faulkner	    statb.st_ctim.tv_nsec != lofs_mstat.st_ctim.tv_nsec)) {
346a7e1d0dRoger A. Faulkner		lofs_mstat = statb;
347a7e1d0dRoger A. Faulkner		rebuild_lofs_cache();
348a7e1d0dRoger A. Faulkner	}
349a7e1d0dRoger A. Faulkner
350186f7fbEdward Pilatowicz	/*
351186f7fbEdward Pilatowicz	 * So now we're going to search the path for any components that
352186f7fbEdward Pilatowicz	 * might be lofs mounts.  We'll start out search from the full
353186f7fbEdward Pilatowicz	 * path and then step back through each parent directly till
354186f7fbEdward Pilatowicz	 * we reach the root.  If we find a lofs mount point in the path
355186f7fbEdward Pilatowicz	 * then we'll replace the initial portion of the path (up
356186f7fbEdward Pilatowicz	 * to that mount point) with the source of that mount point
357186f7fbEdward Pilatowicz	 * and then start our search over again.
358186f7fbEdward Pilatowicz	 *
359186f7fbEdward Pilatowicz	 * Here's some of the variables we're going to use:
360186f7fbEdward Pilatowicz	 *
361186f7fbEdward Pilatowicz	 *	tmp - A pointer to our working copy of the path.  Sometimes
362186f7fbEdward Pilatowicz	 *		this path will be divided into two strings by a
363186f7fbEdward Pilatowicz	 *		'\0' (NUL) character.  The first string is the
364186f7fbEdward Pilatowicz	 *		component we're currently checking and the second
365186f7fbEdward Pilatowicz	 *		string is the path components we've already checked.
366186f7fbEdward Pilatowicz	 *
367186f7fbEdward Pilatowicz	 *	p - A pointer to the last '/' seen in the string.
368186f7fbEdward Pilatowicz	 *
369186f7fbEdward Pilatowicz	 *	p[1] - A pointer to the component of the string we've already
370186f7fbEdward Pilatowicz	 *		checked.
371186f7fbEdward Pilatowicz	 *
372186f7fbEdward Pilatowicz	 * Initially, p will point to the end of our path and p[1] will point
373186f7fbEdward Pilatowicz	 * to an extra '\0' (NUL) that we'll append to the end of the string.
374186f7fbEdward Pilatowicz	 * (This is why we declared tmp with a size of PATH_MAX + 1).
375186f7fbEdward Pilatowicz	 */
376186f7fbEdward Pilatowicz	p = &tmp[strlen(tmp)];
377186f7fbEdward Pilatowicz	p[1] = '\0';
378186f7fbEdward Pilatowicz	for (;;) {
379a7e1d0dRoger A. Faulkner		if ((special = lookup_lofs_mount_point(tmp)) != NULL) {
380186f7fbEdward Pilatowicz			char tmp2[PATH_MAX + 1];
381186f7fbEdward Pilatowicz
382186f7fbEdward Pilatowicz			/*
383186f7fbEdward Pilatowicz			 * We found a lofs mount.  Update the path that we're
384186f7fbEdward Pilatowicz			 * checking and start over.  This means append the
385186f7fbEdward Pilatowicz			 * portion of the path we've already checked to the
386186f7fbEdward Pilatowicz			 * source of the lofs mount and re-start this entire
387186f7fbEdward Pilatowicz			 * lofs resolution loop.  Use resolvepath() to make
388186f7fbEdward Pilatowicz			 * sure there are no consecutive or trailing '/'s
389186f7fbEdward Pilatowicz			 * in the path.
390b00e809Jerry Jelinek			 *
391b00e809Jerry Jelinek			 * However, we need to be careful to handle the case of
392b00e809Jerry Jelinek			 * a lofs mounted file under a lofs mounted file system.
393b00e809Jerry Jelinek			 * In this case, we just keep going.
394186f7fbEdward Pilatowicz			 */
395b00e809Jerry Jelinek
396a7e1d0dRoger A. Faulkner			(void) strlcpy(tmp2, special, sizeof (tmp2) - 1);
397186f7fbEdward Pilatowicz			(void) strlcat(tmp2, "/", sizeof (tmp2) - 1);
398186f7fbEdward Pilatowicz			(void) strlcat(tmp2, &p[1], sizeof (tmp2) - 1);
399b00e809Jerry Jelinek			if ((rv = resolvepath(tmp2, tmp2, sizeof (tmp2) - 1)) >=
400b00e809Jerry Jelinek			    0) {
401b00e809Jerry Jelinek				tmp2[rv] = '\0';
402b00e809Jerry Jelinek				(void) strlcpy(tmp, tmp2, sizeof (tmp) - 1);
403b00e809Jerry Jelinek				p = &tmp[strlen(tmp)];
404b00e809Jerry Jelinek				p[1] = '\0';
405b00e809Jerry Jelinek				continue;
406b00e809Jerry Jelinek			}
407186f7fbEdward Pilatowicz		}
408186f7fbEdward Pilatowicz
409186f7fbEdward Pilatowicz		/* No lofs mount found */
410186f7fbEdward Pilatowicz		if ((p2 = strrchr(tmp, '/')) == NULL) {
411186f7fbEdward Pilatowicz			char tmp2[PATH_MAX];
412186f7fbEdward Pilatowicz
413a7e1d0dRoger A. Faulkner			(void) mutex_unlock(&lofs_lock);
414a7e1d0dRoger A. Faulkner
415186f7fbEdward Pilatowicz			/*
416186f7fbEdward Pilatowicz			 * We know that tmp was an absolute path, so if we
417186f7fbEdward Pilatowicz			 * made it here we know that (p == tmp) and that
418186f7fbEdward Pilatowicz			 * (*p == '\0').  This means that we've managed
419186f7fbEdward Pilatowicz			 * to check the whole path and so we're done.
420186f7fbEdward Pilatowicz			 */
421186f7fbEdward Pilatowicz			assert(p == tmp);
422186f7fbEdward Pilatowicz			assert(p[0] == '\0');
423186f7fbEdward Pilatowicz
424186f7fbEdward Pilatowicz			/* Restore the leading '/' in the path */
425186f7fbEdward Pilatowicz			p[0] = '/';
426186f7fbEdward Pilatowicz
427186f7fbEdward Pilatowicz			if (strcmp(tmp, path) == 0) {
428186f7fbEdward Pilatowicz				/* The path didn't change */
429186f7fbEdward Pilatowicz				return (NULL);
430186f7fbEdward Pilatowicz			}
431186f7fbEdward Pilatowicz
432186f7fbEdward Pilatowicz			/*
433186f7fbEdward Pilatowicz			 * It's possible that lofs source path we just
434186f7fbEdward Pilatowicz			 * obtained contains a symbolic link.  Use
435186f7fbEdward Pilatowicz			 * resolvepath() to clean it up.
436186f7fbEdward Pilatowicz			 */
437186f7fbEdward Pilatowicz			(void) strlcpy(tmp2, tmp, sizeof (tmp2));
438186f7fbEdward Pilatowicz			if ((rv = resolvepath(tmp, tmp, sizeof (tmp) - 1)) >= 0)
439186f7fbEdward Pilatowicz				tmp[rv] = '\0';
440186f7fbEdward Pilatowicz
441186f7fbEdward Pilatowicz			/*
442186f7fbEdward Pilatowicz			 * It's always possible that our lofs source path is
443186f7fbEdward Pilatowicz			 * actually another lofs mount.  So call ourselves
444186f7fbEdward Pilatowicz			 * recursively to resolve that path.
445186f7fbEdward Pilatowicz			 */
446186f7fbEdward Pilatowicz			(void) Plofspath(tmp, tmp, PATH_MAX);
447186f7fbEdward Pilatowicz
448186f7fbEdward Pilatowicz			/* Copy out our final resolved lofs source path */
449186f7fbEdward Pilatowicz			(void) strlcpy(s, tmp, n);
450186f7fbEdward Pilatowicz			dprintf("Plofspath path result '%s'\n", s);
451186f7fbEdward Pilatowicz			return (s);
452186f7fbEdward Pilatowicz		}
453186f7fbEdward Pilatowicz
454186f7fbEdward Pilatowicz		/*
455186f7fbEdward Pilatowicz		 * So the path we just checked is not a lofs mount.  Next we
456186f7fbEdward Pilatowicz		 * want to check the parent path component for a lofs mount.
457186f7fbEdward Pilatowicz		 *
458186f7fbEdward Pilatowicz		 * First, restore any '/' that we replaced with a '\0' (NUL).
459186f7fbEdward Pilatowicz		 * We can determine if we should do this by looking at p[1].
460186f7fbEdward Pilatowicz		 * If p[1] points to a '\0' (NUL) then we know that p points
461186f7fbEdward Pilatowicz		 * to the end of the string and there is no '/' to restore.
462186f7fbEdward Pilatowicz		 * if p[1] doesn't point to a '\0' (NUL) then it points to
463186f7fbEdward Pilatowicz		 * the part of the path that we've already verified so there
464186f7fbEdward Pilatowicz		 * is a '/' to restore.
465186f7fbEdward Pilatowicz		 */
466186f7fbEdward Pilatowicz		if (p[1] != '\0')
467186f7fbEdward Pilatowicz			p[0] = '/';
468186f7fbEdward Pilatowicz
469186f7fbEdward Pilatowicz		/*
470186f7fbEdward Pilatowicz		 * Second, replace the last '/' in the part of the path
471186f7fbEdward Pilatowicz		 * that we've already checked with a '\0' (NUL) so that
472186f7fbEdward Pilatowicz		 * when we loop around we check the parent component of the
473186f7fbEdward Pilatowicz		 * path.
474186f7fbEdward Pilatowicz		 */
475186f7fbEdward Pilatowicz		p2[0] = '\0';
476186f7fbEdward Pilatowicz		p = p2;
477186f7fbEdward Pilatowicz	}
478186f7fbEdward Pilatowicz	/*NOTREACHED*/
479186f7fbEdward Pilatowicz}
480186f7fbEdward Pilatowicz
481186f7fbEdward Pilatowicz/*
482186f7fbEdward Pilatowicz * Pzonepath() - Way too much code to attempt to derive the full path of
483186f7fbEdward Pilatowicz * an object within a zone.
484186f7fbEdward Pilatowicz *
485186f7fbEdward Pilatowicz * Pzonepath() takes a path and attempts to resolve it relative to the
486186f7fbEdward Pilatowicz * root associated with the current process handle.  If it fails it will
487186f7fbEdward Pilatowicz * not update the results string.  It is safe to specify the same pointer
488186f7fbEdward Pilatowicz * for the file string and the results string.
489186f7fbEdward Pilatowicz *
490186f7fbEdward Pilatowicz * Doing this resolution is more difficult than it initially sounds.
491186f7fbEdward Pilatowicz * We can't simply append the file path to the zone root, because in
492186f7fbEdward Pilatowicz * a root directory, '..' is treated the same as '.'.  Also, symbolic
493186f7fbEdward Pilatowicz * links that specify an absolute path need to be interpreted relative
494186f7fbEdward Pilatowicz * to the zone root.
495186f7fbEdward Pilatowicz *
496186f7fbEdward Pilatowicz * It seems like perhaps we could do a chroot(<zone root>) followed by a
497186f7fbEdward Pilatowicz * resolvepath().  But we can't do this because chroot requires special
498186f7fbEdward Pilatowicz * privileges and affects the entire process.  Perhaps if there was a
499186f7fbEdward Pilatowicz * special version of resolvepath() which took an addition root path
500186f7fbEdward Pilatowicz * we could use that, but this isn't ideal either.  The reason is
501186f7fbEdward Pilatowicz * that we want to have special handling for native paths.  (A native path
502186f7fbEdward Pilatowicz * is a path that begins with "/native/" or "/.SUNWnative/".)  Native
503186f7fbEdward Pilatowicz * paths could be passed explicity to this function or could be embedded
504186f7fbEdward Pilatowicz * in a symlink that is part of the path passed into this function.
505186f7fbEdward Pilatowicz * These paths are always lofs mounts of global zone paths, but lofs
506186f7fbEdward Pilatowicz * mounts only exist when a zone is booted.  So if we were to try to do
507186f7fbEdward Pilatowicz * a resolvepath() on a native path when the zone wasn't booted the
508186f7fbEdward Pilatowicz * resolvepath() would fail even though we know that the components
509186f7fbEdward Pilatowicz * exists in the global zone.
510186f7fbEdward Pilatowicz *
511186f7fbEdward Pilatowicz * Given all these constraints, we just implement a path walking function
512186f7fbEdward Pilatowicz * that resolves a file path relative to a zone root by manually inspecting
513186f7fbEdward Pilatowicz * each of the path components and verifying its existence.  This means that
514186f7fbEdward Pilatowicz * we must have access to the zone and that all the components of the
515186f7fbEdward Pilatowicz * path must exist for this operation to succeed.
516186f7fbEdward Pilatowicz */
517186f7fbEdward Pilatowiczchar *
518186f7fbEdward PilatowiczPzonepath(struct ps_prochandle *P, const char *path, char *s, size_t n)
519186f7fbEdward Pilatowicz{
520186f7fbEdward Pilatowicz	char zroot[PATH_MAX], zpath[PATH_MAX], tmp[PATH_MAX], link[PATH_MAX];
521186f7fbEdward Pilatowicz	path_node_t *pn_stack = NULL, *pn_links = NULL, *pn;
522186f7fbEdward Pilatowicz	struct stat64 sb;
523186f7fbEdward Pilatowicz	char *p;
524186f7fbEdward Pilatowicz	int i, rv;
525186f7fbEdward Pilatowicz
526186f7fbEdward Pilatowicz	dprintf("Pzonepath lookup '%s'\n", path);
527186f7fbEdward Pilatowicz
528186f7fbEdward Pilatowicz	/* First lookup the zone root */
529186f7fbEdward Pilatowicz	if (Pzoneroot(P, zroot, sizeof (zroot)) == NULL)
530186f7fbEdward Pilatowicz		return (NULL);
531186f7fbEdward Pilatowicz
532186f7fbEdward Pilatowicz	/*
533186f7fbEdward Pilatowicz	 * Make a temporary copy of the path specified.
534186f7fbEdward Pilatowicz	 * If it's a relative path then make it into an absolute path.
535186f7fbEdward Pilatowicz	 */
536186f7fbEdward Pilatowicz	tmp[0] = '\0';
537186f7fbEdward Pilatowicz	if (path[0] != '/')
538186f7fbEdward Pilatowicz		(void) strlcat(tmp, "/", sizeof (tmp));
539186f7fbEdward Pilatowicz	(void) strlcat(tmp, path, sizeof (tmp));
540186f7fbEdward Pilatowicz
541186f7fbEdward Pilatowicz	/*
542186f7fbEdward Pilatowicz	 * If the path that was passed in is the zone root, we're done.
543186f7fbEdward Pilatowicz	 * If the path that was passed in already contains the zone root
544186f7fbEdward Pilatowicz	 * then strip the zone root out and verify the rest of the path.
545186f7fbEdward Pilatowicz	 */
546186f7fbEdward Pilatowicz	if (strcmp(tmp, zroot) == 0) {
547186f7fbEdward Pilatowicz		(void) Plofspath(zroot, zroot, sizeof (zroot));
548186f7fbEdward Pilatowicz		dprintf("Pzonepath found zone path (1) '%s'\n", zroot);
549186f7fbEdward Pilatowicz		(void) strlcpy(s, zroot, n);
550186f7fbEdward Pilatowicz		return (s);
551186f7fbEdward Pilatowicz	}
552186f7fbEdward Pilatowicz	i = strlen(zroot);
553186f7fbEdward Pilatowicz	if ((strncmp(tmp, zroot, i) == 0) && (tmp[i] == '/'))
554186f7fbEdward Pilatowicz		(void) memmove(tmp, tmp + i, strlen(tmp + i) + 1);
555186f7fbEdward Pilatowicz
556186f7fbEdward Pilatowicz	/* If no path is passed in, then it maps to the zone root */
557186f7fbEdward Pilatowicz	if (strlen(tmp) == 0) {
558186f7fbEdward Pilatowicz		(void) Plofspath(zroot, zroot, sizeof (zroot));
559186f7fbEdward Pilatowicz		dprintf("Pzonepath found zone path (2) '%s'\n", zroot);
560186f7fbEdward Pilatowicz		(void) strlcpy(s, zroot, n);
561186f7fbEdward Pilatowicz		return (s);
562186f7fbEdward Pilatowicz	}
563186f7fbEdward Pilatowicz
564186f7fbEdward Pilatowicz	/*
565186f7fbEdward Pilatowicz	 * Push each path component that we plan to verify onto a stack of
566186f7fbEdward Pilatowicz	 * path components, with parent components at the top of the stack.
567186f7fbEdward Pilatowicz	 * So for example, if we're going to verify the path /foo/bar/bang
568186f7fbEdward Pilatowicz	 * then our stack will look like:
569186f7fbEdward Pilatowicz	 *	foo	(top)
570186f7fbEdward Pilatowicz	 *	bar
571186f7fbEdward Pilatowicz	 *	bang	(bottom)
572186f7fbEdward Pilatowicz	 */
573186f7fbEdward Pilatowicz	while ((p = strrchr(tmp, '/')) != NULL) {
574186f7fbEdward Pilatowicz		*p = '\0';
575186f7fbEdward Pilatowicz		if (pn_push(&pn_stack, &p[1]) != NULL)
576186f7fbEdward Pilatowicz			continue;
577186f7fbEdward Pilatowicz		pn_free(&pn_stack);
578186f7fbEdward Pilatowicz		return (NULL);
579186f7fbEdward Pilatowicz	}
580186f7fbEdward Pilatowicz
581186f7fbEdward Pilatowicz	/* We're going to store the final zone relative path in zpath */
582186f7fbEdward Pilatowicz	*zpath = '\0';
583186f7fbEdward Pilatowicz
584186f7fbEdward Pilatowicz	while (pn_pop(&pn_stack, tmp) != NULL) {
585186f7fbEdward Pilatowicz		/*
586186f7fbEdward Pilatowicz		 * Drop zero length path components (which come from
587186f7fbEdward Pilatowicz		 * consecutive '/'s) and '.' path components.
588186f7fbEdward Pilatowicz		 */
589186f7fbEdward Pilatowicz		if ((strlen(tmp) == 0) || (strcmp(tmp, ".") == 0))
590186f7fbEdward Pilatowicz			continue;
591186f7fbEdward Pilatowicz
592186f7fbEdward Pilatowicz		/*
593186f7fbEdward Pilatowicz		 * Check the current path component for '..', if found
594186f7fbEdward Pilatowicz		 * drop any previous path component.
595186f7fbEdward Pilatowicz		 */
596186f7fbEdward Pilatowicz		if (strcmp(tmp, "..") == 0) {
597186f7fbEdward Pilatowicz			if ((p = strrchr(zpath, '/')) != NULL)
598186f7fbEdward Pilatowicz				*p = '\0';
599186f7fbEdward Pilatowicz			continue;
600186f7fbEdward Pilatowicz		}
601186f7fbEdward Pilatowicz
602186f7fbEdward Pilatowicz		/* The path we want to verify now is zpath + / + tmp. */
603186f7fbEdward Pilatowicz		(void) strlcat(zpath, "/", sizeof (zpath));
604186f7fbEdward Pilatowicz		(void) strlcat(zpath, tmp, sizeof (zpath));
605186f7fbEdward Pilatowicz
606186f7fbEdward Pilatowicz		/*
607186f7fbEdward Pilatowicz		 * Check if this is a native object.  A native object is an
608186f7fbEdward Pilatowicz		 * object from the global zone that is running in a branded
609186f7fbEdward Pilatowicz		 * zone.  These objects are lofs mounted into a zone.  So if a
610186f7fbEdward Pilatowicz		 * branded zone is not booted then lofs mounts won't be setup
611186f7fbEdward Pilatowicz		 * so we won't be able to find these objects.  Luckily, we know
612186f7fbEdward Pilatowicz		 * that they exist in the global zone with the same path sans
613186f7fbEdward Pilatowicz		 * the initial native component, so we'll just strip out the
614186f7fbEdward Pilatowicz		 * native component here.
615186f7fbEdward Pilatowicz		 */
616186f7fbEdward Pilatowicz		if ((strncmp(zpath, "/native", sizeof ("/native")) == 0) ||
617186f7fbEdward Pilatowicz		    (strncmp(zpath, "/.SUNWnative",
618186f7fbEdward Pilatowicz		    sizeof ("/.SUNWnative")) == 0)) {
619186f7fbEdward Pilatowicz
620186f7fbEdward Pilatowicz			/* Free any cached symlink paths */
621186f7fbEdward Pilatowicz			pn_free(&pn_links);
622186f7fbEdward Pilatowicz
623186f7fbEdward Pilatowicz			/* Reconstruct the path from our path component stack */
624186f7fbEdward Pilatowicz			*zpath = '\0';
625186f7fbEdward Pilatowicz			while (pn_pop(&pn_stack, tmp) != NULL) {
626186f7fbEdward Pilatowicz				(void) strlcat(zpath, "/", sizeof (zpath));
627186f7fbEdward Pilatowicz				(void) strlcat(zpath, tmp, sizeof (zpath));
628186f7fbEdward Pilatowicz			}
629186f7fbEdward Pilatowicz
630186f7fbEdward Pilatowicz			/* Verify that the path actually exists */
631186f7fbEdward Pilatowicz			rv = resolvepath(zpath, tmp, sizeof (tmp) - 1);
632186f7fbEdward Pilatowicz			if (rv < 0) {
633186f7fbEdward Pilatowicz				dprintf("Pzonepath invalid native path '%s'\n",
634186f7fbEdward Pilatowicz				    zpath);
635186f7fbEdward Pilatowicz				return (NULL);
636186f7fbEdward Pilatowicz			}
637186f7fbEdward Pilatowicz			tmp[rv] = '\0';
638186f7fbEdward Pilatowicz
639186f7fbEdward Pilatowicz			/* Return the path */
640186f7fbEdward Pilatowicz			dprintf("Pzonepath found native path '%s'\n", tmp);
641186f7fbEdward Pilatowicz			(void) Plofspath(tmp, tmp, sizeof (tmp));
642186f7fbEdward Pilatowicz			(void) strlcpy(s, tmp, n);
643186f7fbEdward Pilatowicz			return (s);
644186f7fbEdward Pilatowicz		}
645186f7fbEdward Pilatowicz
646186f7fbEdward Pilatowicz		/*
647186f7fbEdward Pilatowicz		 * Check if the path points to a symlink.  We do this
648186f7fbEdward Pilatowicz		 * explicitly since any absolute symlink needs to be
649186f7fbEdward Pilatowicz		 * interpreted relativly to the zone root and not "/".
650186f7fbEdward Pilatowicz		 */
651186f7fbEdward Pilatowicz		(void) strlcpy(tmp, zroot, sizeof (tmp));
652186f7fbEdward Pilatowicz		(void) strlcat(tmp, zpath, sizeof (tmp));
653186f7fbEdward Pilatowicz		if (lstat64(tmp, &sb) != 0) {
654186f7fbEdward Pilatowicz			pn_free2(&pn_stack, &pn_links);
655186f7fbEdward Pilatowicz			return (NULL);
656186f7fbEdward Pilatowicz		}
657186f7fbEdward Pilatowicz		if (!S_ISLNK(sb.st_mode)) {
658186f7fbEdward Pilatowicz			/*
659186f7fbEdward Pilatowicz			 * Since the lstat64() above succeeded we know that
660186f7fbEdward Pilatowicz			 * zpath exists, since this is not a symlink loop
661186f7fbEdward Pilatowicz			 * around and check the next path component.
662186f7fbEdward Pilatowicz			 */
663186f7fbEdward Pilatowicz			continue;
664186f7fbEdward Pilatowicz		}
665186f7fbEdward Pilatowicz
666186f7fbEdward Pilatowicz		/*
667186f7fbEdward Pilatowicz		 * Symlink allow for paths with loops.  Make sure
668186f7fbEdward Pilatowicz		 * we're not stuck in a loop.
669186f7fbEdward Pilatowicz		 */
670186f7fbEdward Pilatowicz		for (pn = pn_links; pn != NULL; pn = pn->pn_next) {
671186f7fbEdward Pilatowicz			if (strcmp(zpath, pn->pn_path) != 0)
672186f7fbEdward Pilatowicz				continue;
673186f7fbEdward Pilatowicz
674186f7fbEdward Pilatowicz			/* We have a loop.  Fail. */
675186f7fbEdward Pilatowicz			dprintf("Pzonepath symlink loop '%s'\n", zpath);
676186f7fbEdward Pilatowicz			pn_free2(&pn_stack, &pn_links);
677186f7fbEdward Pilatowicz			return (NULL);
678186f7fbEdward Pilatowicz		}
679186f7fbEdward Pilatowicz
680186f7fbEdward Pilatowicz		/* Save this symlink path for future loop checks */
681186f7fbEdward Pilatowicz		if (pn_push(&pn_links, zpath) == NULL) {
682186f7fbEdward Pilatowicz			/* Out of memory */
683186f7fbEdward Pilatowicz			pn_free2(&pn_stack, &pn_links);
684186f7fbEdward Pilatowicz			return (NULL);
685186f7fbEdward Pilatowicz		}
686186f7fbEdward Pilatowicz
687186f7fbEdward Pilatowicz		/* Now follow the contents of the symlink */
688186f7fbEdward Pilatowicz		bzero(link, sizeof (link));
689186f7fbEdward Pilatowicz		if (readlink(tmp, link, sizeof (link)) == -1) {
690186f7fbEdward Pilatowicz			pn_free2(&pn_stack, &pn_links);
691186f7fbEdward Pilatowicz			return (NULL);
692186f7fbEdward Pilatowicz		}
693186f7fbEdward Pilatowicz
694186f7fbEdward Pilatowicz		dprintf("Pzonepath following symlink '%s' -> '%s'\n",
695186f7fbEdward Pilatowicz		    zpath, link);
696186f7fbEdward Pilatowicz
697186f7fbEdward Pilatowicz		/*
698186f7fbEdward Pilatowicz		 * Push each path component of the symlink target onto our
699186f7fbEdward Pilatowicz		 * path components stack since we need to verify each one.
700186f7fbEdward Pilatowicz		 */
701186f7fbEdward Pilatowicz		while ((p = strrchr(link, '/')) != NULL) {
702186f7fbEdward Pilatowicz			*p = '\0';
703186f7fbEdward Pilatowicz			if (pn_push(&pn_stack, &p[1]) != NULL)
704186f7fbEdward Pilatowicz				continue;
705186f7fbEdward Pilatowicz			pn_free2(&pn_stack, &pn_links);
706186f7fbEdward Pilatowicz			return (NULL);
707186f7fbEdward Pilatowicz		}
708186f7fbEdward Pilatowicz
709186f7fbEdward Pilatowicz		/* absolute or relative symlink? */
710186f7fbEdward Pilatowicz		if (*link == '\0') {
711186f7fbEdward Pilatowicz			/* Absolute symlink, nuke existing zpath. */
712186f7fbEdward Pilatowicz			*zpath = '\0';
713186f7fbEdward Pilatowicz			continue;
714186f7fbEdward Pilatowicz		}
715186f7fbEdward Pilatowicz
716186f7fbEdward Pilatowicz		/*
717186f7fbEdward Pilatowicz		 * Relative symlink.  Push the first path component of the
718186f7fbEdward Pilatowicz		 * symlink target onto our stack for verification and then
719186f7fbEdward Pilatowicz		 * remove the current path component from zpath.
720186f7fbEdward Pilatowicz		 */
721186f7fbEdward Pilatowicz		if (pn_push(&pn_stack, link) == NULL) {
722186f7fbEdward Pilatowicz			pn_free2(&pn_stack, &pn_links);
723186f7fbEdward Pilatowicz			return (NULL);
724186f7fbEdward Pilatowicz		}
725186f7fbEdward Pilatowicz		p = strrchr(zpath, '/');
726186f7fbEdward Pilatowicz		assert(p != NULL);
727186f7fbEdward Pilatowicz		*p = '\0';
728186f7fbEdward Pilatowicz		continue;
729186f7fbEdward Pilatowicz	}
730186f7fbEdward Pilatowicz	pn_free(&pn_links);
731186f7fbEdward Pilatowicz
732186f7fbEdward Pilatowicz	/* Place the final result in zpath */
733186f7fbEdward Pilatowicz	(void) strlcpy(tmp, zroot, sizeof (tmp));
734186f7fbEdward Pilatowicz	(void) strlcat(tmp, zpath, sizeof (tmp));
735186f7fbEdward Pilatowicz	(void) strlcpy(zpath, tmp, sizeof (zpath));
736186f7fbEdward Pilatowicz
737186f7fbEdward Pilatowicz	(void) Plofspath(zpath, zpath, sizeof (zpath));
738186f7fbEdward Pilatowicz	dprintf("Pzonepath found zone path (3) '%s'\n", zpath);
739186f7fbEdward Pilatowicz
740186f7fbEdward Pilatowicz	(void) strlcpy(s, zpath, n);
741186f7fbEdward Pilatowicz	return (s);
742186f7fbEdward Pilatowicz}
743186f7fbEdward Pilatowicz
744186f7fbEdward Pilatowiczchar *
745186f7fbEdward PilatowiczPfindobj(struct ps_prochandle *P, const char *path, char *s, size_t n)
746186f7fbEdward Pilatowicz{
747186f7fbEdward Pilatowicz	int len;
748186f7fbEdward Pilatowicz
749186f7fbEdward Pilatowicz	dprintf("Pfindobj '%s'\n", path);
750186f7fbEdward Pilatowicz
751186f7fbEdward Pilatowicz	/* We only deal with absolute paths */
752186f7fbEdward Pilatowicz	if (path[0] != '/')
753186f7fbEdward Pilatowicz		return (NULL);
754186f7fbEdward Pilatowicz
755186f7fbEdward Pilatowicz	/* First try to resolve the path to some zone */
756186f7fbEdward Pilatowicz	if (Pzonepath(P, path, s, n) != NULL)
757186f7fbEdward Pilatowicz		return (s);
758186f7fbEdward Pilatowicz
759186f7fbEdward Pilatowicz	/* If that fails resolve any lofs links in the path */
760186f7fbEdward Pilatowicz	if (Plofspath(path, s, n) != NULL)
761186f7fbEdward Pilatowicz		return (s);
762186f7fbEdward Pilatowicz
763186f7fbEdward Pilatowicz	/* If that fails then just see if the path exists */
764186f7fbEdward Pilatowicz	if ((len = resolvepath(path, s, n)) > 0) {
765186f7fbEdward Pilatowicz		s[len] = '\0';
766186f7fbEdward Pilatowicz		return (s);
767186f7fbEdward Pilatowicz	}
768186f7fbEdward Pilatowicz
769186f7fbEdward Pilatowicz	return (NULL);
770186f7fbEdward Pilatowicz}
771186f7fbEdward Pilatowicz
772186f7fbEdward Pilatowiczchar *
773186f7fbEdward PilatowiczPfindmap(struct ps_prochandle *P, map_info_t *mptr, char *s, size_t n)
774186f7fbEdward Pilatowicz{
775186f7fbEdward Pilatowicz	file_info_t *fptr = mptr->map_file;
776186f7fbEdward Pilatowicz	char buf[PATH_MAX];
777186f7fbEdward Pilatowicz	int len;
778186f7fbEdward Pilatowicz
779186f7fbEdward Pilatowicz	/* If it's already been explicity set return that */
780186f7fbEdward Pilatowicz	if ((fptr != NULL) && (fptr->file_rname != NULL)) {
781186f7fbEdward Pilatowicz		(void) strlcpy(s, fptr->file_rname, n);
782186f7fbEdward Pilatowicz		return (s);
783186f7fbEdward Pilatowicz	}
784186f7fbEdward Pilatowicz
785186f7fbEdward Pilatowicz	/* If it's the a.out segment, defer to the magical Pexecname() */
786186f7fbEdward Pilatowicz	if ((P->map_exec == mptr) ||
787186f7fbEdward Pilatowicz	    (strcmp(mptr->map_pmap.pr_mapname, "a.out") == 0) ||
788186f7fbEdward Pilatowicz	    ((fptr != NULL) && (fptr->file_lname != NULL) &&
789186f7fbEdward Pilatowicz	    (strcmp(fptr->file_lname, "a.out") == 0))) {
7908f68126Bryan Cantrill		if (Pexecname(P, buf, sizeof (buf)) != NULL) {
7918f68126Bryan Cantrill			(void) strlcpy(s, buf, n);
7928f68126Bryan Cantrill			return (s);
7938f68126Bryan Cantrill		}
794186f7fbEdward Pilatowicz	}
795186f7fbEdward Pilatowicz
796186f7fbEdward Pilatowicz	/* Try /proc first to get the real object name */
797186f7fbEdward Pilatowicz	if ((Pstate(P) != PS_DEAD) && (mptr->map_pmap.pr_mapname[0] != '\0')) {
79869d9b1cToomas Soome		char path[PATH_MAX];
79969d9b1cToomas Soome
80069d9b1cToomas Soome		len = snprintf(path, sizeof (path), "%s/%d/path/%s",
801186f7fbEdward Pilatowicz		    procfs_path, (int)P->pid, mptr->map_pmap.pr_mapname);
80269d9b1cToomas Soome		if (len < 0 || (size_t)len >= sizeof (path))
80369d9b1cToomas Soome			return (NULL);
80469d9b1cToomas Soome
80569d9b1cToomas Soome		if ((len = readlink(path, buf, sizeof (buf))) > 0) {
806186f7fbEdward Pilatowicz			buf[len] = '\0';
807186f7fbEdward Pilatowicz			(void) Plofspath(buf, buf, sizeof (buf));
808186f7fbEdward Pilatowicz			(void) strlcpy(s, buf, n);
809186f7fbEdward Pilatowicz			return (s);
810186f7fbEdward Pilatowicz		}
811186f7fbEdward Pilatowicz	}
812186f7fbEdward Pilatowicz
813186f7fbEdward Pilatowicz	/*
814186f7fbEdward Pilatowicz	 * If we couldn't get the name from /proc, take the lname and
815186f7fbEdward Pilatowicz	 * try to expand it on the current system to a real object path.
816186f7fbEdward Pilatowicz	 */
817186f7fbEdward Pilatowicz	fptr = mptr->map_file;
818186f7fbEdward Pilatowicz	if ((fptr != NULL) && (fptr->file_lname != NULL)) {
819186f7fbEdward Pilatowicz		(void) strlcpy(buf, fptr->file_lname, sizeof (buf));
820186f7fbEdward Pilatowicz		if (Pfindobj(P, buf, buf, sizeof (buf)) == NULL)
821186f7fbEdward Pilatowicz			return (NULL);
822186f7fbEdward Pilatowicz		(void) strlcpy(s, buf, n);
823186f7fbEdward Pilatowicz		return (s);
824186f7fbEdward Pilatowicz	}
825186f7fbEdward Pilatowicz
826186f7fbEdward Pilatowicz	return (NULL);
827186f7fbEdward Pilatowicz}