void cache_object_init(struct cache_object *co) { cache_index_init(&co->ci); cache_init(&co->cc); open_index_file(&co->ci); read_cache_index_file(&co->ci); open_cache_file(&co->cc); read_cache_file(&co->cc); }
/* Prepare for a sequential iteration over the cache file. */ static krb5_error_code KRB5_CALLCONV fcc_start_seq_get(krb5_context context, krb5_ccache id, krb5_cc_cursor *cursor) { krb5_fcc_cursor *fcursor; krb5_error_code ret; fcc_data *data = id->data; k5_cc_mutex_lock(context, &data->lock); fcursor = malloc(sizeof(krb5_fcc_cursor)); if (fcursor == NULL) { k5_cc_mutex_unlock(context, &data->lock); return KRB5_CC_NOMEM; } if (OPENCLOSE(id)) { ret = open_cache_file(context, id, FCC_OPEN_RDONLY); if (ret) { free(fcursor); k5_cc_mutex_unlock(context, &data->lock); return ret; } } /* Make sure we start reading right after the primary principal */ ret = skip_header(context, id); if (ret) { free(fcursor); goto done; } ret = skip_principal(context, id); if (ret) { free(fcursor); goto done; } fcursor->pos = fcc_lseek(data, 0, SEEK_CUR); *cursor = (krb5_cc_cursor)fcursor; done: MAYBE_CLOSE(context, id, ret); k5_cc_mutex_unlock(context, &data->lock); return ret; }
int main(int argc, char *argv[]) { struct cmdline_options opts = { .cache_file = CACHE_FILE, .base_oid = BASE_OID, .conf_file = CONF_FILE, .cache_prog = CACHE_REGEN_PROG, }; bool is_ok; int cache_fd; struct cache_entry *cache_entries; size_t num_cache_entries; int rc; unsigned int attr_idx = SNMPD_IDX_UNKNOWN; size_t req_cache_idx; struct oid_info const *sub_oid; size_t base_oid_len; while (1) { int c = getopt_long(argc, argv, "gns", CMDLINE_OPTIONS, 0); if (c==-1) break; switch (c) { case CMD_HELP: show_help(); case CMD_VERSION: show_version(); case CMD_BASE_OID: opts.base_oid = optarg; break; case CMD_CACHE: opts.cache_file = optarg; break; case CMD_CACHE_PROGRAM: opts.cache_prog = optarg; break; case CMD_CONF: opts.conf_file = optarg; break; case 'g': opts.op_get = 1; break; case 's': opts.op_set = 1; break; case 'n': opts.op_get_next = 1; break; default: fputs("Try '--help' for more information.\n", stderr); exit(EX_USAGE); } } is_ok = false; base_oid_len = strlen(opts.base_oid); if (opts.op_get + opts.op_set + opts.op_get_next > 1) fputs("more than one operation specified\n", stderr); else if (opts.op_get + opts.op_set + opts.op_get_next == 0) fputs("no operation specified\n", stderr); else if (optind + 1 != argc) fputs("no/too much OID specified\n", stderr); else if (strncmp(opts.base_oid, argv[optind], base_oid_len) && argv[optind][base_oid_len] != '\0' && argv[optind][base_oid_len] != '.') fputs("unsupported OID\n", stderr); else if (!parse_oid(&argv[optind][base_oid_len], &sub_oid, &attr_idx)) ; /* noop */ else if ((opts.op_get || opts.op_set) && (sub_oid == NULL || attr_idx == SNMPD_IDX_UNKNOWN || (sub_oid->num_suboid && attr_idx >= sub_oid->num_suboid))) { fprintf(stderr, "sub_oid=%p, idx=%u\n", sub_oid, attr_idx); fputs("unknown OID\n", stderr); return EX_UNAVAILABLE; } else is_ok = true; if (!is_ok) exit(EX_USAGE); if (opts.op_set) { puts("not-writable"); return EXIT_SUCCESS; } if (opts.op_get_next && sub_oid == NULL) { sub_oid = &SUB_OIDS[0]; assert(attr_idx == SNMPD_IDX_UNKNOWN); } assert(sub_oid != NULL); cache_fd = open_cache_file(opts.conf_file, opts.cache_file, opts.cache_prog); if (cache_fd < 0) exit(-cache_fd); rc = read_cache_file(cache_fd, &cache_entries, &num_cache_entries); if (rc < 0) exit(-rc); close(cache_fd); if (sub_oid->num_suboid == 0) { size_t i; if (attr_idx == SNMPD_IDX_UNKNOWN && opts.op_get_next) req_cache_idx = 0; else req_cache_idx = num_cache_entries; for (i = 0; i < num_cache_entries; ++i) { if (cache_entries[i].idx != attr_idx) continue; req_cache_idx = i; if (opts.op_get_next) { ++req_cache_idx; if (req_cache_idx >= num_cache_entries) { req_cache_idx = 0; ++sub_oid; } } break; } } else if (opts.op_get_next) { if (attr_idx == SNMPD_IDX_UNKNOWN) req_cache_idx = 0; else if (attr_idx + 1 != sub_oid->num_suboid) req_cache_idx = attr_idx; else { req_cache_idx = 0; ++sub_oid; } } else req_cache_idx = attr_idx; assert(sub_oid->num_suboid == 0 || req_cache_idx < sub_oid->num_suboid); if (sub_oid->idx == SNMPD_OID_METADATA) { print_metadata(opts.base_oid, sub_oid, req_cache_idx, num_cache_entries); } else if (req_cache_idx < num_cache_entries && sub_oid->oid != 0) { rc = read_sysfs_cache(&cache_entries[req_cache_idx]); if (rc < 0) exit(-rc); print_cache(&cache_entries[req_cache_idx], opts.base_oid, sub_oid); } free(cache_entries); }