| 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 |
#define _GNU_SOURCE |
| 24 |
#include <stdarg.h> |
| 25 |
#include <stddef.h> |
| 26 |
#ifdef HAVE_ALLOCA_H |
| 27 |
# include <alloca.h> |
| 28 |
#endif |
| 29 |
#include <stdlib.h> |
| 30 |
#include <unistd.h> |
| 31 |
#include "libfakechroot.h" |
| 32 |
|
| 33 |
#ifndef __GLIBC__ |
| 34 |
extern char **environ; |
| 35 |
#endif |
| 36 |
|
| 37 |
|
| 38 |
/* |
| 39 |
execl function taken from GNU C Library. |
| 40 |
Copyright (C) 1991,92,94,97,98,99,2002,2005 Free Software Foundation, Inc. |
| 41 |
*/ |
| 42 |
wrapper(execl, int, (const char * path, const char * arg, ...)) |
| 43 |
{ |
| 44 |
size_t argv_max = 1024; |
| 45 |
const char **argv = alloca(argv_max * sizeof(const char *)); |
| 46 |
unsigned int i; |
| 47 |
|
| 48 |
va_list args; |
| 49 |
va_start(args, arg); |
| 50 |
|
| 51 |
debug("execl(\"%s\", \"%s\", ...)", path, arg); |
| 52 |
argv[0] = arg; |
| 53 |
|
| 54 |
i = 0; |
| 55 |
while (argv[i++] != NULL) { |
| 56 |
if (i == argv_max) { |
| 57 |
const char **nptr = alloca((argv_max *= 2) * sizeof(const char *)); |
| 58 |
|
| 59 |
if ((char *) argv + i == (char *) nptr) |
| 60 |
/* Stack grows up. */ |
| 61 |
argv_max += i; |
| 62 |
else |
| 63 |
/* We have a hole in the stack. */ |
| 64 |
argv = (const char **) memcpy(nptr, argv, i |
| 65 |
* sizeof(const char *)); |
| 66 |
} |
| 67 |
|
| 68 |
argv[i] = va_arg(args, const char *); |
| 69 |
} |
| 70 |
va_end(args); |
| 71 |
|
| 72 |
return execve(path, (char * const *) argv, environ); |
| 73 |
} |