| 1 |
/* |
| 2 |
libfakechroot -- fake chroot environment |
| 3 |
Copyright (c) 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_CLEARENV |
| 24 |
|
| 25 |
#ifndef __GLIBC__ |
| 26 |
extern char **environ; |
| 27 |
#endif |
| 28 |
|
| 29 |
#define _GNU_SOURCE 1 |
| 30 |
|
| 31 |
#include <stdlib.h> |
| 32 |
#include <unistd.h> |
| 33 |
#include <string.h> |
| 34 |
#ifdef HAVE_ALLOCA_H |
| 35 |
# include <alloca.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#include "libfakechroot.h" |
| 39 |
|
| 40 |
extern int __clearenv(void); |
| 41 |
|
| 42 |
|
| 43 |
wrapper(clearenv, int, (void)) |
| 44 |
{ |
| 45 |
int j, n; |
| 46 |
char *key, *env; |
| 47 |
char **tmpkey, **tmpenv; |
| 48 |
|
| 49 |
debug("clearenv()"); |
| 50 |
|
| 51 |
/* Preserve old environment variables */ |
| 52 |
tmpkey = alloca( (preserve_env_list_count + 1) * sizeof (char *) ); |
| 53 |
tmpenv = alloca( (preserve_env_list_count + 1) * sizeof (char *) ); |
| 54 |
|
| 55 |
for (j = 0, n = 0; j < preserve_env_list_count; j++) { |
| 56 |
key = preserve_env_list[j]; |
| 57 |
env = getenv(key); |
| 58 |
if (env != NULL) { |
| 59 |
tmpkey[n] = alloca(strlen(key) + 1); |
| 60 |
tmpenv[n] = alloca(strlen(env) + 1); |
| 61 |
strcpy(tmpkey[n], key); |
| 62 |
strcpy(tmpenv[n], env); |
| 63 |
n++; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
tmpkey[n] = NULL; |
| 68 |
tmpenv[n] = NULL; |
| 69 |
|
| 70 |
/* Clear */ |
| 71 |
__clearenv(); |
| 72 |
|
| 73 |
/* Set one variable explicitly so environ won't be NULL */ |
| 74 |
setenv("FAKECHROOT", "true", 0); |
| 75 |
|
| 76 |
/* Recover preserved variables */ |
| 77 |
for (n = 0; tmpkey[n]; n++) { |
| 78 |
if (setenv(tmpkey[n], tmpenv[n], 1) != 0) { |
| 79 |
return -1; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
#else |
| 87 |
typedef int empty_translation_unit; |
| 88 |
#endif |