summaryrefslogtreecommitdiffstats
path: root/src/run.c
blob: f7a1ddd62bce6265e9a3d8cf1120d355bda5e8e6 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
  Copyright (C) 2008, 2009, 2010 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 <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/inotify.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>

#include "config.h"

extern char **environ;
extern struct timeval tv_program_start;
extern struct timeval tv_program_stop;

struct lt_process_args {
	pid_t pid;
	char *dir;
	int fd_notify;
	int fd_tty_master;
};

static int store_config(struct lt_config_app *cfg, char *file)
{
	int fd;
	int mode = S_IRUSR;

	if (lt_sh(cfg, ctl_config)) {
		printf("controled config: %s\n", file);
		mode |= S_IWUSR;
	}

	if (-1 == (fd = open(file, O_CREAT | O_TRUNC | O_RDWR, mode))) {
		perror("open failed");
		return -1;
	}

        if (-1 == write(fd, cfg->sh, sizeof(*cfg->sh))) {
                perror("read failed");
                return -1;
        }

	close(fd);

	return 0;
}

static int get_config_dir(char *buf, int len)
{
	snprintf(buf, len , "%s-XXXXXX", CONFIG_LT_CONFIG);
	if (NULL == mkdtemp(buf)) {
		perror("mkdtemp failed");
		return -1;
	}

	return 0;
}

static int get_fifo(struct lt_config_app *cfg, int fd_notify,
			char *dir, pid_t *pid)
{
	unsigned char buf[1000];
	struct inotify_event *event = (struct inotify_event*) buf;

	if (-1 == read(fd_notify, event, 1000)) {
		perror("read notify failed");
		return -1;
	}

	sscanf(event->name, "fifo-%d", pid);

	PRINT_VERBOSE(cfg, 1, "thread id %d, got fifo: %s\n",
			*pid, event->name);

	return lt_fifo_open(cfg, dir, event->name);
}

static int process_fifo(struct lt_config_app *cfg, struct lt_thread *t)
{
	static char buf[FIFO_MSG_MAXLEN];
	struct lt_fifo_mbase *mbase = (struct lt_fifo_mbase*) buf;
	struct lt_fifo_msym *msym = (struct lt_fifo_msym*) buf;

	if (-1 == lt_fifo_recv(cfg, t, mbase, FIFO_MSG_MAXLEN))
		return -1;

	if ((FIFO_MSG_TYPE_ENTRY != mbase->type) &&
	    (FIFO_MSG_TYPE_EXIT  != mbase->type)) {
		PRINT_VERBOSE(cfg, 1, "unexpected message type %d\n",
				mbase->type);
		return -1;
	}

	if (lt_sh(cfg, counts))
		return lt_stats_sym(cfg, t, msym);

	if (FIFO_MSG_TYPE_ENTRY == msym->h.type) {

		t->indent_depth++;
		lt_out_entry(cfg->sh, &msym->h.tv, msym->h.tid,
				t->indent_depth,
				msym->data + msym->sym,
				msym->data + msym->lib,
				msym->data + msym->arg,
				msym->data + msym->argd);

	} else if (FIFO_MSG_TYPE_EXIT == msym->h.type) {

		lt_out_exit(cfg->sh, &msym->h.tv, msym->h.tid,
				t->indent_depth,
				msym->data + msym->sym,
				msym->data + msym->lib,
				msym->data + msym->arg,
				msym->data + msym->argd);

		t->indent_depth--;
	}

	return 0;
}

static int process(struct lt_config_app *cfg, struct lt_process_args *pa)
{
	int finish = 0, getin = 1, status;
	fd_set cfg_set, wrk_set;
	int fd_notify = pa->fd_notify;
	int fd_tty_master = pa->fd_tty_master;
	int max_fd = fd_notify;
#define MAX(a,b) ((a) < (b) ? (b) : (a))

	FD_ZERO(&cfg_set);

	/* We are here either because of the
	 *   - pipe option
	 *   - or tty output option */

	if (lt_sh(cfg, pipe)) {
		PRINT_VERBOSE(cfg, 1, "doing pipe\n");
		FD_SET(fd_notify, &cfg_set);
		max_fd = fd_notify;
	}

	if (cfg->output_tty) {
		PRINT_VERBOSE(cfg, 1, "doing output tty\n");
		FD_SET(fd_tty_master, &cfg_set);
		max_fd = MAX(fd_notify, fd_tty_master);
	}

	while(!waitpid(pa->pid, &status, WNOHANG) ||
		/* let all the thread fifo close */
		(finish) || 
		/* Get inside at least once, in case the traced program 
		   finished before we got here.  Another case is if there's 
		   an input on notify descriptor, we want to try another 
		   select to be sure we dont miss any other event. */
		(getin))
	{

#define CONTINUE_ZERO_RET() { \
if (!ret) \
	continue; \
if (ret < 0) \
	printf("latrace failed: select bug ret %d\n", ret); \
}

		struct timeval tv = { 0, 100 };
		struct lt_thread *t;
		int ret;

		getin = 0;
		wrk_set = cfg_set;

		ret = select(max_fd + 1, &wrk_set, NULL, NULL, &tv);
		if (-1 == ret) {
			perror("select failed");
			return -1;
		}

		CONTINUE_ZERO_RET();

		/* process notify */
		if (FD_ISSET(fd_notify, &wrk_set)) {
			int fd;
			pid_t pid;

			/* try to get any event at least once again */
			getin = 1;

			if (-1 == (fd = get_fifo(cfg, fd_notify, pa->dir, &pid)))
				continue;

			if (NULL == (t = lt_thread_add(cfg, fd, pid))) {
				close(fd);
				continue;
			}

			finish++;

			FD_SET(fd, &cfg_set);
			max_fd = MAX(max_fd, fd);
			ret--;
		}

		CONTINUE_ZERO_RET();

		if (cfg->output_tty &&
		    FD_ISSET(fd_tty_master, &wrk_set)) {
			if (tty_process(cfg, fd_tty_master))
				FD_CLR(fd_tty_master, &cfg_set);
			ret--;
		}

		CONTINUE_ZERO_RET();

		/* process fifo */
		for(t = cfg->threads; t ; t = t->next) {
			if (FD_ISSET(t->fifo_fd, &wrk_set)) {
				if (-1 == process_fifo(cfg, t)) {
					FD_CLR(t->fifo_fd, &cfg_set);
					gettimeofday(&t->tv_stop, NULL);
					finish--;
				}
				ret--;
			}
		}
#undef CONTINUE_ZERO_RET
#undef MAX
	}

	return status;
}

static int remove_dir(struct lt_config_app *cfg, char *name)
{
	DIR *dir;
	struct dirent *d;

	PRINT_VERBOSE(cfg, 1, "removing %s\n", name);

	if (NULL == (dir = opendir(name))) {
		perror("opendir failed");
		return -1;
	}

	while((d = readdir(dir))) {
		char file[LT_MAXFILE];
		struct stat st;

		if (!strcmp(".", d->d_name) ||
		    (!strcmp("..", d->d_name)))
			continue;

		sprintf(file, "%s/%s", name, d->d_name);
		if (stat(file, &st)) {
			perror("stat failed");
			continue;
		}

		if (S_ISDIR(st.st_mode)) {
			remove_dir(cfg, file);
			continue;
		}

		if (unlink(file))
			perror("unlink failed");
	}

	closedir(dir);

	if (-1 == remove(name)) {
		perror("remove failed");
		return -1;
	}

	return 0;
}

int lt_run(struct lt_config_app *cfg)
{
	char str_dir[LT_MAXFILE];
	char str_cfg[LT_MAXFILE];
	struct lt_process_args pa = { .dir = str_dir };
	int status;

	if (get_config_dir(str_dir, LT_MAXFILE))
		return -1;

	sprintf(str_cfg, "%s/config", str_dir);
	if (store_config(cfg, str_cfg))
		return -1;

	/* new thread notification descriptor */
	if (lt_sh(cfg, pipe) &&
	   (-1 == (pa.fd_notify = lt_fifo_notify_fd(cfg, str_dir))))
		return -1;

	/* tty master descriptor */
	if (cfg->output_tty &&
	    (-1 == (pa.fd_tty_master = tty_master(cfg))))
		return -1;

	gettimeofday(&tv_program_start, NULL);

	if (0 == (pa.pid = fork())) {
		char str_audit[100];

		sprintf(str_audit, "%s/libltaudit.so.%s", CONFIG_LIBDIR,
			CONFIG_VERSION);

		setenv("LD_AUDIT", str_audit, 1);
		setenv("LT_DIR", str_dir, 1);

		if (cfg->output_tty &&
		    tty_init(cfg, pa.fd_tty_master))
			return -1;

		PRINT_VERBOSE(cfg, 1, "executing %s\n", cfg->prog);

		if (-1 == execvp(cfg->prog, cfg->arg)) {
			tty_restore(cfg);
			printf("execve failed for \"%s\" : %s\n", 
				cfg->prog, strerror(errno));
			return -1;
		}
	} else if (pa.pid < 0) {
		perror("fork failed");
		return -1;
	}

	if (lt_sh(cfg, pipe) || cfg->output_tty)
		status = process(cfg, &pa);
	else
		waitpid(pa.pid, &status, 0);

	gettimeofday(&tv_program_stop, NULL);

	printf("\n%s finished - ", cfg->prog);

	if (WIFEXITED(status)) {
		printf("exited, status=%d\n", WEXITSTATUS(status));
	} else if (WIFSIGNALED(status)) {
		printf("killed by signal %d\n", WTERMSIG(status));
	}

	remove_dir(cfg, str_dir);
	return 0;
}