xref: /freebsd/lib/libc/gen/getcwd.c (revision 676041c41ba587285bb934aa2fca290ea7208038)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1991, 1993, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "namespace.h"
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 
36 #include <dirent.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <ssp/ssp.h>
44 #include "un-namespace.h"
45 
46 #include "gen-private.h"
47 
48 #define	ISDOT(dp) \
49 	(dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
50 	    (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
51 
52 extern int __getcwd(char *, size_t);
53 
54 char *
55 __ssp_real(getcwd)(char *pt, size_t size)
56 {
57 	struct dirent *dp;
58 	DIR *dir = NULL;
59 	dev_t dev;
60 	ino_t ino;
61 	int first;
62 	char *bpt;
63 	struct stat s;
64 	dev_t root_dev;
65 	ino_t root_ino;
66 	size_t ptsize;
67 	int save_errno;
68 	char *ept, c;
69 	int fd;
70 
71 	/*
72 	 * If no buffer specified by the user, allocate one as necessary.
73 	 * If a buffer is specified, the size has to be non-zero.  The path
74 	 * is built from the end of the buffer backwards.
75 	 */
76 	if (pt) {
77 		ptsize = 0;
78 		if (!size) {
79 			errno = EINVAL;
80 			return (NULL);
81 		}
82 		if (size == 1) {
83 			errno = ERANGE;
84 			return (NULL);
85 		}
86 		ept = pt + size;
87 	} else {
88 		if ((pt = malloc(ptsize = PATH_MAX)) == NULL)
89 			return (NULL);
90 		ept = pt + ptsize;
91 	}
92 	if (__getcwd(pt, ept - pt) == 0) {
93 		if (*pt != '/') {
94 			bpt = pt;
95 			ept = pt + strlen(pt) - 1;
96 			while (bpt < ept) {
97 				c = *bpt;
98 				*bpt++ = *ept;
99 				*ept-- = c;
100 			}
101 		}
102 		return (pt);
103 	}
104 	bpt = ept - 1;
105 	*bpt = '\0';
106 
107 	/* Save root values, so know when to stop. */
108 	if (stat("/", &s))
109 		goto err;
110 	root_dev = s.st_dev;
111 	root_ino = s.st_ino;
112 
113 	errno = 0;			/* XXX readdir has no error return. */
114 
115 	for (first = 1;; first = 0) {
116 		/* Stat the current level. */
117 		if (dir != NULL ? _fstat(_dirfd(dir), &s) : lstat(".", &s))
118 			goto err;
119 
120 		/* Save current node values. */
121 		ino = s.st_ino;
122 		dev = s.st_dev;
123 
124 		/* Check for reaching root. */
125 		if (root_dev == dev && root_ino == ino) {
126 			*--bpt = '/';
127 			/*
128 			 * It's unclear that it's a requirement to copy the
129 			 * path to the beginning of the buffer, but it's always
130 			 * been that way and stuff would probably break.
131 			 */
132 			bcopy(bpt, pt, ept - bpt);
133 			if (dir)
134 				(void) closedir(dir);
135 			return (pt);
136 		}
137 
138 		/* Open and stat parent directory. */
139 		fd = _openat(dir != NULL ? _dirfd(dir) : AT_FDCWD,
140 				"..", O_RDONLY | O_CLOEXEC);
141 		if (fd == -1)
142 			goto err;
143 		if (dir)
144 			(void) closedir(dir);
145 		if (!(dir = fdopendir(fd)) || _fstat(_dirfd(dir), &s)) {
146 			_close(fd);
147 			goto err;
148 		}
149 
150 		/*
151 		 * If it's a mount point, have to stat each element because
152 		 * the inode number in the directory is for the entry in the
153 		 * parent directory, not the inode number of the mounted file.
154 		 */
155 		save_errno = 0;
156 		if (s.st_dev == dev) {
157 			for (;;) {
158 				if (!(dp = readdir(dir)))
159 					goto notfound;
160 				if (dp->d_fileno == ino)
161 					break;
162 			}
163 		} else
164 			for (;;) {
165 				if (!(dp = readdir(dir)))
166 					goto notfound;
167 				if (ISDOT(dp))
168 					continue;
169 
170 				/* Save the first error for later. */
171 				if (fstatat(_dirfd(dir), dp->d_name, &s,
172 				    AT_SYMLINK_NOFOLLOW)) {
173 					if (!save_errno)
174 						save_errno = errno;
175 					errno = 0;
176 					continue;
177 				}
178 				if (s.st_dev == dev && s.st_ino == ino)
179 					break;
180 			}
181 
182 		/*
183 		 * Check for length of the current name, preceding slash,
184 		 * leading slash.
185 		 */
186 		while (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
187 			size_t len, off;
188 
189 			if (!ptsize) {
190 				errno = ERANGE;
191 				goto err;
192 			}
193 			off = bpt - pt;
194 			len = ept - bpt;
195 			if ((pt = reallocf(pt, ptsize *= 2)) == NULL)
196 				goto err;
197 			bpt = pt + off;
198 			ept = pt + ptsize;
199 			bcopy(bpt, ept - len, len);
200 			bpt = ept - len;
201 		}
202 		if (!first)
203 			*--bpt = '/';
204 		bpt -= dp->d_namlen;
205 		bcopy(dp->d_name, bpt, dp->d_namlen);
206 	}
207 
208 notfound:
209 	/*
210 	 * If readdir set errno, use it, not any saved error; otherwise,
211 	 * didn't find the current directory in its parent directory, set
212 	 * errno to ENOENT.
213 	 */
214 	if (!errno)
215 		errno = save_errno ? save_errno : ENOENT;
216 	/* FALLTHROUGH */
217 err:
218 	save_errno = errno;
219 
220 	if (ptsize)
221 		free(pt);
222 	if (dir)
223 		(void) closedir(dir);
224 
225 	errno = save_errno;
226 	return (NULL);
227 }
228