Debian Codegraph @

src/execle.c

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