Skip to content
runprog.c 1.33 KiB
Newer Older
wlott's avatar
wlott committed
/*
 * Support for run-program.
 *
 */

#include <sys/ioctl.h>
cshapiro's avatar
cshapiro committed
#include <errno.h>
rtoy's avatar
rtoy committed
#include <fcntl.h>
cshapiro's avatar
cshapiro committed
#include <signal.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
wlott's avatar
wlott committed

pid_t
spawn(char *program, char *argv[], char *envp[], char *pty_name,
      int stdin, int stdout, int stderr)
wlott's avatar
wlott committed
{
cshapiro's avatar
cshapiro committed
    pid_t pid;
    sigset_t set;
wlott's avatar
wlott committed
    int fd;

cshapiro's avatar
cshapiro committed
    pid = fork();
wlott's avatar
wlott committed
    if (pid != 0)
	return pid;

    /* Put us in our own process group. */
    setsid();
cshapiro's avatar
cshapiro committed

    /* Unblock all signals. */
    sigemptyset(&set);
    sigprocmask(SIG_SETMASK, &set, NULL);
wlott's avatar
wlott committed

    /* If we are supposed to be part of some other pty, go for it. */
    if (pty_name) {
cshapiro's avatar
cshapiro committed
#ifdef TIOCNOTTY
wlott's avatar
wlott committed
	fd = open("/dev/tty", O_RDWR, 0);
	if (fd >= 0) {
	    ioctl(fd, TIOCNOTTY, 0);
	    close(fd);
	}
#endif

	fd = open(pty_name, O_RDWR, 0);
	dup2(fd, 0);
	dup2(fd, 1);
	dup2(fd, 2);
	close(fd);
    }

cshapiro's avatar
cshapiro committed
    /* Set up stdin, stdout, and stderr. */
wlott's avatar
wlott committed
    if (stdin >= 0)
	dup2(stdin, 0);
    if (stdout >= 0)
	dup2(stdout, 1);
    if (stderr >= 0)
	dup2(stderr, 2);

    /* Close all other fds. */
cshapiro's avatar
cshapiro committed
    for (fd = sysconf(_SC_OPEN_MAX) - 1; fd >= 3; fd--)
ram's avatar
ram committed
	close(fd);
wlott's avatar
wlott committed

    /* Exec the program. */
    execve(program, argv, envp);

    /* It didn't work, so try /bin/sh. */
    argv[0] = program;
    argv[-1] = "sh";
    execve("/bin/sh", argv - 1, envp);
wlott's avatar
wlott committed

    /* The exec didn't work, flame out. */
    exit(1);
}