execute_command_line.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* Implementation of the EXECUTE_COMMAND_LINE intrinsic.
  2. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. Contributed by François-Xavier Coudert.
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. Libgfortran is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libgfortran.h"
  21. #include <string.h>
  22. #ifdef HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #ifdef HAVE_SYS_WAIT_H
  26. #include <sys/wait.h>
  27. #endif
  28. #ifdef HAVE_POSIX_SPAWN
  29. #include <spawn.h>
  30. # ifdef __APPLE__
  31. # include <crt_externs.h>
  32. # define environ (*_NSGetEnviron ())
  33. # else
  34. extern char **environ;
  35. # endif
  36. #endif
  37. #if defined(HAVE_POSIX_SPAWN) || defined(HAVE_FORK)
  38. #include <signal.h>
  39. #endif
  40. enum { EXEC_SYNCHRONOUS = -2, EXEC_NOERROR = 0, EXEC_SYSTEMFAILED,
  41. EXEC_CHILDFAILED, EXEC_INVALIDCOMMAND };
  42. static const char *cmdmsg_values[] =
  43. { "",
  44. "Termination status of the command-language interpreter cannot be obtained",
  45. "Execution of child process impossible",
  46. "Invalid command line" };
  47. static void
  48. set_cmdstat (int *cmdstat, int value)
  49. {
  50. if (cmdstat)
  51. *cmdstat = value;
  52. else if (value > EXEC_NOERROR)
  53. {
  54. #define MSGLEN 200
  55. char msg[MSGLEN] = "EXECUTE_COMMAND_LINE: ";
  56. strncat (msg, cmdmsg_values[value], MSGLEN - strlen(msg) - 1);
  57. runtime_error ("%s", msg);
  58. }
  59. }
  60. #if defined(HAVE_WAITPID) && defined(HAVE_SIGACTION)
  61. static void
  62. sigchld_handler (int signum __attribute__((unused)))
  63. {
  64. while (waitpid ((pid_t)(-1), NULL, WNOHANG) > 0) {}
  65. }
  66. #endif
  67. static void
  68. execute_command_line (const char *command, bool wait, int *exitstat,
  69. int *cmdstat, char *cmdmsg,
  70. gfc_charlen_type command_len,
  71. gfc_charlen_type cmdmsg_len)
  72. {
  73. /* Transform the Fortran string to a C string. */
  74. char *cmd = fc_strdup (command, command_len);
  75. /* Flush all I/O units before executing the command. */
  76. flush_all_units();
  77. #if defined(HAVE_POSIX_SPAWN) || defined(HAVE_FORK)
  78. if (!wait)
  79. {
  80. /* Asynchronous execution. */
  81. pid_t pid;
  82. set_cmdstat (cmdstat, EXEC_NOERROR);
  83. #if defined(HAVE_SIGACTION) && defined(HAVE_WAITPID)
  84. static bool sig_init_saved;
  85. bool sig_init = __atomic_load_n (&sig_init_saved, __ATOMIC_RELAXED);
  86. if (!sig_init)
  87. {
  88. struct sigaction sa;
  89. sa.sa_handler = &sigchld_handler;
  90. sigemptyset(&sa.sa_mask);
  91. sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
  92. sigaction(SIGCHLD, &sa, 0);
  93. __atomic_store_n (&sig_init_saved, true, __ATOMIC_RELAXED);
  94. }
  95. #endif
  96. #ifdef HAVE_POSIX_SPAWN
  97. const char * const argv[] = {"sh", "-c", cmd, NULL};
  98. if (posix_spawn (&pid, "/bin/sh", NULL, NULL,
  99. (char * const* restrict) argv, environ))
  100. set_cmdstat (cmdstat, EXEC_CHILDFAILED);
  101. #elif defined(HAVE_FORK)
  102. if ((pid = fork()) < 0)
  103. set_cmdstat (cmdstat, EXEC_CHILDFAILED);
  104. else if (pid == 0)
  105. {
  106. /* Child process. */
  107. int res = system (cmd);
  108. _exit (WIFEXITED(res) ? WEXITSTATUS(res) : res);
  109. }
  110. #endif
  111. }
  112. else
  113. #endif
  114. {
  115. /* Synchronous execution. */
  116. int res = system (cmd);
  117. if (res == -1)
  118. set_cmdstat (cmdstat, EXEC_SYSTEMFAILED);
  119. #if !defined(HAVE_POSIX_SPAWN) && !defined(HAVE_FORK)
  120. else if (!wait)
  121. set_cmdstat (cmdstat, EXEC_SYNCHRONOUS);
  122. #endif
  123. else if (res == 127 || res == 126
  124. #if defined(WEXITSTATUS) && defined(WIFEXITED)
  125. || (WIFEXITED(res) && WEXITSTATUS(res) == 127)
  126. || (WIFEXITED(res) && WEXITSTATUS(res) == 126)
  127. #endif
  128. )
  129. /* Shell return codes 126 and 127 mean that the command line could
  130. not be executed for various reasons. */
  131. set_cmdstat (cmdstat, EXEC_INVALIDCOMMAND);
  132. else
  133. set_cmdstat (cmdstat, EXEC_NOERROR);
  134. if (res != -1)
  135. {
  136. #if defined(WEXITSTATUS) && defined(WIFEXITED)
  137. *exitstat = WIFEXITED(res) ? WEXITSTATUS(res) : res;
  138. #else
  139. *exitstat = res;
  140. #endif
  141. }
  142. }
  143. free (cmd);
  144. /* Now copy back to the Fortran string if needed. */
  145. if (cmdstat && *cmdstat > EXEC_NOERROR && cmdmsg)
  146. fstrcpy (cmdmsg, cmdmsg_len, cmdmsg_values[*cmdstat],
  147. strlen (cmdmsg_values[*cmdstat]));
  148. }
  149. extern void
  150. execute_command_line_i4 (const char *command, GFC_LOGICAL_4 *wait,
  151. GFC_INTEGER_4 *exitstat, GFC_INTEGER_4 *cmdstat,
  152. char *cmdmsg, gfc_charlen_type command_len,
  153. gfc_charlen_type cmdmsg_len);
  154. export_proto(execute_command_line_i4);
  155. void
  156. execute_command_line_i4 (const char *command, GFC_LOGICAL_4 *wait,
  157. GFC_INTEGER_4 *exitstat, GFC_INTEGER_4 *cmdstat,
  158. char *cmdmsg, gfc_charlen_type command_len,
  159. gfc_charlen_type cmdmsg_len)
  160. {
  161. bool w = wait ? *wait : true;
  162. int estat, estat_initial, cstat;
  163. if (exitstat)
  164. estat_initial = estat = *exitstat;
  165. execute_command_line (command, w, &estat, cmdstat ? &cstat : NULL,
  166. cmdmsg, command_len, cmdmsg_len);
  167. if (exitstat && estat != estat_initial)
  168. *exitstat = estat;
  169. if (cmdstat)
  170. *cmdstat = cstat;
  171. }
  172. extern void
  173. execute_command_line_i8 (const char *command, GFC_LOGICAL_8 *wait,
  174. GFC_INTEGER_8 *exitstat, GFC_INTEGER_8 *cmdstat,
  175. char *cmdmsg, gfc_charlen_type command_len,
  176. gfc_charlen_type cmdmsg_len);
  177. export_proto(execute_command_line_i8);
  178. void
  179. execute_command_line_i8 (const char *command, GFC_LOGICAL_8 *wait,
  180. GFC_INTEGER_8 *exitstat, GFC_INTEGER_8 *cmdstat,
  181. char *cmdmsg, gfc_charlen_type command_len,
  182. gfc_charlen_type cmdmsg_len)
  183. {
  184. bool w = wait ? *wait : true;
  185. int estat, estat_initial, cstat;
  186. if (exitstat)
  187. estat_initial = estat = *exitstat;
  188. execute_command_line (command, w, &estat, cmdstat ? &cstat : NULL,
  189. cmdmsg, command_len, cmdmsg_len);
  190. if (exitstat && estat != estat_initial)
  191. *exitstat = estat;
  192. if (cmdstat)
  193. *cmdstat = cstat;
  194. }