main.c 1.1 KB

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