xref: /illumos-gate/usr/src/lib/libdevinfo/devinfo_realpath.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
1*ff2aee48Scth /*
2*ff2aee48Scth  * CDDL HEADER START
3*ff2aee48Scth  *
4*ff2aee48Scth  * The contents of this file are subject to the terms of the
5*ff2aee48Scth  * Common Development and Distribution License (the "License").
6*ff2aee48Scth  * You may not use this file except in compliance with the License.
7*ff2aee48Scth  *
8*ff2aee48Scth  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*ff2aee48Scth  * or http://www.opensolaris.org/os/licensing.
10*ff2aee48Scth  * See the License for the specific language governing permissions
11*ff2aee48Scth  * and limitations under the License.
12*ff2aee48Scth  *
13*ff2aee48Scth  * When distributing Covered Code, include this CDDL HEADER in each
14*ff2aee48Scth  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*ff2aee48Scth  * If applicable, add the following below this CDDL HEADER, with the
16*ff2aee48Scth  * fields enclosed by brackets "[]" replaced with your own identifying
17*ff2aee48Scth  * information: Portions Copyright [yyyy] [name of copyright owner]
18*ff2aee48Scth  *
19*ff2aee48Scth  * CDDL HEADER END
20*ff2aee48Scth  */
21*ff2aee48Scth /*
22*ff2aee48Scth  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*ff2aee48Scth  * Use is subject to license terms.
24*ff2aee48Scth  */
25*ff2aee48Scth 
26*ff2aee48Scth /*
27*ff2aee48Scth  * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
28*ff2aee48Scth  *
29*ff2aee48Scth  * Redistribution and use in source and binary forms, with or without
30*ff2aee48Scth  * modification, are permitted provided that the following conditions
31*ff2aee48Scth  * are met:
32*ff2aee48Scth  * 1. Redistributions of source code must retain the above copyright
33*ff2aee48Scth  *    notice, this list of conditions and the following disclaimer.
34*ff2aee48Scth  * 2. Redistributions in binary form must reproduce the above copyright
35*ff2aee48Scth  *    notice, this list of conditions and the following disclaimer in the
36*ff2aee48Scth  *    documentation and/or other materials provided with the distribution.
37*ff2aee48Scth  * 3. The names of the authors may not be used to endorse or promote
38*ff2aee48Scth  *    products derived from this software without specific prior written
39*ff2aee48Scth  *    permission.
40*ff2aee48Scth  *
41*ff2aee48Scth  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
42*ff2aee48Scth  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*ff2aee48Scth  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*ff2aee48Scth  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*ff2aee48Scth  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*ff2aee48Scth  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*ff2aee48Scth  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*ff2aee48Scth  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*ff2aee48Scth  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*ff2aee48Scth  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*ff2aee48Scth  * SUCH DAMAGE.
52*ff2aee48Scth  */
53*ff2aee48Scth /*
54*ff2aee48Scth  * http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdlib/realpath.c
55*ff2aee48Scth  * $OpenBSD: realpath.c,v 1.13 2005/08/08 08:05:37 espie Exp $
56*ff2aee48Scth  */
57*ff2aee48Scth 
58*ff2aee48Scth #include <stdio.h>
59*ff2aee48Scth #include <unistd.h>
60*ff2aee48Scth #include <string.h>
61*ff2aee48Scth #include <limits.h>
62*ff2aee48Scth #include <errno.h>
63*ff2aee48Scth #include <sys/types.h>
64*ff2aee48Scth #include <sys/stat.h>
65*ff2aee48Scth #include <sys/param.h>
66*ff2aee48Scth 
67*ff2aee48Scth /*
68*ff2aee48Scth  * char *s_realpath(const char *path, char resolved_path[MAXPATHLEN]);
69*ff2aee48Scth  *
70*ff2aee48Scth  * Find the real name of path, by removing all ".", ".." and symlink
71*ff2aee48Scth  * components.  Returns (resolved) on success, or (NULL) on failure,
72*ff2aee48Scth  * in which case the path which caused trouble is left in (resolved).
73*ff2aee48Scth  *
74*ff2aee48Scth  * DEVINFO: For libdevinfo we have added code to special case symlinks into
75*ff2aee48Scth  * /devices - the path below that point is known to not point to any
76*ff2aee48Scth  * additional symlinks.  This knowledge allows us to avoid causing attach.
77*ff2aee48Scth  */
78*ff2aee48Scth char *
s_realpath(const char * path,char * resolved)79*ff2aee48Scth s_realpath(const char *path, char *resolved)
80*ff2aee48Scth {
81*ff2aee48Scth 	struct stat sb;
82*ff2aee48Scth 	char *p, *q, *s;
83*ff2aee48Scth 	size_t left_len, resolved_len;
84*ff2aee48Scth 	unsigned symlinks;
85*ff2aee48Scth 	int serrno, slen;
86*ff2aee48Scth 	char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
87*ff2aee48Scth 
88*ff2aee48Scth 	serrno = errno;
89*ff2aee48Scth 	symlinks = 0;
90*ff2aee48Scth 	if (path[0] == '/') {
91*ff2aee48Scth 		resolved[0] = '/';
92*ff2aee48Scth 		resolved[1] = '\0';
93*ff2aee48Scth 		if (path[1] == '\0')
94*ff2aee48Scth 			return (resolved);
95*ff2aee48Scth 		resolved_len = 1;
96*ff2aee48Scth 		left_len = strlcpy(left, path + 1, sizeof (left));
97*ff2aee48Scth 	} else {
98*ff2aee48Scth 		if (getcwd(resolved, PATH_MAX) == NULL) {
99*ff2aee48Scth 			(void) strlcpy(resolved, ".", PATH_MAX);
100*ff2aee48Scth 			return (NULL);
101*ff2aee48Scth 		}
102*ff2aee48Scth 		resolved_len = strlen(resolved);
103*ff2aee48Scth 		left_len = strlcpy(left, path, sizeof (left));
104*ff2aee48Scth 	}
105*ff2aee48Scth 	if (left_len >= sizeof (left) || resolved_len >= PATH_MAX) {
106*ff2aee48Scth 		errno = ENAMETOOLONG;
107*ff2aee48Scth 		return (NULL);
108*ff2aee48Scth 	}
109*ff2aee48Scth 
110*ff2aee48Scth 	/*
111*ff2aee48Scth 	 * Iterate over path components in `left'.
112*ff2aee48Scth 	 */
113*ff2aee48Scth 	while (left_len != 0) {
114*ff2aee48Scth 		/*
115*ff2aee48Scth 		 * Extract the next path component and adjust `left'
116*ff2aee48Scth 		 * and its length.
117*ff2aee48Scth 		 */
118*ff2aee48Scth 		p = strchr(left, '/');
119*ff2aee48Scth 		s = p ? p : left + left_len;
120*ff2aee48Scth 		if (s - left >= sizeof (next_token)) {
121*ff2aee48Scth 			errno = ENAMETOOLONG;
122*ff2aee48Scth 			return (NULL);
123*ff2aee48Scth 		}
124*ff2aee48Scth 		(void) memcpy(next_token, left, s - left);
125*ff2aee48Scth 		next_token[s - left] = '\0';
126*ff2aee48Scth 		left_len -= s - left;
127*ff2aee48Scth 		if (p != NULL)
128*ff2aee48Scth 			(void) memmove(left, s + 1, left_len + 1);
129*ff2aee48Scth 		if (resolved[resolved_len - 1] != '/') {
130*ff2aee48Scth 			if (resolved_len + 1 >= PATH_MAX) {
131*ff2aee48Scth 				errno = ENAMETOOLONG;
132*ff2aee48Scth 				return (NULL);
133*ff2aee48Scth 			}
134*ff2aee48Scth 			resolved[resolved_len++] = '/';
135*ff2aee48Scth 			resolved[resolved_len] = '\0';
136*ff2aee48Scth 		}
137*ff2aee48Scth 		if (next_token[0] == '\0')
138*ff2aee48Scth 			continue;
139*ff2aee48Scth 		else if (strcmp(next_token, ".") == 0)
140*ff2aee48Scth 			continue;
141*ff2aee48Scth 		else if (strcmp(next_token, "..") == 0) {
142*ff2aee48Scth 			/*
143*ff2aee48Scth 			 * Strip the last path component except when we have
144*ff2aee48Scth 			 * single "/"
145*ff2aee48Scth 			 */
146*ff2aee48Scth 			if (resolved_len > 1) {
147*ff2aee48Scth 				resolved[resolved_len - 1] = '\0';
148*ff2aee48Scth 				q = strrchr(resolved, '/') + 1;
149*ff2aee48Scth 				*q = '\0';
150*ff2aee48Scth 				resolved_len = q - resolved;
151*ff2aee48Scth 			}
152*ff2aee48Scth 			continue;
153*ff2aee48Scth 		}
154*ff2aee48Scth 
155*ff2aee48Scth 		/*
156*ff2aee48Scth 		 * Append the next path component and lstat() it. If
157*ff2aee48Scth 		 * lstat() fails we still can return successfully if
158*ff2aee48Scth 		 * there are no more path components left.
159*ff2aee48Scth 		 */
160*ff2aee48Scth 		resolved_len = strlcat(resolved, next_token, PATH_MAX);
161*ff2aee48Scth 		if (resolved_len >= PATH_MAX) {
162*ff2aee48Scth 			errno = ENAMETOOLONG;
163*ff2aee48Scth 			return (NULL);
164*ff2aee48Scth 		}
165*ff2aee48Scth 
166*ff2aee48Scth 		/*
167*ff2aee48Scth 		 * DEVINFO: Check if link points into /devices and resolve
168*ff2aee48Scth 		 * without causing attach if that is the case - there are no
169*ff2aee48Scth 		 * further symlinks in /devices.
170*ff2aee48Scth 		 */
171*ff2aee48Scth 		if (strcmp(resolved, "/devices") == 0) {
172*ff2aee48Scth 			resolved[resolved_len] = '/';
173*ff2aee48Scth 			resolved_len = strlcat(resolved, left, sizeof (left));
174*ff2aee48Scth 			left_len = 0;
175*ff2aee48Scth 			continue;
176*ff2aee48Scth 		}
177*ff2aee48Scth 
178*ff2aee48Scth 		if (lstat(resolved, &sb) != 0) {
179*ff2aee48Scth 			if (errno == ENOENT && p == NULL) {
180*ff2aee48Scth 				errno = serrno;
181*ff2aee48Scth 				return (resolved);
182*ff2aee48Scth 			}
183*ff2aee48Scth 			return (NULL);
184*ff2aee48Scth 		}
185*ff2aee48Scth 
186*ff2aee48Scth 		if (S_ISLNK(sb.st_mode)) {
187*ff2aee48Scth 			if (symlinks++ > MAXSYMLINKS) {
188*ff2aee48Scth 				errno = ELOOP;
189*ff2aee48Scth 				return (NULL);
190*ff2aee48Scth 			}
191*ff2aee48Scth 			slen = readlink(resolved, symlink,
192*ff2aee48Scth 			    sizeof (symlink) - 1);
193*ff2aee48Scth 			if (slen < 0)
194*ff2aee48Scth 				return (NULL);
195*ff2aee48Scth 			symlink[slen] = '\0';
196*ff2aee48Scth 
197*ff2aee48Scth 			if (symlink[0] == '/') {
198*ff2aee48Scth 				resolved[1] = 0;
199*ff2aee48Scth 				resolved_len = 1;
200*ff2aee48Scth 			} else if (resolved_len > 1) {
201*ff2aee48Scth 				/* Strip the last path component. */
202*ff2aee48Scth 				resolved[resolved_len - 1] = '\0';
203*ff2aee48Scth 				q = strrchr(resolved, '/') + 1;
204*ff2aee48Scth 				*q = '\0';
205*ff2aee48Scth 				resolved_len = q - resolved;
206*ff2aee48Scth 			}
207*ff2aee48Scth 
208*ff2aee48Scth 			/*
209*ff2aee48Scth 			 * If there are any path components left, then
210*ff2aee48Scth 			 * append them to symlink. The result is placed
211*ff2aee48Scth 			 * in `left'.
212*ff2aee48Scth 			 */
213*ff2aee48Scth 			if (p != NULL) {
214*ff2aee48Scth 				if (symlink[slen - 1] != '/') {
215*ff2aee48Scth 					if (slen + 1 >= sizeof (symlink)) {
216*ff2aee48Scth 						errno = ENAMETOOLONG;
217*ff2aee48Scth 						return (NULL);
218*ff2aee48Scth 					}
219*ff2aee48Scth 					symlink[slen] = '/';
220*ff2aee48Scth 					symlink[slen + 1] = 0;
221*ff2aee48Scth 				}
222*ff2aee48Scth 				left_len = strlcat(symlink, left,
223*ff2aee48Scth 				    sizeof (left));
224*ff2aee48Scth 				if (left_len >= sizeof (left)) {
225*ff2aee48Scth 					errno = ENAMETOOLONG;
226*ff2aee48Scth 					return (NULL);
227*ff2aee48Scth 				}
228*ff2aee48Scth 			}
229*ff2aee48Scth 			left_len = strlcpy(left, symlink, sizeof (left));
230*ff2aee48Scth 		}
231*ff2aee48Scth 	}
232*ff2aee48Scth 
233*ff2aee48Scth 	/*
234*ff2aee48Scth 	 * Remove trailing slash except when the resolved pathname
235*ff2aee48Scth 	 * is a single "/".
236*ff2aee48Scth 	 */
237*ff2aee48Scth 	if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
238*ff2aee48Scth 		resolved[resolved_len - 1] = '\0';
239*ff2aee48Scth 	return (resolved);
240*ff2aee48Scth }
241