Пример #1
0
rhcs_clustat *parse_rhcs_clustat(FILE *in) {
    char *buf;
    size_t len;
    int done = 0;
    XML_Parser parser;
    rhcs_clustat *clustat;

    nodes = 0;
    services = 0;

    clustat = mp_calloc(1, sizeof(rhcs_clustat));

    // Create Parser
    parser = XML_ParserCreate(NULL);
    XML_SetUserData(parser, clustat);
    XML_SetElementHandler(parser, rhcs_clustat_startElement, rhcs_clustat_stopElement);

    buf = mp_calloc(BUFFERSIZE, 1);

    do {
        len = fread(buf, 1, BUFFERSIZE, in);
        if (!XML_Parse(parser, buf, len, done)) {
            fprintf(stderr,
                    "%s at line %d\n",
                    XML_ErrorString(XML_GetErrorCode(parser)),
                    (int) XML_GetCurrentLineNumber(parser));
            return NULL;
        }
    } while (len == BUFFERSIZE && done == 0);
    XML_ParserFree(parser);

    return clustat;
}
Пример #2
0
END_TEST

// mp_calloc
START_TEST (test_calloc_ok) {
    char *dest;

    dest = mp_calloc(10, 10);

    free(dest);
}
Пример #3
0
/*
 * Could/should be changed to not allocate qdigs if qrop is NULL
 * Performance wouldn't suffer too much with a test on every loop iteration.
 */
void
mpi_divqr(mpi *qrop, mpi *rrop, mpi *num, mpi *den)
{
    long i, j;			/* counters */
    int qsign;			/* sign of quotient */
    int rsign;			/* sign of remainder */
    BNI qsize;			/* size of quotient */
    BNI rsize;			/* size of remainder */
    BNS qest;			/* estimative of quotient value */
    BNS *qdigs, *rdigs;		/* work copy or result */
    BNS *ndigs, *ddigs;		/* work copy or divisor and dividend */
    BNI value;			/* temporary result */
    long svalue;		/* signed temporary result (2's complement) */
    BNS carry, scarry, denorm;	/* carry and normalization */
    BNI dpos, npos;		/* offsets in data */

    /* get signs */
    rsign = num->sign;
    qsign = rsign ^ den->sign;

    /* check for special case */
    if (num->size < den->size) {
	/* quotient is zero and remainder is numerator */
	if (rrop && rrop->digs != num->digs) {
	    if (rrop->alloc < num->size) {
		rrop->digs = mp_realloc(rrop->digs, sizeof(BNS) * num->size);
		rrop->alloc = num->size;
	    }
	    rrop->size = num->size;
	    memcpy(rrop->digs, num->digs, sizeof(BNS) * num->size);
	    rrop->sign = rsign;
	}
	if (qrop)
	    mpi_seti(qrop, 0);

	return;
    }

    /* estimate result sizes */
    rsize = den->size;
    qsize = num->size - den->size + 1;

    /* offsets */
    npos = num->size - 1;
    dpos = den->size - 1;

    /* allocate space for quotient and remainder */
    if (qrop == NULL || qrop->digs == num->digs || qrop->digs == den->digs)
	qdigs = mp_calloc(1, sizeof(BNS) * qsize);
    else {
	if (qrop->alloc < qsize) {
	    qrop->digs = mp_realloc(qrop->digs, sizeof(BNS) * qsize);
	    qrop->alloc = qsize;
	}
	memset(qrop->digs, '\0', sizeof(BNS) * qsize);
	qdigs = qrop->digs;
    }
    if (rrop) {
	if (rrop->digs == num->digs || rrop->digs == den->digs)
	    rdigs = mp_calloc(1, sizeof(BNS) * rsize);
	else {
	    if (rrop->alloc < rsize) {
		rrop->digs = mp_realloc(rrop->digs, sizeof(BNS) * rsize);
		rrop->alloc = rsize;
	    }
	    memset(rrop->digs, '\0', sizeof(BNS) * rsize);
	    rdigs = rrop->digs;
	}
    }
    else
	rdigs = NULL;	/* fix gcc warning */

    /* special case, only one word in divisor */
    if (dpos == 0) {
	for (carry = 0, i = npos; i >= 0; i--) {
	    value = ((BNI)carry << BNSBITS) + num->digs[i];
	    qdigs[i] = (BNS)(value / den->digs[0]);
	    carry = (BNS)(value % den->digs[0]);
	}
	if (rrop)
	    rdigs[0] = carry;

	goto mpi_divqr_done;
    }

    /* make work copy of numerator */
    ndigs = mp_malloc(sizeof(BNS) * (num->size + 1));
    /* allocate one extra word an update offset */
    memcpy(ndigs, num->digs, sizeof(BNS) * num->size);
    ndigs[num->size] = 0;
    ++npos;

    /* normalize */
    denorm = (BNS)((BNI)CARRY / ((BNI)(den->digs[dpos]) + 1));

    if (denorm > 1) {
	/* i <= num->size because ndigs has an extra word */
	for (carry = 0, i = 0; i <= num->size; i++) {
	    value = ndigs[i] * (BNI)denorm + carry;
	    ndigs[i] = (BNS)value;
	    carry = (BNS)(value >> BNSBITS);
	}
	/* make work copy of denominator */
	ddigs = mp_malloc(sizeof(BNS) * den->size);
	memcpy(ddigs, den->digs, sizeof(BNS) * den->size);
	for (carry = 0, i = 0; i < den->size; i++) {
	    value = ddigs[i] * (BNI)denorm + carry;
	    ddigs[i] = (BNS)value;
	    carry = (BNS)(value >> BNSBITS);
	}
    }
    else
Пример #4
0
void
mpi_mul(mpi *rop, mpi *op1, mpi *op2)
{
    int sign;				/* sign flag */
    BNS *digs;				/* result data */
    long xsize;				/* result size */

    /* get result sign */
    sign = op1->sign ^ op2->sign;

    /* check for special cases */
    if (op1->size == 1) {
	if (*op1->digs == 0) {
	    /* multiply by 0 */
	    mpi_seti(rop, 0);
	    return;
	}
	else if (*op1->digs == 1) {
	    /* multiply by +-1 */
	    if (rop->alloc < op2->size) {
		rop->digs = mp_realloc(rop->digs, sizeof(BNS) * op2->size);
		rop->alloc = op2->size;
	    }
	    rop->size = op2->size;
	    memmove(rop->digs, op2->digs, sizeof(BNS) * op2->size);
	    rop->sign = op2->size > 1 || *op2->digs ? sign : 0;

	    return;
	}
    }
    else if (op2->size == 1) {
	if (*op2->digs == 0) {
	    /* multiply by 0 */
	    mpi_seti(rop, 0);
	    return;
	}
	else if (*op2->digs == 1) {
	    /* multiply by +-1 */
	    if (rop->alloc < op1->size) {
		rop->digs = mp_realloc(rop->digs, sizeof(BNS) * op1->size);
		rop->alloc = op1->size;
	    }
	    rop->size = op1->size;
	    memmove(rop->digs, op1->digs, sizeof(BNS) * op1->size);
	    rop->sign = op1->size > 1 || *op1->digs ? sign : 0;

	    return;
	}
    }

    /* allocate result data and set it to zero */
    xsize = op1->size + op2->size;
    if (rop->digs == op1->digs || rop->digs == op2->digs)
	/* rop is also an operand */
	digs = mp_calloc(1, sizeof(BNS) * xsize);
    else {
	if (rop->alloc < xsize) {
	    rop->digs = mp_realloc(rop->digs, sizeof(BNS) * xsize);
	    rop->alloc = xsize;
	}
	digs = rop->digs;
	memset(digs, '\0', sizeof(BNS) * xsize);
    }

    /* multiply operands */
    xsize = mp_mul(digs, op1->digs, op2->digs, op1->size, op2->size);

    /* store result in rop */
    if (digs != rop->digs) {
	/* if rop was an operand, free old data */
	mp_free(rop->digs);
	rop->digs = digs;
    }
    rop->size = xsize;

    /* set result sign */
    rop->sign = sign;
}
Пример #5
0
void rhcs_clustat_startElement(void *clustat, const char *name, const char **atts) {
    const char **k, **v;
    int i;

    if (strcmp(name, "cluster") == 0) {
        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) {
            if (strcmp(*k, "name") == 0)
                ((rhcs_clustat *)clustat)->name = mp_strdup(*v);
            else if (strcmp(*k, "id") == 0)
                ((rhcs_clustat *)clustat)->id = (unsigned int) strtol(*v, NULL, 10);
        }
        return;
    }

    if (strcmp(name, "node") == 0) {
        ((rhcs_clustat *)clustat)->node = mp_realloc(((rhcs_conf *)clustat)->node, (nodes+2)*sizeof(rhcs_clustat_node));
        ((rhcs_clustat *)clustat)->node[nodes] = mp_calloc(1, sizeof(rhcs_clustat_node));
        ((rhcs_clustat *)clustat)->node[nodes+1] = NULL;
        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) {
            if (strcmp(*k, "name") == 0)
                ((rhcs_clustat *)clustat)->node[nodes]->name = mp_strdup(*v);
            else if (strcmp(*k, "state") == 0)
                ((rhcs_clustat *)clustat)->node[nodes]->state = (unsigned int) strtol(*v, NULL, 10);
            else if (strcmp(*k, "rgmanager") == 0)
                ((rhcs_clustat *)clustat)->node[nodes]->rgmanager = (unsigned int) strtol(*v, NULL, 10);
            else if (strcmp(*k, "nodeid") == 0)
                ((rhcs_clustat *)clustat)->node[nodes]->id = (unsigned int) strtol(*v, NULL, 16);
            if (strcmp(*k, "local") == 0 && strcmp(*v, "1") == 0)
                ((rhcs_clustat *)clustat)->local = ((rhcs_clustat *)clustat)->node[nodes];
        }
        nodes++;
        return;
    }

    if (strcmp(name, "group") == 0) {
        ((rhcs_clustat *)clustat)->group = mp_realloc(((rhcs_clustat *)clustat)->group, (services+2)*sizeof(rhcs_clustat_group));
        ((rhcs_clustat *)clustat)->group[services] = mp_calloc(1, sizeof(rhcs_clustat_group));
        ((rhcs_clustat *)clustat)->group[services+1] = NULL;
        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) {
            if (strcmp(*k, "name") == 0) {
                ((rhcs_clustat *)clustat)->group[services]->name = mp_strdup(*v);
                strsep(&((rhcs_clustat *)clustat)->group[services]->name, ":");
            } else if (strcmp(*k, "state") == 0) {
                ((rhcs_clustat *)clustat)->group[services]->state = (unsigned int) strtol(*v, NULL, 10);
            } else if (strcmp(*k, "owner") == 0) {
                ((rhcs_clustat *)clustat)->group[services]->owner = NULL;
                for(i=0; i < nodes; i++) {
                    if(strcmp(*v, ((rhcs_clustat *)clustat)->node[i]->name) == 0) {
                        ((rhcs_clustat *)clustat)->group[services]->owner = ((rhcs_clustat *)clustat)->node[i];
                    }
                }
            } else if (strcmp(*k, "last_owner") == 0) {
                for(i=0; i < nodes; i++) {
                    if(strcmp(*v, ((rhcs_clustat *)clustat)->node[i]->name) == 0) {
                        ((rhcs_clustat *)clustat)->group[services]->last = ((rhcs_clustat *)clustat)->node[i];
                    }
                }
            }
        }
        services++;
        return;
    }
}
Пример #6
0
void rhcs_conf_startElement(void *conf, const char *name, const char **atts) {
    const char **k, **v;
    int i;

    if (strcmp(name, "cluster") == 0) {
        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) {
            if (strcmp(*k, "name") == 0)
                ((rhcs_conf *)conf)->name = mp_strdup(*v);
            else if (strcmp(*k, "alias") == 0)
                ((rhcs_conf *)conf)->alias = mp_strdup(*v);
            else if (strcmp(*k, "config_version") == 0)
                ((rhcs_conf *)conf)->version = (unsigned int) strtol(*v, NULL, 10);
        }
        return;
    }
    if (strcmp(name, "clusternode") == 0) {
        ((rhcs_conf *)conf)->node = mp_realloc(((rhcs_conf *)conf)->node, (nodes+2)*sizeof(rhcs_conf_node));
        ((rhcs_conf *)conf)->node[nodes] = mp_calloc(1, sizeof(rhcs_conf_node));
        ((rhcs_conf *)conf)->node[nodes+1] = NULL;
        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) {
            if (strcmp(*k, "name") == 0)
                ((rhcs_conf *)conf)->node[nodes]->name = mp_strdup(*v);
            else if (strcmp(*k, "nodeid") == 0)
                ((rhcs_conf *)conf)->node[nodes]->id = (unsigned int) strtol(*v, NULL, 10);
            else if (strcmp(*k, "votes") == 0)
                ((rhcs_conf *)conf)->node[nodes]->votes = (unsigned int) strtol(*v, NULL, 10);
        }
        nodes++;
        return;
    }
    if (strcmp(name, "failoverdomain") == 0) {
        ((rhcs_conf *)conf)->fodomain = mp_realloc(((rhcs_conf *)conf)->fodomain, (fodomains+2)*sizeof(rhcs_conf_fodom));
        ((rhcs_conf *)conf)->fodomain[fodomains] = mp_calloc(1, sizeof(rhcs_conf_fodom));
        ((rhcs_conf *)conf)->fodomain[fodomains+1] = NULL;
        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) {
            if (strcmp(*k, "name") == 0)
                ((rhcs_conf *)conf)->fodomain[fodomains]->name = mp_strdup(*v);
            else if (strcmp(*k, "nofailback") == 0)
                ((rhcs_conf *)conf)->fodomain[fodomains]->failback = (strcmp(*v, "0") == 0);
            else if (strcmp(*k, "ordered") == 0)
                ((rhcs_conf *)conf)->fodomain[fodomains]->ordered = (strcmp(*v, "1") == 0);
            else if (strcmp(*k, "restricted") == 0)
                ((rhcs_conf *)conf)->fodomain[fodomains]->restricted = (strcmp(*v, "1") == 0);
        }
        fodomain_nodes = 0;
        return;
    }
    if (strcmp(name, "failoverdomainnode") == 0) {
        ((rhcs_conf *)conf)->fodomain[fodomains]->node = mp_realloc(((rhcs_conf *)conf)->fodomain[fodomains]->node, (fodomain_nodes+2)*sizeof(rhcs_conf_fodom_node));
        ((rhcs_conf *)conf)->fodomain[fodomains]->node[fodomain_nodes] = mp_calloc(1, sizeof(rhcs_conf_fodom_node));
        ((rhcs_conf *)conf)->fodomain[fodomains]->node[fodomain_nodes+1] = NULL;
        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) {
            if (strcmp(*k, "name") == 0)
                for (i=0; i < nodes; i++) {
                    if (strcmp(*v, ((rhcs_conf *)conf)->node[i]->name) == 0) {
                        ((rhcs_conf *)conf)->fodomain[fodomains]->node[fodomain_nodes]->node = ((rhcs_conf *)conf)->node[i];
                    }
                }
            else if (strcmp(*k, "priority") == 0)
                ((rhcs_conf *)conf)->fodomain[fodomains]->node[fodomain_nodes]->priority = (unsigned int) strtol(*v, NULL, 10);
        }
        fodomain_nodes++;
        return;
    }
    if (strcmp(name, "service") == 0) {
        ((rhcs_conf *)conf)->service = mp_realloc(((rhcs_conf *)conf)->service, (services+2)*sizeof(rhcs_conf_service));
        ((rhcs_conf *)conf)->service[services] = mp_calloc(1, sizeof(rhcs_conf_service));
        ((rhcs_conf *)conf)->service[services+1] = NULL;
        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) {
            if (strcmp(*k, "name") == 0)
                ((rhcs_conf *)conf)->service[services]->name = mp_strdup(*v);
            else if (strcmp(*k, "domain") == 0)
                for (i=0; i < fodomains; i++) {
                    if (strcmp(*v, ((rhcs_conf *)conf)->fodomain[i]->name) == 0) {
                        ((rhcs_conf *)conf)->service[services]->fodomain = ((rhcs_conf *)conf)->fodomain[i];
                    }
                }
            else if (strcmp(*k, "autostart") == 0)
                ((rhcs_conf *)conf)->service[services]->autostart = (strcmp(*v, "1") == 0);
            else if (strcmp(*k, "exclusive") == 0)
                ((rhcs_conf *)conf)->service[services]->exclusive = (strcmp(*v, "1") == 0);
            else if (strcmp(*k, "recovery") == 0) {
                if (strcmp(*v, "relocate") == 0)
                    ((rhcs_conf *)conf)->service[services]->recovery = RELOCATE;
                else if (strcmp(*v, "restart") == 0)
                    ((rhcs_conf *)conf)->service[services]->recovery = RESTART;
            }
        }
        services++;
        return;
    }
    /*
        printf("%s\n", name);

        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2)
            printf("  '%s' => '%s'\n", *k, *v);
    */
}