static int check(kdump_ctx *ctx) { kdump_attr_t attr; kdump_addr_t symval; kdump_status status; int rc, tmprc; attr.type = kdump_string; attr.val.string = vmcore; status = kdump_set_attr(ctx, "linux.vmcoreinfo.raw", &attr); if (status != kdump_ok) { fprintf(stderr, "Cannot set vmcoreinfo: %s\n", kdump_err_str(ctx)); return TEST_ERR; } rc = TEST_OK; attr.type = kdump_string; /* Check modifications */ attr.val.string = ALT_OSRELEASE; status = kdump_set_attr(ctx, ATTR_LINES ".OSRELEASE", &attr); if (status != kdump_ok) { fprintf(stderr, "Cannot modify %s: %s\n", ATTR_LINES ".OSRELEASE", kdump_err_str(ctx)); return TEST_ERR; } tmprc = check_string(ctx, ATTR_OSRELEASE, ALT_OSRELEASE); if (tmprc == TEST_ERR) return tmprc; if (tmprc != TEST_OK) rc = tmprc; attr.val.string= str(ALT_PAGESIZE); status = kdump_set_attr(ctx, ATTR_LINES ".PAGESIZE", &attr); if (status != kdump_ok) { fprintf(stderr, "Cannot modify %s: %s\n", ATTR_LINES ".PAGESIZE", kdump_err_str(ctx)); return TEST_ERR; } tmprc = check_number(ctx, ATTR_PAGESIZE, ALT_PAGESIZE); if (tmprc == TEST_ERR) return tmprc; if (tmprc != TEST_OK) rc = tmprc; attr.val.string= str(ALT_SYM_VALUE); status = kdump_set_attr(ctx, ATTR_LINES ".SYMBOL(" SYM_NAME ")", &attr); if (status != kdump_ok) { fprintf(stderr, "Cannot modify %s: %s\n", ATTR_LINES ".SYMBOL(" SYM_NAME ")", kdump_err_str(ctx)); return TEST_ERR; } status = kdump_vmcoreinfo_symbol(ctx, SYM_NAME, &symval); if (status != kdump_ok) { fprintf(stderr, "%s: Cannot get value: %s\n", SYM_NAME, kdump_err_str(ctx)); return TEST_ERR; } if (symval != ALT_SYM_VALUE) { fprintf(stderr, "%s: Invalid attribute value: %llx != %llx\n", SYM_NAME, (long long) symval, (long long) ALT_SYM_VALUE); rc = TEST_FAIL; } else printf("%s = %llx\n", SYM_NAME, (long long) symval); attr.val.string= str(ALT_LEN_VALUE); status = kdump_set_attr(ctx, ATTR_LINES ".LENGTH(" LEN_NAME ")", &attr); if (status != kdump_ok) { fprintf(stderr, "Cannot modify %s: %s\n", ATTR_LINES ".LENGTH(" LEN_NAME ")", kdump_err_str(ctx)); return TEST_ERR; } tmprc = check_number(ctx, ATTR_LEN, ALT_LEN_VALUE); if (tmprc == TEST_ERR) return tmprc; if (tmprc != TEST_OK) rc = tmprc; attr.val.string= str(ALT_NUM_VALUE); status = kdump_set_attr(ctx, ATTR_LINES ".NUMBER(" NUM_NAME ")", &attr); if (status != kdump_ok) { fprintf(stderr, "Cannot modify %s: %s\n", ATTR_LINES ".NUMBER(" NUM_NAME ")", kdump_err_str(ctx)); return TEST_ERR; } tmprc = check_number(ctx, ATTR_NUM, ALT_NUM_VALUE); if (tmprc == TEST_ERR) return tmprc; if (tmprc != TEST_OK) rc = tmprc; attr.val.string= str(ALT_OFF_VALUE); status = kdump_set_attr(ctx, ATTR_LINES ".OFFSET(" OFF_NAME ")", &attr); if (status != kdump_ok) { fprintf(stderr, "Cannot modify %s: %s\n", ATTR_LINES ".OFFSET(" OFF_NAME ")", kdump_err_str(ctx)); return TEST_ERR; } tmprc = check_number(ctx, ATTR_OFF, ALT_OFF_VALUE); if (tmprc == TEST_ERR) return tmprc; if (tmprc != TEST_OK) rc = tmprc; attr.val.string= str(ALT_SIZE_VALUE); status = kdump_set_attr(ctx, ATTR_LINES ".SIZE(" SIZE_NAME ")", &attr); if (status != kdump_ok) { fprintf(stderr, "Cannot modify %s: %s\n", ATTR_LINES ".SIZE(" SIZE_NAME ")", kdump_err_str(ctx)); return TEST_ERR; } tmprc = check_number(ctx, ATTR_SIZE, ALT_SIZE_VALUE); if (tmprc == TEST_ERR) return tmprc; if (tmprc != TEST_OK) rc = tmprc; /* Check cleanup */ return rc; }
int main(int argc, char **argv) { kdump_ctx *ctx; kdump_attr_iter_t it; kdump_attr_t attr; unsigned seen[ARRAY_SIZE(attrs)]; unsigned i; kdump_status res; int rc; ctx = kdump_new(); if (!ctx) { perror("Cannot initialize dump context"); return TEST_ERR; } /* Non-existent path must fail. */ res = kdump_attr_iter_start(ctx, NOPATH, &it); if (res == kdump_ok) { fprintf(stderr, "Found non-existent path %s??\n", NOPATH); return TEST_FAIL; } else if (res != kdump_nokey) { fprintf(stderr, "Unexpected error for %s: %s\n", NOPATH, kdump_err_str(ctx)); return TEST_FAIL; } /* Set the values and verify that all keys are found. */ for (i = 0; i < ARRAY_SIZE(attrs); ++i) { const struct attrdef *ad = &attrs[i]; char key[MAXATTRLEN]; sprintf(key, "%s.%s", ATTRPATH, ad->name); attr.type = kdump_string; attr.val.string = ad->value; res = kdump_set_attr(ctx, key, &attr); if (res != kdump_ok) { fprintf(stderr, "Cannot set %s: %s\n", key, kdump_err_str(ctx)); return TEST_FAIL; } } res = kdump_attr_iter_start(ctx, ATTRPATH, &it); if (res != kdump_ok) { fprintf(stderr, "Cannot start iteration: %s\n", kdump_err_str(ctx)); return TEST_FAIL; } memset(seen, 0, sizeof seen); rc = TEST_OK; while (it.key) { for (i = 0; i < ARRAY_SIZE(attrs); ++i) if (!strcmp(it.key, attrs[i].name)) break; if (i >= ARRAY_SIZE(attrs)) { fprintf(stderr, "Unknown key: %s\n", it.key); rc = TEST_FAIL; } else { if (seen[i]) { fprintf(stderr, "Duplicate key: %s\n", it.key); rc = TEST_FAIL; } else seen[i] = 1; res = kdump_attr_ref_get(ctx, &it.pos, &attr); if (res != kdump_ok) { fprintf(stderr, "Cannot get value of %s: %s\n", it.key, kdump_err_str(ctx)); rc = TEST_FAIL; } if (attr.type != kdump_string) { fprintf(stderr, "Wrong type: %d\n", attr.type); rc = TEST_FAIL; } else if (strcmp(attrs[i].value, attr.val.string)) { fprintf(stderr, "Value mismatch for %s:" " expect %s, found %s\n", it.key, attrs[i].value, attr.val.string); rc = TEST_FAIL; } else printf("%s = %s\n", it.key, attr.val.string); } res = kdump_attr_iter_next(ctx, &it); if (res != kdump_ok) { fprintf(stderr, "Cannot advance iterator: %s\n", kdump_err_str(ctx)); rc = TEST_FAIL; break; } } res = kdump_attr_iter_next(ctx, &it); if (res == kdump_ok) { fprintf(stderr, "Advancing past end succeeds??\n"); rc = TEST_FAIL; } else if (res != kdump_invalid) { fprintf(stderr, "Unexpected error advancing past end: %s\n", kdump_err_str(ctx)); rc = TEST_FAIL; } kdump_attr_iter_end(ctx, &it); for (i = 0; i < ARRAY_SIZE(attrs); ++i) if (!seen[i]) { fprintf(stderr, "Key not found: %s.%s\n", ATTRPATH, attrs[i].name); rc = TEST_FAIL; } return rc; }
static int check(kdump_ctx_t *ctx) { unsigned cnt; kdump_attr_t attr; kdump_status status; /* First value. */ cnt = 1; attr.type = KDUMP_STRING; attr.val.string = vmcore1; status = kdump_set_attr(ctx, "linux.vmcoreinfo.raw", &attr); if (status != KDUMP_OK) { fprintf(stderr, "#%d: Cannot set vmcoreinfo: %s\n", cnt, kdump_get_err(ctx)); return TEST_ERR; } status = kdump_get_attr(ctx, "linux.vmcoreinfo.lines.DIR.SUB.VAL", &attr); if (status != KDUMP_OK) { fprintf(stderr, "#%d: Cannot get vmcoreinfo: %s\n", cnt, kdump_get_err(ctx)); return TEST_ERR; } if (attr.type != KDUMP_STRING || strcmp(attr.val.string, "test1")) { fprintf(stderr, "#%d: Invalid attribute value\n", cnt); return TEST_FAIL; } printf("#%d: DIR.SUB.VAL=%s\n", cnt, attr.val.string); /* (Conflicting) second value */ cnt = 2; attr.type = KDUMP_STRING; attr.val.string = vmcore2; status = kdump_set_attr(ctx, "linux.vmcoreinfo.raw", &attr); if (status != KDUMP_OK) { fprintf(stderr, "#%d: Cannot set vmcoreinfo: %s\n", cnt, kdump_get_err(ctx)); return TEST_ERR; } status = kdump_get_attr(ctx, "linux.vmcoreinfo.lines.DIR.SUB", &attr); if (status != KDUMP_OK) { fprintf(stderr, "#%d: Cannot get vmcoreinfo: %s\n", cnt, kdump_get_err(ctx)); return TEST_ERR; } if (attr.type != KDUMP_STRING || strcmp(attr.val.string, "test2")) { fprintf(stderr, "#%d: Invalid attribute value\n", cnt); return TEST_FAIL; } printf("#%d: DIR.SUB=%s\n", cnt, attr.val.string); attr.type = KDUMP_NIL; status = kdump_set_attr(ctx, "linux.vmcoreinfo.raw", &attr); if (status != KDUMP_OK) { fprintf(stderr, "Cannot clear vmcoreinfo: %s\n", kdump_get_err(ctx)); return TEST_ERR; } status = kdump_get_attr(ctx, "linux.vmcoreinfo.lines.DIR.SUB", &attr); if (status == KDUMP_OK) { fprintf(stderr, "vmcoreinfo not cleared!\n"); return TEST_ERR; } else if (status != KDUMP_ERR_NOKEY) { fprintf(stderr, "Unexpected failure after unset: %s\n", kdump_get_err(ctx)); return TEST_ERR; } printf("DIR.SUB is now clear\n"); return TEST_OK; }