procd: get rid of putenv usage.
[project/procd.git] / jail / preload.c
1 /*
2  * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #define _GNU_SOURCE
15 #include <sys/types.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <dlfcn.h>
20
21 #include "seccomp.h"
22 #include "../preload.h"
23
24 static main_t __main__;
25
26 static int __preload_main__(int argc, char **argv, char **envp)
27 {
28         char *env_file = getenv("SECCOMP_FILE");
29
30         if (!env_file || !env_file[0]) {
31                 ERROR("SECCOMP_FILE not specified\n");
32                 return -1;
33         }
34
35         if (install_syscall_filter(*argv, env_file))
36                 return -1;
37
38         unsetenv("LD_PRELOAD");
39         unsetenv("SECCOMP_FILE");
40
41         return (*__main__)(argc, argv, envp);
42 }
43
44 int __libc_start_main(main_t main,
45                         int argc,
46                         char **argv,
47                         ElfW(auxv_t) *auxvec,
48                         __typeof (main) init,
49                         void (*fini) (void),
50                         void (*rtld_fini) (void),
51                         void *stack_end)
52 {
53         start_main_t __start_main__;
54
55         __start_main__ = dlsym(RTLD_NEXT, "__libc_start_main");
56         if (!__start_main__)
57                 INFO("failed to find __libc_start_main %s\n", dlerror());
58
59         __main__ = main;
60
61         return (*__start_main__)(__preload_main__, argc, argv, auxvec,
62                 init, fini, rtld_fini, stack_end);
63 }
64
65 void __uClibc_main(main_t main,
66                         int argc,
67                         char **argv,
68                         void (*app_init)(void),
69                         void (*app_fini)(void),
70                         void (*rtld_fini)(void),
71                         void *stack_end attribute_unused)
72 {
73         uClibc_main __start_main__;
74
75         __start_main__ = dlsym(RTLD_NEXT, "__uClibc_main");
76         if (!__start_main__)
77                 INFO("failed to find __uClibc_main %s\n", dlerror());
78
79         __main__ = main;
80
81         return (*__start_main__)(__preload_main__, argc, argv,
82                 app_init, app_fini, rtld_fini, stack_end);
83 }