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