forked from mchehab/rasdaemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rasdaemon.c
122 lines (101 loc) · 2.72 KB
/
rasdaemon.c
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
/*
* Copyright (C) 2013 Mauro Carvalho Chehab <[email protected]>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <argp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "ras-record.h"
#include "ras-logger.h"
#include "ras-events.h"
/*
* Arguments(argp) handling logic and main
*/
#define TOOL_NAME "rasdaemon"
#define TOOL_DESCRIPTION "RAS daemon to log the RAS events."
#define ARGS_DOC "<options>"
const char *argp_program_version = TOOL_NAME " " VERSION;
const char *argp_program_bug_address = "Mauro Carvalho Chehab <[email protected]>";
struct arguments {
int record_events;
int enable_ras;
int foreground;
};
static error_t parse_opt(int k, char *arg, struct argp_state *state)
{
struct arguments *args = state->input;
switch (k) {
case 'e':
args->enable_ras++;
break;
case 'd':
args->enable_ras--;
break;
#ifdef HAVE_SQLITE3
case 'r':
args->record_events++;
break;
#endif
case 'f':
args->foreground++;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
long user_hz;
int main(int argc, char *argv[])
{
struct arguments args;
int idx = -1;
const struct argp_option options[] = {
{"enable", 'e', 0, 0, "enable RAS events and exit", 0},
{"disable", 'd', 0, 0, "disable RAS events and exit", 0},
#ifdef HAVE_SQLITE3
{"record", 'r', 0, 0, "record events via sqlite3", 0},
#endif
{"foreground", 'f', 0, 0, "run foreground, not daemonize"},
{ 0, 0, 0, 0, 0, 0 }
};
const struct argp argp = {
.options = options,
.parser = parse_opt,
.doc = TOOL_DESCRIPTION,
.args_doc = ARGS_DOC,
};
memset (&args, 0, sizeof(args));
user_hz = sysconf(_SC_CLK_TCK);
argp_parse(&argp, argc, argv, 0, &idx, &args);
if (idx < 0) {
argp_help(&argp, stderr, ARGP_HELP_STD_HELP, TOOL_NAME);
return -1;
}
if (args.enable_ras) {
int enable;
enable = (args.enable_ras > 0) ? 1 : 0;
toggle_ras_mc_event(enable);
return 0;
}
openlog(TOOL_NAME, 0, LOG_DAEMON);
if (!args.foreground)
if (daemon(0,0))
exit(EXIT_FAILURE);
handle_ras_events(args.record_events);
return 0;
}