// Initializes one of the db's root node pointers from data in the file header static couchstore_error_t read_db_root(Db *db, node_pointer **root, void *root_data, int root_size) { couchstore_error_t errcode = COUCHSTORE_SUCCESS; if (root_size > 0) { error_unless(root_size >= ROOT_BASE_SIZE, COUCHSTORE_ERROR_CORRUPT); *root = read_root(root_data, root_size); error_unless(*root, COUCHSTORE_ERROR_ALLOC_FAIL); error_unless((*root)->pointer < db->header.position, COUCHSTORE_ERROR_CORRUPT); } else { *root = NULL; } cleanup: return errcode; }
couchstore_error_t decode_index_header(const char *bytes, size_t len, index_header_t **header) { index_header_t *h = NULL; char *b = NULL, *uncomp = NULL; uint16_t num_seqs, i, j, sz, num_part_versions; size_t uncompLen; /* First 16 bytes are md5 checksum (group signature). */ if (len <= 16) { return COUCHSTORE_ERROR_CORRUPT; } if (snappy_uncompressed_length(bytes + 16, len, &uncompLen) != SNAPPY_OK) { return COUCHSTORE_ERROR_CORRUPT; } b = uncomp = (char *) malloc(uncompLen); if (b == NULL) { return COUCHSTORE_ERROR_ALLOC_FAIL; } if (snappy_uncompress(bytes + 16, len - 16, b, &uncompLen) != SNAPPY_OK) { goto alloc_error; } h = (index_header_t *) malloc(sizeof(index_header_t)); if (h == NULL) { goto alloc_error; } h->seqs = NULL; h->id_btree_state = NULL; h->view_states = NULL; h->replicas_on_transfer = NULL; h->pending_transition.active = NULL; h->pending_transition.passive = NULL; h->pending_transition.unindexable = NULL; h->unindexable_seqs = NULL; memcpy(h->signature, bytes, 16); h->version = (uint8_t) b[0]; b += 1; h->num_partitions = dec_uint16(b); b += 2; memcpy(&h->active_bitmask, b, BITMASK_BYTE_SIZE); b += BITMASK_BYTE_SIZE; memcpy(&h->passive_bitmask, b, BITMASK_BYTE_SIZE); b += BITMASK_BYTE_SIZE; memcpy(&h->cleanup_bitmask, b, BITMASK_BYTE_SIZE); b += BITMASK_BYTE_SIZE; num_seqs = dec_uint16(b); b += 2; h->seqs = sorted_list_create(part_seq_cmp); if (h->seqs == NULL) { goto alloc_error; } for (i = 0; i < num_seqs; ++i) { part_seq_t pseq; pseq.part_id = dec_uint16(b); b += 2; pseq.seq = dec_uint48(b); b += 6; if (sorted_list_add(h->seqs, &pseq, sizeof(pseq)) != 0) { goto alloc_error; } } sz = dec_uint16(b); b += 2; h->id_btree_state = read_root((void *) b, (int) sz); b += sz; h->num_views = (uint8_t) b[0]; b += 1; h->view_states = (node_pointer **) malloc(sizeof(node_pointer *) * h->num_views); if (h->view_states == NULL) { goto alloc_error; } for (i = 0; i < (uint16_t) h->num_views; ++i) { sz = dec_uint16(b); b += 2; h->view_states[i] = read_root((void *) b, (int) sz); b += sz; } h->has_replica = b[0] == 0 ? 0 : 1; b += 1; sz = dec_uint16(b); b += 2; h->replicas_on_transfer = sorted_list_create(part_id_cmp); if (h->replicas_on_transfer == NULL) { goto alloc_error; } for (i = 0; i < sz; ++i) { uint16_t part_id = dec_uint16(b); b += 2; if (sorted_list_add(h->replicas_on_transfer, &part_id, sizeof(part_id)) != 0) { goto alloc_error; } } sz = dec_uint16(b); b += 2; h->pending_transition.active = sorted_list_create(part_id_cmp); if (h->pending_transition.active == NULL) { goto alloc_error; } for (i = 0; i < sz; ++i) { uint16_t part_id = dec_uint16(b); b += 2; if (sorted_list_add(h->pending_transition.active, &part_id, sizeof(part_id)) != 0) { goto alloc_error; } } sz = dec_uint16(b); b += 2; h->pending_transition.passive = sorted_list_create(part_id_cmp); if (h->pending_transition.passive == NULL) { goto alloc_error; } for (i = 0; i < sz; ++i) { uint16_t part_id = dec_uint16(b); b += 2; if (sorted_list_add(h->pending_transition.passive, &part_id, sizeof(part_id)) != 0) { goto alloc_error; } } sz = dec_uint16(b); b += 2; h->pending_transition.unindexable = sorted_list_create(part_id_cmp); if (h->pending_transition.unindexable == NULL) { goto alloc_error; } for (i = 0; i < sz; ++i) { uint16_t part_id = dec_uint16(b); b += 2; if (sorted_list_add(h->pending_transition.unindexable, &part_id, sizeof(part_id)) != 0) { goto alloc_error; } } num_seqs = dec_uint16(b); b += 2; h->unindexable_seqs = sorted_list_create(part_seq_cmp); if (h->unindexable_seqs == NULL) { goto alloc_error; } for (i = 0; i < num_seqs; ++i) { part_seq_t pseq; pseq.part_id = dec_uint16(b); b += 2; pseq.seq = dec_uint48(b); b += 6; if (sorted_list_add(h->unindexable_seqs, &pseq, sizeof(pseq)) != 0) { goto alloc_error; } } if (h->version >= 2) { num_part_versions = dec_uint16(b); b += 2; h->part_versions = sorted_list_create(part_versions_cmp); if (h->part_versions == NULL) { goto alloc_error; } for (i = 0; i < num_part_versions; ++i) { part_version_t pver; pver.part_id = dec_uint16(b); b += 2; pver.num_failover_log = dec_uint16(b); b += 2; pver.failover_log = (failover_log_t *) malloc( sizeof(failover_log_t) * pver.num_failover_log); if (pver.failover_log == NULL) { goto alloc_error; } for (j = 0; j < pver.num_failover_log; ++j) { memcpy(&pver.failover_log[j].uuid, b, 8); b += 8; pver.failover_log[j].seq = dec_uint64(b); b += 8; } if (sorted_list_add(h->part_versions, &pver, sizeof(pver)) != 0) { free(pver.failover_log); goto alloc_error; } } } free(uncomp); *header = h; return COUCHSTORE_SUCCESS; alloc_error: free_index_header(h); free(uncomp); return COUCHSTORE_ERROR_ALLOC_FAIL; }
int main(int argc, char **argv) { struct server *s = NULL; pthread_t pt, ctl; int c, is_forward = 0; const char *config = SR_CONFIG_FILE; int daemon = 0; while ((c = getopt(argc,argv,"c:vhfd")) != -1) { switch(c) { case 'c': config = optarg; break; case 'h': help(argv[0]); exit(0); break; case 'f': is_forward = 1; break; case 'd': daemon = 1; break; case '?': printf("Try -h please\n"); exit(0); break; case 'v': printf("dnspod-sr 0.01\n"); exit(0); break; default: exit(0); break; } } sanity_test(0); drop_privilege("./"); daemonrize(daemon); trig_signals(1); global_now = time(NULL); //for read root.z g_nameservers[0] = g_nameservers[1] = NULL; init_globe(); init_mempool(); s = server_init(); s->is_forward = is_forward; read_config(config, (char *)s->logpath, s->forward, g_nameservers); // add default dns server 8.8.8.8, 114.114.114.114 if (g_nameservers[0] == NULL) { assert(g_nameservers[1] == NULL); g_nameservers[0] = strdup("119.29.29.29"); g_nameservers[1] = strdup("8.8.4.4"); } if (g_nameservers[1] == NULL) { if (strcmp(g_nameservers[0], "119.29.29.29") == 0) { g_nameservers[1] = strdup("8.8.4.4"); } else { g_nameservers[1] = strdup("119.29.29.29"); } } // if (create_fetcher(s, s->nfetcher) < 0) dns_error(0, "create worker"); if (create_author(s, s->nquizzer) < 0) dns_error(0, "create author"); if (pthread_create(&pt, NULL, (void *) time_cron, s) != 0) dns_error(0, "time cron error"); if (pthread_create(&ctl, NULL, (void *)recv_update, s) != 0) { dns_error(0, "recv update thread error"); } read_root(s->datasets, s->ttlexp); print_basic_debug(); global_serv = s; run_sentinel(s); return 0; }