summaryrefslogtreecommitdiffstats
path: root/src/audit-init.c
blob: 5ab978c395b845d89dd0360b511236cdc2ec14e4 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
  Copyright (C) 2008, 2009 Jiri Olsa <olsajiri@gmail.com>

  This file is part of the latrace.

  The latrace is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  The latrace 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.

  You should have received a copy of the GNU General Public License
  along with the latrace (file COPYING).  If not, see 
  <http://www.gnu.org/licenses/>.
*/


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#include "config.h"

struct lt_config_audit cfg;

static int read_config(char *dir)
{
	int fd;
	off_t len;
	char file[LT_MAXFILE];

	memset(&cfg, 0, sizeof(cfg));

	cfg.dir = dir;
	sprintf(file, "%s/config", dir);

	if (-1 == (fd = open(file, O_RDONLY))) {
		perror("open failed");
		return -1;
	}

	if (-1 == read(fd, &cfg.sh, sizeof(cfg.sh))) {
		perror("read failed");
		return -1;
	}

	if (-1 == (len = lseek(fd, 0, SEEK_END))) {
		perror("lseek failed");
		return -1;
	}

	if (len != sizeof(cfg.sh)) {
		printf("config file size differs\n");
		return -1;
	}

	if (LT_MAGIC != cfg.sh.magic) {
		printf("config file magic check failed\n");
		return -1;
	}

	return 0;
}

static int get_names(struct lt_config_audit *cfg, char *names, char **ptr)
{
	char* s;
	int cnt = 0;

	PRINT_VERBOSE(cfg->sh.verbose, 1, "names: [%s] max: %d\n", 
			names, LT_NAMES_MAX);

	s = strchr(names, LT_NAMES_SEP);
	while(NULL != (s = strchr(names, LT_NAMES_SEP)) && (cnt < LT_NAMES_MAX)) {
		*s = 0x0;
		PRINT_VERBOSE(cfg->sh.verbose, 1, "got: %s", names);
		ptr[cnt++] = names;
		names = ++s;
	}

	if (cnt) {
		ptr[cnt++] = names;
		PRINT_VERBOSE(cfg->sh.verbose, 1, "got: %s\n", names);
	}

	if (!cnt && *names) {
		ptr[0] = names;
		cnt = 1;
		PRINT_VERBOSE(cfg->sh.verbose, 1, "got: %s\n", names);
	}

	ptr[cnt] = NULL;

	if (!cnt)
		return -1;

	PRINT_VERBOSE(cfg->sh.verbose, 1, "got %d entries\n", cnt);
	return cnt;
}

int audit_init(int argc, char **argv, char **env)
{
	if (-1 == read_config(getenv("LT_DIR")))
		return -1;

	/* -Aa */
	if (cfg.sh.args_enabled && lt_args_init(&cfg.sh))
		return -1;

	/* -t */
	if ((*cfg.sh.libs_to) && 
	    (-1 == (cfg.libs_to_cnt = get_names(&cfg, cfg.sh.libs_to, cfg.libs_to)))) {
		printf("latrace failed to parse libs to\n");
		return -1;
	}

	/* -f */
	if ((*cfg.sh.libs_from) && 
	    (-1 == (cfg.libs_from_cnt = get_names(&cfg, cfg.sh.libs_from, cfg.libs_from)))) {
		printf("latrace failed to parse libs from\n");
		return -1;
	}

	/* -l */
	if ((*cfg.sh.libs_both) && 
	    (-1 == (cfg.libs_both_cnt = get_names(&cfg, cfg.sh.libs_both, cfg.libs_both)))) {
		printf("latrace failed to parse libs from\n");
		return -1;
	}

	/* -s */
	if ((*cfg.sh.symbols) && 
	    (-1 == (cfg.symbols_cnt = get_names(&cfg, cfg.sh.symbols, cfg.symbols)))) {
		printf("latrace failed to parse symbols\n");
		return -1;
	}

	/* -b */
	if ((*cfg.sh.flow_below) && 
	    (-1 == (cfg.flow_below_cnt = get_names(&cfg, cfg.sh.flow_below, cfg.flow_below)))) {
		printf("latrace failed to parse symbols in flow-below option\n");
		return -1;
	}

	/* -L */
	if (*cfg.sh.libs_subst) {

		char *ptr[LT_NAMES_MAX];
		int cnt;

		if (-1 == (cnt = get_names(&cfg, cfg.sh.libs_subst, ptr))) {
			printf("latrace failed to parse input for subst option\n");
			return -1;
		}

		if (-1 == lt_objsearch_init(&cfg, ptr, cnt)) {
			printf("latrace failed to nitialize subst option\n");
			return -1;
		}
	}

	/* -o */
	cfg.sh.fout = stdout;
	if ((*cfg.sh.output) && (NULL == (cfg.sh.fout = fopen(cfg.sh.output, "w")))) {
		printf("latrace failed to open output file %s\n", cfg.sh.output);
		return -1;
	}

	/* -E */
	if (cfg.sh.not_follow_exec)
		unsetenv("LD_AUDIT");

	/* -F */
	if (cfg.sh.not_follow_fork)
		cfg.sh.pid = getpid();

	cfg.init_ok = 1;
	return 0;
}

void finalize(void) __attribute__((destructor));

void
finalize(void)
{
	if ((!cfg.sh.pipe) && (*cfg.sh.output))
		fclose(cfg.sh.fout);
}