static ya_result config_zone_section_init(config_data *config) { ya_result return_code; tmp_zone_idx++; /* store the previously configured zone, if any */ config_zone_section_register(config); /* make a new zone section ready */ tmp_zones = zone_alloc(); if(FAIL(return_code = confs_init(zone_tab, tmp_zones))) { zone_free(tmp_zones); tmp_zones = NULL; osformatln(termerr, "config: zone: configuration initialize (zone): %r", return_code); } return return_code; }
void worker_thread_process(void *data) { packet_t *packet; packet_t *next; assert(data); packet = (packet_t *)data; packet->meta_hd = meta_hd_pp[sys_thread_id_get()]; assert(packet->meta_hd != NULL); //meta_buffer_sys_clear(packet->meta_hd); packet->flag &= ~PKT_HANDLE_MASK;/*清理上次的结果*/ module_list_process(module_hd_p, pktag_hd_p, packet->pktag, packet); if (packet->flag & PKT_LOOP_NEXT) { threadpool_add_task(tp, worker_thread_process, packet, 0); } else if (packet->flag & PKT_DONT_FREE) { meta_buffer_sys_clear(packet->meta_hd); } else { do { next = packet->next_packet; meta_buffer_sys_clear(packet->meta_hd); zone_free(packet_zone, packet); packet = next; } while (packet != NULL); } }
static void config_zone_section_register(config_data *config) { if(tmp_zones != NULL) { ya_result return_code; if(FAIL(return_code = zone_register(&config->zones, tmp_zones))) { switch(return_code) { case DATABASE_ZONE_MISSING_DOMAIN: case DATABASE_ZONE_MISSING_MASTER: { log_err("config: zone: section #%d: %r", tmp_zone_idx, return_code); exit(EXIT_FAILURE); break; } default: { log_err("config: zone: section #%d: %r", tmp_zone_idx, return_code); zone_free(tmp_zones); break; } } } tmp_zones = NULL; } }
void process_loop(module_hd_t *module_head) { int32_t status, tag_id; uint64_t pkt_num = 0; extern int system_exit; module_info_t *recv; packet_t *packet; if (g_conf.mode == MODE_LIVE || g_conf.mode == MODE_FILE) { /*收包,线程中加入处理*/ status = 0; packet_zone = zone_init("pcap_read", sizeof(packet_t) + MAX_PACKET_LEN, MAX_PACKET_HANDLE); assert(packet_zone); recv = module_info_get_from_name(module_head, "recv"); assert(recv && recv->ops->process); do { do { packet = (void *)zone_alloc(packet_zone, 0); } while(packet == NULL); tag_id = recv->ops->process(recv, packet); if (tag_id > 0) { assert(packet->data); pkt_num++; status = threadpool_add_task(tp, worker_thread_process, packet, 0); if (status != 0) { log_error(syslog_p, "Threadpool add task, status %d\n", status); status = 0; } if (g_conf.pkt_num != 0 && pkt_num >= g_conf.pkt_num) { system_exit = 1; } } else { status = tag_id; zone_free(packet_zone, packet); } } while( status >= 0 && !system_exit); if (g_conf.mode == MODE_FILE) { sleep(1);/*等待最后加入的work处理完成*/ } } else if (g_conf.mode == MODE_SE) { do { status = module_list_process(module_head, pktag_hd_p, -1, NULL); pkt_num++; if (g_conf.pkt_num != 0 && pkt_num >= g_conf.pkt_num) { system_exit = 1; } } while (status >= 0 && !system_exit); } }
Sound::~Sound() { SoundContext::Ref ctx = m_ctx.lock(); if (ctx) { if (m_is) ZMusic.Get().Dec(kNumStreamingBuffers*kStreamingBufferSize, 0); for (SourceVec::iterator it = m_sources.begin(); it != m_sources.end(); ++it) { SoundContext::Source &source = *it; if (source.mapped) { SoundContext::WriteLock L(ctx->m_m); ctx->UnmapSource(source); } if (source.source) m_alDriver->SyncDeleteSources(ALDRIVER_SIG 1, &source.source); } for (int i = 0; i < kNumStreamingBuffers; ++i) { if (m_sbufs[i]) m_alDriver->SyncDeleteBuffers(ALDRIVER_SIG 1, &m_sbufs[i]); } // NOTE: in Sound::Tick we issue a custom ALDriver command that queries the status // of the source (alSourcei(AL_SOURCE_STATE). This is an asynchronous command that // writes to a member of a Source object contained in the m_sources[] vector of this // object. // // There is no chance of contention here (or crashing because we write to this // data after the destructor is finished) because in order for us to issue this command // the source must have a valid openAL source/buffer attached. That means we call // ALDriver::DeleteSources() & ALDriver::DeleteBuffers() which are both synchronous operations // and therefore our custom command is guaranteed to be executed before the destructor returns } m_ib.reset(); if (m_vbd) delete m_vbd; if (m_bsi) delete m_bsi; if (m_is) delete m_is; if (m_blockData) zone_free(m_blockData); }
void destroy_zone(struct ZONE * zp) { if( zp == NULL) { fprintf(stderr, "Zone::destroy_zone called with null pointer\n"); return; } if( zp->magic != ZPMAGIC ){ fprintf(stderr, "Zone::destroy_zone called with invalid zp\n"); return; } # ifdef DEBUG if( zp -> count > 0) fprintf(stderr,"Zone::destroy_zone some (%d) used area found\n", zp->count); # endif # if 1 /* dual implementation, one */ while( zp->next != (struct MEMHEAD*)zp ){ zone_free(zp, zp->next+1); /* +1 == sizeof(struct MEMHEAD) */ } # else /* dual implementation, two */ { unsigned int i; struct MEMHEAD *p; for(i = 0; i< zp->cnt; i++){ p = zp->next->next; free(zp->next); zp->next =p; } zp->cnt = 0; } # endif zp->magic = 0; free(zp); # ifdef DEBUG fprintf(stderr,"Zone::destroy_zone called, access count:%d\n", zp->acnt); # endif }
/** * List all keys and their rollover time. If listed_zone is set limit * to that zone * \param sockfd client socket * \param listed_zone name of the zone * \param dbconn active database connection * \return 0 ok, 1 fail. */ static int perform_rollover_list(int sockfd, const char *listed_zone, db_connection_t *dbconn) { zone_list_t *zonelist = NULL; zone_t *zone = NULL; const zone_t *zone_walk = NULL; const char* fmt = "%-31s %-8s %-30s\n"; if (listed_zone) { zone = zone_new_get_by_name(dbconn, listed_zone); } else { zonelist = zone_list_new_get(dbconn); } if (listed_zone && !zone) { ods_log_error("[%s] zone '%s' not found", module_str, listed_zone); client_printf(sockfd, "zone '%s' not found\n", listed_zone); return 1; } if (!zone && !zonelist) { ods_log_error("[%s] error enumerating zones", module_str); client_printf(sockfd, "error enumerating zones\n"); return 1; } client_printf(sockfd, "Keys:\n"); client_printf(sockfd, fmt, "Zone:", "Keytype:", "Rollover expected:"); if (zone) { print_zone(sockfd, fmt, zone); zone_free(zone); return 0; } while ((zone_walk = zone_list_next(zonelist))) { print_zone(sockfd, fmt, zone_walk); } zone_list_free(zonelist); return 0; }
void * zone_realloc(struct ZONE * zp, void *ptr, size_t size) { struct MEMHEAD * p; if( zp == NULL) { fprintf(stderr, "Zone::zone_realloc called with null pointer\n"); return NULL; } if( zp->magic != ZPMAGIC ){ fprintf(stderr, "Zone::zone_realloc called with invalid zp\n"); return NULL; } acntup(); if( ptr == NULL) { return zone_malloc(zp, size); } if( size == 0 ){ zone_free(zp, ptr); return NULL; } CHKSET(zp, ptr); p = (struct MEMHEAD *) realloc((char *)ptr - sizeof(struct MEMHEAD), size + sizeof(struct MEMHEAD)); if( NULL == p ) return NULL; (p->next)->prev = p; /* realloc preserves data*/ (p->prev)->next = p; CHKSET(zp, p+1); return p + 1; }
int main(int argc, char *argv[]) { plan(10); /* Prepare query. */ knot_pkt_t *query = knot_pkt_new(NULL, 512, NULL); if (query == NULL) { return KNOT_ERROR; /* Fatal */ } knot_dname_t *qname = knot_dname_from_str("beef."); int ret = knot_pkt_put_question(query, qname, KNOT_CLASS_IN, KNOT_RRTYPE_A); knot_dname_free(&qname, NULL); if (ret != KNOT_EOK) { knot_pkt_free(&query); return KNOT_ERROR; /* Fatal */ } /* Prepare response */ uint8_t rbuf[65535]; size_t rlen = sizeof(rbuf); memcpy(rbuf, query->wire, query->size); knot_wire_flags_set_qr(rbuf); rrl_req_t rq; rq.w = rbuf; rq.len = rlen; rq.query = query; rq.flags = 0; /* 1. create rrl table */ rrl_table_t *rrl = rrl_create(RRL_SIZE); ok(rrl != NULL, "rrl: create"); /* 2. set rate limit */ uint32_t rate = 10; rrl_setrate(rrl, rate); is_int(rate, rrl_rate(rrl), "rrl: setrate"); /* 3. setlocks */ ret = rrl_setlocks(rrl, RRL_LOCKS); is_int(KNOT_EOK, ret, "rrl: setlocks"); /* 4. N unlimited requests. */ conf_zone_t *zone_conf = malloc(sizeof(conf_zone_t)); conf_init_zone(zone_conf); zone_conf->name = strdup("rrl."); zone_t *zone = zone_new(zone_conf); struct sockaddr_storage addr; struct sockaddr_storage addr6; sockaddr_set(&addr, AF_INET, "1.2.3.4", 0); sockaddr_set(&addr6, AF_INET6, "1122:3344:5566:7788::aabb", 0); ret = 0; for (unsigned i = 0; i < rate; ++i) { if (rrl_query(rrl, &addr, &rq, zone) != KNOT_EOK || rrl_query(rrl, &addr6, &rq, zone) != KNOT_EOK) { ret = KNOT_ELIMIT; break; } } is_int(0, ret, "rrl: unlimited IPv4/v6 requests"); #ifdef ENABLE_TIMED_TESTS /* 5. limited request */ ret = rrl_query(rrl, &addr, &rq, zone); is_int(0, ret, "rrl: throttled IPv4 request"); /* 6. limited IPv6 request */ ret = rrl_query(rrl, &addr6, &rq, zone); is_int(0, ret, "rrl: throttled IPv6 request"); #else skip_block(2, "Timed tests not enabled"); #endif /* 7. invalid values. */ ret = 0; rrl_create(0); // NULL ret += rrl_setrate(0, 0); // 0 ret += rrl_rate(0); // 0 ret += rrl_setlocks(0,0); // -1 ret += rrl_query(0, 0, 0, 0); // -1 ret += rrl_query(rrl, 0, 0, 0); // -1 ret += rrl_query(rrl, (void*)0x1, 0, 0); // -1 ret += rrl_destroy(0); // -1 is_int(-488, ret, "rrl: not crashed while executing functions on NULL context"); #ifdef ENABLE_TIMED_TESTS /* 8. hopscotch test */ struct runnable_data rd = { 1, rrl, &addr, &rq, zone }; rrl_hopscotch(&rd); ok(rd.passed, "rrl: hashtable is ~ consistent"); /* 9. reseed */ is_int(0, rrl_reseed(rrl), "rrl: reseed"); /* 10. hopscotch after reseed. */ rrl_hopscotch(&rd); ok(rd.passed, "rrl: hashtable is ~ consistent"); #else skip_block(3, "Timed tests not enabled"); #endif zone_free(&zone); knot_pkt_free(&query); rrl_destroy(rrl); return 0; }
static time_t perform_enforce(int sockfd, engine_type *engine, int bForceUpdate, task_type* task, db_connection_t *dbconn) { zone_list_t *zonelist = NULL; zone_t *zone, *firstzone = NULL; policy_t *policy; key_data_list_t *keylist; const key_data_t *key; time_t t_next, t_now = time_now(), t_reschedule = -1; /* Flags that indicate tasks to be scheduled after zones have been * enforced. */ int bSignerConfNeedsWriting = 0; int bSubmitToParent = 0; int bRetractFromParent = 0; int zone_updated; if (!(zonelist = zone_list_new(dbconn)) /*|| zone_list_associated_fetch(zonelist)*/ || zone_list_get(zonelist)) { zone_list_free(zonelist); zonelist = NULL; } if (!zonelist) { /* TODO: log error */ ods_log_error("[%s] zonelist NULL", module_str); /* TODO: backoff? */ return t_reschedule; } for (zone = zone_list_get_next(zonelist); zone; zone_free(zone), zone = zone_list_get_next(zonelist)) { if (engine->need_to_reload || engine->need_to_exit) break; if (!bForceUpdate && (zone_next_change(zone) == -1)) { continue; } else if (zone_next_change(zone) > t_now && !bForceUpdate) { /* This zone needs no update, however it might be the first * for future updates */ if (zone_next_change(zone) < t_reschedule || !firstzone) { t_reschedule = zone_next_change(zone); if (firstzone) { zone_free(firstzone); } firstzone = zone; zone = NULL; /* keeps firstzone from being freed. */ } continue; } if (!(policy = zone_get_policy(zone))) { client_printf(sockfd, "Next update for zone %s NOT scheduled " "because policy is missing !\n", zone_name(zone)); if (zone_next_change(zone) != -1 && (zone_set_next_change(zone, -1) || zone_update(zone))) { /* TODO: Log error */ } continue; } if (policy_passthrough(policy)) { ods_log_info("Passing through zone %s.\n", zone_name(zone)); zone_set_signconf_needs_writing(zone, 1); zone_update(zone); bSignerConfNeedsWriting = 1; policy_free(policy); continue; } zone_updated = 0; t_next = update(engine, dbconn, zone, policy, t_now, &zone_updated); policy_free(policy); bSignerConfNeedsWriting |= zone_signconf_needs_writing(zone); keylist = zone_get_keys(zone); while ((key = key_data_list_next(keylist))) { if (key_data_ds_at_parent(key) == KEY_DATA_DS_AT_PARENT_SUBMIT) { ods_log_warning("[%s] please submit DS " "with keytag %d for zone %s", module_str, key_data_keytag(key)&0xFFFF, zone_name(zone)); bSubmitToParent = 1; } else if (key_data_ds_at_parent(key) == KEY_DATA_DS_AT_PARENT_RETRACT) { ods_log_warning("[%s] please retract DS " "with keytag %d for zone %s", module_str, key_data_keytag(key)&0xFFFF, zone_name(zone)); bRetractFromParent = 1; } } key_data_list_free(keylist); if (t_next == -1) { client_printf(sockfd, "Next update for zone %s NOT scheduled " "by enforcer !\n", zone_name(zone)); ods_log_debug("Next update for zone %s NOT scheduled " "by enforcer !\n", zone_name(zone)); } else { /* Invalid schedule time then skip the zone.*/ char tbuf[32] = "date/time invalid\n"; /* at least 26 bytes */ ctime_r(&t_next, tbuf); /* note that ctime_r inserts \n */ client_printf(sockfd, "Next update for zone %s scheduled at %s", zone_name(zone), tbuf); ods_log_debug("Next update for zone %s scheduled at %s", zone_name(zone), tbuf); } if (zone_next_change(zone) != t_next) { zone_set_next_change(zone, t_next); zone_updated = 1; } /* * Commit the changes to the zone if there where any. */ if (zone_updated) { if (zone_update(zone)) { ods_log_debug("[%s] error zone_update(%s)", module_str, zone_name(zone)); } } /* * Find out when to schedule the next change. */ if (zone_next_change(zone) != -1 && (zone_next_change(zone) < t_reschedule || !firstzone)) { t_reschedule = zone_next_change(zone); if (firstzone) { zone_free(firstzone); } firstzone = zone; zone = NULL; } } zone_list_free(zonelist); /* * Schedule the next change if needed. */ if (firstzone) { reschedule_enforce(task, t_reschedule, zone_name(firstzone)); zone_free(firstzone); } /* Launch signer configuration writer task when one of the * zones indicated that it needs to be written. * TODO: unschedule it first! */ if (bSignerConfNeedsWriting) { task_type *signconf = signconf_task(dbconn, "signconf", "signer configurations"); enf_schedule_task(sockfd,engine,signconf,"signconf"); } else { ods_log_info("[%s] No changes to any signconf file required", module_str); } /* Launch ds-submit task when one of the updated key states has the * DS_SUBMIT flag set. */ if (bSubmitToParent) { task_type *submit = keystate_ds_submit_task(engine); enf_schedule_task(sockfd, engine, submit, "ds-submit"); } /* Launch ds-retract task when one of the updated key states has the * DS_RETRACT flag set. */ if (bRetractFromParent) { task_type *retract = keystate_ds_retract_task(engine); enf_schedule_task(sockfd, engine, retract, "ds-retract"); } return t_reschedule; }
RADRT_API bool RADRT_CALL Decode(const void *buff, AddrSize buffLength, Image &out) { RAD_ASSERT(buff&&buffLength); RAD_ASSERT(buffLength <= std::numeric_limits<stream::SPos>::max()); out.Free(); stream::MemInputBuffer ib(buff, (stream::SPos)buffLength); stream::InputStream is(ib); { char sig[SigSize]; if (is.Read(sig, SigSize, 0) != SigSize) return false; // NOTE: png_sig_cmp() returns 0 if the sig matches the PNG sig. if (png_sig_cmp((png_bytep)sig, 0, SigSize)) return false; } png_structp png; png_infop info; png = png_create_read_struct_2(PNG_LIBPNG_VER_STRING, 0, PNGErrorHandler, PNGWarningHandler, 0, PNGMalloc, PNGFree); if (!png) return false; info = png_create_info_struct(png); if (!info) { png_destroy_read_struct(&png, 0, 0); return false; } png_set_read_fn(png, &is, PNGRead); png_set_sig_bytes(png, SigSize); try { png_uint_32 w, h; int bd, color, interlace; png_read_info(png, info); png_get_IHDR(png, info, &w, &h, &bd, &color, &interlace, 0, 0); if (!out.AllocateFrames(1)||!out.AllocateMipmaps(0, 1)) { png_destroy_read_struct(&png, &info, 0); return false; } Mipmap &m = out.frames[0].mipmaps[0]; out.format = Format(png, info, bd, color); if (out.format == InvalidFormat) { out.Free(); png_destroy_read_struct(&png, &info, 0); return false; } out.bpp = FormatBPP(out.format); if (!out.AllocateMipmap(0, 0, w, h, w * out.bpp, w * h * out.bpp)) { out.Free(); png_destroy_read_struct(&png, &info, 0); return false; } png_set_strip_16(png); if (color == PNG_COLOR_TYPE_PALETTE) { png_set_palette_to_rgb(png); } if (color == PNG_COLOR_TYPE_GRAY && bd < 8) { png_set_expand_gray_1_2_4_to_8(png); } if (png_get_valid(png, info, PNG_INFO_tRNS)) { png_set_tRNS_to_alpha(png); } png_set_swap(png); int passes = png_set_interlace_handling(png); png_bytep dstRow = 0; png_bytep imgRow = 0; png_uint_32 stride = (png_uint_32)png_get_rowbytes(png, info); RAD_ASSERT(stride >= m.stride); if (stride != m.stride) { dstRow = (png_bytep)safe_zone_malloc(ZImageCodec, stride, 0); } for (int pass = 0; pass < passes; ++pass) { if (stride == m.stride) { dstRow = (png_bytep)m.data; // reset. } imgRow = (png_bytep)m.data; for (png_uint_32 y = 0; y < h; ++y) { if (pass > 0 && stride != m.stride) { RAD_ASSERT(imgRow != dstRow); // interlaced image, copy the result of the last pass. memcpy(dstRow, imgRow, m.stride); } png_read_rows(png, &dstRow, 0, 1); if (stride != m.stride) { // imgRow/dstRow are correct, we are copying from dstRow to imgRow. memcpy(imgRow, dstRow, m.stride); } else { dstRow += m.stride; } imgRow += m.stride; } } if (stride != m.stride) { RAD_ASSERT(dstRow); zone_free(dstRow); } png_read_end(png, info); } catch (PNGException&) { out.Free(); png_destroy_read_struct(&png, &info, 0); return false; } png_destroy_read_struct(&png, &info, 0); return true; }
static int perform_keystate_list(int sockfd, db_connection_t *dbconn, const char* filterZone, char** filterKeytype, char** filterKeystate, void (printheader)(int sockfd), void (printkey)(int sockfd, zone_t* zone, key_data_t* key, char*tchange, hsm_key_t* hsmKey)) { key_data_list_t* key_list; key_data_t* key; zone_t *zone = NULL; char* tchange; hsm_key_t *hsmkey; int cmp; int i, skipPrintKey; if (!(key_list = key_data_list_new_get(dbconn))) { client_printf_err(sockfd, "Unable to get list of keys, memory " "allocation or database error!\n"); return 1; } if (printheader) { (*printheader)(sockfd); } while ((key = key_data_list_get_next(key_list))) { /* only refetches zone if different from previous */ if (zone && (db_value_cmp(zone_id(zone), key_data_zone_id(key), &cmp) || cmp)) { zone_free(zone); zone = NULL; } if (!zone) { zone = key_data_get_zone(key); } hsmkey = key_data_get_hsm_key(key); key_data_cache_key_states(key); tchange = map_keytime(zone, key); /* allocs */ skipPrintKey = 0; if(printkey == NULL) skipPrintKey = 1; if(filterZone != NULL && strcmp(zone_name(zone), filterZone)) skipPrintKey = 1; for(i=0; filterKeytype && filterKeytype[i]; i++) if(!strcasecmp(filterKeytype[i],key_data_role_text(key))) break; if(filterKeytype && filterKeytype[i] == NULL) skipPrintKey = 1; for(i=0; filterKeystate && filterKeystate[i]; i++) if(!strcasecmp(filterKeystate[i],map_keystate(key))) break; if(filterKeystate && filterKeystate[i] == NULL) skipPrintKey = 1; if (!skipPrintKey) { (*printkey)(sockfd, zone, key, tchange, hsmkey); } free(tchange); hsm_key_free(hsmkey); key_data_free(key); } zone_free(zone); key_data_list_free(key_list); return 0; }