summaryrefslogtreecommitdiffstats
path: root/src/ctl.c
blob: 91ff09eef2d6f266885e1bdfa469788810bea9bd (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
/*
  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 <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>

#include "config.h"


static void usage() NORETURN;
static void usage()
{
	printf("usage: latrace-ctl [-d] config\n\n");
	printf("    -d <0/1>  - disable/enable auditing\n");
	printf("\n");

	exit(1);
}

static int mmap_config(struct lt_config_ctl *cfg)
{
	struct lt_config_shared* sh;
	int page = sysconf(_SC_PAGE_SIZE);
	int fd, len;

	if (-1 == (fd = open(cfg->config, O_RDWR))) {
		perror("open failed");
		return -1;
	}

	/* align the shared config length */
	len = sizeof(struct lt_config_shared);
	len = (len + page) & ~(page - 1);

	sh = mmap(NULL, len,
		PROT_READ | PROT_WRITE,
		MAP_SHARED, fd, 0);

	if ((void *) -1 == sh) {
		PRINT_VERBOSE(cfg, 1,
			"mmap failed: %s\n", strerror(errno));
		return -1;
	}

	cfg->sh = sh;
	return 0;
}

static int config_ctl(struct lt_config_ctl *cfg, int argc, char **argv)
{
	memset(cfg, 0x0, sizeof(*cfg));

	while (1) {
		int c;
		int option_index = 0;
		static struct option long_options[] = {
			{"disable", required_argument, 0, 'd'},
			{"help", no_argument, 0, 'h'},
			{0, 0, 0, 0}
		};

		c = getopt_long(argc, argv, "+d:",
                        long_options, &option_index);

		if (c == -1)
			break;

		switch (c) {
		case 'd':
			cfg->set_disabled = 1;
			cfg->disabled = atoi(optarg);
			break;
		}; /* switch */
	} /* while */

	if (optind < argc)
		cfg->config = argv[optind];
	else {
		printf("failed: no config specified\n");
		usage();
	}

	return 0;
}

int main_ctl(int argc, char **argv)
{
	struct lt_config_ctl cfg;

	if (config_ctl(&cfg, argc, argv))
		return -1;

	if (mmap_config(&cfg))
		return -1;

	printf("controling config %s\n", cfg.config);

	if (cfg.set_disabled) {
		printf(" -> disabled = %d\n", cfg.disabled);
		lt_sh(&cfg, disabled) = cfg.disabled;
	}

	return 0;
}