Blame docs/code/proc-streams/main.c

Packit b5b901
#include <stdio.h>
Packit b5b901
#include <string.h>
Packit b5b901
#include <inttypes.h>
Packit b5b901
Packit b5b901
#include <uv.h>
Packit b5b901
Packit b5b901
uv_loop_t *loop;
Packit b5b901
uv_process_t child_req;
Packit b5b901
uv_process_options_t options;
Packit b5b901
Packit b5b901
void on_exit(uv_process_t *req, int64_t exit_status, int term_signal) {
Packit b5b901
    fprintf(stderr, "Process exited with status %" PRId64 ", signal %d\n", exit_status, term_signal);
Packit b5b901
    uv_close((uv_handle_t*) req, NULL);
Packit b5b901
}
Packit b5b901
Packit b5b901
int main() {
Packit b5b901
    loop = uv_default_loop();
Packit b5b901
Packit b5b901
    size_t size = 500;
Packit b5b901
    char path[size];
Packit b5b901
    uv_exepath(path, &size);
Packit b5b901
    strcpy(path + (strlen(path) - strlen("proc-streams")), "test");
Packit b5b901
Packit b5b901
    char* args[2];
Packit b5b901
    args[0] = path;
Packit b5b901
    args[1] = NULL;
Packit b5b901
Packit b5b901
    /* ... */
Packit b5b901
Packit b5b901
    options.stdio_count = 3;
Packit b5b901
    uv_stdio_container_t child_stdio[3];
Packit b5b901
    child_stdio[0].flags = UV_IGNORE;
Packit b5b901
    child_stdio[1].flags = UV_IGNORE;
Packit b5b901
    child_stdio[2].flags = UV_INHERIT_FD;
Packit b5b901
    child_stdio[2].data.fd = 2;
Packit b5b901
    options.stdio = child_stdio;
Packit b5b901
Packit b5b901
    options.exit_cb = on_exit;
Packit b5b901
    options.file = args[0];
Packit b5b901
    options.args = args;
Packit b5b901
Packit b5b901
    int r;
Packit b5b901
    if ((r = uv_spawn(loop, &child_req, &options))) {
Packit b5b901
        fprintf(stderr, "%s\n", uv_strerror(r));
Packit b5b901
        return 1;
Packit b5b901
    }
Packit b5b901
Packit b5b901
    return uv_run(loop, UV_RUN_DEFAULT);
Packit b5b901
}