cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
ae23c9
/*
ae23c9
 * udev-kvm-check.c
ae23c9
 *
ae23c9
 * Copyright 2018 Red Hat, Inc.
ae23c9
 *
ae23c9
 * This is free software; you can redistribute it and/or modify it
ae23c9
 * under the terms of the GNU General Public License as published by
ae23c9
 * the Free Software Foundation; either version 2 of the License, or
ae23c9
 * (at your option) any later version.
ae23c9
 *
ae23c9
 * This program is distributed in the hope that it will be useful, but
ae23c9
 * WITHOUT ANY WARRANTY; without even the implied warranty of
ae23c9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ae23c9
 * General Public License for more details.
ae23c9
 *
ae23c9
 * You should have received a copy of the GNU General Public License
ae23c9
 * along with this program; if not, write to the Free Software Foundation,
ae23c9
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ae23c9
 *
ae23c9
 */
ae23c9
ae23c9
#include <ctype.h>
ae23c9
#include <syslog.h>
ae23c9
#include <stdio.h>
ae23c9
#include <stdlib.h>
ae23c9
#include <string.h>
ae23c9
ae23c9
#define DEFAULT 0
ae23c9
#define FACILITY "kvm"
ae23c9
#define SYSCONFIG_KVM "/etc/sysconfig/kvm"
ae23c9
ae23c9
#define COUNT_MSG \
ae23c9
    "%d %s now active"
ae23c9
ae23c9
#define SUBSCRIPTION_MSG \
ae23c9
    "%d %s now active; your Red Hat Enterprise Linux subscription" \
ae23c9
    " limit is %d guests. Please review your Red Hat Enterprise Linux" \
ae23c9
    " subscription agreement or contact your Red Hat" \
ae23c9
    " support representative for more information. You" \
ae23c9
    " may review the Red Hat Enterprise subscription" \
ae23c9
    " limits at http://www.redhat.com/rhel-virt-limits"
ae23c9
ae23c9
int get_threshold_from_file(FILE *fp)
ae23c9
{
ae23c9
    static const char key[] = "THRESHOLD=";
ae23c9
    int pos = 0;
ae23c9
    int thres;
ae23c9
    int ch;
ae23c9
ae23c9
start:
ae23c9
    /* State START - at beginning of line, search for beginning of "THRESHOLD="
ae23c9
     * string.
ae23c9
     */
ae23c9
    ch = getc(fp);
ae23c9
    if (ch == EOF) {
ae23c9
        return DEFAULT;
ae23c9
    }
ae23c9
    if (isspace(ch)) {
ae23c9
        goto start;
ae23c9
    }
ae23c9
    if (ch == 'T') {
ae23c9
        pos = 1;
ae23c9
        goto key;
ae23c9
    }
ae23c9
    goto eol;
ae23c9
ae23c9
eol:
ae23c9
    /* State EOL - loop until end of line */
ae23c9
    ch = getc(fp);
ae23c9
    if (ch == EOF) {
ae23c9
        return DEFAULT;
ae23c9
    }
ae23c9
    if (ch == '\n') {
ae23c9
        goto start;
ae23c9
    }
ae23c9
    goto eol;
ae23c9
ae23c9
key:
ae23c9
    /* State KEY - match "THRESHOLD=" string, go to THRESHOLD if found */
ae23c9
    ch = getc(fp);
ae23c9
    if (ch == EOF) {
ae23c9
        return DEFAULT;
ae23c9
    }
ae23c9
    if (ch == key[pos]) {
ae23c9
        pos++;
ae23c9
        if (key[pos] == 0) {
ae23c9
            goto threshold;
ae23c9
        } else {
ae23c9
            goto key;
ae23c9
        }
ae23c9
    }
ae23c9
    goto eol;
ae23c9
ae23c9
threshold:
ae23c9
    /* State THRESHOLD - parse number using fscanf, expect comment or space
ae23c9
     * or EOL.
ae23c9
     */
ae23c9
    ch = getc(fp);
ae23c9
    if (ch == EOF) {
ae23c9
        return DEFAULT;
ae23c9
    }
ae23c9
    if (!isdigit(ch)) {
ae23c9
        goto eol;
ae23c9
    }
ae23c9
    ungetc(ch, fp);
ae23c9
    if (fscanf(fp, "%d", &thres) != 1) {
ae23c9
        return DEFAULT;
ae23c9
    }
ae23c9
    ch = getc(fp);
ae23c9
    if (ch == '#' || ch == EOF || ch == '\n' || isspace(ch)) {
ae23c9
        return thres;
ae23c9
    }
ae23c9
    goto eol;
ae23c9
}
ae23c9
ae23c9
int get_threshold()
ae23c9
{
ae23c9
    FILE *fp = fopen(SYSCONFIG_KVM, "r");
ae23c9
    int val;
ae23c9
ae23c9
    if (!fp) {
ae23c9
        return DEFAULT;
ae23c9
    }
ae23c9
ae23c9
    val = get_threshold_from_file(fp);
ae23c9
    fclose (fp);
ae23c9
    return val;
ae23c9
}
ae23c9
ae23c9
const char *guest(int count)
ae23c9
{
ae23c9
    return (count == 1 ? "guest" : "guests");
ae23c9
}
ae23c9
ae23c9
void emit_count_message(int count)
ae23c9
{
ae23c9
    openlog(FACILITY, LOG_CONS, LOG_USER);
ae23c9
    syslog(LOG_INFO, COUNT_MSG, count, guest(count));
ae23c9
    closelog();
ae23c9
}
ae23c9
ae23c9
void emit_subscription_message(int count, int threshold)
ae23c9
{
ae23c9
    openlog(FACILITY, LOG_CONS, LOG_USER);
ae23c9
    syslog(LOG_WARNING, SUBSCRIPTION_MSG, count, guest(count), threshold);
ae23c9
    closelog();
ae23c9
}
ae23c9
ae23c9
int main(int argc, char **argv)
ae23c9
{
ae23c9
    int count, threshold;
ae23c9
ae23c9
    if (argc < 3)
ae23c9
        exit(1);
ae23c9
ae23c9
    count = atoi(argv[1]);
ae23c9
    threshold = get_threshold();
ae23c9
ae23c9
    if (!strcmp(argv[2], "create")) {
ae23c9
        if (threshold == 0) {
ae23c9
            emit_count_message(count);
ae23c9
        } else if (count > threshold) {
ae23c9
            emit_subscription_message(count, threshold);
ae23c9
        }
ae23c9
    } else {
ae23c9
        if (count >= threshold) {
ae23c9
            emit_count_message(count);
ae23c9
        }
ae23c9
    }
ae23c9
ae23c9
    return 0;
ae23c9
}