run-command.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef RUN_COMMAND_H
  2. #define RUN_COMMAND_H
  3. enum {
  4. ERR_RUN_COMMAND_FORK = 10000,
  5. ERR_RUN_COMMAND_EXEC,
  6. ERR_RUN_COMMAND_PIPE,
  7. ERR_RUN_COMMAND_WAITPID,
  8. ERR_RUN_COMMAND_WAITPID_WRONG_PID,
  9. ERR_RUN_COMMAND_WAITPID_SIGNAL,
  10. ERR_RUN_COMMAND_WAITPID_NOEXIT,
  11. };
  12. #define IS_RUN_COMMAND_ERR(x) (-(x) >= ERR_RUN_COMMAND_FORK)
  13. struct child_process {
  14. const char **argv;
  15. pid_t pid;
  16. /*
  17. * Using .in, .out, .err:
  18. * - Specify 0 for no redirections (child inherits stdin, stdout,
  19. * stderr from parent).
  20. * - Specify -1 to have a pipe allocated as follows:
  21. * .in: returns the writable pipe end; parent writes to it,
  22. * the readable pipe end becomes child's stdin
  23. * .out, .err: returns the readable pipe end; parent reads from
  24. * it, the writable pipe end becomes child's stdout/stderr
  25. * The caller of start_command() must close the returned FDs
  26. * after it has completed reading from/writing to it!
  27. * - Specify > 0 to set a channel to a particular FD as follows:
  28. * .in: a readable FD, becomes child's stdin
  29. * .out: a writable FD, becomes child's stdout/stderr
  30. * .err > 0 not supported
  31. * The specified FD is closed by start_command(), even in case
  32. * of errors!
  33. */
  34. int in;
  35. int out;
  36. int err;
  37. const char *dir;
  38. const char *const *env;
  39. unsigned no_stdin:1;
  40. unsigned no_stdout:1;
  41. unsigned no_stderr:1;
  42. unsigned perf_cmd:1; /* if this is to be perf sub-command */
  43. unsigned stdout_to_stderr:1;
  44. void (*preexec_cb)(void);
  45. };
  46. int start_command(struct child_process *);
  47. int finish_command(struct child_process *);
  48. int run_command(struct child_process *);
  49. extern int run_hook(const char *index_file, const char *name, ...);
  50. #define RUN_COMMAND_NO_STDIN 1
  51. #define RUN_PERF_CMD 2 /*If this is to be perf sub-command */
  52. #define RUN_COMMAND_STDOUT_TO_STDERR 4
  53. int run_command_v_opt(const char **argv, int opt);
  54. /*
  55. * env (the environment) is to be formatted like environ: "VAR=VALUE".
  56. * To unset an environment variable use just "VAR".
  57. */
  58. int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env);
  59. /*
  60. * The purpose of the following functions is to feed a pipe by running
  61. * a function asynchronously and providing output that the caller reads.
  62. *
  63. * It is expected that no synchronization and mutual exclusion between
  64. * the caller and the feed function is necessary so that the function
  65. * can run in a thread without interfering with the caller.
  66. */
  67. struct async {
  68. /*
  69. * proc writes to fd and closes it;
  70. * returns 0 on success, non-zero on failure
  71. */
  72. int (*proc)(int fd, void *data);
  73. void *data;
  74. int out; /* caller reads from here and closes it */
  75. pid_t pid;
  76. };
  77. int start_async(struct async *async);
  78. int finish_async(struct async *async);
  79. #endif