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