File: checkattr.c

package info (click to toggle)
libkdumpfile 0.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,800 kB
  • sloc: ansic: 34,639; sh: 3,853; python: 1,490; makefile: 755
file content (486 lines) | stat: -rw-r--r-- 11,159 bytes parent folder | download | duplicates (2)
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/* Check dump attributes.
   Copyright (C) 2016 Petr Tesarik <ptesarik@suse.com>

   This file is free software; you can redistribute it and/or modify
   it under the terms of either

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at
       your option) any later version

   or

     * 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

   or both in parallel, as here.

   libkdumpfile 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 copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see <http://www.gnu.org/licenses/>.
*/

#define _GNU_SOURCE

#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <libkdumpfile/kdumpfile.h>

#include "testutil.h"

static int
check_noattr(kdump_ctx_t *ctx, char *key)
{
	kdump_attr_t attr;
	kdump_status res;

	printf("Checking no %s... ", key);
	res = kdump_get_attr(ctx, key, &attr);
	if (res == KDUMP_OK) {
		puts("FAILED");
		return TEST_FAIL;
	} else if (res != KDUMP_ERR_NODATA) {
		fprintf(stderr, "Cannot get attribute %s: %s\n",
			key, kdump_get_err(ctx));
		return TEST_ERR;
	}

	puts("OK");
	return TEST_OK;
}

static int
check_attr(kdump_ctx_t *ctx, char *key, const kdump_attr_t *expect, int chkval)
{
	kdump_attr_t attr;

	printf("Checking %s... ", key);
	if (kdump_get_attr(ctx, key, &attr) != KDUMP_OK) {
		puts("FAILED");
		fprintf(stderr, "Cannot get attribute %s: %s\n",
			key, kdump_get_err(ctx));
		return TEST_FAIL;
	}

	if (attr.type != expect->type) {
		puts("FAILED");
		fprintf(stderr, "Type mismatch for %s: expect %u, got %u\n",
			key, (unsigned) expect->type, (unsigned) attr.type);
		return TEST_FAIL;
	}

	if (!chkval)
		goto out;

	switch (attr.type) {
	case KDUMP_DIRECTORY:
		/* Nothing to check (beyond type) */
		break;

	case KDUMP_NUMBER:
		if (attr.val.number == expect->val.number)
			break;

		puts("FAILED");
		fprintf(stderr, "%s value mismatch: ", key);
		fprintf(stderr, "expect %llu, got %llu\n",
			(unsigned long long) expect->val.number,
			(unsigned long long) attr.val.number);
		return TEST_FAIL;

	case KDUMP_ADDRESS:
		if (attr.val.address == expect->val.address)
			break;

		puts("FAILED");
		fprintf(stderr, "%s value mismatch: ", key);
		fprintf(stderr, "expect 0x%016llx, got 0x%016llx\n",
			(unsigned long long) expect->val.address,
			(unsigned long long) attr.val.address);
		return TEST_FAIL;

	case KDUMP_STRING:
		if (!strcmp(attr.val.string, expect->val.string))
			break;

		puts("FAILED");
		fprintf(stderr, "%s value mismatch: ", key);
		fprintf(stderr, "expect %s, got %s\n",
			expect->val.string, attr.val.string);
		return TEST_FAIL;

	default:
		puts("FATAL");
		fprintf(stderr, "INTERNAL ERROR: Invalid attr type: %u\n",
			(unsigned) attr.type);
		return TEST_FAIL;
	}

 out:
	puts("OK");
	return TEST_OK;
}

static inline int
bit_value(const struct number_array *nums, unsigned bit)
{
	unsigned off = bit >> 3;
	return off < nums->n
		? nums->val[off] & (1ULL << (bit & 7))
		: 0;
}
static int
check_attr_bmp(kdump_ctx_t *ctx, char *key, const struct number_array *expect)
{
	kdump_attr_t attr;
	unsigned char bits[expect->n];
	kdump_status status;
	kdump_addr_t bit;
	unsigned i;

	printf("Checking %s... ", key);
	if (kdump_get_attr(ctx, key, &attr) != KDUMP_OK) {
		puts("FAILED");
		fprintf(stderr, "Cannot get attribute %s: %s\n",
			key, kdump_get_err(ctx));
		return TEST_FAIL;
	}

	if (attr.type != KDUMP_BITMAP) {
		puts("FAILED");
		fprintf(stderr, "Type mismatch for %s: expect %u, got %u\n",
			key, (unsigned) KDUMP_BITMAP, (unsigned) attr.type);
		return TEST_FAIL;
	}

	status = kdump_bmp_get_bits(attr.val.bitmap,
				    0, (expect->n << 3) - 1, bits);
	if (status != KDUMP_OK) {
		puts("FAILED");
		fprintf(stderr, "Cannot get bitmap bits: %s\n",
			kdump_bmp_get_err(attr.val.bitmap));
		return TEST_FAIL;
	}

	for (i = 0; i < expect->n; ++i) {
		if (bits[i] != expect->val[i]) {
			puts("FAILED");
			fprintf(stderr, "%s value mismatch at index %u: ",
				key, i);
			fprintf(stderr, "expect 0x%02x, got 0x%02x\n",
				(unsigned) expect->val[i], bits[i]);
			return TEST_FAIL;
		}
	}

	bit = 0;
	while (bit < (expect->n << 3)) {
		kdump_addr_t clear = bit;
		kdump_addr_t set = bit;
		kdump_addr_t next;

		status = kdump_bmp_find_clear(attr.val.bitmap, &clear);
		if (status != KDUMP_OK) {
			puts("FAILED");
			fprintf(stderr, "Cannot find %s bit at %" KDUMP_PRIuADDR ": %s\n",
				"clear", bit, kdump_bmp_get_err(attr.val.bitmap));
			return TEST_FAIL;

		}
		if (clear < bit) {
			puts("FAILED");
			fprintf(stderr, "Invalid %s bit: %" KDUMP_PRIuADDR
				" < %" KDUMP_PRIuADDR "\n", "clear", clear, bit);
			return TEST_FAIL;
		}

		status = kdump_bmp_find_set(attr.val.bitmap, &set);
		if (status == KDUMP_ERR_NODATA) {
			set = expect->n << 3;
		} else if (status != KDUMP_OK) {
			puts("FAILED");
			fprintf(stderr, "Cannot find %s bit at %" KDUMP_PRIuADDR ": %s\n",
				"set", bit, kdump_bmp_get_err(attr.val.bitmap));
			return TEST_FAIL;

		}
		if (set < bit) {
			puts("FAILED");
			fprintf(stderr, "Invalid %s bit: %" KDUMP_PRIuADDR
				" < %" KDUMP_PRIuADDR "\n", "set", set, bit);
			return TEST_FAIL;
		}

		if (set == clear) {
			puts("FAILED");
			fprintf(stderr, "Bit %" KDUMP_PRIuADDR " both %s and %s\n",
				set, "clear", "set");
			return TEST_FAIL;
		}

		for (next = bit; next < clear; ++next)
			if (bit_value(expect, next) == 0) {
				puts("FAILED");
				fprintf(stderr, "%s %s bit %" KDUMP_PRIuADDR " not found!\n",
					key, "clear", next);
				return TEST_FAIL;
			}

		for (next = bit; next < set; ++next)
			if (bit_value(expect, next) != 0) {
				puts("FAILED");
				fprintf(stderr, "%s %s bit %" KDUMP_PRIuADDR " not found!\n",
					key, "set", next);
				return TEST_FAIL;
			}

		bit = clear > set ? clear : set;
	}

	puts("OK");
	return TEST_OK;
}

static int
check_attr_blob(kdump_ctx_t *ctx, char *key, const struct blob *expect)
{
	kdump_attr_t attr;
	unsigned char *data;
	size_t size;
	size_t i;

	printf("Checking %s... ", key);
	if (kdump_get_attr(ctx, key, &attr) != KDUMP_OK) {
		puts("FAILED");
		fprintf(stderr, "Cannot get attribute %s: %s\n",
			key, kdump_get_err(ctx));
		return TEST_FAIL;
	}

	if (attr.type != KDUMP_BLOB) {
		puts("FAILED");
		fprintf(stderr, "Type mismatch for %s: expect %u, got %u\n",
			key, (unsigned) KDUMP_BLOB, (unsigned) attr.type);
		return TEST_FAIL;
	}

	data = kdump_blob_pin(attr.val.blob);
	size = kdump_blob_size(attr.val.blob);
	if (size != expect->length) {
		kdump_blob_unpin(attr.val.blob);
		puts("FAILED");
		fprintf(stderr, "Size mismatch for %s: expect %zd, got %zd\n",
			key, expect->length, size);
		return TEST_FAIL;
	}

	for (i = 0; i < size; ++i) {
		if (data[i] != expect->data[i]) {
			kdump_blob_unpin(attr.val.blob);
			puts("FAILED");
			fprintf(stderr, "%s value mismatch at index %zu: ",
				key, i);
			fprintf(stderr, "expect 0x%02x, got 0x%02x\n",
				expect->data[i], data[i]);
			return TEST_FAIL;
		}
	}
	kdump_blob_unpin(attr.val.blob);

	puts("OK");
	return TEST_OK;
}

static int
check_attr_val(kdump_ctx_t *ctx, char *key, char *val)
{
	char *sep, *p, savedsep;
	unsigned long long number;
	char *string;
	struct number_array number_array;
	struct blob *blob;
	struct param param;
	kdump_attr_t attr;
	int rc;

	sep = strchrnul(val, ':');
	savedsep = *sep;

	p = sep;
	do {
		*p-- = '\0';
	} while (p >= val && isspace(*p));

	if (!strcmp(val, "directory")) {
		attr.type = KDUMP_DIRECTORY;
		return check_attr(ctx, key, &attr, 0);
	} else if (!strcmp(val, "number")) {
		attr.type = KDUMP_NUMBER;
		param.type = param_number;
		param.number = &number;
	} else if (!strcmp(val, "address")) {
		attr.type = KDUMP_ADDRESS;
		param.type = param_number;
		param.number = &number;
	} else if (!strcmp(val, "string")) {
		attr.type = KDUMP_STRING;
		param.type = param_string;
		param.string = &string;
		string = NULL;
	} else if (!strcmp(val, "bitmap")) {
		attr.type = KDUMP_BITMAP;
		param.type = param_number_array;
		param.number_array = &number_array;
		number_array.val = NULL;
	} else if (!strcmp(val, "blob")) {
		attr.type = KDUMP_BLOB;
		param.type = param_blob;
		param.blob = &blob;
		blob = NULL;
	} else if (!strcmp(val, "nil")) {
		return check_noattr(ctx, key);
	} else {
		fprintf(stderr, "Invalid type: %s\n", val);
		return TEST_FAIL;
	}

	if (!savedsep)
		return check_attr(ctx, key, &attr, 0);

	param.key = key;

	p = sep + 1;
	while (*p && isspace(*p))
		++p;

	rc = set_param(&param, p);
	if (rc != TEST_OK)
		return rc;

	switch (attr.type) {
	case KDUMP_NUMBER:  attr.val.number = number;  break;
	case KDUMP_ADDRESS: attr.val.address = number; break;
	case KDUMP_STRING:  attr.val.string = string;  break;
	case KDUMP_BITMAP:
		return check_attr_bmp(ctx, key, &number_array);
	case KDUMP_BLOB:
		return check_attr_blob(ctx, key, blob);
	default:
		fprintf(stderr, "INTERNAL ERROR: Invalid attr type: %u\n",
			(unsigned) attr.type);
		return TEST_FAIL;
	}

	return check_attr(ctx, key, &attr, 1);
}

static int
check_attrs(FILE *parm, kdump_ctx_t *ctx)
{
	char *line, *key, *val;
	size_t linesz;
	unsigned linenum;
	int tmprc, rc = TEST_OK;

	line = NULL;
	linesz = 0;
	linenum = 0;

	puts("Dump looks fine. Now checking attributes.");
	while (getline(&line, &linesz, parm) > 0) {
		++linenum;

		if (parse_key_val(line, &key, &val)) {
			fprintf(stderr, "Malformed check: %s\n", line);
			rc = TEST_FAIL;
			break;
		}

		if (!key)
			continue;

		tmprc = check_attr_val(ctx, key, val);
		if (tmprc != TEST_OK) {
			rc = tmprc;
			fprintf(stderr, "Error on line #%d\n", linenum);
			if (rc == TEST_ERR)
				break;
		}
	}

	if (line)
		free(line);
	return rc;
}

static int
check_attrs_fds(FILE *parm, unsigned nfiles, const int *dumpfds)
{
	kdump_ctx_t *ctx;
	kdump_status res;
	int rc;

	ctx = kdump_new();
	if (!ctx) {
		perror("Cannot initialize dump context");
		return TEST_ERR;
	}

	res = kdump_open_fdset(ctx, nfiles, dumpfds);
	if (res != KDUMP_OK) {
		fprintf(stderr, "Cannot open dump: %s\n", kdump_get_err(ctx));
		rc = TEST_ERR;
	} else
		rc = check_attrs(parm, ctx);

	kdump_free(ctx);
	return rc;
}

static int
check_attr_args(unsigned nfiles, char **argv)
{
	unsigned i;
	int fds[nfiles];
	int rc;

	for (i = 0; i < nfiles; ++i) {
		fds[i] = open(argv[i], O_RDONLY);
		if (fds[i] < 0) {
			perror(argv[i]);
			return TEST_ERR;
		}
	}

	rc = check_attrs_fds(stdin, nfiles, fds);

	for (i = 0; i < nfiles; ++i)
		if (close(fds[i]) < 0) {
			perror("Cannot close dump file");
			rc = TEST_ERR;
		}

	return rc;
}

int
main(int argc, char **argv)
{
	if (argc < 2) {
		fprintf(stderr, "Usage: %s <dump>[...]\n", argv[0]);
		return TEST_ERR;
	}

	return check_attr_args(argc - 1, argv + 1);
}