aboutsummaryrefslogtreecommitdiffstats
path: root/src/udev-kvm-check.c
blob: 43cb9d93b547933f4b5ca391b65fea734680160c (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
#include <ctype.h>
#include <syslog.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DEFAULT 0
#define FACILITY "kvm"
#define SYSCONFIG_KVM "/etc/sysconfig/kvm"

#define COUNT_MSG \
    "%d %s now active"

#define SUBSCRIPTION_MSG \
    "%d %s now active; your Red Hat Enterprise Linux subscription" \
    " limit is %d guests. Please review your Red Hat Enterprise Linux" \
    " subscription agreement or contact your Red Hat" \
    " support representative for more information. You" \
    " may review the Red Hat Enterprise subscription" \
    " limits at http://www.redhat.com/rhel-virt-limits"

int get_threshold_from_file(FILE *fp)
{
    static const char key[] = "THRESHOLD=";
    int pos = 0;
    int thres;
    char ch;

start:
    /* State START - at beginning of line, search for beginning of "THRESHOLD="
     * string.
     */
    ch = getc(fp);
    if (ch == EOF) {
        return DEFAULT;
    }
    if (isspace(ch)) {
        goto start;
    }
    if (ch == 'T') {
        pos = 1;
        goto key;
    }
    goto eol;

eol:
    /* State EOL - loop until end of line */
    ch = getc(fp);
    if (ch == EOF) {
        return DEFAULT;
    }
    if (ch == '\n') {
        goto start;
    }
    goto eol;

key:
    /* State KEY - match "THRESHOLD=" string, go to THRESHOLD if found */
    ch = getc(fp);
    if (ch == EOF) {
        return DEFAULT;
    }
    if (ch == key[pos]) {
        pos++;
        if (key[pos] == 0) {
            goto threshold;
        } else {
            goto key;
        }
    }
    goto eol;

threshold:
    /* State THRESHOLD - parse number using fscanf, expect comment or space
     * or EOL.
     */
    ch = getc(fp);
    if (ch == EOF) {
        return DEFAULT;
    }
    if (!isdigit(ch)) {
        goto eol;
    }
    ungetc(ch, fp);
    if (fscanf(fp, "%d", &thres) != 1) {
        return DEFAULT;
    }
    ch = getc(fp);
    if (ch == '#' || ch == EOF || ch == '\n' || isspace(ch)) {
        return thres;
    }
    goto eol;
}

int get_threshold()
{
    FILE *fp = fopen(SYSCONFIG_KVM, "r");
    int val = get_threshold_from_file(fp);

    fclose (fp);
    return val;
}

const char *guest(int count)
{
    return (count == 1 ? "guest" : "guests");
}

void emit_count_message(int count)
{
    openlog(FACILITY, LOG_CONS, LOG_USER);
    syslog(LOG_INFO, COUNT_MSG, count, guest(count));
    closelog();
}

void emit_subscription_message(int count, int threshold)
{
    openlog(FACILITY, LOG_CONS, LOG_USER);
    syslog(LOG_WARNING, SUBSCRIPTION_MSG, count, guest(count), threshold);
    closelog();
}

int main(int argc, char **argv)
{
    int count, threshold;

    if (argc < 3)
        exit(1);

    count = atoi(argv[1]);
    threshold = get_threshold();

    if (!strcmp(argv[2], "create")) {
        if (threshold == 0) {
            emit_count_message(count);
        } else if (count > threshold) {
            emit_subscription_message(count, threshold);
        }
    } else {
        if (count >= threshold) {
            emit_count_message(count);
        }
    }

    return 0;
}