utrace: Forward SIGTERM to the traced process
[project/procd.git] / trace / 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/ptrace.h>
16 #include <sys/types.h>
17 #include <signal.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <dlfcn.h>
23
24 #include "../preload.h"
25
26 #define ERROR(fmt, ...) do { \
27         fprintf(stderr,"perload-jail: "fmt, ## __VA_ARGS__); \
28         } while (0)
29
30 static main_t __main__;
31
32 static int __preload_main__(int argc, char **argv, char **envp)
33 {
34         unsetenv("LD_PRELOAD");
35         kill(getpid(), SIGSTOP);
36
37         return (*__main__)(argc, argv, envp);
38 }
39
40 int __libc_start_main(main_t main,
41                         int argc,
42                         char **argv,
43                         ElfW(auxv_t) *auxvec,
44                         __typeof (main) init,
45                         void (*fini) (void),
46                         void (*rtld_fini) (void),
47                         void *stack_end)
48 {
49         start_main_t __start_main__;
50
51         __start_main__ = dlsym(RTLD_NEXT, "__libc_start_main");
52         if (!__start_main__)
53                 ERROR("failed to find __libc_start_main %s\n", dlerror());
54
55         __main__ = main;
56
57         return (*__start_main__)(__preload_main__, argc, argv, auxvec,
58                 init, fini, rtld_fini, stack_end);
59 }
60
61 void __uClibc_main(main_t main,
62                         int argc,
63                         char **argv,
64                         void (*app_init)(void),
65                         void (*app_fini)(void),
66                         void (*rtld_fini)(void),
67                         void *stack_end attribute_unused)
68 {
69         uClibc_main __start_main__;
70
71         __start_main__ = dlsym(RTLD_NEXT, "__uClibc_main");
72         if (!__start_main__)
73                 ERROR("failed to find __uClibc_main %s\n", dlerror());
74
75         __main__ = main;
76
77         return (*__start_main__)(__preload_main__, argc, argv,
78                 app_init, app_fini, rtld_fini, stack_end);
79 }