server.c
author viric@llimona
Thu, 27 Sep 2007 00:25:54 +0200
changeset 58 2cf8c513d18f
parent 53 07500c5c53cb
child 66 b2469563a1dc
permissions -rw-r--r--
added authors.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53
07500c5c53cb Adding license and web html.
viric@llimona
parents: 49
diff changeset
     1
/*
07500c5c53cb Adding license and web html.
viric@llimona
parents: 49
diff changeset
     2
    Terminal Mixer - multi-point multi-user access to terminal applications
07500c5c53cb Adding license and web html.
viric@llimona
parents: 49
diff changeset
     3
    Copyright (C) 2007  LluĂ­s Batlle i Rossell
07500c5c53cb Adding license and web html.
viric@llimona
parents: 49
diff changeset
     4
07500c5c53cb Adding license and web html.
viric@llimona
parents: 49
diff changeset
     5
    Please find the license in the provided COPYING file.
07500c5c53cb Adding license and web html.
viric@llimona
parents: 49
diff changeset
     6
*/
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
     7
#include <stdio.h>
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
     8
#include <stdlib.h>
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
     9
#include <sys/types.h>
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    10
#include <sys/select.h>
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    11
#include <unistd.h>
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    12
#include <errno.h>
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    13
#include <string.h>
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    14
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    15
#include "main.h"
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    16
#include "handlers.h"
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    17
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    18
static void loop()
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    19
{
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    20
    fd_set read_set;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    21
    int maxfd;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    22
    int stdin_opened;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    23
    int res;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    24
41
954941c6e40a Added 'nohup' option -n.
viric@llimona
parents: 40
diff changeset
    25
    if (command_line.s_param.nohup)
954941c6e40a Added 'nohup' option -n.
viric@llimona
parents: 40
diff changeset
    26
        stdin_opened = 0;
954941c6e40a Added 'nohup' option -n.
viric@llimona
parents: 40
diff changeset
    27
    else
954941c6e40a Added 'nohup' option -n.
viric@llimona
parents: 40
diff changeset
    28
        stdin_opened = 1;
954941c6e40a Added 'nohup' option -n.
viric@llimona
parents: 40
diff changeset
    29
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    30
    do
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    31
    {
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    32
        FD_ZERO(&read_set);
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    33
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    34
        if (stdin_opened)
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    35
            FD_SET(0, &read_set);
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    36
        maxfd = 0;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    37
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    38
        app_control_prepare_read_fdset(&read_set, &maxfd);
33
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    39
        if (command_line.s_param.serve_unix)
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    40
            s_unix_prepare_read_fdset(&read_set, &maxfd);
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    41
        if (command_line.s_param.serve_tcp)
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    42
            s_tcp_prepare_read_fdset(&read_set, &maxfd);
49
1cead94cfd99 Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents: 44
diff changeset
    43
#ifdef linux
44
41241a0f84bf Almost fixed eth transport.
viric@llimona
parents: 41
diff changeset
    44
        if (command_line.s_param.serve_eth)
41241a0f84bf Almost fixed eth transport.
viric@llimona
parents: 41
diff changeset
    45
            s_eth_prepare_read_fdset(&read_set, &maxfd);
49
1cead94cfd99 Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents: 44
diff changeset
    46
#endif /* linux */
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    47
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    48
        /* Will block */
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    49
        res = select(maxfd + 1, &read_set, 0, 0, 0);
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    50
        if (res == -1)
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    51
        {
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    52
            if (errno == EINTR)
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    53
                continue;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    54
            else
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    55
                error("Error in select()");
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    56
        }
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    57
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    58
        res = app_control_process_read_fdset(&read_set);
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    59
        if (res == -1) /* app_stdout and app_stderr closed */
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    60
            break;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    61
41
954941c6e40a Added 'nohup' option -n.
viric@llimona
parents: 40
diff changeset
    62
        if (stdin_opened && FD_ISSET(0, &read_set))
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    63
        {
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    64
            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
    65
            /* 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
    66
            app_control_local_send_to_stdin(stream_buffer, res);
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    67
            if (res == 0)
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    68
                stdin_opened = 0;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    69
        }
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    70
33
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    71
        if (command_line.s_param.serve_unix)
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    72
            s_unix_process_read_fdset(&read_set);
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    73
        if (command_line.s_param.serve_tcp)
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    74
            s_tcp_process_read_fdset(&read_set);
49
1cead94cfd99 Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents: 44
diff changeset
    75
#ifdef linux
44
41241a0f84bf Almost fixed eth transport.
viric@llimona
parents: 41
diff changeset
    76
        if (command_line.s_param.serve_eth)
41241a0f84bf Almost fixed eth transport.
viric@llimona
parents: 41
diff changeset
    77
            s_eth_process_read_fdset(&read_set);
49
1cead94cfd99 Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents: 44
diff changeset
    78
#endif /* linux */
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    79
    } while(1);
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    80
}
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    81
29
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    82
int server()
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    83
{
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    84
    int child;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    85
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    86
    command_line.is_server = 1;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    87
36
da427c23d755 Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents: 33
diff changeset
    88
    app_control_start();
da427c23d755 Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents: 33
diff changeset
    89
29
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    90
    /* 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
    91
     * 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
    92
    if (command_line.s_param.serve_unix)
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    93
        s_unix_update_served(command_line.s_param.max_served);
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    94
29
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
    95
    if (command_line.s_param.serve_tcp)
33
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
    96
        s_tcp_update_served(command_line.s_param.max_served);
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
    97
49
1cead94cfd99 Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents: 44
diff changeset
    98
#ifdef linux
44
41241a0f84bf Almost fixed eth transport.
viric@llimona
parents: 41
diff changeset
    99
    if (command_line.s_param.serve_eth)
41241a0f84bf Almost fixed eth transport.
viric@llimona
parents: 41
diff changeset
   100
        s_eth_init();
49
1cead94cfd99 Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents: 44
diff changeset
   101
#endif /* linux */
44
41241a0f84bf Almost fixed eth transport.
viric@llimona
parents: 41
diff changeset
   102
40
da3ea7f666e3 Fixed env vars and paths for the unix socket.
viric@llimona
parents: 36
diff changeset
   103
    child = fork_app(command_line.s_param.command);
da3ea7f666e3 Fixed env vars and paths for the unix socket.
viric@llimona
parents: 36
diff changeset
   104
41
954941c6e40a Added 'nohup' option -n.
viric@llimona
parents: 40
diff changeset
   105
    if (command_line.s_param.nohup)
954941c6e40a Added 'nohup' option -n.
viric@llimona
parents: 40
diff changeset
   106
    {
954941c6e40a Added 'nohup' option -n.
viric@llimona
parents: 40
diff changeset
   107
        close(0);
954941c6e40a Added 'nohup' option -n.
viric@llimona
parents: 40
diff changeset
   108
        close(1);
954941c6e40a Added 'nohup' option -n.
viric@llimona
parents: 40
diff changeset
   109
        close(2);
954941c6e40a Added 'nohup' option -n.
viric@llimona
parents: 40
diff changeset
   110
    } else
954941c6e40a Added 'nohup' option -n.
viric@llimona
parents: 40
diff changeset
   111
        if (command_line.s_param.run_in_subterminal)
954941c6e40a Added 'nohup' option -n.
viric@llimona
parents: 40
diff changeset
   112
            prepare_user_terminal();
40
da3ea7f666e3 Fixed env vars and paths for the unix socket.
viric@llimona
parents: 36
diff changeset
   113
da3ea7f666e3 Fixed env vars and paths for the unix socket.
viric@llimona
parents: 36
diff changeset
   114
    install_signal_forwarders(child);
da3ea7f666e3 Fixed env vars and paths for the unix socket.
viric@llimona
parents: 36
diff changeset
   115
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
   116
    loop();
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
   117
29
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
   118
    if (command_line.s_param.serve_unix)
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
   119
        s_unix_shutdown();
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
   120
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
   121
    if (command_line.s_param.serve_tcp)
33
010af11f521e Raw implementation for tcp.
lbatlle@npdl268.bpo.hp.com
parents: 32
diff changeset
   122
        s_tcp_shutdown();
29
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
   123
49
1cead94cfd99 Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents: 44
diff changeset
   124
#ifdef linux
44
41241a0f84bf Almost fixed eth transport.
viric@llimona
parents: 41
diff changeset
   125
    if (command_line.s_param.serve_eth)
41241a0f84bf Almost fixed eth transport.
viric@llimona
parents: 41
diff changeset
   126
        s_eth_shutdown();
49
1cead94cfd99 Fixing a few bugs. The program runs well in Linux and Mac OS X.
viric@llimona
parents: 44
diff changeset
   127
#endif /* linux */
44
41241a0f84bf Almost fixed eth transport.
viric@llimona
parents: 41
diff changeset
   128
41
954941c6e40a Added 'nohup' option -n.
viric@llimona
parents: 40
diff changeset
   129
    if (!command_line.s_param.nohup &&
954941c6e40a Added 'nohup' option -n.
viric@llimona
parents: 40
diff changeset
   130
            command_line.s_param.run_in_subterminal)
29
91286c3ecebc Added getopt, and some things got based on parameters.
viric@llimona
parents: 26
diff changeset
   131
        restore_user_terminal();
23
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
   132
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
   133
    return 0;
b3e6c6ffc69c Moving the client out.
viric@llimona
parents:
diff changeset
   134
}