gspawn.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* gspawn.h - Process launching
  2. *
  3. * Copyright 2000 Red Hat, Inc.
  4. *
  5. * SPDX-License-Identifier: LGPL-2.1-or-later
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef __G_SPAWN_H__
  21. #define __G_SPAWN_H__
  22. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  23. #error "Only <glib.h> can be included directly."
  24. #endif
  25. #include <glib/gerror.h>
  26. G_BEGIN_DECLS
  27. /* I'm not sure I remember our proposed naming convention here. */
  28. /**
  29. * G_SPAWN_ERROR:
  30. *
  31. * Error domain for spawning processes. Errors in this domain will
  32. * be from the #GSpawnError enumeration. See #GError for information on
  33. * error domains.
  34. */
  35. #define G_SPAWN_ERROR g_spawn_error_quark ()
  36. /**
  37. * GSpawnError:
  38. * @G_SPAWN_ERROR_FORK: Fork failed due to lack of memory.
  39. * @G_SPAWN_ERROR_READ: Read or select on pipes failed.
  40. * @G_SPAWN_ERROR_CHDIR: Changing to working directory failed.
  41. * @G_SPAWN_ERROR_ACCES: execv() returned `EACCES`
  42. * @G_SPAWN_ERROR_PERM: execv() returned `EPERM`
  43. * @G_SPAWN_ERROR_TOO_BIG: execv() returned `E2BIG`
  44. * @G_SPAWN_ERROR_2BIG: deprecated alias for %G_SPAWN_ERROR_TOO_BIG (deprecated since GLib 2.32)
  45. * @G_SPAWN_ERROR_NOEXEC: execv() returned `ENOEXEC`
  46. * @G_SPAWN_ERROR_NAMETOOLONG: execv() returned `ENAMETOOLONG`
  47. * @G_SPAWN_ERROR_NOENT: execv() returned `ENOENT`
  48. * @G_SPAWN_ERROR_NOMEM: execv() returned `ENOMEM`
  49. * @G_SPAWN_ERROR_NOTDIR: execv() returned `ENOTDIR`
  50. * @G_SPAWN_ERROR_LOOP: execv() returned `ELOOP`
  51. * @G_SPAWN_ERROR_TXTBUSY: execv() returned `ETXTBUSY`
  52. * @G_SPAWN_ERROR_IO: execv() returned `EIO`
  53. * @G_SPAWN_ERROR_NFILE: execv() returned `ENFILE`
  54. * @G_SPAWN_ERROR_MFILE: execv() returned `EMFILE`
  55. * @G_SPAWN_ERROR_INVAL: execv() returned `EINVAL`
  56. * @G_SPAWN_ERROR_ISDIR: execv() returned `EISDIR`
  57. * @G_SPAWN_ERROR_LIBBAD: execv() returned `ELIBBAD`
  58. * @G_SPAWN_ERROR_FAILED: Some other fatal failure,
  59. * `error->message` should explain.
  60. *
  61. * Error codes returned by spawning processes.
  62. */
  63. typedef enum
  64. {
  65. G_SPAWN_ERROR_FORK, /* fork failed due to lack of memory */
  66. G_SPAWN_ERROR_READ, /* read or select on pipes failed */
  67. G_SPAWN_ERROR_CHDIR, /* changing to working dir failed */
  68. G_SPAWN_ERROR_ACCES, /* execv() returned EACCES */
  69. G_SPAWN_ERROR_PERM, /* execv() returned EPERM */
  70. G_SPAWN_ERROR_TOO_BIG,/* execv() returned E2BIG */
  71. G_SPAWN_ERROR_2BIG GLIB_DEPRECATED_ENUMERATOR_IN_2_32_FOR(G_SPAWN_ERROR_TOO_BIG) = G_SPAWN_ERROR_TOO_BIG,
  72. G_SPAWN_ERROR_NOEXEC, /* execv() returned ENOEXEC */
  73. G_SPAWN_ERROR_NAMETOOLONG, /* "" "" ENAMETOOLONG */
  74. G_SPAWN_ERROR_NOENT, /* "" "" ENOENT */
  75. G_SPAWN_ERROR_NOMEM, /* "" "" ENOMEM */
  76. G_SPAWN_ERROR_NOTDIR, /* "" "" ENOTDIR */
  77. G_SPAWN_ERROR_LOOP, /* "" "" ELOOP */
  78. G_SPAWN_ERROR_TXTBUSY, /* "" "" ETXTBUSY */
  79. G_SPAWN_ERROR_IO, /* "" "" EIO */
  80. G_SPAWN_ERROR_NFILE, /* "" "" ENFILE */
  81. G_SPAWN_ERROR_MFILE, /* "" "" EMFLE */
  82. G_SPAWN_ERROR_INVAL, /* "" "" EINVAL */
  83. G_SPAWN_ERROR_ISDIR, /* "" "" EISDIR */
  84. G_SPAWN_ERROR_LIBBAD, /* "" "" ELIBBAD */
  85. G_SPAWN_ERROR_FAILED /* other fatal failure, error->message
  86. * should explain
  87. */
  88. } GSpawnError;
  89. /**
  90. * G_SPAWN_EXIT_ERROR:
  91. *
  92. * Error domain used by g_spawn_check_wait_status(). The code
  93. * will be the program exit code.
  94. */
  95. #define G_SPAWN_EXIT_ERROR g_spawn_exit_error_quark ()
  96. /**
  97. * GSpawnChildSetupFunc:
  98. * @data: user data passed to the function.
  99. *
  100. * Specifies the type of the setup function passed to g_spawn_async(),
  101. * g_spawn_sync() and g_spawn_async_with_pipes(), which can, in very
  102. * limited ways, be used to affect the child's execution.
  103. *
  104. * On POSIX platforms, the function is called in the child after GLib
  105. * has performed all the setup it plans to perform, but before calling
  106. * exec(). Actions taken in this function will only affect the child,
  107. * not the parent.
  108. *
  109. * On Windows, the function is called in the parent. Its usefulness on
  110. * Windows is thus questionable. In many cases executing the child setup
  111. * function in the parent can have ill effects, and you should be very
  112. * careful when porting software to Windows that uses child setup
  113. * functions.
  114. *
  115. * However, even on POSIX, you are extremely limited in what you can
  116. * safely do from a #GSpawnChildSetupFunc, because any mutexes that were
  117. * held by other threads in the parent process at the time of the fork()
  118. * will still be locked in the child process, and they will never be
  119. * unlocked (since the threads that held them don't exist in the child).
  120. * POSIX allows only async-signal-safe functions (see signal(7)) to be
  121. * called in the child between fork() and exec(), which drastically limits
  122. * the usefulness of child setup functions.
  123. *
  124. * In particular, it is not safe to call any function which may
  125. * call malloc(), which includes POSIX functions such as setenv().
  126. * If you need to set up the child environment differently from
  127. * the parent, you should use g_get_environ(), g_environ_setenv(),
  128. * and g_environ_unsetenv(), and then pass the complete environment
  129. * list to the `g_spawn...` function.
  130. */
  131. typedef void (* GSpawnChildSetupFunc) (gpointer data);
  132. /**
  133. * GSpawnFlags:
  134. * @G_SPAWN_DEFAULT: no flags, default behaviour
  135. * @G_SPAWN_LEAVE_DESCRIPTORS_OPEN: the parent's open file descriptors will
  136. * be inherited by the child; otherwise all descriptors except stdin,
  137. * stdout and stderr will be closed before calling exec() in the child.
  138. * @G_SPAWN_DO_NOT_REAP_CHILD: the child will not be automatically reaped;
  139. * you must use g_child_watch_add() yourself (or call waitpid() or handle
  140. * `SIGCHLD` yourself), or the child will become a zombie.
  141. * @G_SPAWN_SEARCH_PATH: `argv[0]` need not be an absolute path, it will be
  142. * looked for in the user's `PATH`.
  143. * @G_SPAWN_STDOUT_TO_DEV_NULL: the child's standard output will be discarded,
  144. * instead of going to the same location as the parent's standard output.
  145. * @G_SPAWN_STDERR_TO_DEV_NULL: the child's standard error will be discarded.
  146. * @G_SPAWN_CHILD_INHERITS_STDIN: the child will inherit the parent's standard
  147. * input (by default, the child's standard input is attached to `/dev/null`).
  148. * @G_SPAWN_FILE_AND_ARGV_ZERO: the first element of `argv` is the file to
  149. * execute, while the remaining elements are the actual argument vector
  150. * to pass to the file. Normally g_spawn_async_with_pipes() uses `argv[0]`
  151. * as the file to execute, and passes all of `argv` to the child.
  152. * @G_SPAWN_SEARCH_PATH_FROM_ENVP: if `argv[0]` is not an absolute path,
  153. * it will be looked for in the `PATH` from the passed child environment.
  154. * Since: 2.34
  155. * @G_SPAWN_CLOEXEC_PIPES: create all pipes with the `O_CLOEXEC` flag set.
  156. * Since: 2.40
  157. * @G_SPAWN_CHILD_INHERITS_STDOUT: the child will inherit the parent's standard output.
  158. * Since: 2.74
  159. * @G_SPAWN_CHILD_INHERITS_STDERR: the child will inherit the parent's standard error.
  160. * Since: 2.74
  161. * @G_SPAWN_STDIN_FROM_DEV_NULL: the child's standard input is attached to `/dev/null`.
  162. * Since: 2.74
  163. *
  164. * Flags passed to g_spawn_sync(), g_spawn_async() and g_spawn_async_with_pipes().
  165. */
  166. typedef enum
  167. {
  168. G_SPAWN_DEFAULT = 0,
  169. G_SPAWN_LEAVE_DESCRIPTORS_OPEN = 1 << 0,
  170. G_SPAWN_DO_NOT_REAP_CHILD = 1 << 1,
  171. /* look for argv[0] in the path i.e. use execvp() */
  172. G_SPAWN_SEARCH_PATH = 1 << 2,
  173. /* Dump output to /dev/null */
  174. G_SPAWN_STDOUT_TO_DEV_NULL = 1 << 3,
  175. G_SPAWN_STDERR_TO_DEV_NULL = 1 << 4,
  176. G_SPAWN_CHILD_INHERITS_STDIN = 1 << 5,
  177. G_SPAWN_FILE_AND_ARGV_ZERO = 1 << 6,
  178. G_SPAWN_SEARCH_PATH_FROM_ENVP = 1 << 7,
  179. G_SPAWN_CLOEXEC_PIPES = 1 << 8,
  180. /**
  181. * G_SPAWN_CHILD_INHERITS_STDOUT:
  182. *
  183. * The child will inherit the parent's standard output.
  184. *
  185. * Since: 2.74
  186. */
  187. G_SPAWN_CHILD_INHERITS_STDOUT = 1 << 9,
  188. /**
  189. * G_SPAWN_CHILD_INHERITS_STDERR:
  190. *
  191. * The child will inherit the parent's standard error.
  192. *
  193. * Since: 2.74
  194. */
  195. G_SPAWN_CHILD_INHERITS_STDERR = 1 << 10,
  196. /**
  197. * G_SPAWN_STDIN_FROM_DEV_NULL:
  198. *
  199. * The child's standard input is attached to `/dev/null`.
  200. *
  201. * Since: 2.74
  202. */
  203. G_SPAWN_STDIN_FROM_DEV_NULL = 1 << 11
  204. } GSpawnFlags;
  205. GLIB_AVAILABLE_IN_ALL
  206. GQuark g_spawn_error_quark (void);
  207. GLIB_AVAILABLE_IN_ALL
  208. GQuark g_spawn_exit_error_quark (void);
  209. GLIB_AVAILABLE_IN_ALL
  210. gboolean g_spawn_async (const gchar *working_directory,
  211. gchar **argv,
  212. gchar **envp,
  213. GSpawnFlags flags,
  214. GSpawnChildSetupFunc child_setup,
  215. gpointer user_data,
  216. GPid *child_pid,
  217. GError **error);
  218. /* Opens pipes for non-NULL standard_output, standard_input, standard_error,
  219. * and returns the parent's end of the pipes.
  220. */
  221. GLIB_AVAILABLE_IN_ALL
  222. gboolean g_spawn_async_with_pipes (const gchar *working_directory,
  223. gchar **argv,
  224. gchar **envp,
  225. GSpawnFlags flags,
  226. GSpawnChildSetupFunc child_setup,
  227. gpointer user_data,
  228. GPid *child_pid,
  229. gint *standard_input,
  230. gint *standard_output,
  231. gint *standard_error,
  232. GError **error);
  233. GLIB_AVAILABLE_IN_2_68
  234. gboolean g_spawn_async_with_pipes_and_fds (const gchar *working_directory,
  235. const gchar * const *argv,
  236. const gchar * const *envp,
  237. GSpawnFlags flags,
  238. GSpawnChildSetupFunc child_setup,
  239. gpointer user_data,
  240. gint stdin_fd,
  241. gint stdout_fd,
  242. gint stderr_fd,
  243. const gint *source_fds,
  244. const gint *target_fds,
  245. gsize n_fds,
  246. GPid *child_pid_out,
  247. gint *stdin_pipe_out,
  248. gint *stdout_pipe_out,
  249. gint *stderr_pipe_out,
  250. GError **error);
  251. /* Lets you provide fds for stdin/stdout/stderr */
  252. GLIB_AVAILABLE_IN_2_58
  253. gboolean g_spawn_async_with_fds (const gchar *working_directory,
  254. gchar **argv,
  255. gchar **envp,
  256. GSpawnFlags flags,
  257. GSpawnChildSetupFunc child_setup,
  258. gpointer user_data,
  259. GPid *child_pid,
  260. gint stdin_fd,
  261. gint stdout_fd,
  262. gint stderr_fd,
  263. GError **error);
  264. /* If standard_output or standard_error are non-NULL, the full
  265. * standard output or error of the command will be placed there.
  266. */
  267. GLIB_AVAILABLE_IN_ALL
  268. gboolean g_spawn_sync (const gchar *working_directory,
  269. gchar **argv,
  270. gchar **envp,
  271. GSpawnFlags flags,
  272. GSpawnChildSetupFunc child_setup,
  273. gpointer user_data,
  274. gchar **standard_output,
  275. gchar **standard_error,
  276. gint *wait_status,
  277. GError **error);
  278. GLIB_AVAILABLE_IN_ALL
  279. gboolean g_spawn_command_line_sync (const gchar *command_line,
  280. gchar **standard_output,
  281. gchar **standard_error,
  282. gint *wait_status,
  283. GError **error);
  284. GLIB_AVAILABLE_IN_ALL
  285. gboolean g_spawn_command_line_async (const gchar *command_line,
  286. GError **error);
  287. GLIB_AVAILABLE_IN_2_70
  288. gboolean g_spawn_check_wait_status (gint wait_status,
  289. GError **error);
  290. GLIB_DEPRECATED_IN_2_70_FOR(g_spawn_check_wait_status)
  291. gboolean g_spawn_check_exit_status (gint wait_status,
  292. GError **error);
  293. GLIB_AVAILABLE_IN_ALL
  294. void g_spawn_close_pid (GPid pid);
  295. G_END_DECLS
  296. #endif /* __G_SPAWN_H__ */