summaryrefslogtreecommitdiff
path: root/qa/src/exercise_fault.c
blob: 3c75471f1838742d6c163defc0a97cc3001b2af9 (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
/*
 * Copyright (c) 2011 Ken McDonell.  All Rights Reserved.
 *
 * Exercise fault injection infrastructure.
 */

#include <pcp/pmapi.h>
#include <pcp/impl.h>
#include <pcp/fault.h>
#include <string.h>
#include <errno.h>

static void
exercise(void)
{
    int		i;
    void	*p;
    for (i = 1; i <= 10; i++) {
	__pmFaultInject("QA:1", PM_FAULT_ALLOC);
	p = malloc(10);
	if (p == NULL)
	    fprintf(stderr, "malloc:1[%d] %s\n", i, strerror(errno));
	else
	    free(p);
	__pmFaultInject("QA:2", PM_FAULT_ALLOC);
	p = malloc(100);
	if (p == NULL)
	    fprintf(stderr, "malloc:2[%d] %s\n", i, strerror(errno));
	else
	    free(p);
	__pmFaultInject("QA:3", PM_FAULT_ALLOC);
	p = malloc(1000);
	if (p == NULL)
	    fprintf(stderr, "malloc:3[%d] %s\n", i, strerror(errno));
	else
	    free(p);
    }
}

int
main(int argc, char *argv[])
{
    int		c;
    int		sts;
    int		errflag = 0;
    char	*usage = "[-D debug]";

    __pmSetProgname(argv[0]);

    while ((c = getopt(argc, argv, "D:")) != EOF) {
	switch (c) {

	case 'D':	/* debug flag */
	    sts = __pmParseDebug(optarg);
	    if (sts < 0) {
		fprintf(stderr, "%s: unrecognized debug flag specification (%s)\n",
		    pmProgname, optarg);
		errflag++;
	    }
	    else
		pmDebug |= sts;
	    break;

	case '?':
	default:
	    errflag++;
	    break;
	}
    }

    if (errflag || optind != argc) {
	fprintf(stderr, "Usage: %s %s\n", pmProgname, usage);
	exit(1);
    }

    exercise();

    return 0;
}