File: rasdaemon.c

package info (click to toggle)
rasdaemon 0.8.4-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 2,832 kB
  • sloc: ansic: 17,870; sh: 4,983; perl: 2,383; makefile: 212
file content (242 lines) | stat: -rw-r--r-- 5,287 bytes parent folder | download
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
// SPDX-License-Identifier: GPL-2.0-or-later

/*
 * Copyright (C) 2013 Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
 */

#include <argp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "ras-erst.h"
#include "ras-events.h"
#include "ras-logger.h"
#include "ras-poison-page-stat.h"
#include "ras-record.h"
#include "ras-mc-handler.h"
#include "types.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>"
#define DISABLE "DISABLE"
#define MC_CE_STAT_THRESHOLD "MC_CE_STAT_THRESHOLD"
#define POISON_STAT_THRESHOLD "POISON_STAT_THRESHOLD"

const char *argp_program_version = TOOL_NAME " " VERSION;
const char *argp_program_bug_address = "Mauro Carvalho Chehab <mchehab@kernel.org>";

struct arguments {
	int record_events;
	int enable_ras;
	int enable_ipmitool;
	int foreground;
	int offline;
};

enum OFFLINE_ARG_KEYS {
	SMCA = 0x100,
	MODEL,
	FAMILY,
	BANK_NUM,
	IPID_REG,
	STATUS_REG,
	SYNDROME_REG
};

struct ras_mc_offline_event event;

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
#ifdef HAVE_OPENBMC_UNIFIED_SEL
	case 'i':
		args->enable_ipmitool++;
		break;
#endif
	case 'f':
		args->foreground++;
		break;
#ifdef HAVE_MCE
	case 'p':
		if (state->argc < 4)
			argp_state_help(state, stdout, ARGP_HELP_LONG | ARGP_HELP_EXIT_ERR);
		args->offline++;
		break;
#endif
	default:
		return ARGP_ERR_UNKNOWN;
	}
	return 0;
}

#ifdef HAVE_MCE
static error_t parse_opt_offline(int key, char *arg,
				 struct argp_state *state)
{
	switch (key) {
	case SMCA:
		event.smca = true;
		break;
	case MODEL:
		event.model = strtoul(arg, NULL, 0);
		break;
	case FAMILY:
		event.family = strtoul(arg, NULL, 0);
		break;
	case BANK_NUM:
		event.bank = atoi(arg);
		break;
	case IPID_REG:
		event.ipid = strtoull(arg, NULL, 0);
		break;
	case STATUS_REG:
		event.status = strtoull(arg, NULL, 0);
		break;
	case SYNDROME_REG:
		event.synd = strtoull(arg, NULL, 0);
		break;
	default:
		return ARGP_ERR_UNKNOWN;
	}
	return 0;
}
#endif

long user_hz;

int main(int argc, char *argv[])
{
	struct arguments args;
	int idx = -1;

	choices_disable = getenv(DISABLE);

	if (getenv(MC_CE_STAT_THRESHOLD))
		mc_ce_stat_threshold = strtoull(getenv(MC_CE_STAT_THRESHOLD), NULL, 0);
	if (mc_ce_stat_threshold)
		log(TERM, LOG_INFO, "Threshold of memory Corrected Errors statistics is %lld\n", mc_ce_stat_threshold);

#ifdef HAVE_POISON_PAGE_STAT
	if (getenv(POISON_STAT_THRESHOLD))
		poison_stat_threshold = strtoull(getenv(POISON_STAT_THRESHOLD), NULL, 0);
	if (poison_stat_threshold)
		log(TERM, LOG_INFO, "Threshold of poison page statistics is %lld kB\n", poison_stat_threshold);
#endif

#ifdef HAVE_MCE
	const struct argp_option offline_options[] = {
		{"smca", SMCA, 0, 0, "AMD SMCA Error Decoding"},
		{"model", MODEL, "MODEL", 0, "CPU Model"},
		{"family", FAMILY, "FAMILY", 0, "CPU Family"},
		{"bank", BANK_NUM, "BANK_NUM", 0, "Bank Number"},
		{"ipid", IPID_REG, "IPID_REG", 0, "IPID Register (for SMCA systems only)"},
		{"status", STATUS_REG, "STATUS_REG", 0, "Status Register"},
		{"synd", SYNDROME_REG, "SYNDROME_REG", 0, "Syndrome Register"},
		{0, 0, 0, 0, 0, 0},
	};

	struct argp offline_argp = {
		.options = offline_options,
		.parser = parse_opt_offline,
		.doc = TOOL_DESCRIPTION,
		.args_doc = ARGS_DOC,
	};

	struct argp_child offline_parser[] = {
		{&offline_argp, 0, "Post-Processing Options:", 0},
		{0, 0, 0, 0},
	};
#endif

	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"},
#ifdef HAVE_OPENBMC_UNIFIED_SEL
		{"ipmitool", 'i', 0, 0, "enable ipmitool logging", 0},
#endif
#ifdef HAVE_MCE
		{"post-processing", 'p', 0, 0,
		"Post-processing MCE's with raw register values"},
#endif

		{ 0, 0, 0, 0, 0, 0 }
	};
	const struct argp argp = {
		.options = options,
		.parser = parse_opt,
		.doc = TOOL_DESCRIPTION,
		.args_doc = ARGS_DOC,
#ifdef HAVE_MCE
		.children = offline_parser,
#endif
	};
	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;
	}

#ifdef HAVE_MCE
	if (args.offline) {
		ras_offline_mce_event(&event);
		return 0;
	}
#endif

	openlog(TOOL_NAME, 0, LOG_DAEMON);
	if (!args.foreground)
		if (daemon(0, 0))
			exit(EXIT_FAILURE);

#ifdef HAVE_ERST
#ifdef HAVE_MCE
	if (choices_disable && strlen(choices_disable) != 0 &&
	    strstr(choices_disable, "ras:erst"))
		log(ALL, LOG_INFO, "Disabled ras:erst from config\n");
	else
		handle_erst();
#endif
#endif

	handle_ras_events(args.record_events, args.enable_ipmitool);

	return 0;
}