gspawn.h 12 KB

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