aboutsummaryrefslogtreecommitdiff
path: root/daemon/opd_pipe.c
blob: 3c8197956265b57af2b3887245c463955a28055b (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
/**
 * @file daemon/opd_pipe.c
 * Functions handling the $SESSIONDIR/opd_pipe FIFO special file.
 * NOTE: This code is dealing with potentially insecure input.
 *
 * @remark Copyright 2008 OProfile authors
 * @remark Read the file COPYING
 *
 * @author Daniel Hansel
 */

#include "opd_pipe.h"
#include "opd_printf.h"
#include "op_config.h"

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

static int fifo;
static FILE * fifo_fd = NULL;

void opd_create_pipe(void)
{
	mode_t orig_umask = umask(0111);
	if (mkfifo(op_pipe_file, 0666) == -1) {
		if (errno != EEXIST) {
			perror("oprofiled: couldn't create pipe: ");
			exit(EXIT_FAILURE);
		}
	}
	umask(orig_umask);
}


void opd_open_pipe(void)
{
	fifo = open(op_pipe_file, O_RDONLY | O_NONBLOCK);
	if (fifo == -1) {
		perror("oprofiled: couldn't open pipe: ");
		exit(EXIT_FAILURE);
	}
}


void opd_close_pipe(void)
{
	if (fifo_fd)
		fclose(fifo_fd);
	close(fifo);
}


int is_jitconv_requested(void)
{
	/* number of dropped (unknown) requests */
	static long nr_drops = 0;
	/* modulus to output only a few warnings to avoid flooding oprofiled.log */
	static int mod_cnt_drops = 1;
	char line[256];
	int i, ret = 0;

	/* get a file descriptor to the pipe */
	if (!fifo_fd)
		fifo_fd = fdopen(fifo, "r");

	if (fifo_fd == NULL) {
		perror("oprofiled: couldn't create file descriptor: ");
		exit(EXIT_FAILURE);
	}

	/* read up to 99 lines to check for 'do_jitconv' */
	for (i = 0; i < 99; i++) {
		/* just break if no new line is found */
		if (fgets(line, 256, fifo_fd) == NULL)
			break;
		line[strlen(line) - 1] = '\0';

		if (strstr(line, "do_jitconv") != NULL) {
			ret = 1;
		} else {
			nr_drops++;

			if (nr_drops % mod_cnt_drops == 0) {
				printf(
				       "Warning: invalid pipe request received (dropped request(s): %ld)\n",
				       nr_drops);
				/* increase modulus to avoid flooding log file */
				mod_cnt_drops *= 5;
			}
		}
	}

	return ret;
}