static struct wproc_job *create_job(int type, void *arg, time_t timeout, const char *cmd) { struct wproc_job *job; struct wproc_worker *wp; wp = get_worker(cmd); if (!wp) return NULL; job = calloc(1, sizeof(*job)); if (!job) { logit(NSLOG_RUNTIME_ERROR, TRUE, "Error: Failed to allocate memory for worker job: %s\n", strerror(errno)); return NULL; } job->wp = wp; job->id = get_job_id(wp); job->type = type; job->arg = arg; job->timeout = timeout; if (fanout_add(wp->jobs, job->id, job) < 0 || !(job->command = strdup(cmd))) { free(job); return NULL; } return job; }
int main(int argc, char **argv) { unsigned long k; struct test_data *td; t_set_colors(0); t_start("fanout tests"); run_tests(10, 64); run_tests(512, 64); run_tests(64, 64); run_tests(511, 17); destroyed = 0; fot = fanout_create(512); ok_int(fanout_remove(fot, 12398) == NULL, 1, "remove on empty table must yield NULL"); ok_int(fanout_get(fot, 123887987) == NULL, 1, "get on empty table must yield NULL"); for (k = 0; k < 16385; k++) { struct test_data *tdata = calloc(1, sizeof(*td)); tdata->key = k; asprintf(&tdata->name, "%lu", k); fanout_add(fot, k, tdata); } td = fanout_get(fot, k - 1); ok_int(td != NULL, 1, "get must get what add inserts"); ok_int(fanout_remove(fot, k + 1) == NULL, 1, "remove on non-inserted key must yield NULL"); ok_int(fanout_get(fot, k + 1) == NULL, 1, "get on non-inserted must yield NULL"); fanout_destroy(fot, pdest); ok_int((int)destroyed, (int)k, "destroy counter while free()'ing"); return t_end(); }
static void run_tests(int ntests, int fo_size) { struct tcase *tc; unsigned long last_ptr, *ptr; int i, added = 0, removed = 0; fanout_table *fo; last_ptr = ntests; fo = fanout_create(fo_size); tc = calloc(ntests, sizeof(*tc)); for (i = 0; i < ntests; i++) { tc[i].value = i; if (!fanout_add(fo, tc[i].key, &tc[i].value)) added++; } ok_int(added, ntests, "Adding stuff must work"); while ((ptr = (unsigned long *)fanout_remove(fo, 0))) { ok_int((int)*ptr, (int)last_ptr - 1, "Removing a bunch of them"); removed++; last_ptr = *ptr; } ok_int(added, removed, "Removing should work as well as adding"); fanout_destroy(fo, destructor); ok_int(destroyed, 0, "Expected no entries in destructor"); fo = fanout_create(fo_size); for (i = 0; i < ntests; i++) { tc[i].value = i; if (!fanout_add(fo, tc[i].key, &tc[i].value)) added++; } fanout_destroy(fo, destructor); ok_int(destroyed, ntests, "Expected ntest entries in destructor"); destroyed = 0; free(tc); }
static int downtime_add(scheduled_downtime *dt) { unsigned long prev_downtime_id; scheduled_downtime *trigger = NULL; struct host *h; struct service *s; if (!dt) return DT_ENULL; log_debug_info(DEBUGL_DOWNTIME, 0, "downtime_add(): id=%lu; type=%s; host=%s; service=%s\n", dt->downtime_id, dt->type == HOST_DOWNTIME ? "host" : "service", dt->host_name, dt->service_description); /* * check for errors. * host_name must always be set */ if (!dt->host_name) return DT_EHOST; /* service_description should only be set for service downtime */ if ((dt->type == HOST_DOWNTIME) != (!dt->service_description)) return DT_ETYPE; /* type must be either SERVICE_DOWNTIME or HOST_DOWNTIME */ if (dt->type != SERVICE_DOWNTIME && dt->type != HOST_DOWNTIME) return DT_ETYPE; /* triggered downtime must be triggered by an existing downtime */ if (dt->triggered_by && !(trigger = find_downtime(ANY_DOWNTIME, dt->triggered_by))) return DT_ETRIGGER; /* non-triggered downtime must have start_time < end_time */ if (!trigger && dt->start_time >= dt->end_time) return DT_ETIME; /* flexible downtime must have a duration */ if (!dt->fixed && !dt->duration) return DT_ETIME; /* the object we're adding downtime for must exist */ if (!dt->service_description) { if (!(h = find_host(dt->host_name))) return DT_EHOST; } else if (!(s = find_service(dt->host_name, dt->service_description))) { return DT_ESERVICE; } /* set downtime_id if not already set */ prev_downtime_id = next_downtime_id; if (!dt->downtime_id) { dt->downtime_id = next_downtime_id++; } else if (dt->downtime_id > next_downtime_id) { next_downtime_id = dt->downtime_id + 1; } if (fanout_add(dt_fanout, dt->downtime_id, dt) < 0) { next_downtime_id = prev_downtime_id; return errno; } if(defer_downtime_sorting || !scheduled_downtime_list || downtime_compar(&dt, &scheduled_downtime_list) < 0) { if (scheduled_downtime_list) { scheduled_downtime_list->prev = dt; } dt->next = scheduled_downtime_list; dt->prev = NULL; scheduled_downtime_list = dt; } else { scheduled_downtime *cur; /* add new downtime to downtime list, sorted by start time */ for(cur = scheduled_downtime_list; cur; cur = cur->next) { if(downtime_compar(&dt, &cur) < 0) { dt->prev = cur->prev; if (cur->prev) cur->prev->next = dt; dt->next = cur; cur->prev = dt; break; } if (!cur->next) { dt->next = NULL; cur->next = dt; dt->prev = cur; break; } } } return OK; }