2 * Copyright (C) 2005 Max Okumoto.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
41 * Replace the current process.
44 Proc_Exec(const ProcStuff *ps)
47 if (ps->in != STDIN_FILENO) {
49 * Redirect the child's stdin to the input fd
50 * and reset it to the beginning (again).
52 if (dup2(ps->in, STDIN_FILENO) == -1)
53 Punt("Cannot dup2: %s", strerror(errno));
54 lseek(STDIN_FILENO, (off_t)0, SEEK_SET);
57 if (ps->out != STDOUT_FILENO) {
59 * Redirect the child's stdout to the output fd.
61 if (dup2(ps->out, STDOUT_FILENO) == -1)
62 Punt("Cannot dup2: %s", strerror(errno));
66 if (ps->err != STDERR_FILENO) {
68 * Redirect the child's stderr to the err fd.
70 if (dup2(ps->err, STDERR_FILENO) == -1)
71 Punt("Cannot dup2: %s", strerror(errno));
75 if (ps->merge_errors) {
77 * Send stderr to parent process too.
79 if (dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
80 Punt("Cannot dup2: %s", strerror(errno));
83 if (commandShell->unsetenv) {
84 /* for the benfit of ksh */
89 * The file descriptors for stdin, stdout, or stderr might
90 * have been marked close-on-exec. Clear the flag on all
93 fcntl(STDIN_FILENO, F_SETFD,
94 fcntl(STDIN_FILENO, F_GETFD) & (~FD_CLOEXEC));
95 fcntl(STDOUT_FILENO, F_SETFD,
96 fcntl(STDOUT_FILENO, F_GETFD) & (~FD_CLOEXEC));
97 fcntl(STDERR_FILENO, F_SETFD,
98 fcntl(STDERR_FILENO, F_GETFD) & (~FD_CLOEXEC));
103 * Become a process group leader, so we can kill it and all
104 * its descendants in one fell swoop, by killing its process
105 * family, but not commit suicide.
110 setpgid(0, getpid());
112 #endif /* USE_PGRP */
115 if (ps->searchpath) {
116 execvp(ps->argv[0], ps->argv);
118 write(STDERR_FILENO, ps->argv[0], strlen(ps->argv[0]));
119 write(STDERR_FILENO, ": ", 2);
120 write(STDERR_FILENO, strerror(errno), strlen(strerror(errno)));
121 write(STDERR_FILENO, "\n", 1);
123 execv(commandShell->path, ps->argv);
126 "Could not execute shell\n",
127 sizeof("Could not execute shell"));
131 * Since we are the child process, exit without flushing buffers.