/* * Handles adding the command and macros to the kvvec, * as well as shipping the command off to a designated * worker */ static int wproc_run_job(worker_job *job, nagios_macros *mac) { static struct kvvec kvv = KVVEC_INITIALIZER; worker_process *wp; /* * get_worker() also adds job to the workers list * and sets job_id */ wp = get_worker(job); if (!wp || job->id < 0) return ERROR; /* * XXX FIXME: add environment macros as * kvvec_addkv(kvv, "env", "NAGIOS_LALAMACRO=VALUE"); * kvvec_addkv(kvv, "env", "NAGIOS_LALAMACRO2=VALUE"); * so workers know to add them to environment. For now, * we don't support that though. */ if (!kvvec_init(&kvv, 4)) /* job_id, type, command and timeout */ return ERROR; kvvec_addkv(&kvv, "job_id", (char *)mkstr("%d", job->id)); kvvec_addkv(&kvv, "type", (char *)mkstr("%d", job->type)); kvvec_addkv(&kvv, "command", job->command); kvvec_addkv(&kvv, "timeout", (char *)mkstr("%u", job->timeout)); send_kvvec(wp->sd, &kvv); wp->jobs_running++; wp->jobs_started++; return 0; }
static int send_command(int sd, int events, void *discard) { char buf[8192]; int ret; simple_worker *wp; struct kvvec *kvv; ret = read(sd, buf, sizeof(buf)); if (ret == 0) { iobroker_close(iobs, sd); return 0; } if (ret < 0) { printf("main: Failed to read() from fd %d: %s", sd, strerror(errno)); } /* this happens when we're reading from stdin */ buf[--ret] = 0; kvv = kvvec_create(5); wp = wps[wp_index++ % NWPS]; kvvec_addkv(kvv, "job_id", (char *)mkstr("%d", wp->job_index++)); kvvec_addkv_wlen(kvv, "command", sizeof("command") - 1, buf, ret); kvvec_addkv(kvv, "timeout", (char *)mkstr("%d", 10)); printf("Sending kvvec with %d pairs to worker %d\n", kvv->kv_pairs, wp->pid); worker_send_kvvec(wp->sd, kvv); kvvec_destroy(kvv, 0); return 0; }
/* * Handles adding the command and macros to the kvvec, * as well as shipping the command off to a designated * worker */ static int wproc_run_job(struct wproc_job *job, nagios_macros *mac) { static struct kvvec kvv = KVVEC_INITIALIZER; struct kvvec_buf *kvvb; struct kvvec *env_kvvp = NULL; struct kvvec_buf *env_kvvb = NULL; struct wproc_worker *wp; int ret, result = OK; if (!job || !job->wp) return ERROR; wp = job->wp; if (!kvvec_init(&kvv, 4)) /* job_id, type, command and timeout */ return ERROR; kvvec_addkv(&kvv, "job_id", (char *)mkstr("%d", job->id)); kvvec_addkv(&kvv, "type", (char *)mkstr("%d", job->type)); kvvec_addkv(&kvv, "command", job->command); kvvec_addkv(&kvv, "timeout", (char *)mkstr("%u", job->timeout)); /* Add the macro environment variables */ if(mac) { env_kvvp = macros_to_kvv(mac); if(NULL != env_kvvp) { env_kvvb = kvvec2buf(env_kvvp, '=', '\n', 0); if(NULL == env_kvvb) { kvvec_destroy(env_kvvp, KVVEC_FREE_KEYS); } else { kvvec_addkv_wlen(&kvv, "env", strlen("env"), env_kvvb->buf, env_kvvb->buflen); } } } kvvb = build_kvvec_buf(&kvv); ret = write(wp->sd, kvvb->buf, kvvb->bufsize); if (ret != (int)kvvb->bufsize) { logit(NSLOG_RUNTIME_ERROR, TRUE, "wproc: '%s' seems to be choked. ret = %d; bufsize = %lu: errno = %d (%s)\n", wp->name, ret, kvvb->bufsize, errno, strerror(errno)); destroy_job(job); result = ERROR; } else { wp->jobs_running++; wp->jobs_started++; loadctl.jobs_running++; } if(NULL != env_kvvp) kvvec_destroy(env_kvvp, KVVEC_FREE_KEYS); if(NULL != env_kvvb) { free(env_kvvb->buf); free(env_kvvb); } free(kvvb->buf); free(kvvb); return result; }
/* * Handles adding the command and macros to the kvvec, * as well as shipping the command off to a designated * worker */ static int wproc_run_job(struct wproc_job *job, nagios_macros *mac) { static struct kvvec kvv = KVVEC_INITIALIZER; struct kvvec_buf *kvvb; struct wproc_worker *wp; int ret, result = OK; if (!job || !job->wp) return ERROR; wp = job->wp; /* * XXX FIXME: add environment macros as * kvvec_addkv(kvv, "env", "NAGIOS_LALAMACRO=VALUE"); * kvvec_addkv(kvv, "env", "NAGIOS_LALAMACRO2=VALUE"); * so workers know to add them to environment. For now, * we don't support that though. */ if (!kvvec_init(&kvv, 4)) /* job_id, type, command and timeout */ return ERROR; kvvec_addkv(&kvv, "job_id", (char *)mkstr("%d", job->id)); kvvec_addkv(&kvv, "type", (char *)mkstr("%d", job->type)); kvvec_addkv(&kvv, "command", job->command); kvvec_addkv(&kvv, "timeout", (char *)mkstr("%u", job->timeout)); kvvb = build_kvvec_buf(&kvv); ret = write(wp->sd, kvvb->buf, kvvb->bufsize); if (ret != (int)kvvb->bufsize) { logit(NSLOG_RUNTIME_ERROR, TRUE, "wproc: '%s' seems to be choked. ret = %d; bufsize = %lu: errno = %d (%s)\n", wp->name, ret, kvvb->bufsize, errno, strerror(errno)); destroy_job(job); result = ERROR; } else { wp->jobs_running++; wp->jobs_started++; loadctl.jobs_running++; } free(kvvb->buf); free(kvvb); return result; }
static void add_vars(struct kvvec *kvv, const char **ary, int len) { int i; for (i = 0; i < len && ary[i]; i++) { char *arg = strdup(test_data[i]); char *eq = strchr(arg, '='); if (eq) { *eq++ = 0; } kvvec_addkv(kvv, strdup(arg), eq ? strdup(eq) : NULL); free(arg); } }
static void job_error(child_process *cp, struct kvvec *kvv, const char *fmt, ...) { char msg[4096]; int len; va_list ap; int ret; va_start(ap, fmt); len = vsnprintf(msg, sizeof(msg) - 1, fmt, ap); va_end(ap); if (cp) { kvvec_addkv(kvv, "job_id", (char *)mkstr("%d", cp->id)); } kvvec_addkv_wlen(kvv, "error_msg", 9, msg, len); ret = send_kvvec(master_sd, kvv); if (ret < 0 && errno == EPIPE) exit_worker(); kvvec_destroy(kvv, 0); }
static int finish_job(child_process *cp, int reason) { static struct kvvec resp = KVVEC_INITIALIZER; struct rusage *ru = &cp->rusage; int i, ret; /* how many key/value pairs do we need? */ if (kvvec_init(&resp, 12 + cp->request->kv_pairs) == NULL) { /* what the hell do we do now? */ exit_worker(); } gettimeofday(&cp->stop, NULL); if (running_jobs != squeue_size(sq)) { wlog("running_jobs(%d) != squeue_size(sq) (%d)\n", running_jobs, squeue_size(sq)); wlog("started: %d; running: %d; finished: %d\n", started, running_jobs, started - running_jobs); } /* * we must remove the job's timeout ticker, * or we'll end up accessing an already free()'d * pointer, or the pointer to a different child. */ squeue_remove(sq, cp->sq_event); /* get rid of still open filedescriptors */ if (cp->outstd.fd != -1) iobroker_close(iobs, cp->outstd.fd); if (cp->outerr.fd != -1) iobroker_close(iobs, cp->outerr.fd); cp->runtime = tv_delta_f(&cp->start, &cp->stop); /* * Now build the return message. * First comes the request, minus environment variables */ for (i = 0; i < cp->request->kv_pairs; i++) { struct key_value *kv = &cp->request->kv[i]; /* skip environment macros */ if (kv->key_len == 3 && !strcmp(kv->key, "env")) { continue; } kvvec_addkv_wlen(&resp, kv->key, kv->key_len, kv->value, kv->value_len); } kvvec_addkv(&resp, "wait_status", (char *)mkstr("%d", cp->ret)); kvvec_addkv_wlen(&resp, "outstd", 6, cp->outstd.buf, cp->outstd.len); kvvec_addkv_wlen(&resp, "outerr", 6, cp->outerr.buf, cp->outerr.len); kvvec_add_tv(&resp, "start", cp->start); kvvec_add_tv(&resp, "stop", cp->stop); kvvec_addkv(&resp, "runtime", (char *)mkstr("%f", cp->runtime)); if (!reason) { /* child exited nicely */ kvvec_addkv(&resp, "exited_ok", "1"); kvvec_add_tv(&resp, "ru_utime", ru->ru_utime); kvvec_add_tv(&resp, "ru_stime", ru->ru_stime); kvvec_add_long(&resp, "ru_minflt", ru->ru_minflt); kvvec_add_long(&resp, "ru_majflt", ru->ru_majflt); kvvec_add_long(&resp, "ru_nswap", ru->ru_nswap); kvvec_add_long(&resp, "ru_inblock", ru->ru_inblock); kvvec_add_long(&resp, "ru_oublock", ru->ru_oublock); kvvec_add_long(&resp, "ru_nsignals", ru->ru_nsignals); } else { /* some error happened */ kvvec_addkv(&resp, "exited_ok", "0"); kvvec_addkv(&resp, "error_code", (char *)mkstr("%d", reason)); } ret = send_kvvec(master_sd, &resp); if (ret < 0 && errno == EPIPE) exit_worker(); running_jobs--; if (cp->outstd.buf) { free(cp->outstd.buf); cp->outstd.buf = NULL; } if (cp->outerr.buf) { free(cp->outerr.buf); cp->outerr.buf = NULL; } kvvec_destroy(cp->request, KVVEC_FREE_ALL); free(cp->cmd); free(cp); return 0; }
int finish_job(child_process *cp, int reason) { static struct kvvec resp = KVVEC_INITIALIZER; struct rusage *ru = &cp->ei->rusage; int i, ret; /* get rid of still open filedescriptors */ if (cp->outstd.fd != -1) { gather_output(cp, &cp->outstd, 1); iobroker_close(iobs, cp->outstd.fd); } if (cp->outerr.fd != -1) { gather_output(cp, &cp->outerr, 1); iobroker_close(iobs, cp->outerr.fd); } /* Make sure network-supplied data doesn't contain nul bytes */ strip_nul_bytes(cp->outstd); strip_nul_bytes(cp->outerr); /* how many key/value pairs do we need? */ if (kvvec_init(&resp, 12 + cp->request->kv_pairs) == NULL) { /* what the hell do we do now? */ exit_worker(1, "Failed to init response key/value vector"); } gettimeofday(&cp->ei->stop, NULL); if (running_jobs != squeue_size(sq)) { wlog("running_jobs(%d) != squeue_size(sq) (%d)\n", running_jobs, squeue_size(sq)); wlog("started: %d; running: %d; finished: %d\n", started, running_jobs, started - running_jobs); } cp->ei->runtime = tv_delta_f(&cp->ei->start, &cp->ei->stop); /* * Now build the return message. * First comes the request, minus environment variables */ for (i = 0; i < cp->request->kv_pairs; i++) { struct key_value *kv = &cp->request->kv[i]; /* skip environment macros */ if (kv->key_len == 3 && !strcmp(kv->key, "env")) { continue; } kvvec_addkv_wlen(&resp, kv->key, kv->key_len, kv->value, kv->value_len); } kvvec_addkv(&resp, "wait_status", mkstr("%d", cp->ret)); kvvec_add_tv(&resp, "start", cp->ei->start); kvvec_add_tv(&resp, "stop", cp->ei->stop); kvvec_addkv(&resp, "runtime", mkstr("%f", cp->ei->runtime)); if (!reason) { /* child exited nicely (or with a signal, so check wait_status) */ kvvec_addkv(&resp, "exited_ok", "1"); kvvec_add_tv(&resp, "ru_utime", ru->ru_utime); kvvec_add_tv(&resp, "ru_stime", ru->ru_stime); kvvec_add_long(&resp, "ru_minflt", ru->ru_minflt); kvvec_add_long(&resp, "ru_majflt", ru->ru_majflt); kvvec_add_long(&resp, "ru_inblock", ru->ru_inblock); kvvec_add_long(&resp, "ru_oublock", ru->ru_oublock); } else { /* some error happened */ kvvec_addkv(&resp, "exited_ok", "0"); kvvec_addkv(&resp, "error_code", mkstr("%d", reason)); } kvvec_addkv_wlen(&resp, "outerr", 6, cp->outerr.buf, cp->outerr.len); kvvec_addkv_wlen(&resp, "outstd", 6, cp->outstd.buf, cp->outstd.len); ret = worker_send_kvvec(master_sd, &resp); if (ret < 0 && errno == EPIPE) exit_worker(1, "Failed to send kvvec struct to master"); return 0; }