xref: /titanic_51/usr/src/lib/libproc/common/Pzone.c (revision 3221df98598173bea3b143712532cdd09f4fbd0f)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <assert.h>
28 #include <dlfcn.h>
29 #include <errno.h>
30 #include <libzonecfg.h>
31 #include <link.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <sys/list.h>
35 #include <sys/types.h>
36 #include <sys/mkdev.h>
37 #include <sys/mman.h>
38 #include <sys/mnttab.h>
39 
40 #include "Pcontrol.h"
41 
42 struct path_node {
43 	struct path_node	*pn_next;
44 	char			*pn_path;
45 };
46 typedef struct path_node path_node_t;
47 
48 static path_node_t *
49 pn_push(path_node_t **pnp, char *path)
50 {
51 	path_node_t *pn;
52 
53 	if ((pn = calloc(sizeof (path_node_t), 1)) == NULL)
54 		return (NULL);
55 
56 	if ((pn->pn_path = strdup(path)) == NULL) {
57 		free(pn);
58 		return (NULL);
59 	}
60 	pn->pn_next = *pnp;
61 	return (*pnp = pn);
62 }
63 
64 static void
65 pn_free(path_node_t **pnp)
66 {
67 	path_node_t *pn;
68 
69 	while (*pnp != NULL) {
70 		pn = *pnp;
71 		*pnp = pn->pn_next;
72 		free(pn->pn_path);
73 		free(pn);
74 	}
75 }
76 
77 static void
78 pn_free2(path_node_t **pn1, path_node_t **pn2)
79 {
80 	pn_free(pn1);
81 	pn_free(pn2);
82 }
83 
84 static char *
85 pn_pop(path_node_t **pnp, char *path)
86 {
87 	path_node_t *pn;
88 
89 	if (*pnp == NULL)
90 		return (NULL);
91 
92 	pn = *pnp;
93 	*pnp = pn->pn_next;
94 	pn->pn_next = NULL;
95 
96 	if (path == NULL) {
97 		pn_free(&pn);
98 		return (NULL);
99 	}
100 	(void) strlcpy(path, pn->pn_path, PATH_MAX);
101 	pn_free(&pn);
102 	return (path);
103 }
104 
105 
106 /*
107  * Libzonecfg.so links against libproc, so libproc can't link against
108  * libzonecfg.so.  Also, libzonecfg.so is optional and might not be
109  * installed.  Hence instead of relying on linking to access libzonecfg.so,
110  * we'll try dlopening it here.  This trick is borrowed from
111  * libc`zone_get_id(), see that function for more detailed comments.
112  */
113 static int
114 i_zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz)
115 {
116 	typedef	int (*zone_get_zonepath_t)(char *, char *, size_t);
117 	static zone_get_zonepath_t zone_get_zonepath_fp = NULL;
118 
119 	if (zone_get_zonepath_fp == NULL) {
120 		/* There's no harm in doing this multiple times. */
121 		void *dlhandle = dlopen("libzonecfg.so.1", RTLD_LAZY);
122 		void *sym = (void *)(-1);
123 		if (dlhandle != NULL &&
124 		    (sym = dlsym(dlhandle, "zone_get_zonepath")) == NULL) {
125 			sym = (void *)(-1);
126 			(void) dlclose(dlhandle);
127 		}
128 		zone_get_zonepath_fp = (zone_get_zonepath_t)sym;
129 	}
130 
131 	/* If we've successfully loaded it, call the real function */
132 	if (zone_get_zonepath_fp != (zone_get_zonepath_t)(-1))
133 		return (zone_get_zonepath_fp(zone_name, zonepath, rp_sz));
134 	return (Z_NO_ZONE);
135 }
136 
137 char *
138 Pbrandname(struct ps_prochandle *P, char *buf, size_t buflen)
139 {
140 	long	addr;
141 
142 	if ((addr = Pgetauxval(P, AT_SUN_BRANDNAME)) == -1)
143 		return (NULL);
144 
145 	if (Pread_string(P, buf, buflen, addr) == -1)
146 		return (NULL);
147 
148 	return (buf);
149 }
150 
151 /*
152  * Get the zone name from the core file if we have it; look up the
153  * name based on the zone id if this is a live process.
154  */
155 char *
156 Pzonename(struct ps_prochandle *P, char *s, size_t n)
157 {
158 	if (P->state == PS_IDLE) {
159 		errno = ENODATA;
160 		return (NULL);
161 	}
162 
163 	if (P->state == PS_DEAD) {
164 		if (P->core->core_zonename == NULL) {
165 			errno = ENODATA;
166 			return (NULL);
167 		}
168 		(void) strlcpy(s, P->core->core_zonename, n);
169 	} else {
170 		if (getzonenamebyid(P->status.pr_zoneid, s, n) < 0)
171 			return (NULL);
172 		s[n - 1] = '\0';
173 	}
174 	return (s);
175 }
176 
177 char *
178 Pzoneroot(struct ps_prochandle *P, char *s, size_t n)
179 {
180 	char zname[ZONENAME_MAX], zpath[PATH_MAX], tmp[PATH_MAX];
181 	int rv;
182 
183 	if (P->zoneroot != NULL) {
184 		(void) strlcpy(s, P->zoneroot, n);
185 		return (s);
186 	}
187 
188 	if ((Pzonename(P, zname, sizeof (zname)) == NULL) ||
189 	    (strcmp(zname, GLOBAL_ZONENAME) == 0)) {
190 		if ((P->zoneroot = strdup("")) == NULL) {
191 			errno = ENOMEM;
192 			return (NULL);
193 		}
194 		dprintf("Pzoneroot defaulting to '%s'\n", GLOBAL_ZONENAME);
195 		(void) strlcpy(s, P->zoneroot, n);
196 		return (s);
197 	}
198 
199 	if (i_zone_get_zonepath(zname, zpath, sizeof (zpath)) != Z_OK) {
200 		if ((P->zoneroot = strdup("")) == NULL) {
201 			errno = ENOMEM;
202 			return (NULL);
203 		}
204 		dprintf(
205 		    "Pzoneroot zone not found '%s', defaulting to '%s'\n",
206 		    zname, GLOBAL_ZONENAME);
207 		(void) strlcpy(s, P->zoneroot, n);
208 		return (s);
209 	}
210 	(void) strlcat(zpath, "/root", sizeof (zpath));
211 
212 	if ((rv = resolvepath(zpath, tmp, sizeof (tmp) - 1)) < 0) {
213 		if ((P->zoneroot = strdup("")) == NULL) {
214 			errno = ENOMEM;
215 			return (NULL);
216 		}
217 		dprintf(
218 		    "Pzoneroot can't access '%s:%s', defaulting to '%s'\n",
219 		    zname, zpath, GLOBAL_ZONENAME);
220 		(void) strlcpy(s, P->zoneroot, n);
221 		return (s);
222 	}
223 	tmp[rv] = '\0';
224 	(void) strlcpy(zpath, tmp, sizeof (zpath));
225 
226 	if ((P->zoneroot = strdup(zpath)) == NULL) {
227 		errno = ENOMEM;
228 		return (NULL);
229 	}
230 	dprintf("Pzoneroot found zone root '%s:%s'\n", zname, zpath);
231 	(void) strlcpy(s, P->zoneroot, n);
232 	return (s);
233 }
234 
235 /*
236  * Plofspath() takes a path, "path",  and removes any lofs components from
237  * that path.  The resultant path (if different from the starting path)
238  * is placed in "s", which is limited to "n" characters, and the return
239  * value is the pointer s.  If there are no lofs components in the path
240  * the NULL is returned and s is not modified.  It's ok for "path" and
241  * "s" to be the same pointer.  (ie, the results can be stored directly
242  * in the input buffer.)  The path that is passed in must be an absolute
243  * path.
244  *
245  * Example:
246  *	if "path" == "/foo/bar", and "/candy/" is lofs mounted on "/foo/"
247  *	then "/candy/bar/" will be written into "s" and "s" will be returned.
248  */
249 char *
250 Plofspath(const char *path, char *s, size_t n)
251 {
252 	char tmp[PATH_MAX + 1];
253 	struct mnttab mt, mt_find;
254 	FILE *fp;
255 	char *p, *p2;
256 	int rv;
257 
258 	dprintf("Plofspath path '%s'\n", path);
259 
260 	/* We only deal with absolute paths */
261 	if (path[0] != '/')
262 		return (NULL);
263 
264 	/* Open /etc/mnttab */
265 	if ((fp = fopen(MNTTAB, "r")) == NULL)
266 		return (NULL);
267 
268 	/* Make a copy of the path so that we can muck with it */
269 	(void) strlcpy(tmp, path, sizeof (tmp) - 1);
270 
271 	/*
272 	 * Use resolvepath() to make sure there are no consecutive or
273 	 * trailing '/'s in the path.
274 	 */
275 	if ((rv = resolvepath(tmp, tmp, sizeof (tmp) - 1)) >= 0)
276 		tmp[rv] = '\0';
277 
278 	/*
279 	 * So now we're going to search the path for any components that
280 	 * might be lofs mounts.  We'll start out search from the full
281 	 * path and then step back through each parent directly till
282 	 * we reach the root.  If we find a lofs mount point in the path
283 	 * then we'll replace the initial portion of the path (up
284 	 * to that mount point) with the source of that mount point
285 	 * and then start our search over again.
286 	 *
287 	 * Here's some of the variables we're going to use:
288 	 *
289 	 *	tmp - A pointer to our working copy of the path.  Sometimes
290 	 *		this path will be divided into two strings by a
291 	 *		'\0' (NUL) character.  The first string is the
292 	 *		component we're currently checking and the second
293 	 *		string is the path components we've already checked.
294 	 *
295 	 *	p - A pointer to the last '/' seen in the string.
296 	 *
297 	 *	p[1] - A pointer to the component of the string we've already
298 	 *		checked.
299 	 *
300 	 * Initially, p will point to the end of our path and p[1] will point
301 	 * to an extra '\0' (NUL) that we'll append to the end of the string.
302 	 * (This is why we declared tmp with a size of PATH_MAX + 1).
303 	 */
304 	p = &tmp[strlen(tmp)];
305 	p[1] = '\0';
306 	for (;;) {
307 		/* Check if tmp is a mount point */
308 		rewind(fp);
309 		bzero(&mt_find, sizeof (mt_find));
310 		mt_find.mnt_mountp = tmp;
311 		rv = getmntany(fp, &mt, &mt_find);
312 
313 		/* We only care about lofs mount points */
314 		if ((rv == 0) && (strcmp(mt.mnt_fstype, "lofs") == 0)) {
315 			char tmp2[PATH_MAX + 1];
316 
317 			/*
318 			 * We found a lofs mount.  Update the path that we're
319 			 * checking and start over.  This means append the
320 			 * portion of the path we've already checked to the
321 			 * source of the lofs mount and re-start this entire
322 			 * lofs resolution loop.  Use resolvepath() to make
323 			 * sure there are no consecutive or trailing '/'s
324 			 * in the path.
325 			 */
326 			(void) strlcpy(tmp2, mt.mnt_special, sizeof (tmp2) - 1);
327 			(void) strlcat(tmp2, "/", sizeof (tmp2) - 1);
328 			(void) strlcat(tmp2, &p[1], sizeof (tmp2) - 1);
329 			(void) strlcpy(tmp, tmp2, sizeof (tmp) - 1);
330 			if ((rv = resolvepath(tmp, tmp, sizeof (tmp) - 1)) >= 0)
331 				tmp[rv] = '\0';
332 			p = &tmp[strlen(tmp)];
333 			p[1] = '\0';
334 			continue;
335 		}
336 
337 		/* No lofs mount found */
338 		if ((p2 = strrchr(tmp, '/')) == NULL) {
339 			char tmp2[PATH_MAX];
340 
341 			/*
342 			 * We know that tmp was an absolute path, so if we
343 			 * made it here we know that (p == tmp) and that
344 			 * (*p == '\0').  This means that we've managed
345 			 * to check the whole path and so we're done.
346 			 */
347 			assert(p == tmp);
348 			assert(p[0] == '\0');
349 			(void) fclose(fp);
350 
351 			/* Restore the leading '/' in the path */
352 			p[0] = '/';
353 
354 			if (strcmp(tmp, path) == 0) {
355 				/* The path didn't change */
356 				return (NULL);
357 			}
358 
359 			/*
360 			 * It's possible that lofs source path we just
361 			 * obtained contains a symbolic link.  Use
362 			 * resolvepath() to clean it up.
363 			 */
364 			(void) strlcpy(tmp2, tmp, sizeof (tmp2));
365 			if ((rv = resolvepath(tmp, tmp, sizeof (tmp) - 1)) >= 0)
366 				tmp[rv] = '\0';
367 
368 			/*
369 			 * It's always possible that our lofs source path is
370 			 * actually another lofs mount.  So call ourselves
371 			 * recursively to resolve that path.
372 			 */
373 			(void) Plofspath(tmp, tmp, PATH_MAX);
374 
375 			/* Copy out our final resolved lofs source path */
376 			(void) strlcpy(s, tmp, n);
377 			dprintf("Plofspath path result '%s'\n", s);
378 			return (s);
379 		}
380 
381 		/*
382 		 * So the path we just checked is not a lofs mount.  Next we
383 		 * want to check the parent path component for a lofs mount.
384 		 *
385 		 * First, restore any '/' that we replaced with a '\0' (NUL).
386 		 * We can determine if we should do this by looking at p[1].
387 		 * If p[1] points to a '\0' (NUL) then we know that p points
388 		 * to the end of the string and there is no '/' to restore.
389 		 * if p[1] doesn't point to a '\0' (NUL) then it points to
390 		 * the part of the path that we've already verified so there
391 		 * is a '/' to restore.
392 		 */
393 		if (p[1] != '\0')
394 			p[0] = '/';
395 
396 		/*
397 		 * Second, replace the last '/' in the part of the path
398 		 * that we've already checked with a '\0' (NUL) so that
399 		 * when we loop around we check the parent component of the
400 		 * path.
401 		 */
402 		p2[0] = '\0';
403 		p = p2;
404 	}
405 	/*NOTREACHED*/
406 }
407 
408 /*
409  * Pzonepath() - Way too much code to attempt to derive the full path of
410  * an object within a zone.
411  *
412  * Pzonepath() takes a path and attempts to resolve it relative to the
413  * root associated with the current process handle.  If it fails it will
414  * not update the results string.  It is safe to specify the same pointer
415  * for the file string and the results string.
416  *
417  * Doing this resolution is more difficult than it initially sounds.
418  * We can't simply append the file path to the zone root, because in
419  * a root directory, '..' is treated the same as '.'.  Also, symbolic
420  * links that specify an absolute path need to be interpreted relative
421  * to the zone root.
422  *
423  * It seems like perhaps we could do a chroot(<zone root>) followed by a
424  * resolvepath().  But we can't do this because chroot requires special
425  * privileges and affects the entire process.  Perhaps if there was a
426  * special version of resolvepath() which took an addition root path
427  * we could use that, but this isn't ideal either.  The reason is
428  * that we want to have special handling for native paths.  (A native path
429  * is a path that begins with "/native/" or "/.SUNWnative/".)  Native
430  * paths could be passed explicity to this function or could be embedded
431  * in a symlink that is part of the path passed into this function.
432  * These paths are always lofs mounts of global zone paths, but lofs
433  * mounts only exist when a zone is booted.  So if we were to try to do
434  * a resolvepath() on a native path when the zone wasn't booted the
435  * resolvepath() would fail even though we know that the components
436  * exists in the global zone.
437  *
438  * Given all these constraints, we just implement a path walking function
439  * that resolves a file path relative to a zone root by manually inspecting
440  * each of the path components and verifying its existence.  This means that
441  * we must have access to the zone and that all the components of the
442  * path must exist for this operation to succeed.
443  */
444 char *
445 Pzonepath(struct ps_prochandle *P, const char *path, char *s, size_t n)
446 {
447 	char zroot[PATH_MAX], zpath[PATH_MAX], tmp[PATH_MAX], link[PATH_MAX];
448 	path_node_t *pn_stack = NULL, *pn_links = NULL, *pn;
449 	struct stat64 sb;
450 	char *p;
451 	int i, rv;
452 
453 	dprintf("Pzonepath lookup '%s'\n", path);
454 
455 	/* First lookup the zone root */
456 	if (Pzoneroot(P, zroot, sizeof (zroot)) == NULL)
457 		return (NULL);
458 
459 	/*
460 	 * Make a temporary copy of the path specified.
461 	 * If it's a relative path then make it into an absolute path.
462 	 */
463 	tmp[0] = '\0';
464 	if (path[0] != '/')
465 		(void) strlcat(tmp, "/", sizeof (tmp));
466 	(void) strlcat(tmp, path, sizeof (tmp));
467 
468 	/*
469 	 * If the path that was passed in is the zone root, we're done.
470 	 * If the path that was passed in already contains the zone root
471 	 * then strip the zone root out and verify the rest of the path.
472 	 */
473 	if (strcmp(tmp, zroot) == 0) {
474 		(void) Plofspath(zroot, zroot, sizeof (zroot));
475 		dprintf("Pzonepath found zone path (1) '%s'\n", zroot);
476 		(void) strlcpy(s, zroot, n);
477 		return (s);
478 	}
479 	i = strlen(zroot);
480 	if ((strncmp(tmp, zroot, i) == 0) && (tmp[i] == '/'))
481 		(void) memmove(tmp, tmp + i, strlen(tmp + i) + 1);
482 
483 	/* If no path is passed in, then it maps to the zone root */
484 	if (strlen(tmp) == 0) {
485 		(void) Plofspath(zroot, zroot, sizeof (zroot));
486 		dprintf("Pzonepath found zone path (2) '%s'\n", zroot);
487 		(void) strlcpy(s, zroot, n);
488 		return (s);
489 	}
490 
491 	/*
492 	 * Push each path component that we plan to verify onto a stack of
493 	 * path components, with parent components at the top of the stack.
494 	 * So for example, if we're going to verify the path /foo/bar/bang
495 	 * then our stack will look like:
496 	 *	foo	(top)
497 	 *	bar
498 	 *	bang	(bottom)
499 	 */
500 	while ((p = strrchr(tmp, '/')) != NULL) {
501 		*p = '\0';
502 		if (pn_push(&pn_stack, &p[1]) != NULL)
503 			continue;
504 		pn_free(&pn_stack);
505 		return (NULL);
506 	}
507 
508 	/* We're going to store the final zone relative path in zpath */
509 	*zpath = '\0';
510 
511 	while (pn_pop(&pn_stack, tmp) != NULL) {
512 		/*
513 		 * Drop zero length path components (which come from
514 		 * consecutive '/'s) and '.' path components.
515 		 */
516 		if ((strlen(tmp) == 0) || (strcmp(tmp, ".") == 0))
517 			continue;
518 
519 		/*
520 		 * Check the current path component for '..', if found
521 		 * drop any previous path component.
522 		 */
523 		if (strcmp(tmp, "..") == 0) {
524 			if ((p = strrchr(zpath, '/')) != NULL)
525 				*p = '\0';
526 			continue;
527 		}
528 
529 		/* The path we want to verify now is zpath + / + tmp. */
530 		(void) strlcat(zpath, "/", sizeof (zpath));
531 		(void) strlcat(zpath, tmp, sizeof (zpath));
532 
533 		/*
534 		 * Check if this is a native object.  A native object is an
535 		 * object from the global zone that is running in a branded
536 		 * zone.  These objects are lofs mounted into a zone.  So if a
537 		 * branded zone is not booted then lofs mounts won't be setup
538 		 * so we won't be able to find these objects.  Luckily, we know
539 		 * that they exist in the global zone with the same path sans
540 		 * the initial native component, so we'll just strip out the
541 		 * native component here.
542 		 */
543 		if ((strncmp(zpath, "/native", sizeof ("/native")) == 0) ||
544 		    (strncmp(zpath, "/.SUNWnative",
545 		    sizeof ("/.SUNWnative")) == 0)) {
546 
547 			/* Free any cached symlink paths */
548 			pn_free(&pn_links);
549 
550 			/* Reconstruct the path from our path component stack */
551 			*zpath = '\0';
552 			while (pn_pop(&pn_stack, tmp) != NULL) {
553 				(void) strlcat(zpath, "/", sizeof (zpath));
554 				(void) strlcat(zpath, tmp, sizeof (zpath));
555 			}
556 
557 			/* Verify that the path actually exists */
558 			rv = resolvepath(zpath, tmp, sizeof (tmp) - 1);
559 			if (rv < 0) {
560 				dprintf("Pzonepath invalid native path '%s'\n",
561 				    zpath);
562 				return (NULL);
563 			}
564 			tmp[rv] = '\0';
565 
566 			/* Return the path */
567 			dprintf("Pzonepath found native path '%s'\n", tmp);
568 			(void) Plofspath(tmp, tmp, sizeof (tmp));
569 			(void) strlcpy(s, tmp, n);
570 			return (s);
571 		}
572 
573 		/*
574 		 * Check if the path points to a symlink.  We do this
575 		 * explicitly since any absolute symlink needs to be
576 		 * interpreted relativly to the zone root and not "/".
577 		 */
578 		(void) strlcpy(tmp, zroot, sizeof (tmp));
579 		(void) strlcat(tmp, zpath, sizeof (tmp));
580 		if (lstat64(tmp, &sb) != 0) {
581 			pn_free2(&pn_stack, &pn_links);
582 			return (NULL);
583 		}
584 		if (!S_ISLNK(sb.st_mode)) {
585 			/*
586 			 * Since the lstat64() above succeeded we know that
587 			 * zpath exists, since this is not a symlink loop
588 			 * around and check the next path component.
589 			 */
590 			continue;
591 		}
592 
593 		/*
594 		 * Symlink allow for paths with loops.  Make sure
595 		 * we're not stuck in a loop.
596 		 */
597 		for (pn = pn_links; pn != NULL; pn = pn->pn_next) {
598 			if (strcmp(zpath, pn->pn_path) != 0)
599 				continue;
600 
601 			/* We have a loop.  Fail. */
602 			dprintf("Pzonepath symlink loop '%s'\n", zpath);
603 			pn_free2(&pn_stack, &pn_links);
604 			return (NULL);
605 		}
606 
607 		/* Save this symlink path for future loop checks */
608 		if (pn_push(&pn_links, zpath) == NULL) {
609 			/* Out of memory */
610 			pn_free2(&pn_stack, &pn_links);
611 			return (NULL);
612 		}
613 
614 		/* Now follow the contents of the symlink */
615 		bzero(link, sizeof (link));
616 		if (readlink(tmp, link, sizeof (link)) == -1) {
617 			pn_free2(&pn_stack, &pn_links);
618 			return (NULL);
619 		}
620 
621 		dprintf("Pzonepath following symlink '%s' -> '%s'\n",
622 		    zpath, link);
623 
624 		/*
625 		 * Push each path component of the symlink target onto our
626 		 * path components stack since we need to verify each one.
627 		 */
628 		while ((p = strrchr(link, '/')) != NULL) {
629 			*p = '\0';
630 			if (pn_push(&pn_stack, &p[1]) != NULL)
631 				continue;
632 			pn_free2(&pn_stack, &pn_links);
633 			return (NULL);
634 		}
635 
636 		/* absolute or relative symlink? */
637 		if (*link == '\0') {
638 			/* Absolute symlink, nuke existing zpath. */
639 			*zpath = '\0';
640 			continue;
641 		}
642 
643 		/*
644 		 * Relative symlink.  Push the first path component of the
645 		 * symlink target onto our stack for verification and then
646 		 * remove the current path component from zpath.
647 		 */
648 		if (pn_push(&pn_stack, link) == NULL) {
649 			pn_free2(&pn_stack, &pn_links);
650 			return (NULL);
651 		}
652 		p = strrchr(zpath, '/');
653 		assert(p != NULL);
654 		*p = '\0';
655 		continue;
656 	}
657 	pn_free(&pn_links);
658 
659 	/* Place the final result in zpath */
660 	(void) strlcpy(tmp, zroot, sizeof (tmp));
661 	(void) strlcat(tmp, zpath, sizeof (tmp));
662 	(void) strlcpy(zpath, tmp, sizeof (zpath));
663 
664 	(void) Plofspath(zpath, zpath, sizeof (zpath));
665 	dprintf("Pzonepath found zone path (3) '%s'\n", zpath);
666 
667 	(void) strlcpy(s, zpath, n);
668 	return (s);
669 }
670 
671 char *
672 Pfindobj(struct ps_prochandle *P, const char *path, char *s, size_t n)
673 {
674 	int len;
675 
676 	dprintf("Pfindobj '%s'\n", path);
677 
678 	/* We only deal with absolute paths */
679 	if (path[0] != '/')
680 		return (NULL);
681 
682 	/* First try to resolve the path to some zone */
683 	if (Pzonepath(P, path, s, n) != NULL)
684 		return (s);
685 
686 	/* If that fails resolve any lofs links in the path */
687 	if (Plofspath(path, s, n) != NULL)
688 		return (s);
689 
690 	/* If that fails then just see if the path exists */
691 	if ((len = resolvepath(path, s, n)) > 0) {
692 		s[len] = '\0';
693 		return (s);
694 	}
695 
696 	return (NULL);
697 }
698 
699 char *
700 Pfindmap(struct ps_prochandle *P, map_info_t *mptr, char *s, size_t n)
701 {
702 	file_info_t *fptr = mptr->map_file;
703 	char buf[PATH_MAX];
704 	int len;
705 
706 	/* If it's already been explicity set return that */
707 	if ((fptr != NULL) && (fptr->file_rname != NULL)) {
708 		(void) strlcpy(s, fptr->file_rname, n);
709 		return (s);
710 	}
711 
712 	/* If it's the a.out segment, defer to the magical Pexecname() */
713 	if ((P->map_exec == mptr) ||
714 	    (strcmp(mptr->map_pmap.pr_mapname, "a.out") == 0) ||
715 	    ((fptr != NULL) && (fptr->file_lname != NULL) &&
716 	    (strcmp(fptr->file_lname, "a.out") == 0))) {
717 		(void) Pexecname(P, buf, sizeof (buf));
718 		(void) strlcpy(s, buf, n);
719 		return (s);
720 	}
721 
722 	/* Try /proc first to get the real object name */
723 	if ((Pstate(P) != PS_DEAD) && (mptr->map_pmap.pr_mapname[0] != '\0')) {
724 		(void) snprintf(buf, sizeof (buf), "%s/%d/path/%s",
725 		    procfs_path, (int)P->pid, mptr->map_pmap.pr_mapname);
726 		if ((len = readlink(buf, buf, sizeof (buf))) > 0) {
727 			buf[len] = '\0';
728 			(void) Plofspath(buf, buf, sizeof (buf));
729 			(void) strlcpy(s, buf, n);
730 			return (s);
731 		}
732 	}
733 
734 	/*
735 	 * If we couldn't get the name from /proc, take the lname and
736 	 * try to expand it on the current system to a real object path.
737 	 */
738 	fptr = mptr->map_file;
739 	if ((fptr != NULL) && (fptr->file_lname != NULL)) {
740 		(void) strlcpy(buf, fptr->file_lname, sizeof (buf));
741 		if (Pfindobj(P, buf, buf, sizeof (buf)) == NULL)
742 			return (NULL);
743 		(void) strlcpy(s, buf, n);
744 		return (s);
745 	}
746 
747 	return (NULL);
748 }
749