xref: /libuv/docs/code/proc-streams/main.c (revision d59d6e6f)
1 #include <stdio.h>
2 #include <string.h>
3 #include <inttypes.h>
4 
5 #include <uv.h>
6 
7 uv_loop_t *loop;
8 uv_process_t child_req;
9 uv_process_options_t options;
10 
on_exit(uv_process_t * req,int64_t exit_status,int term_signal)11 void on_exit(uv_process_t *req, int64_t exit_status, int term_signal) {
12     fprintf(stderr, "Process exited with status %" PRId64 ", signal %d\n", exit_status, term_signal);
13     uv_close((uv_handle_t*) req, NULL);
14 }
15 
main()16 int main() {
17     loop = uv_default_loop();
18 
19     size_t size = 500;
20     char path[size];
21     uv_exepath(path, &size);
22     strcpy(path + (strlen(path) - strlen("proc-streams")), "test");
23 
24     char* args[2];
25     args[0] = path;
26     args[1] = NULL;
27 
28     /* ... */
29 
30     options.stdio_count = 3;
31     uv_stdio_container_t child_stdio[3];
32     child_stdio[0].flags = UV_IGNORE;
33     child_stdio[1].flags = UV_IGNORE;
34     child_stdio[2].flags = UV_INHERIT_FD;
35     child_stdio[2].data.fd = 2;
36     options.stdio = child_stdio;
37 
38     options.exit_cb = on_exit;
39     options.file = args[0];
40     options.args = args;
41 
42     int r;
43     if ((r = uv_spawn(loop, &child_req, &options))) {
44         fprintf(stderr, "%s\n", uv_strerror(r));
45         return 1;
46     }
47 
48     return uv_run(loop, UV_RUN_DEFAULT);
49 }
50