summaryrefslogtreecommitdiffstats
path: root/trace/preload.c
blob: 99dd90675e6ff4386c6bd35ba82d14c3bfd439be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#define _GNU_SOURCE
#include <sys/ptrace.h>
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dlfcn.h>

#include "../preload.h"

#define ERROR(fmt, ...) do { \
	fprintf(stderr,"perload-jail: "fmt, ## __VA_ARGS__); \
	} while (0)

static main_t __main__;

static int __preload_main__(int argc, char **argv, char **envp)
{
	unsetenv("LD_PRELOAD");
	ptrace(PTRACE_TRACEME);
	kill(getpid(), SIGSTOP);

	return (*__main__)(argc, argv, envp);
}

int __libc_start_main(main_t main,
			int argc,
			char **argv,
			ElfW(auxv_t) *auxvec,
			__typeof (main) init,
			void (*fini) (void),
			void (*rtld_fini) (void),
			void *stack_end)
{
	start_main_t __start_main__;

	__start_main__ = dlsym(RTLD_NEXT, "__libc_start_main");
	if (!__start_main__)
		ERROR("failed to find __libc_start_main %s\n", dlerror());

	__main__ = main;

	return (*__start_main__)(__preload_main__, argc, argv, auxvec,
		init, fini, rtld_fini, stack_end);
}

void __uClibc_main(main_t main,
			int argc,
			char **argv,
			void (*app_init)(void),
			void (*app_fini)(void),
			void (*rtld_fini)(void),
			void *stack_end attribute_unused)
{
	uClibc_main __start_main__;

	__start_main__ = dlsym(RTLD_NEXT, "__uClibc_main");
	if (!__start_main__)
		ERROR("failed to find __uClibc_main %s\n", dlerror());

	__main__ = main;

	return (*__start_main__)(__preload_main__, argc, argv,
		app_init, app_fini, rtld_fini, stack_end);
}