app_term.c
author viric@llimona
Mon, 24 Sep 2007 13:59:40 +0200
changeset 38 f1e581c862d5
parent 29 91286c3ecebc
child 41 954941c6e40a
permissions -rw-r--r--
Improved help. Moving to 0.2.

#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <pty.h>

#include "main.h"

enum
{
    STDIN,
    STDOUT,
    STDERR
};

static void set_close_on_exec(int fd)
{
    int res;
    res = fcntl(fd, F_SETFD, FD_CLOEXEC);
    if (res == -1)
        error("Set close-on-exec failed");
}

static void give_terminal(int parent[], int child[])
{
    struct termios tios;
    struct winsize wsize;
    int res;
    int master, slave;

    res = tcgetattr(1, &tios);
    if (res == -1)
        error("tcgetatttr");

    res = ioctl(1, TIOCGWINSZ, &wsize);
    if (res == -1)
        error("ioctl TIOCGWINSZ");

    res = openpty(&master, &slave, 0, &tios, &wsize);
    if (res == -1)
        error("openpty");

    parent[STDIN]  = master;
    parent[STDOUT] = master;
    parent[STDERR] = master;
    child[STDIN]  = slave;
    child[STDOUT] = slave;
    child[STDERR] = slave;
    set_close_on_exec(master);
    set_close_on_exec(slave);
}

static void give_pipes(int parent[], int child[])
{
    int p_stdin[2]; /* from us to mpg321 */
    int p_stdout[2]; /* from mpg321 to us */
    int p_stderr[2]; /* from mpg321 to us, its stderr */

    pipe(p_stdin);
    pipe(p_stdout);
    pipe(p_stderr);

    parent[STDIN]  = p_stdin[1];
    parent[STDOUT] = p_stdout[0];
    parent[STDERR] = p_stderr[0];
    child[STDIN]   = p_stdin[0];
    child[STDOUT]  = p_stdout[1];
    child[STDERR]  = p_stderr[1];
    set_close_on_exec(parent[STDIN]);
    set_close_on_exec(parent[STDOUT]);
    set_close_on_exec(parent[STDERR]);
    set_close_on_exec(child[STDIN]);
    set_close_on_exec(child[STDOUT]);
    set_close_on_exec(child[STDERR]);
}

int fork_app(char * const command[])
{
    int p_parent[3];
    int p_child[3];
    int pid;
    int res;

    if (command_line.s_param.run_in_subterminal)
        give_terminal(p_parent, p_child);
    else
        give_pipes(p_parent, p_child);

    /* globals */
    app_stdin  = p_parent[STDIN];  /* For us to write */
    app_stdout = p_parent[STDOUT]; /* For us to read */
    app_stderr = p_parent[STDERR]; /* For us to read */

    pid = fork();

    switch(pid)
    {
        case 0: /* child */
            res = dup2(p_child[STDIN], 0);
            if (res == -1) error("Dup2 stdin");

            res = dup2(p_child[STDOUT], 1);
            if (res == -1) error("Dup2 stdout");

            res = dup2(p_child[STDERR], 2);
            if (res == -1) error("Dup2 stderr");

            execvp(command[0], command);

            error("Cannot execvp %s", command[0]);
        case -1:
            error("Failed fork");
        default: /* parent */
            close(p_child[STDIN]);
            close(p_child[STDOUT]);
            close(p_child[STDERR]);
    }

    return pid;
}