windows-spawn.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Auxiliary functions for the creation of subprocesses. Native Windows API.
  2. Copyright (C) 2001, 2003-2022 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2003.
  4. This file is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. This file is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #ifndef _WINDOWS_SPAWN_H
  15. #define _WINDOWS_SPAWN_H
  16. #include <stdbool.h>
  17. #include <stdint.h>
  18. #include <stdlib.h>
  19. /* Get declarations of the native Windows API functions. */
  20. #define WIN32_LEAN_AND_MEAN
  21. #include <windows.h>
  22. /* Prepares an argument vector before calling spawn().
  23. Note that spawn() does not by itself call the command interpreter
  24. (getenv ("COMSPEC") != NULL ? getenv ("COMSPEC") :
  25. ({ OSVERSIONINFO v; v.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  26. GetVersionEx(&v);
  27. v.dwPlatformId == VER_PLATFORM_WIN32_NT;
  28. }) ? "cmd.exe" : "command.com").
  29. Instead it simply concatenates the arguments, separated by ' ', and calls
  30. CreateProcess(). We must quote the arguments since Windows CreateProcess()
  31. interprets characters like ' ', '\t', '\\', '"' (but not '<' and '>') in a
  32. special way:
  33. - Space and tab are interpreted as delimiters. They are not treated as
  34. delimiters if they are surrounded by double quotes: "...".
  35. - Unescaped double quotes are removed from the input. Their only effect is
  36. that within double quotes, space and tab are treated like normal
  37. characters.
  38. - Backslashes not followed by double quotes are not special.
  39. - But 2*n+1 backslashes followed by a double quote become
  40. n backslashes followed by a double quote (n >= 0):
  41. \" -> "
  42. \\\" -> \"
  43. \\\\\" -> \\"
  44. - '*', '?' characters may get expanded through wildcard expansion in the
  45. callee: By default, in the callee, the initialization code before main()
  46. takes the result of GetCommandLine(), wildcard-expands it, and passes it
  47. to main(). The exceptions to this rule are:
  48. - programs that inspect GetCommandLine() and ignore argv,
  49. - mingw programs that have a global variable 'int _CRT_glob = 0;',
  50. - Cygwin programs, when invoked from a Cygwin program.
  51. prepare_spawn creates and returns a new argument vector, where the arguments
  52. are appropriately quoted and an additional argument "sh.exe" has been added
  53. at the beginning. The new argument vector is freshly allocated. The memory
  54. for all its elements is allocated within *MEM_TO_FREE, which is freshly
  55. allocated as well. In case of memory allocation failure, NULL is returned,
  56. with errno set.
  57. */
  58. extern const char ** prepare_spawn (const char * const *argv,
  59. char **mem_to_free);
  60. /* Composes the command to be passed to CreateProcess().
  61. ARGV must contain appropriately quoted arguments, as returned by
  62. prepare_spawn.
  63. Returns a freshly allocated string. In case of memory allocation failure,
  64. NULL is returned, with errno set. */
  65. extern char * compose_command (const char * const *argv)
  66. _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE;
  67. /* Composes the block of memory that contains the environment variables.
  68. ENVP must contain an environment (a NULL-terminated array of string of the
  69. form VARIABLE=VALUE).
  70. Returns a freshly allocated block of memory. In case of memory allocation
  71. failure, NULL is returned, with errno set. */
  72. extern char * compose_envblock (const char * const *envp)
  73. _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE;
  74. /* This struct keeps track of which handles to pass to a subprocess, and with
  75. which flags. All of the handles here are inheritable.
  76. Regarding handle inheritance, see
  77. <https://docs.microsoft.com/en-us/windows/win32/sysinfo/handle-inheritance> */
  78. struct inheritable_handles
  79. {
  80. /* The number of occupied entries in the two arrays below.
  81. 3 <= count <= allocated. */
  82. size_t count;
  83. /* The number of allocated entries in the two arrays below. */
  84. size_t allocated;
  85. /* handles[0..count-1] are the occupied entries.
  86. handles[fd] is either INVALID_HANDLE_VALUE or an inheritable handle. */
  87. HANDLE *handles;
  88. /* flags[0..count-1] are the occupied entries.
  89. flags[fd] is only relevant if handles[fd] != INVALID_HANDLE_VALUE.
  90. It is a bit mask consisting of:
  91. - 32 for O_APPEND.
  92. */
  93. unsigned char *flags;
  94. };
  95. /* Initializes a set of inheritable handles, filling in all inheritable handles
  96. assigned to file descriptors.
  97. If DUPLICATE is true, the handles stored in the set are duplicates.
  98. Returns 0 upon success. In case of failure, -1 is returned, with errno set.
  99. */
  100. extern int init_inheritable_handles (struct inheritable_handles *inh_handles,
  101. bool duplicate);
  102. /* Fills a set of inheritable handles into a STARTUPINFO for CreateProcess().
  103. Returns 0 upon success. In case of failure, -1 is returned, with errno set.
  104. */
  105. extern int compose_handles_block (const struct inheritable_handles *inh_handles,
  106. STARTUPINFOA *sinfo);
  107. /* Frees the memory held by a set of inheritable handles. */
  108. extern void free_inheritable_handles (struct inheritable_handles *inh_handles);
  109. /* Converts a CreateProcess() error code (retrieved through GetLastError()) to
  110. an errno value. */
  111. extern int convert_CreateProcess_error (DWORD error);
  112. /* Creates a subprocess.
  113. MODE is either P_WAIT or P_NOWAIT.
  114. PROGNAME is the program to invoke.
  115. ARGV is the NULL-terminated array of arguments, ARGV[0] being PROGNAME by
  116. convention.
  117. ENVP is the NULL-terminated set of environment variable assignments, or NULL
  118. to inherit the initial environ variable assignments from the caller and
  119. ignore all calls to putenv(), setenv(), unsetenv() done in the caller.
  120. CURRDIR is the directory in which to start the program, or NULL to inherit
  121. the working directory from the caller.
  122. STDIN_HANDLE, STDOUT_HANDLE, STDERR_HANDLE are the handles to use for the
  123. first three file descriptors in the callee process.
  124. Returns
  125. - 0 for success (if MODE is P_WAIT), or
  126. - a handle that be passed to _cwait (on Windows) or waitpid (on OS/2), or
  127. - -1 upon error, with errno set.
  128. */
  129. extern intptr_t spawnpvech (int mode,
  130. const char *progname, const char * const *argv,
  131. const char * const *envp,
  132. const char *currdir,
  133. HANDLE stdin_handle, HANDLE stdout_handle,
  134. HANDLE stderr_handle);
  135. #endif /* _WINDOWS_SPAWN_H */