gspawn.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* gspawn.h - Process launching
  2. *
  3. * Copyright 2000 Red Hat, Inc.
  4. *
  5. * GLib is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public License as
  7. * published by the Free Software Foundation; either version 2 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * GLib 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
  16. * License along with GLib; see the file COPYING.LIB. If not, write
  17. * to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. * Boston, MA 02111-1307, USA.
  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
  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. #ifndef G_DISABLE_DEPRECATED
  72. G_SPAWN_ERROR_2BIG = G_SPAWN_ERROR_TOO_BIG,
  73. #endif
  74. G_SPAWN_ERROR_NOEXEC, /* execv() returned ENOEXEC */
  75. G_SPAWN_ERROR_NAMETOOLONG, /* "" "" ENAMETOOLONG */
  76. G_SPAWN_ERROR_NOENT, /* "" "" ENOENT */
  77. G_SPAWN_ERROR_NOMEM, /* "" "" ENOMEM */
  78. G_SPAWN_ERROR_NOTDIR, /* "" "" ENOTDIR */
  79. G_SPAWN_ERROR_LOOP, /* "" "" ELOOP */
  80. G_SPAWN_ERROR_TXTBUSY, /* "" "" ETXTBUSY */
  81. G_SPAWN_ERROR_IO, /* "" "" EIO */
  82. G_SPAWN_ERROR_NFILE, /* "" "" ENFILE */
  83. G_SPAWN_ERROR_MFILE, /* "" "" EMFLE */
  84. G_SPAWN_ERROR_INVAL, /* "" "" EINVAL */
  85. G_SPAWN_ERROR_ISDIR, /* "" "" EISDIR */
  86. G_SPAWN_ERROR_LIBBAD, /* "" "" ELIBBAD */
  87. G_SPAWN_ERROR_FAILED /* other fatal failure, error->message
  88. * should explain
  89. */
  90. } GSpawnError;
  91. /**
  92. * G_SPAWN_EXIT_ERROR:
  93. *
  94. * Error domain used by g_spawn_check_exit_status(). The code
  95. * will be the program exit code.
  96. */
  97. #define G_SPAWN_EXIT_ERROR g_spawn_exit_error_quark ()
  98. /**
  99. * GSpawnChildSetupFunc:
  100. * @user_data: user data to pass to the function.
  101. *
  102. * Specifies the type of the setup function passed to g_spawn_async(),
  103. * g_spawn_sync() and g_spawn_async_with_pipes(), which can, in very
  104. * limited ways, be used to affect the child's execution.
  105. *
  106. * On POSIX platforms, the function is called in the child after GLib
  107. * has performed all the setup it plans to perform, but before calling
  108. * exec(). Actions taken in this function will only affect the child,
  109. * not the parent.
  110. *
  111. * On Windows, the function is called in the parent. Its usefulness on
  112. * Windows is thus questionable. In many cases executing the child setup
  113. * function in the parent can have ill effects, and you should be very
  114. * careful when porting software to Windows that uses child setup
  115. * functions.
  116. *
  117. * However, even on POSIX, you are extremely limited in what you can
  118. * safely do from a #GSpawnChildSetupFunc, because any mutexes that were
  119. * held by other threads in the parent process at the time of the fork()
  120. * will still be locked in the child process, and they will never be
  121. * unlocked (since the threads that held them don't exist in the child).
  122. * POSIX allows only async-signal-safe functions (see signal(7)) to be
  123. * called in the child between fork() and exec(), which drastically limits
  124. * the usefulness of child setup functions.
  125. *
  126. * In particular, it is not safe to call any function which may
  127. * call malloc(), which includes POSIX functions such as setenv().
  128. * If you need to set up the child environment differently from
  129. * the parent, you should use g_get_environ(), g_environ_setenv(),
  130. * and g_environ_unsetenv(), and then pass the complete environment
  131. * list to the `g_spawn...` function.
  132. */
  133. typedef void (* GSpawnChildSetupFunc) (gpointer user_data);
  134. /**
  135. * GSpawnFlags:
  136. * @G_SPAWN_DEFAULT: no flags, default behaviour
  137. * @G_SPAWN_LEAVE_DESCRIPTORS_OPEN: the parent's open file descriptors will
  138. * be inherited by the child; otherwise all descriptors except stdin,
  139. * stdout and stderr will be closed before calling exec() in the child.
  140. * @G_SPAWN_DO_NOT_REAP_CHILD: the child will not be automatically reaped;
  141. * you must use g_child_watch_add() yourself (or call waitpid() or handle
  142. * `SIGCHLD` yourself), or the child will become a zombie.
  143. * @G_SPAWN_SEARCH_PATH: `argv[0]` need not be an absolute path, it will be
  144. * looked for in the user's `PATH`.
  145. * @G_SPAWN_STDOUT_TO_DEV_NULL: the child's standard output will be discarded,
  146. * instead of going to the same location as the parent's standard output.
  147. * @G_SPAWN_STDERR_TO_DEV_NULL: the child's standard error will be discarded.
  148. * @G_SPAWN_CHILD_INHERITS_STDIN: the child will inherit the parent's standard
  149. * input (by default, the child's standard input is attached to `/dev/null`).
  150. * @G_SPAWN_FILE_AND_ARGV_ZERO: the first element of `argv` is the file to
  151. * execute, while the remaining elements are the actual argument vector
  152. * to pass to the file. Normally g_spawn_async_with_pipes() uses `argv[0]`
  153. * as the file to execute, and passes all of `argv` to the child.
  154. * @G_SPAWN_SEARCH_PATH_FROM_ENVP: if `argv[0]` is not an abolute path,
  155. * it will be looked for in the `PATH` from the passed child environment.
  156. * Since: 2.34
  157. * @G_SPAWN_CLOEXEC_PIPES: create all pipes with the `O_CLOEXEC` flag set.
  158. * Since: 2.40
  159. *
  160. * Flags passed to g_spawn_sync(), g_spawn_async() and g_spawn_async_with_pipes().
  161. */
  162. typedef enum
  163. {
  164. G_SPAWN_DEFAULT = 0,
  165. G_SPAWN_LEAVE_DESCRIPTORS_OPEN = 1 << 0,
  166. G_SPAWN_DO_NOT_REAP_CHILD = 1 << 1,
  167. /* look for argv[0] in the path i.e. use execvp() */
  168. G_SPAWN_SEARCH_PATH = 1 << 2,
  169. /* Dump output to /dev/null */
  170. G_SPAWN_STDOUT_TO_DEV_NULL = 1 << 3,
  171. G_SPAWN_STDERR_TO_DEV_NULL = 1 << 4,
  172. G_SPAWN_CHILD_INHERITS_STDIN = 1 << 5,
  173. G_SPAWN_FILE_AND_ARGV_ZERO = 1 << 6,
  174. G_SPAWN_SEARCH_PATH_FROM_ENVP = 1 << 7,
  175. G_SPAWN_CLOEXEC_PIPES = 1 << 8
  176. } GSpawnFlags;
  177. GLIB_AVAILABLE_IN_ALL
  178. GQuark g_spawn_error_quark (void);
  179. GLIB_AVAILABLE_IN_ALL
  180. GQuark g_spawn_exit_error_quark (void);
  181. GLIB_AVAILABLE_IN_ALL
  182. gboolean g_spawn_async (const gchar *working_directory,
  183. gchar **argv,
  184. gchar **envp,
  185. GSpawnFlags flags,
  186. GSpawnChildSetupFunc child_setup,
  187. gpointer user_data,
  188. GPid *child_pid,
  189. GError **error);
  190. /* Opens pipes for non-NULL standard_output, standard_input, standard_error,
  191. * and returns the parent's end of the pipes.
  192. */
  193. GLIB_AVAILABLE_IN_ALL
  194. gboolean g_spawn_async_with_pipes (const gchar *working_directory,
  195. gchar **argv,
  196. gchar **envp,
  197. GSpawnFlags flags,
  198. GSpawnChildSetupFunc child_setup,
  199. gpointer user_data,
  200. GPid *child_pid,
  201. gint *standard_input,
  202. gint *standard_output,
  203. gint *standard_error,
  204. GError **error);
  205. /* If standard_output or standard_error are non-NULL, the full
  206. * standard output or error of the command will be placed there.
  207. */
  208. GLIB_AVAILABLE_IN_ALL
  209. gboolean g_spawn_sync (const gchar *working_directory,
  210. gchar **argv,
  211. gchar **envp,
  212. GSpawnFlags flags,
  213. GSpawnChildSetupFunc child_setup,
  214. gpointer user_data,
  215. gchar **standard_output,
  216. gchar **standard_error,
  217. gint *exit_status,
  218. GError **error);
  219. GLIB_AVAILABLE_IN_ALL
  220. gboolean g_spawn_command_line_sync (const gchar *command_line,
  221. gchar **standard_output,
  222. gchar **standard_error,
  223. gint *exit_status,
  224. GError **error);
  225. GLIB_AVAILABLE_IN_ALL
  226. gboolean g_spawn_command_line_async (const gchar *command_line,
  227. GError **error);
  228. GLIB_AVAILABLE_IN_2_34
  229. gboolean g_spawn_check_exit_status (gint exit_status,
  230. GError **error);
  231. GLIB_AVAILABLE_IN_ALL
  232. void g_spawn_close_pid (GPid pid);
  233. #ifndef __GTK_DOC_IGNORE__
  234. #ifdef G_OS_WIN32
  235. #define g_spawn_async g_spawn_async_utf8
  236. #define g_spawn_async_with_pipes g_spawn_async_with_pipes_utf8
  237. #define g_spawn_sync g_spawn_sync_utf8
  238. #define g_spawn_command_line_sync g_spawn_command_line_sync_utf8
  239. #define g_spawn_command_line_async g_spawn_command_line_async_utf8
  240. GLIB_AVAILABLE_IN_ALL
  241. gboolean g_spawn_async_utf8 (const gchar *working_directory,
  242. gchar **argv,
  243. gchar **envp,
  244. GSpawnFlags flags,
  245. GSpawnChildSetupFunc child_setup,
  246. gpointer user_data,
  247. GPid *child_pid,
  248. GError **error);
  249. GLIB_AVAILABLE_IN_ALL
  250. gboolean g_spawn_async_with_pipes_utf8 (const gchar *working_directory,
  251. gchar **argv,
  252. gchar **envp,
  253. GSpawnFlags flags,
  254. GSpawnChildSetupFunc child_setup,
  255. gpointer user_data,
  256. GPid *child_pid,
  257. gint *standard_input,
  258. gint *standard_output,
  259. gint *standard_error,
  260. GError **error);
  261. GLIB_AVAILABLE_IN_ALL
  262. gboolean g_spawn_sync_utf8 (const gchar *working_directory,
  263. gchar **argv,
  264. gchar **envp,
  265. GSpawnFlags flags,
  266. GSpawnChildSetupFunc child_setup,
  267. gpointer user_data,
  268. gchar **standard_output,
  269. gchar **standard_error,
  270. gint *exit_status,
  271. GError **error);
  272. GLIB_AVAILABLE_IN_ALL
  273. gboolean g_spawn_command_line_sync_utf8 (const gchar *command_line,
  274. gchar **standard_output,
  275. gchar **standard_error,
  276. gint *exit_status,
  277. GError **error);
  278. GLIB_AVAILABLE_IN_ALL
  279. gboolean g_spawn_command_line_async_utf8 (const gchar *command_line,
  280. GError **error);
  281. #endif
  282. #endif
  283. G_END_DECLS
  284. #endif /* __G_SPAWN_H__ */