server.c
author viric@llimona
Mon, 24 Sep 2007 13:59:40 +0200
changeset 38 f1e581c862d5
parent 36 da427c23d755
child 40 da3ea7f666e3
permissions -rw-r--r--
Improved help. Moving to 0.2.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#include "main.h"
#include "handlers.h"

static void loop()
{
    fd_set read_set;
    int maxfd;
    int stdin_opened;
    int res;

    stdin_opened = 1;
    do
    {
        FD_ZERO(&read_set);

        if (stdin_opened)
            FD_SET(0, &read_set);
        maxfd = 0;

        app_control_prepare_read_fdset(&read_set, &maxfd);
        if (command_line.s_param.serve_unix)
            s_unix_prepare_read_fdset(&read_set, &maxfd);
        if (command_line.s_param.serve_tcp)
            s_tcp_prepare_read_fdset(&read_set, &maxfd);

        /* Will block */
        res = select(maxfd + 1, &read_set, 0, 0, 0);
        if (res == -1)
        {
            if (errno == EINTR)
                continue;
            else
                error("Error in select()");
        }

        res = app_control_process_read_fdset(&read_set);
        if (res == -1) /* app_stdout and app_stderr closed */
            break;

        if (FD_ISSET(0, &read_set))
        {
            res = read(0, stream_buffer, stream_buffer_size);
            /* if res is 0, the fcall will close app_stdin */
            app_control_local_send_to_stdin(stream_buffer, res);
            if (res == 0)
                stdin_opened = 0;
        }

        if (command_line.s_param.serve_unix)
            s_unix_process_read_fdset(&read_set);
        if (command_line.s_param.serve_tcp)
            s_tcp_process_read_fdset(&read_set);
    } while(1);
}

int server()
{
    int child;

    command_line.is_server = 1;

    app_control_start();

    child = fork_app(command_line.s_param.command);

    if (command_line.s_param.run_in_subterminal)
        prepare_user_terminal();

    install_signal_forwarders(child);

    /* in raw mode, the signals for Control-C, ... will be generated by the
     * slave pty. The master will receive the key codes. */
    if (command_line.s_param.serve_unix)
        s_unix_update_served(command_line.s_param.max_served);

    if (command_line.s_param.serve_tcp)
        s_tcp_update_served(command_line.s_param.max_served);

    loop();

    if (command_line.s_param.serve_unix)
        s_unix_shutdown();

    if (command_line.s_param.serve_tcp)
        s_tcp_shutdown();

    if (command_line.s_param.run_in_subterminal)
        restore_user_terminal();

    return 0;
}