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