1 /* pspp - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 #include "data/settings.h"
32 #include "language/command.h"
33 #include "language/lexer/lexer.h"
34 #include "libpspp/assertion.h"
35 #include "libpspp/compiler.h"
36 #include "libpspp/i18n.h"
37 #include "libpspp/message.h"
38 #include "libpspp/str.h"
39 #include "libpspp/string-array.h"
40 #include "libpspp/temp-file.h"
41 #include "output/text-item.h"
44 #include "gl/intprops.h"
45 #include "gl/localcharset.h"
46 #include "gl/read-file.h"
47 #include "gl/timespec.h"
48 #include "gl/xalloc.h"
49 #include "gl/xmalloca.h"
52 #define _(msgid) gettext (msgid)
53 #define N_(msgid) msgid
57 run_commands (const struct string_array *commands, double time_limit)
59 if (time_limit != DBL_MAX)
61 msg (SE, _("Time limit not supported on this platform."));
65 for (size_t i = 0; i < commands->n; i++)
67 /* XXX No way to capture command output */
68 char *s = recode_string (locale_charset (), "UTF-8",
69 commands->strings[i], -1);
70 int retval = system (s);
75 msg (SE, _("%s: Command exited with status %d."),
76 commands->strings[i], retval);
84 run_command (const char *command, struct timespec timeout)
86 /* Same exit codes used by 'sh'. */
88 EXIT_CANNOT_INVOKE = 126,
92 /* Create a temporary file to capture command output. */
93 FILE *output_file = create_temp_file ();
96 msg (SE, _("Failed to create temporary file (%s)."), strerror (errno));
100 int dev_null_fd = open ("/dev/null", O_RDONLY);
103 msg (SE, _("/dev/null: Failed to open (%s)."), strerror (errno));
104 fclose (output_file);
108 char *locale_command = recode_string (locale_charset (), "UTF-8",
115 fclose (output_file);
116 free (locale_command);
118 msg (SE, _("Couldn't fork: %s."), strerror (errno));
123 /* Running in the child. */
125 /* Set up timeout. */
126 if (timeout.tv_sec < TYPE_MAXIMUM (time_t))
128 signal (SIGALRM, SIG_DFL);
130 struct timespec left = timespec_sub (timeout, current_timespec ());
131 if (timespec_sign (left) <= 0)
134 struct itimerval it = {
136 .tv_sec = left.tv_sec,
137 .tv_usec = left.tv_nsec / 1000
140 if (setitimer (ITIMER_REAL, &it, NULL) < 0)
141 error (1, errno, _("Failed to set timeout."));
144 /* Set up file descriptors:
145 - /dev/null for stdin
146 - Temporary file to capture stdout and stderr.
147 - Close everything else.
149 dup2 (dev_null_fd, 0);
150 dup2 (fileno (output_file), 1);
151 dup2 (fileno (output_file), 2);
153 for (int fd = 3; fd < 256; fd++)
156 /* Choose the shell. */
157 const char *shell = getenv ("SHELL");
161 /* Run subprocess. */
162 execl (shell, shell, "-c", locale_command, NULL);
164 /* Failed to start the shell. */
165 _exit (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
168 /* Running in the parent. */
170 free (locale_command);
172 /* Wait for child to exit. */
177 pid_t retval = waitpid (pid, &status, 0);
195 msg (SW, _("While running \"%s\", waiting for child process "
197 command, strerror (errno));
201 if (WIFSIGNALED (status))
203 int signum = WTERMSIG (status);
204 if (signum == SIGALRM)
205 msg (SW, _("Command \"%s\" timed out."), command);
207 msg (SW, _("Command \"%s\" terminated by signal %d."), command, signum);
210 else if (WIFEXITED (status) && WEXITSTATUS (status))
212 int exit_code = WEXITSTATUS (status);
213 const char *detail = (exit_code == EXIT_ENOENT
214 ? _("Command or shell not found")
215 : exit_code == EXIT_CANNOT_INVOKE
216 ? _("Could not invoke command or shell")
219 msg (SW, _("Command \"%s\" exited with status %d (%s)."),
220 command, exit_code, detail);
222 msg (SW, _("Command \"%s\" exited with status %d."),
227 rewind (output_file);
229 char *locale_output = fread_file (output_file, 0, &length);
232 msg (SW, _("Command \"%s\" output could not be read (%s)."),
233 command, strerror (errno));
238 char *output = recode_string ("UTF-8", locale_charset (),
241 /* Drop final new-line, if any. */
242 char *end = strchr (output, '\0');
243 if (end > output && end[-1] == '\n')
246 text_item_submit (text_item_create_nocopy (TEXT_ITEM_LOG, output));
248 free (locale_output);
254 run_commands (const struct string_array *commands, double time_limit)
256 struct timespec timeout = timespec_add (dtotimespec (time_limit),
257 current_timespec ());
259 for (size_t i = 0; i < commands->n; i++)
261 if (!run_command (commands->strings[i], timeout))
270 cmd_host (struct lexer *lexer, struct dataset *ds UNUSED)
272 if (settings_get_safer_mode ())
274 msg (SE, _("This command not allowed when the %s option is set."), "SAFER");
278 if (!lex_force_match_id (lexer, "COMMAND")
279 || !lex_force_match (lexer, T_EQUALS)
280 || !lex_force_match (lexer, T_LBRACK)
281 || !lex_force_string (lexer))
284 struct string_array commands = STRING_ARRAY_INITIALIZER;
285 while (lex_token (lexer) == T_STRING)
287 string_array_append (&commands, lex_tokcstr (lexer));
290 if (!lex_force_match (lexer, T_RBRACK))
292 string_array_destroy (&commands);
296 double time_limit = DBL_MAX;
297 if (lex_match_id (lexer, "TIMELIMIT"))
299 if (!lex_force_match (lexer, T_EQUALS)
300 || !lex_force_num (lexer))
302 string_array_destroy (&commands);
306 double num = lex_number (lexer);
308 time_limit = num < 0.0 ? 0.0 : num;
311 enum cmd_result result = lex_end_of_command (lexer);
312 if (result == CMD_SUCCESS && !run_commands (&commands, time_limit))
313 result = CMD_FAILURE;
314 string_array_destroy (&commands);