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.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
     1
#include <stdio.h>
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
     2
#include <stdlib.h>
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
     3
#include <sys/types.h>
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
     4
#include <sys/select.h>
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
     5
#include <unistd.h>
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
     6
#include <errno.h>
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
     7
#include <string.h>
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
     8
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
     9
#include "main.h"
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    10
#include "handlers.h"
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    11
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    12
static void loop()
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    13
{
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    14
    fd_set read_set;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    15
    int maxfd;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    16
    int stdin_opened;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    17
    int res;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    18
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    19
    stdin_opened = 1;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    20
    do
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    21
    {
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    22
        FD_ZERO(&read_set);
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    24
        if (stdin_opened)
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    25
            FD_SET(0, &read_set);
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    26
        maxfd = 0;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    27
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    28
        app_control_prepare_read_fdset(&read_set, &maxfd);
33
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    29
        if (command_line.s_param.serve_unix)
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    30
            s_unix_prepare_read_fdset(&read_set, &maxfd);
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    31
        if (command_line.s_param.serve_tcp)
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    32
            s_tcp_prepare_read_fdset(&read_set, &maxfd);
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    33
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    34
        /* Will block */
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    35
        res = select(maxfd + 1, &read_set, 0, 0, 0);
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    36
        if (res == -1)
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    37
        {
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    38
            if (errno == EINTR)
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    39
                continue;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    40
            else
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    41
                error("Error in select()");
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    42
        }
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    43
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    44
        res = app_control_process_read_fdset(&read_set);
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    45
        if (res == -1) /* app_stdout and app_stderr closed */
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    46
            break;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    47
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    48
        if (FD_ISSET(0, &read_set))
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    49
        {
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    50
            res = read(0, stream_buffer, stream_buffer_size);
32
df110f784648 Added read-only clients, added stdin-close option to clients.
lbatlle@npdl268.bpo.hp.com
parents: 29
diff changeset
    51
            /* if res is 0, the fcall will close app_stdin */
df110f784648 Added read-only clients, added stdin-close option to clients.
lbatlle@npdl268.bpo.hp.com
parents: 29
diff changeset
    52
            app_control_local_send_to_stdin(stream_buffer, res);
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    53
            if (res == 0)
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    54
                stdin_opened = 0;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    55
        }
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    56
33
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    57
        if (command_line.s_param.serve_unix)
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    58
            s_unix_process_read_fdset(&read_set);
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    59
        if (command_line.s_param.serve_tcp)
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    60
            s_tcp_process_read_fdset(&read_set);
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    61
    } while(1);
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    62
}
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    63
29
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    64
int server()
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    65
{
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    66
    int child;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    67
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    68
    command_line.is_server = 1;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    69
36
da427c23d755 Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents: 33
diff changeset
    70
    app_control_start();
da427c23d755 Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents: 33
diff changeset
    71
29
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    72
    child = fork_app(command_line.s_param.command);
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    73
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    74
    if (command_line.s_param.run_in_subterminal)
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    75
        prepare_user_terminal();
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    76
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    77
    install_signal_forwarders(child);
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    78
29
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    79
    /* in raw mode, the signals for Control-C, ... will be generated by the
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    80
     * slave pty. The master will receive the key codes. */
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    81
    if (command_line.s_param.serve_unix)
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    82
        s_unix_update_served(command_line.s_param.max_served);
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    83
29
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    84
    if (command_line.s_param.serve_tcp)
33
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    85
        s_tcp_update_served(command_line.s_param.max_served);
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    86
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    87
    loop();
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    88
29
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    89
    if (command_line.s_param.serve_unix)
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    90
        s_unix_shutdown();
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    91
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    92
    if (command_line.s_param.serve_tcp)
33
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    93
        s_tcp_shutdown();
29
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    94
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    95
    if (command_line.s_param.run_in_subterminal)
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    96
        restore_user_terminal();
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    97
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    98
    return 0;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    99
}