summaryrefslogtreecommitdiff
path: root/tools/sanity_check/main.cpp
blob: 29245cd37a96f7e94ae1b79a5e7a23ee22cf3af8 (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
/*
 *
 *   program:  sanityCheck
 *   file:     main.c
 *   author:   Todd Naugle
 *   date:     11/17/2010
 *
 *   Desc:  Command line version of the sanity check functions found in jack
*/

#include <algorithm>
#include <stdio.h>
#include <string>
#include <vector>

#include "systemtest.h"


using namespace std;

typedef int (*testfuncPtr) ();
typedef int (*testfuncOpPtr) (string);

typedef struct
{
	string			switchText;			// ie -option
	string			swOptionText;       // option arguments for just this swtich.
	string			descriptionText;	// Help Text on what this does
	string			failureText;		// What to say when this test fails
	bool			hasOption;			// Set true if this switch has option paramters
	testfuncPtr		functionPtr;		// Function to call
	testfuncOpPtr	opFunctionPtr;		// Function with option string to call
	string			optionArg;			// Storage used to hold any options passed in by the user
} testRecord;

static vector<testRecord>	gTestSet;

static vector<string>		gValidSwitchList;
static vector<string>		gSwitchDescriptionList;

static vector<string>		gSwitchesReceived;

int
ExecuteAll()
{
	bool OK = true;

	OK &= system_user_can_rtprio();

	if (system_has_frequencyscaling()) {
		OK &= !system_uses_frequencyscaling();
	}

	OK &= !(system_memlock_amount() == 0);

	return OK;
}

int
HasGroup(string name)
{
	return system_has_group(name.c_str());
}

int
IsMemberOfGroup(string name)
{
	return system_user_in_group(name.c_str());
}

int
CheckFreqScaling()
{
	bool OK = true;

	if (system_has_frequencyscaling()) {
		OK &= !system_uses_frequencyscaling();
	}

	return OK;
}

int
CheckMemoryLocking()
{
	return !(system_memlock_amount() == 0);
}

int
PrintUsage()
{
	printf("\n");
	printf("  sanityCheck - A program to verify proper system settings for use with audio applications (Ardour/Jack/Mixbus).\n");
	printf("\n");
	printf("  Usage:  sanityCheck [OPTIONS]\n");
	printf("\n");
	printf("  Options are as follows:\n");
	printf("\n");
	printf("\n");

	vector<testRecord>::iterator		itr;

	for (itr = gTestSet.begin(); itr != gTestSet.end(); ++itr) {
		printf("%20s %s :\t%s\n", (*itr).switchText.c_str(), (*itr).swOptionText.c_str(), (*itr).descriptionText.c_str());
	}

	printf("\n");

	return true;
}

void
DefineSwitches()
{
	testRecord rec;

	// Global switches
	rec.switchText = "-a";
	rec.swOptionText = "";
	rec.descriptionText = "Checks for a working RT system. Same as -rt -freqscaling -memlock";
	rec.failureText = "";
	rec.hasOption = false;
	rec.functionPtr = &ExecuteAll;
	rec.optionArg = "";
	gTestSet.push_back(rec);

	rec.switchText = "-h";
	rec.swOptionText = "";
	rec.descriptionText = "Print usage";
	rec.failureText = "";
	rec.hasOption = false;
	rec.functionPtr = &PrintUsage;
	rec.optionArg = "";
	gTestSet.push_back(rec);

	// Switches for various tests that can be performed.
	rec.switchText = "-rt";
	rec.swOptionText = "";
	rec.descriptionText = "Verfiy that the user can run tasks with realtime priority";
	rec.failureText = "";
	rec.hasOption = false;
	rec.functionPtr = &system_user_can_rtprio;
	rec.optionArg = "";
	gTestSet.push_back(rec);

	rec.switchText = "-hasrtlimits";
	rec.swOptionText = "";
	rec.descriptionText = "Verfiy the system has a limits.conf and the audio group can use realtime";
	rec.failureText = "";
	rec.hasOption = false;
	rec.functionPtr = &system_has_rtprio_limits_conf;
	rec.optionArg = "";
	gTestSet.push_back(rec);

	rec.switchText = "-hasgroup";
	rec.swOptionText = "<groupname>";
	rec.descriptionText = "Verfiy that the system has a group named <groupname>";
	rec.failureText = "";
	rec.hasOption = true;
	rec.opFunctionPtr = &HasGroup;
	rec.optionArg = "";
	gTestSet.push_back(rec);

	rec.switchText = "-hasaudiogroup";
	rec.swOptionText = "";
	rec.descriptionText = "Verfiy that the system has an audio group (audio or jackuser) defined";
	rec.failureText = "";
	rec.hasOption = false;
	rec.functionPtr = &system_has_audiogroup;
	rec.optionArg = "";
	gTestSet.push_back(rec);

	rec.switchText = "-memberofgroup";
	rec.swOptionText = "<groupname>";
	rec.descriptionText = "Verfiy that the user is a member of the group named <groupname>";
	rec.failureText = "";
	rec.hasOption = true;
	rec.opFunctionPtr = &IsMemberOfGroup;
	rec.optionArg = "";
	gTestSet.push_back(rec);

	rec.switchText = "-memberaudiogroup";
	rec.swOptionText = "";
	rec.descriptionText = "Verfiy that the user is a member of the audio group (audio or jackuser)";
	rec.failureText = "";
	rec.hasOption = false;
	rec.functionPtr = &system_user_in_audiogroup;
	rec.optionArg = "";
	gTestSet.push_back(rec);

	rec.switchText = "-freqscaling";
	rec.swOptionText = "";
	rec.descriptionText = "Check to see if frequency scaling is being used by the CPU";
	rec.failureText = "";
	rec.hasOption = false;
	rec.functionPtr = &CheckFreqScaling;
	rec.optionArg = "";
	gTestSet.push_back(rec);

	rec.switchText = "-memlock";
	rec.swOptionText = "";
	rec.descriptionText = "Check to see if the user is able to lock memory";
	rec.failureText = "";
	rec.hasOption = false;
	rec.functionPtr = &CheckMemoryLocking;
	rec.optionArg = "";
	gTestSet.push_back(rec);

}

bool
ParseSwitches(
	int		argc,
	char	**argv)
{
	string	tmp;
	vector<testRecord>::iterator	itr;
	bool	OK = true;
	int	i;

	if (argc == 1) {
		gSwitchesReceived.push_back("-a");
	}
	else {
		for (i = 1; i < argc && OK == true; i++) {
			tmp = argv[i];

			for (itr = gTestSet.begin(); itr != gTestSet.end(); ++itr) {
				if (tmp == (*itr).switchText) {
					if ((*itr).hasOption == true) {
						if (++i < argc) {
							string	op = argv[i];
							if (op[0] == '-') {
								// reqiured option for this switch is missing
								--i;
								OK = false;
								break;
							}
							(*itr).optionArg = op;
							break;
						}
						else {
							// reqiured option for this switch is missing
							--i;
							OK = false;
							break;
						}
					}
					break;
				}
			}

			if (OK && itr != gTestSet.end()) {
				// Known option switch found
				gSwitchesReceived.push_back(tmp);
			}
			else {
				// Unknown option
				OK = false;
			}
		}
	}

	if (OK) {
		// All switches are at least valid, now check to make sure they are all valid to
		// be used together.

		if (gSwitchesReceived.size() > 1) {
			// make sure help is not mixed with other options
			vector<string>::iterator	swItr;
			tmp = "-h";

			swItr = find(gSwitchesReceived.begin(), gSwitchesReceived.end(), tmp);

			if (swItr != gSwitchesReceived.end()) {
				gSwitchesReceived.clear();
				gSwitchesReceived.push_back("-h");
			}

			// make sure -a is only used by itself
			tmp = "-a";
			swItr = find(gSwitchesReceived.begin(), gSwitchesReceived.end(), tmp);

			if (swItr != gSwitchesReceived.end()) {
				gSwitchesReceived.clear();
				gSwitchesReceived.push_back("-a");
			}
		}

		return true;
	}
	else {
		fprintf(stderr, "\n");
		fprintf(stderr, "ERROR - Invalid Option: %s\n", (const char *) argv[--i]);
		fprintf(stderr, "Check syntax\n");
		PrintUsage();
		return false;
	}
}

bool
Execute()
{
	bool OK = true;
	vector<string>::iterator	itr;
	vector<testRecord>::iterator	testItr;

	for (itr = gSwitchesReceived.begin(); itr != gSwitchesReceived.end(); ++itr) {
		for (testItr = gTestSet.begin(); testItr != gTestSet.end(); ++testItr) {
			if ((*itr) == (*testItr).switchText) {
				break;
			}
		}

		bool result;
		if ((*testItr).hasOption) {
			result = ((*testItr).opFunctionPtr((*testItr).optionArg) != 0);
		}
		else {
			result = ((*testItr).functionPtr() != 0);
		}

		if (result == 0) {
			// Check for a Failure message and print it if found.
			if (!(*testItr).failureText.empty()) {
				printf("\n%s\n", (*testItr).failureText.c_str());
			}
		}

		OK &= result;
	}

	return OK;
}

int
main(
	int		 argc,
	char 	**argv)
{
	int status = 0;

	DefineSwitches();

	if (ParseSwitches(argc, argv)) {
		if (Execute() == false) {
			printf("\nSanity Check Failed!\n\n");
			status = -1;
		}
		else {
			printf("\nSanity Check OK!\n\n");
		}
	}
	else {
		status = -1;
	}

	return status;
}