| 1 |
/* |
| 2 |
libfakechroot -- fake chroot environment |
| 3 |
Copyright (c) 2010, 2013 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 |
#ifdef HAVE___OPENDIR2 |
| 24 |
|
| 25 |
#include <dirent.h> |
| 26 |
#include "libfakechroot.h" |
| 27 |
|
| 28 |
|
| 29 |
/*- |
| 30 |
* Copyright (c) 1983, 1993 |
| 31 |
* The Regents of the University of California. All rights reserved. |
| 32 |
* |
| 33 |
* Redistribution and use in source and binary forms, with or without |
| 34 |
* modification, are permitted provided that the following conditions |
| 35 |
* are met: |
| 36 |
* 1. Redistributions of source code must retain the above copyright |
| 37 |
* notice, this list of conditions and the following disclaimer. |
| 38 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 39 |
* notice, this list of conditions and the following disclaimer in the |
| 40 |
* documentation and/or other materials provided with the distribution. |
| 41 |
* 4. Neither the name of the University nor the names of its contributors |
| 42 |
* may be used to endorse or promote products derived from this software |
| 43 |
* without specific prior written permission. |
| 44 |
* |
| 45 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 46 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 47 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 48 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 49 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 50 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 51 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 52 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 53 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 54 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 55 |
* SUCH DAMAGE. |
| 56 |
*/ |
| 57 |
|
| 58 |
#if defined(LIBC_SCCS) && !defined(lint) |
| 59 |
static char sccsid[] = "@(#)opendir.c 8.8 (Berkeley) 5/1/95"; |
| 60 |
#endif /* LIBC_SCCS and not lint */ |
| 61 |
#include <sys/cdefs.h> |
| 62 |
__FBSDID("$FreeBSD: src/lib/libc/gen/opendir.c,v 1.25.2.2.2.1 2010/06/14 02:09:06 kensmith Exp $"); |
| 63 |
|
| 64 |
/*#include "namespace.h"*/ |
| 65 |
#include <sys/param.h> |
| 66 |
#include <sys/mount.h> |
| 67 |
#include <sys/stat.h> |
| 68 |
|
| 69 |
#include <dirent.h> |
| 70 |
#include <errno.h> |
| 71 |
#include <fcntl.h> |
| 72 |
#include <stdlib.h> |
| 73 |
#include <string.h> |
| 74 |
#include <unistd.h> |
| 75 |
/*#include "un-namespace.h"*/ |
| 76 |
|
| 77 |
/*#include "telldir.h"*/ |
| 78 |
|
| 79 |
#define _fcntl fcntl |
| 80 |
#define _fstat fstat |
| 81 |
#define _fstatfs fstatfs |
| 82 |
#define _getdirentries getdirentries |
| 83 |
#define _close close |
| 84 |
#define _open open |
| 85 |
|
| 86 |
|
| 87 |
/* |
| 88 |
* Copyright (c) 1983, 1993 |
| 89 |
* The Regents of the University of California. All rights reserved. |
| 90 |
* |
| 91 |
* Copyright (c) 2000 |
| 92 |
* Daniel Eischen. 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 |
* $FreeBSD: src/lib/libc/gen/telldir.h,v 1.3.2.1.4.1 2010/06/14 02:09:06 kensmith Exp $ |
| 119 |
*/ |
| 120 |
|
| 121 |
/* |
| 122 |
* One of these structures is malloced to describe the current directory |
| 123 |
* position each time telldir is called. It records the current magic |
| 124 |
* cookie returned by getdirentries and the offset within the buffer |
| 125 |
* associated with that return value. |
| 126 |
*/ |
| 127 |
struct ddloc { |
| 128 |
LIST_ENTRY(ddloc) loc_lqe; /* entry in list */ |
| 129 |
long loc_index; /* key associated with structure */ |
| 130 |
long loc_seek; /* magic cookie returned by getdirentries */ |
| 131 |
long loc_loc; /* offset of entry in buffer */ |
| 132 |
}; |
| 133 |
|
| 134 |
/* |
| 135 |
* One of these structures is malloced for each DIR to record telldir |
| 136 |
* positions. |
| 137 |
*/ |
| 138 |
struct _telldir { |
| 139 |
LIST_HEAD(, ddloc) td_locq; /* list of locations */ |
| 140 |
long td_loccnt; /* index of entry for sequential readdir's */ |
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
static DIR * __opendir_common(int, const char *, int); |
| 145 |
|
| 146 |
wrapper(__opendir2, DIR *, (const char * name, int flags)) |
| 147 |
{ |
| 148 |
int fd; |
| 149 |
struct stat statb; |
| 150 |
|
| 151 |
debug("__opendir2(\"%s\", %d)", name, flags); |
| 152 |
/* |
| 153 |
* stat() before _open() because opening of special files may be |
| 154 |
* harmful. |
| 155 |
*/ |
| 156 |
if (stat(name, &statb) != 0) |
| 157 |
return (NULL); |
| 158 |
if (!S_ISDIR(statb.st_mode)) { |
| 159 |
errno = ENOTDIR; |
| 160 |
return (NULL); |
| 161 |
} |
| 162 |
if ((fd = _open(name, O_RDONLY | O_NONBLOCK)) == -1) |
| 163 |
return (NULL); |
| 164 |
|
| 165 |
return __opendir_common(fd, name, flags); |
| 166 |
} |
| 167 |
|
| 168 |
static int |
| 169 |
opendir_compar(const void *p1, const void *p2) |
| 170 |
{ |
| 171 |
|
| 172 |
return (strcmp((*(const struct dirent **)p1)->d_name, |
| 173 |
(*(const struct dirent **)p2)->d_name)); |
| 174 |
} |
| 175 |
|
| 176 |
/* |
| 177 |
* Common routine for opendir(3), __opendir2(3) and fdopendir(3). |
| 178 |
*/ |
| 179 |
static DIR * |
| 180 |
__opendir_common(int fd, const char *name, int flags) |
| 181 |
{ |
| 182 |
DIR *dirp; |
| 183 |
int incr; |
| 184 |
int saved_errno; |
| 185 |
int unionstack; |
| 186 |
struct stat statb; |
| 187 |
|
| 188 |
dirp = NULL; |
| 189 |
/* _fstat() the open handler because the file may have changed. */ |
| 190 |
if (_fstat(fd, &statb) != 0) |
| 191 |
goto fail; |
| 192 |
if (!S_ISDIR(statb.st_mode)) { |
| 193 |
errno = ENOTDIR; |
| 194 |
goto fail; |
| 195 |
} |
| 196 |
if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 || |
| 197 |
(dirp = malloc(sizeof(DIR) + sizeof(struct _telldir))) == NULL) |
| 198 |
goto fail; |
| 199 |
|
| 200 |
dirp->dd_td = (struct _telldir *)((char *)dirp + sizeof(DIR)); |
| 201 |
LIST_INIT(&dirp->dd_td->td_locq); |
| 202 |
dirp->dd_td->td_loccnt = 0; |
| 203 |
|
| 204 |
/* |
| 205 |
* Use the system page size if that is a multiple of DIRBLKSIZ. |
| 206 |
* Hopefully this can be a big win someday by allowing page |
| 207 |
* trades to user space to be done by _getdirentries(). |
| 208 |
*/ |
| 209 |
incr = getpagesize(); |
| 210 |
if ((incr % DIRBLKSIZ) != 0) |
| 211 |
incr = DIRBLKSIZ; |
| 212 |
|
| 213 |
/* |
| 214 |
* Determine whether this directory is the top of a union stack. |
| 215 |
*/ |
| 216 |
if (flags & DTF_NODUP) { |
| 217 |
struct statfs sfb; |
| 218 |
|
| 219 |
if (_fstatfs(fd, &sfb) < 0) |
| 220 |
goto fail; |
| 221 |
unionstack = !strcmp(sfb.f_fstypename, "unionfs") |
| 222 |
|| (sfb.f_flags & MNT_UNION); |
| 223 |
} else { |
| 224 |
unionstack = 0; |
| 225 |
} |
| 226 |
|
| 227 |
if (unionstack) { |
| 228 |
int len = 0; |
| 229 |
int space = 0; |
| 230 |
char *buf = 0; |
| 231 |
char *ddptr = 0; |
| 232 |
char *ddeptr; |
| 233 |
int n; |
| 234 |
struct dirent **dpv; |
| 235 |
|
| 236 |
/* |
| 237 |
* The strategy here is to read all the directory |
| 238 |
* entries into a buffer, sort the buffer, and |
| 239 |
* remove duplicate entries by setting the inode |
| 240 |
* number to zero. |
| 241 |
*/ |
| 242 |
|
| 243 |
do { |
| 244 |
/* |
| 245 |
* Always make at least DIRBLKSIZ bytes |
| 246 |
* available to _getdirentries |
| 247 |
*/ |
| 248 |
if (space < DIRBLKSIZ) { |
| 249 |
space += incr; |
| 250 |
len += incr; |
| 251 |
buf = reallocf(buf, len); |
| 252 |
if (buf == NULL) |
| 253 |
goto fail; |
| 254 |
ddptr = buf + (len - space); |
| 255 |
} |
| 256 |
|
| 257 |
n = _getdirentries(fd, ddptr, space, &dirp->dd_seek); |
| 258 |
if (n > 0) { |
| 259 |
ddptr += n; |
| 260 |
space -= n; |
| 261 |
} |
| 262 |
} while (n > 0); |
| 263 |
|
| 264 |
ddeptr = ddptr; |
| 265 |
flags |= __DTF_READALL; |
| 266 |
|
| 267 |
/* |
| 268 |
* Re-open the directory. |
| 269 |
* This has the effect of rewinding back to the |
| 270 |
* top of the union stack and is needed by |
| 271 |
* programs which plan to fchdir to a descriptor |
| 272 |
* which has also been read -- see fts.c. |
| 273 |
*/ |
| 274 |
if (flags & DTF_REWIND) { |
| 275 |
(void)_close(fd); |
| 276 |
if ((fd = _open(name, O_RDONLY)) == -1) { |
| 277 |
saved_errno = errno; |
| 278 |
free(buf); |
| 279 |
free(dirp); |
| 280 |
errno = saved_errno; |
| 281 |
return (NULL); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
/* |
| 286 |
* There is now a buffer full of (possibly) duplicate |
| 287 |
* names. |
| 288 |
*/ |
| 289 |
dirp->dd_buf = buf; |
| 290 |
|
| 291 |
/* |
| 292 |
* Go round this loop twice... |
| 293 |
* |
| 294 |
* Scan through the buffer, counting entries. |
| 295 |
* On the second pass, save pointers to each one. |
| 296 |
* Then sort the pointers and remove duplicate names. |
| 297 |
*/ |
| 298 |
for (dpv = 0;;) { |
| 299 |
n = 0; |
| 300 |
ddptr = buf; |
| 301 |
while (ddptr < ddeptr) { |
| 302 |
struct dirent *dp; |
| 303 |
|
| 304 |
dp = (struct dirent *) ddptr; |
| 305 |
if ((long)dp & 03L) |
| 306 |
break; |
| 307 |
if ((dp->d_reclen <= 0) || |
| 308 |
(dp->d_reclen > (ddeptr + 1 - ddptr))) |
| 309 |
break; |
| 310 |
ddptr += dp->d_reclen; |
| 311 |
if (dp->d_fileno) { |
| 312 |
if (dpv) |
| 313 |
dpv[n] = dp; |
| 314 |
n++; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
if (dpv) { |
| 319 |
struct dirent *xp; |
| 320 |
|
| 321 |
/* |
| 322 |
* This sort must be stable. |
| 323 |
*/ |
| 324 |
mergesort(dpv, n, sizeof(*dpv), |
| 325 |
opendir_compar); |
| 326 |
|
| 327 |
dpv[n] = NULL; |
| 328 |
xp = NULL; |
| 329 |
|
| 330 |
/* |
| 331 |
* Scan through the buffer in sort order, |
| 332 |
* zapping the inode number of any |
| 333 |
* duplicate names. |
| 334 |
*/ |
| 335 |
for (n = 0; dpv[n]; n++) { |
| 336 |
struct dirent *dp = dpv[n]; |
| 337 |
|
| 338 |
if ((xp == NULL) || |
| 339 |
strcmp(dp->d_name, xp->d_name)) { |
| 340 |
xp = dp; |
| 341 |
} else { |
| 342 |
dp->d_fileno = 0; |
| 343 |
} |
| 344 |
if (dp->d_type == DT_WHT && |
| 345 |
(flags & DTF_HIDEW)) |
| 346 |
dp->d_fileno = 0; |
| 347 |
} |
| 348 |
|
| 349 |
free(dpv); |
| 350 |
break; |
| 351 |
} else { |
| 352 |
dpv = malloc((n+1) * sizeof(struct dirent *)); |
| 353 |
if (dpv == NULL) |
| 354 |
break; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
dirp->dd_len = len; |
| 359 |
dirp->dd_size = ddptr - dirp->dd_buf; |
| 360 |
} else { |
| 361 |
dirp->dd_len = incr; |
| 362 |
dirp->dd_size = 0; |
| 363 |
dirp->dd_buf = malloc(dirp->dd_len); |
| 364 |
if (dirp->dd_buf == NULL) |
| 365 |
goto fail; |
| 366 |
dirp->dd_seek = 0; |
| 367 |
flags &= ~DTF_REWIND; |
| 368 |
} |
| 369 |
|
| 370 |
dirp->dd_loc = 0; |
| 371 |
dirp->dd_fd = fd; |
| 372 |
dirp->dd_flags = flags; |
| 373 |
dirp->dd_lock = NULL; |
| 374 |
|
| 375 |
/* |
| 376 |
* Set up seek point for rewinddir. |
| 377 |
*/ |
| 378 |
dirp->dd_rewind = telldir(dirp); |
| 379 |
|
| 380 |
return (dirp); |
| 381 |
|
| 382 |
fail: |
| 383 |
saved_errno = errno; |
| 384 |
free(dirp); |
| 385 |
(void)_close(fd); |
| 386 |
errno = saved_errno; |
| 387 |
return (NULL); |
| 388 |
} |
| 389 |
|
| 390 |
#else |
| 391 |
typedef int empty_translation_unit; |
| 392 |
#endif |