1 /* run.c --- routines for executing subprocesses.
3 This file is part of GNU CVS.
5 GNU CVS is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
10 This program 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
13 GNU General Public License for more details. */
18 extern int execvp PROTO((char *file, char **argv));
21 static void run_add_arg PROTO((const char *s));
23 extern char *strtok ();
26 * To exec a program under CVS, first call run_setup() to setup initial
27 * arguments. The argument to run_setup will be parsed into whitespace
28 * separated words and added to the global run_argv list.
30 * Then, optionally call run_arg() for each additional argument that you'd like
31 * to pass to the executed program.
33 * Finally, call run_exec() to execute the program with the specified arguments.
34 * The execvp() syscall will be used, so that the PATH is searched correctly.
35 * File redirections can be performed in the call to run_exec().
37 static char **run_argv;
39 static int run_argc_allocated;
50 /* clean out any malloc'ed values from run_argv */
51 for (i = 0; i < run_argc; i++)
56 run_argv[i] = (char *) 0;
61 run_prog = xstrdup (prog);
63 /* put each word into run_argv, allocating it as we go */
64 for (cp = strtok (run_prog, " \t"); cp; cp = strtok ((char *) NULL, " \t"))
80 /* allocate more argv entries if we've run out */
81 if (run_argc >= run_argc_allocated)
83 run_argc_allocated += 50;
84 run_argv = (char **) xrealloc ((char *) run_argv,
85 run_argc_allocated * sizeof (char **));
89 run_argv[run_argc++] = xstrdup (s);
91 run_argv[run_argc] = (char *) 0; /* not post-incremented on purpose! */
97 run_exec (stin, stout, sterr, flags)
103 int shin, shout, sherr;
104 int mode_out, mode_err;
111 sigset_t sigset_mask, sigset_omask;
112 struct sigaction act, iact, qact;
117 struct sigvec vec, ivec, qvec;
120 RETSIGTYPE (*istat) (), (*qstat) ();
126 #ifdef SERVER_SUPPORT
127 cvs_outerr (server_active ? "S" : " ", 1);
129 cvs_outerr ("-> system(", 0);
131 cvs_outerr (")\n", 0);
133 if (noexec && (flags & RUN_REALLY) == 0)
136 /* make sure that we are null terminated, since we didn't calloc */
137 run_add_arg ((char *)0);
139 /* setup default file descriptor numbers */
144 /* set the file modes for stdout and stderr */
145 mode_out = mode_err = O_WRONLY | O_CREAT;
146 mode_out |= ((flags & RUN_STDOUT_APPEND) ? O_APPEND : O_TRUNC);
147 mode_err |= ((flags & RUN_STDERR_APPEND) ? O_APPEND : O_TRUNC);
149 if (stin && (shin = open (stin, O_RDONLY)) == -1)
152 error (0, errno, "cannot open %s for reading (prog %s)",
156 if (stout && (shout = open (stout, mode_out, 0666)) == -1)
159 error (0, errno, "cannot open %s for writing (prog %s)",
163 if (sterr && (flags & RUN_COMBINED) == 0)
165 if ((sherr = open (sterr, mode_err, 0666)) == -1)
168 error (0, errno, "cannot open %s for writing (prog %s)",
174 /* Make sure we don't flush this twice, once in the subprocess. */
178 /* The output files, if any, are now created. Do the fork and dups.
180 We use vfork not so much for a performance boost (the
181 performance boost, if any, is modest on most modern unices),
182 but for the sake of systems without a memory management unit,
183 which find it difficult or impossible to implement fork at all
184 (e.g. Amiga). The other solution is spawn (see
185 windows-NT/run.c). */
196 (void) dup2 (shin, 0);
201 (void) dup2 (shout, 1);
202 (void) close (shout);
204 if (flags & RUN_COMBINED)
208 (void) dup2 (sherr, 2);
209 (void) close (sherr);
212 #ifdef SETXID_SUPPORT
214 ** This prevents a user from creating a privileged shell
215 ** from the text editor when the SETXID_SUPPORT option is selected.
217 if (!strcmp (run_argv[0], Editor) && setegid (getgid ()))
219 error (0, errno, "cannot set egid to gid");
224 /* dup'ing is done. try to run it now */
225 (void) execvp (run_argv[0], run_argv);
226 error (0, errno, "cannot exec %s", run_argv[0]);
235 /* the parent. Ignore some signals for now */
237 if (flags & RUN_SIGIGNORE)
239 act.sa_handler = SIG_IGN;
240 (void) sigemptyset (&act.sa_mask);
242 (void) sigaction (SIGINT, &act, &iact);
243 (void) sigaction (SIGQUIT, &act, &qact);
247 (void) sigemptyset (&sigset_mask);
248 (void) sigaddset (&sigset_mask, SIGINT);
249 (void) sigaddset (&sigset_mask, SIGQUIT);
250 (void) sigprocmask (SIG_SETMASK, &sigset_mask, &sigset_omask);
254 if (flags & RUN_SIGIGNORE)
256 memset ((char *)&vec, 0, sizeof (vec));
257 vec.sv_handler = SIG_IGN;
258 (void) sigvec (SIGINT, &vec, &ivec);
259 (void) sigvec (SIGQUIT, &vec, &qvec);
262 mask = sigblock (sigmask (SIGINT) | sigmask (SIGQUIT));
264 istat = signal (SIGINT, SIG_IGN);
265 qstat = signal (SIGQUIT, SIG_IGN);
269 /* wait for our process to die and munge return status */
271 while ((w = waitpid (pid, &status, 0)) == -1 && errno == EINTR)
274 while ((w = wait (&status)) != pid)
276 if (w == -1 && errno != EINTR)
286 #ifndef VMS /* status is return status */
287 else if (WIFEXITED (status))
288 rc = WEXITSTATUS (status);
289 else if (WIFSIGNALED (status))
291 if (WTERMSIG (status) == SIGPIPE)
292 error (1, 0, "broken pipe");
298 rc = WEXITSTATUS (status);
301 /* restore the signals */
303 if (flags & RUN_SIGIGNORE)
305 (void) sigaction (SIGINT, &iact, (struct sigaction *)NULL);
306 (void) sigaction (SIGQUIT, &qact, (struct sigaction *)NULL);
309 (void) sigprocmask (SIG_SETMASK, &sigset_omask, (sigset_t *)NULL);
312 if (flags & RUN_SIGIGNORE)
314 (void) sigvec (SIGINT, &ivec, (struct sigvec *)NULL);
315 (void) sigvec (SIGQUIT, &qvec, (struct sigvec *)NULL);
318 (void) sigsetmask (mask);
320 (void) signal (SIGINT, istat);
321 (void) signal (SIGQUIT, qstat);
325 /* cleanup the open file descriptors */
328 (void) close (sherr);
330 /* ensure things are received by the parent in the correct order
331 * relative to the protocol pipe
336 (void) close (shout);
338 /* ensure things are received by the parent in the correct order
339 * relative to the protocol pipe
359 void (*outfn) PROTO ((const char *, size_t));
363 else if (fp == stdout)
367 error (1, 0, "internal error: bad argument to run_print");
368 /* Solely to placate gcc -Wall.
369 FIXME: it'd be better to use a function named `fatal' that
370 is known never to return. Then kludges wouldn't be necessary. */
374 for (i = 0; i < run_argc; i++)
377 (*outfn) (run_argv[i], 0);
379 if (i != run_argc - 1)
384 /* Return value is NULL for error, or if noexec was set. If there was an
385 error, return NULL and I'm not sure whether errno was set (the Red Hat
386 Linux 4.1 popen manpage was kind of vague but discouraging; and the noexec
387 case complicates this even aside from popen behavior). */
390 run_popen (cmd, mode)
395 (void) fprintf (stderr, "%s-> run_popen(%s,%s)\n",
396 CLIENT_SERVER_STR, cmd, mode);
400 return (popen (cmd, mode));
404 piped_child (command, tofdp, fromfdp)
405 const char **command;
410 int to_child_pipe[2];
411 int from_child_pipe[2];
413 if (pipe (to_child_pipe) < 0)
414 error (1, errno, "cannot create pipe");
415 if (pipe (from_child_pipe) < 0)
416 error (1, errno, "cannot create pipe");
418 #ifdef USE_SETMODE_BINARY
419 setmode (to_child_pipe[0], O_BINARY);
420 setmode (to_child_pipe[1], O_BINARY);
421 setmode (from_child_pipe[0], O_BINARY);
422 setmode (from_child_pipe[1], O_BINARY);
431 error (1, errno, "cannot fork");
434 if (dup2 (to_child_pipe[0], STDIN_FILENO) < 0)
435 error (1, errno, "cannot dup2 pipe");
436 if (close (to_child_pipe[1]) < 0)
437 error (1, errno, "cannot close pipe");
438 if (close (from_child_pipe[0]) < 0)
439 error (1, errno, "cannot close pipe");
440 if (dup2 (from_child_pipe[1], STDOUT_FILENO) < 0)
441 error (1, errno, "cannot dup2 pipe");
443 /* Okay to cast out const below - execvp don't return anyhow. */
444 execvp ((char *)command[0], (char **)command);
445 error (1, errno, "cannot exec %s", command[0]);
447 if (close (to_child_pipe[0]) < 0)
448 error (1, errno, "cannot close pipe");
449 if (close (from_child_pipe[1]) < 0)
450 error (1, errno, "cannot close pipe");
452 *tofdp = to_child_pipe[1];
453 *fromfdp = from_child_pipe[0];
463 if (fcntl (fd, F_SETFD, 1) == -1)
464 error (1, errno, "can't set close-on-exec flag on %d", fd);