| 1 |
/* |
| 2 |
libfakechroot -- fake chroot environment |
| 3 |
Copyright (c) 2013-2015 Piotr Roszatycki <[email protected]> |
| 4 |
|
| 5 |
This library is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU Lesser General Public |
| 7 |
License as published by the Free Software Foundation; either |
| 8 |
version 2.1 of the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This library is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public |
| 16 |
License along with this library; if not, write to the Free Software |
| 17 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
|
| 23 |
#define _BSD_SOURCE |
| 24 |
#define _GNU_SOURCE |
| 25 |
#define _DEFAULT_SOURCE |
| 26 |
|
| 27 |
#ifdef HAVE___LXSTAT64 |
| 28 |
# define _LARGEFILE64_SOURCE |
| 29 |
#endif |
| 30 |
|
| 31 |
#ifdef HAVE_SYS_SYSCALL_H |
| 32 |
#include <sys/syscall.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
|
| 36 |
#ifdef SYS_getcwd |
| 37 |
|
| 38 |
/* |
| 39 |
* Copyright (C) 2000-2006 Erik Andersen <[email protected]> |
| 40 |
* |
| 41 |
* Licensed under the LGPL v2.1. |
| 42 |
*/ |
| 43 |
|
| 44 |
#include <errno.h> |
| 45 |
|
| 46 |
#include <sys/types.h> |
| 47 |
#include <unistd.h> |
| 48 |
#include <stdlib.h> |
| 49 |
|
| 50 |
#include "libfakechroot.h" |
| 51 |
|
| 52 |
#define MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 53 |
|
| 54 |
LOCAL char * getcwd_real(char *buf, size_t size) |
| 55 |
{ |
| 56 |
int ret; |
| 57 |
char *path; |
| 58 |
size_t alloc_size = size; |
| 59 |
|
| 60 |
if (size == 0) { |
| 61 |
if (buf != NULL) { |
| 62 |
__set_errno(EINVAL); |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
alloc_size = MAX (PATH_MAX, getpagesize ()); |
| 66 |
} |
| 67 |
path=buf; |
| 68 |
if (buf == NULL) { |
| 69 |
path = malloc(alloc_size); |
| 70 |
if (path == NULL) |
| 71 |
return NULL; |
| 72 |
} |
| 73 |
ret = syscall(SYS_getcwd, path, alloc_size); |
| 74 |
if (ret >= 0) |
| 75 |
{ |
| 76 |
if (buf == NULL && size == 0) |
| 77 |
buf = realloc(path, ret); |
| 78 |
if (buf == NULL) |
| 79 |
buf = path; |
| 80 |
return buf; |
| 81 |
} |
| 82 |
if (buf == NULL) |
| 83 |
free (path); |
| 84 |
return NULL; |
| 85 |
} |
| 86 |
|
| 87 |
#else |
| 88 |
|
| 89 |
/* $OpenBSD: getcwd.c,v 1.14 2005/08/08 08:05:34 espie Exp $ */ |
| 90 |
/* |
| 91 |
* Copyright (c) 1989, 1991, 1993 |
| 92 |
* The Regents of the University of California. All rights reserved. |
| 93 |
* |
| 94 |
* Redistribution and use in source and binary forms, with or without |
| 95 |
* modification, are permitted provided that the following conditions |
| 96 |
* are met: |
| 97 |
* 1. Redistributions of source code must retain the above copyright |
| 98 |
* notice, this list of conditions and the following disclaimer. |
| 99 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 100 |
* notice, this list of conditions and the following disclaimer in the |
| 101 |
* documentation and/or other materials provided with the distribution. |
| 102 |
* 3. Neither the name of the University nor the names of its contributors |
| 103 |
* may be used to endorse or promote products derived from this software |
| 104 |
* without specific prior written permission. |
| 105 |
* |
| 106 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 107 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 108 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 109 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 110 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 111 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 112 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 113 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 114 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 115 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 116 |
* SUCH DAMAGE. |
| 117 |
*/ |
| 118 |
|
| 119 |
#include <errno.h> |
| 120 |
|
| 121 |
#ifdef HAVE___LXSTAT64 |
| 122 |
# ifndef _LARGEFILE64_SOURCE |
| 123 |
# define _LARGEFILE64_SOURCE |
| 124 |
# endif |
| 125 |
#endif |
| 126 |
|
| 127 |
#include <sys/param.h> |
| 128 |
#include <sys/stat.h> |
| 129 |
|
| 130 |
#include <dirent.h> |
| 131 |
#include <stdio.h> |
| 132 |
#include <stdlib.h> |
| 133 |
#include <string.h> |
| 134 |
#include <unistd.h> |
| 135 |
|
| 136 |
#include "libfakechroot.h" |
| 137 |
|
| 138 |
#ifdef HAVE___LXSTAT64 |
| 139 |
# include "__lxstat64.h" |
| 140 |
# define STAT_T stat64 |
| 141 |
# define FSTAT fstat64 |
| 142 |
# define LSTAT(path, buf) nextcall(__lxstat64)(_STAT_VER, path, buf) |
| 143 |
#else |
| 144 |
# include "lstat.h" |
| 145 |
# define STAT_T stat |
| 146 |
# define FSTAT fstat |
| 147 |
# define LSTAT(path, buf) nextcall(lstat)(path, buf) |
| 148 |
#endif |
| 149 |
|
| 150 |
#if !defined(OPENDIR_CALLS___OPEN) && !defined(OPENDIR_CALLS___OPENDIR2) |
| 151 |
# include "opendir.h" |
| 152 |
# define OPENDIR(path) nextcall(opendir)(path) |
| 153 |
#else |
| 154 |
# define OPENDIR(path) opendir(path) |
| 155 |
#endif |
| 156 |
|
| 157 |
#undef MAXPATHLEN |
| 158 |
#define MAXPATHLEN FAKECHROOT_PATH_MAX |
| 159 |
|
| 160 |
#ifdef HAVE_STRUCT_DIRENT_D_NAMLEN |
| 161 |
# define NAMLEN(d) ((d)->d_namlen) |
| 162 |
#else |
| 163 |
# define NAMLEN(d) (strlen ((d)->d_name)) |
| 164 |
#endif |
| 165 |
|
| 166 |
#define ISDOT(dp) \ |
| 167 |
(dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \ |
| 168 |
(dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) |
| 169 |
|
| 170 |
LOCAL char * getcwd_real(char *pt, size_t size) |
| 171 |
{ |
| 172 |
struct dirent *dp; |
| 173 |
DIR *dir = NULL; |
| 174 |
dev_t dev; |
| 175 |
ino_t ino; |
| 176 |
int first; |
| 177 |
char *bpt, *bup; |
| 178 |
struct STAT_T s; |
| 179 |
dev_t root_dev; |
| 180 |
ino_t root_ino; |
| 181 |
size_t ptsize, upsize; |
| 182 |
int save_errno; |
| 183 |
char *ept, *eup, *up; |
| 184 |
|
| 185 |
debug("getcwd_real(&pt, %d)", size); |
| 186 |
|
| 187 |
/* |
| 188 |
* If no buffer specified by the user, allocate one as necessary. |
| 189 |
* If a buffer is specified, the size has to be non-zero. The path |
| 190 |
* is built from the end of the buffer backwards. |
| 191 |
*/ |
| 192 |
if (pt) { |
| 193 |
ptsize = 0; |
| 194 |
if (!size) { |
| 195 |
errno = EINVAL; |
| 196 |
return (NULL); |
| 197 |
} |
| 198 |
ept = pt + size; |
| 199 |
} else { |
| 200 |
if ((pt = malloc(ptsize = MAXPATHLEN)) == NULL) |
| 201 |
debug("getcwd_real(NULL, %d)", pt, size); |
| 202 |
return (NULL); |
| 203 |
ept = pt + ptsize; |
| 204 |
} |
| 205 |
bpt = ept - 1; |
| 206 |
*bpt = '\0'; |
| 207 |
|
| 208 |
/* |
| 209 |
* Allocate bytes for the string of "../"'s. |
| 210 |
* Should always be enough (it's 340 levels). If it's not, allocate |
| 211 |
* as necessary. Special * case the first stat, it's ".", not "..". |
| 212 |
*/ |
| 213 |
if ((up = malloc(upsize = MAXPATHLEN)) == NULL) |
| 214 |
goto err; |
| 215 |
eup = up + upsize; |
| 216 |
bup = up; |
| 217 |
up[0] = '.'; |
| 218 |
up[1] = '\0'; |
| 219 |
|
| 220 |
/* Save root values, so know when to stop. */ |
| 221 |
if (LSTAT("/", &s)) |
| 222 |
goto err; |
| 223 |
root_dev = s.st_dev; |
| 224 |
root_ino = s.st_ino; |
| 225 |
|
| 226 |
errno = 0; /* XXX readdir has no error return. */ |
| 227 |
|
| 228 |
for (first = 1;; first = 0) { |
| 229 |
/* Stat the current level. */ |
| 230 |
if (LSTAT(up, &s)) |
| 231 |
goto err; |
| 232 |
|
| 233 |
/* Save current node values. */ |
| 234 |
ino = s.st_ino; |
| 235 |
dev = s.st_dev; |
| 236 |
/* Check for reaching root. */ |
| 237 |
if (root_dev == dev && root_ino == ino) { |
| 238 |
*--bpt = '/'; |
| 239 |
/* |
| 240 |
* It's unclear that it's a requirement to copy the |
| 241 |
* path to the beginning of the buffer, but it's always |
| 242 |
* been that way and stuff would probably break. |
| 243 |
*/ |
| 244 |
memmove(pt, bpt, ept - bpt); |
| 245 |
free(up); |
| 246 |
debug("getcwd_real(\"%s\", %d)", pt, size); |
| 247 |
return (pt); |
| 248 |
} |
| 249 |
|
| 250 |
/* |
| 251 |
* Build pointer to the parent directory, allocating memory |
| 252 |
* as necessary. Max length is 3 for "../", the largest |
| 253 |
* possible component name, plus a trailing NUL. |
| 254 |
*/ |
| 255 |
if (bup + 3 + MAXNAMLEN + 1 >= eup) { |
| 256 |
char *nup; |
| 257 |
|
| 258 |
if ((nup = realloc(up, upsize *= 2)) == NULL) |
| 259 |
goto err; |
| 260 |
bup = nup + (bup - up); |
| 261 |
up = nup; |
| 262 |
eup = up + upsize; |
| 263 |
} |
| 264 |
*bup++ = '.'; |
| 265 |
*bup++ = '.'; |
| 266 |
*bup = '\0'; |
| 267 |
|
| 268 |
/* Open and stat parent directory. */ |
| 269 |
if (!(dir = OPENDIR(up)) || FSTAT(dirfd(dir), &s)) |
| 270 |
goto err; |
| 271 |
|
| 272 |
/* Add trailing slash for next directory. */ |
| 273 |
*bup++ = '/'; |
| 274 |
|
| 275 |
/* |
| 276 |
* If it's a mount point, have to stat each element because |
| 277 |
* the inode number in the directory is for the entry in the |
| 278 |
* parent directory, not the inode number of the mounted file. |
| 279 |
*/ |
| 280 |
save_errno = 0; |
| 281 |
if (s.st_dev == dev) { |
| 282 |
for (;;) { |
| 283 |
if (!(dp = readdir(dir))) |
| 284 |
goto notfound; |
| 285 |
if (dp->d_fileno == ino) |
| 286 |
break; |
| 287 |
} |
| 288 |
} else |
| 289 |
for (;;) { |
| 290 |
if (!(dp = readdir(dir))) |
| 291 |
goto notfound; |
| 292 |
if (ISDOT(dp)) |
| 293 |
continue; |
| 294 |
memcpy(bup, dp->d_name, NAMLEN(dp) + 1); |
| 295 |
|
| 296 |
/* Save the first error for later. */ |
| 297 |
if (LSTAT(up, &s)) { |
| 298 |
if (!save_errno) |
| 299 |
save_errno = errno; |
| 300 |
errno = 0; |
| 301 |
continue; |
| 302 |
} |
| 303 |
if (s.st_dev == dev && s.st_ino == ino) |
| 304 |
break; |
| 305 |
} |
| 306 |
|
| 307 |
/* |
| 308 |
* Check for length of the current name, preceding slash, |
| 309 |
* leading slash. |
| 310 |
*/ |
| 311 |
if (bpt - pt < NAMLEN(dp) + (first ? 1 : 2)) { |
| 312 |
size_t len; |
| 313 |
char *npt; |
| 314 |
|
| 315 |
if (!ptsize) { |
| 316 |
errno = ERANGE; |
| 317 |
goto err; |
| 318 |
} |
| 319 |
len = ept - bpt; |
| 320 |
if ((npt = realloc(pt, ptsize *= 2)) == NULL) |
| 321 |
goto err; |
| 322 |
bpt = npt + (bpt - pt); |
| 323 |
pt = npt; |
| 324 |
ept = pt + ptsize; |
| 325 |
memmove(ept - len, bpt, len); |
| 326 |
bpt = ept - len; |
| 327 |
} |
| 328 |
if (!first) |
| 329 |
*--bpt = '/'; |
| 330 |
bpt -= NAMLEN(dp); |
| 331 |
memcpy(bpt, dp->d_name, NAMLEN(dp)); |
| 332 |
(void)closedir(dir); |
| 333 |
|
| 334 |
/* Truncate any file name. */ |
| 335 |
*bup = '\0'; |
| 336 |
} |
| 337 |
|
| 338 |
notfound: |
| 339 |
/* |
| 340 |
* If readdir set errno, use it, not any saved error; otherwise, |
| 341 |
* didn't find the current directory in its parent directory, set |
| 342 |
* errno to ENOENT. |
| 343 |
*/ |
| 344 |
if (!errno) |
| 345 |
errno = save_errno ? save_errno : ENOENT; |
| 346 |
/* FALLTHROUGH */ |
| 347 |
err: |
| 348 |
save_errno = errno; |
| 349 |
|
| 350 |
if (ptsize) |
| 351 |
free(pt); |
| 352 |
free(up); |
| 353 |
if (dir) |
| 354 |
(void)closedir(dir); |
| 355 |
|
| 356 |
errno = save_errno; |
| 357 |
|
| 358 |
debug("getcwd_real(NULL, %d)", pt, size); |
| 359 |
return (NULL); |
| 360 |
} |
| 361 |
|
| 362 |
#endif |