main.c 19 KB
Newer Older
1
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
2 3
 * main.c: main vlc source
 * (c)1998 VideoLAN
4
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
5 6
 * Includes the main() function for vlc. Parses command line, start interface
 * and spawn threads.
7
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
8

9
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
10
 * Preamble
11
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
12 13 14 15 16 17
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Vincent Seguin's avatar
Vincent Seguin committed
18

19
#include <sys/soundcard.h>                                 /* audio_output.h */
Michel Kaempf's avatar
Michel Kaempf committed
20 21 22 23

#include "config.h"
#include "common.h"
#include "mtime.h"
24
#include "vlc_thread.h"
Michel Kaempf's avatar
Michel Kaempf committed
25
#include "input_vlan.h"
Vincent Seguin's avatar
Vincent Seguin committed
26
#include "intf_msg.h"
Michel Kaempf's avatar
Michel Kaempf committed
27
#include "interface.h"
Vincent Seguin's avatar
Vincent Seguin committed
28 29
#include "audio_output.h"
#include "main.h"
Michel Kaempf's avatar
Michel Kaempf committed
30

31
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
32 33
 * Command line options constants. If something is changed here, be sure that
 * GetConfiguration and Usage are also changed.
34
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
35 36 37 38 39 40 41 42

/* Long options return values - note that values corresponding to short options
 * chars, and in general any regular char, should be avoided */
#define OPT_NOAUDIO             150
#define OPT_STEREO              151
#define OPT_MONO                152

#define OPT_NOVIDEO             160
43 44 45 46
#define OPT_DISPLAY             161
#define OPT_WIDTH               162
#define OPT_HEIGHT              163
#define OPT_COLOR               164
Michel Kaempf's avatar
Michel Kaempf committed
47 48

#define OPT_NOVLANS             170
49 50
#define OPT_SERVER              171
#define OPT_PORT                172
51

Michel Kaempf's avatar
Michel Kaempf committed
52 53
/* Long options */
static const struct option longopts[] =
54 55
{
    /*  name,               has_arg,    flag,   val */
Michel Kaempf's avatar
Michel Kaempf committed
56 57

    /* General/common options */
58 59
    {   "help",             0,          0,      'h' },
    {   "version",          0,          0,      'v' },
Michel Kaempf's avatar
Michel Kaempf committed
60 61

    /* Audio options */
62
    {   "noaudio",          0,          0,      OPT_NOAUDIO },
Michel Kaempf's avatar
Michel Kaempf committed
63
    {   "stereo",           0,          0,      OPT_STEREO },
64
    {   "mono",             0,          0,      OPT_MONO },
Michel Kaempf's avatar
Michel Kaempf committed
65 66

    /* Video options */
67 68
    {   "novideo",          0,          0,      OPT_NOVIDEO },
    {   "display",          1,          0,      OPT_DISPLAY },
69
    {   "width",            1,          0,      OPT_WIDTH },
70 71 72
    {   "height",           1,          0,      OPT_HEIGHT },
    {   "grayscale",        0,          0,      'g' },
    {   "color",            0,          0,      OPT_COLOR },
Michel Kaempf's avatar
Michel Kaempf committed
73

74
    /* Input options */
Michel Kaempf's avatar
Michel Kaempf committed
75
    {   "novlans",          0,          0,      OPT_NOVLANS },
76 77
    {   "server",           1,          0,      OPT_SERVER },
    {   "port",             1,          0,      OPT_PORT },
Michel Kaempf's avatar
Michel Kaempf committed
78 79 80 81 82

    {   0,                  0,          0,      0 }
};

/* Short options */
Vincent Seguin's avatar
Vincent Seguin committed
83
static const char *psz_shortopts = "hvg";
Michel Kaempf's avatar
Michel Kaempf committed
84

85
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
86
 * Global variable program_data - this is the one and only, see main.h
87
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
88
main_t *p_main;
Michel Kaempf's avatar
Michel Kaempf committed
89

90
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
91
 * Local prototypes
92
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
93 94
static void SetDefaultConfiguration ( void );
static int  GetConfiguration        ( int i_argc, char *ppsz_argv[], char *ppsz_env[] );
Michel Kaempf's avatar
Michel Kaempf committed
95
static void Usage                   ( void );
Vincent Seguin's avatar
Vincent Seguin committed
96
static void Version                 ( void );
Vincent Seguin's avatar
Vincent Seguin committed
97

Michel Kaempf's avatar
Michel Kaempf committed
98 99 100
static void InitSignalHandler       ( void );
static void SignalHandler           ( int i_signal );

101
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
102
 * main: parse command line, start interface and spawn threads
103
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
104 105
 * Steps during program execution are:
 *      -configuration parsing and messages interface initialization
Vincent Seguin's avatar
Vincent Seguin committed
106
 *      -openning of audio output device and some global modules
Michel Kaempf's avatar
Michel Kaempf committed
107
 *      -execution of interface, which exit on error or on user request
Vincent Seguin's avatar
Vincent Seguin committed
108
 *      -closing of audio output device and some global modules
Michel Kaempf's avatar
Michel Kaempf committed
109
 * On error, the spawned threads are cancelled, and the openned devices closed.
110
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
111 112
int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
{
113 114
    main_t  main_data;                      /* root of all data - see main.h */
    p_main = &main_data;                       /* set up the global variable */
Vincent Seguin's avatar
Vincent Seguin committed
115

Michel Kaempf's avatar
Michel Kaempf committed
116 117
    /*
     * Read configuration, initialize messages interface and set up program
118
     */
Vincent Seguin's avatar
Vincent Seguin committed
119
    p_main->p_msg = intf_MsgCreate();
120
    if( !p_main->p_msg )                         /* start messages interface */
Michel Kaempf's avatar
Michel Kaempf committed
121
    {
Vincent Seguin's avatar
Vincent Seguin committed
122
        fprintf(stderr, "critical error: can't initialize messages interface (%s)\n",
Michel Kaempf's avatar
Michel Kaempf committed
123 124 125
                strerror(errno));
        return(errno);
    }
Vincent Seguin's avatar
Vincent Seguin committed
126
    if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) )/* parse command line */
Michel Kaempf's avatar
Michel Kaempf committed
127
    {
Vincent Seguin's avatar
Vincent Seguin committed
128
        intf_MsgDestroy();
Michel Kaempf's avatar
Michel Kaempf committed
129 130
        return(errno);
    }
131
    intf_MsgImm( COPYRIGHT_MESSAGE "\n" );          /* print welcome message */
Michel Kaempf's avatar
Michel Kaempf committed
132 133 134 135

    /*
     * Initialize shared resources and libraries
     */
Vincent Seguin's avatar
Vincent Seguin committed
136
    if( main_data.b_vlans && input_VlanCreate() )
Michel Kaempf's avatar
Michel Kaempf committed
137 138
    {
        /* On error during vlans initialization, switch of vlans */
Vincent Seguin's avatar
Vincent Seguin committed
139 140
        intf_Msg("Virtual LANs initialization failed : vlans management is desactivated\n");
        main_data.b_vlans = 0;
Michel Kaempf's avatar
Michel Kaempf committed
141
    }
142

Michel Kaempf's avatar
Michel Kaempf committed
143
    /*
Vincent Seguin's avatar
Vincent Seguin committed
144
     * Open audio device and start aout thread
Michel Kaempf's avatar
Michel Kaempf committed
145
     */
Vincent Seguin's avatar
Vincent Seguin committed
146 147 148 149 150 151 152 153 154
    if( main_data.b_audio )
    {
        main_data.p_aout = aout_CreateThread( NULL );
	if( main_data.p_aout == NULL )
	{
	    /* On error during audio initialization, switch of audio */
            intf_Msg("Audio initialization failed : audio is desactivated\n");
            main_data.b_audio = 0;
	}
Michel Kaempf's avatar
Michel Kaempf committed
155
    }
156

Michel Kaempf's avatar
Michel Kaempf committed
157 158 159
    /*
     * Run interface
     */
Vincent Seguin's avatar
Vincent Seguin committed
160 161 162 163 164 165 166
    main_data.p_intf = intf_Create();
    if( main_data.p_intf != NULL )
    {
	InitSignalHandler();               /* prepare signals for interception */
 	intf_Run( main_data.p_intf );
	intf_Destroy( main_data.p_intf );
    }
Michel Kaempf's avatar
Michel Kaempf committed
167

Michel Kaempf's avatar
Michel Kaempf committed
168
    /*
Michel Kaempf's avatar
Michel Kaempf committed
169 170
     * Close audio device
     */
Vincent Seguin's avatar
Vincent Seguin committed
171
    if( main_data.b_audio )
Michel Kaempf's avatar
Michel Kaempf committed
172
    {
Vincent Seguin's avatar
Vincent Seguin committed
173
        aout_DestroyThread( main_data.p_aout, NULL );
Michel Kaempf's avatar
Michel Kaempf committed
174
    }
Michel Kaempf's avatar
Michel Kaempf committed
175 176 177 178

    /*
     * Free shared resources and libraries
     */
Vincent Seguin's avatar
Vincent Seguin committed
179
    if( main_data.b_vlans )
180 181 182
    {
        input_VlanDestroy();
    }
Michel Kaempf's avatar
Michel Kaempf committed
183 184 185 186

    /*
     * Terminate messages interface and program
     */
Vincent Seguin's avatar
Vincent Seguin committed
187 188
    intf_Msg( "Program terminated.\n" );
    intf_MsgDestroy();
Michel Kaempf's avatar
Michel Kaempf committed
189 190 191
    return( 0 );
}

192
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
193
 * main_GetIntVariable: get the int value of an environment variable
194
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
195
 * This function is used to read some default parameters in modules.
196
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
197 198
int main_GetIntVariable( char *psz_name, int i_default )
{
199 200 201
    char *      psz_env;                                /* environment value */
    char *      psz_end;                             /* end of parsing index */
    long int    i_value;                                            /* value */
Vincent Seguin's avatar
Vincent Seguin committed
202 203 204

    psz_env = getenv( psz_name );
    if( psz_env )
205
    {
206 207 208 209
        i_value = strtol( psz_env, &psz_end, 0 );
        if( (*psz_env != '\0') && (*psz_end == '\0') )
        {
            return( i_value );
210
        }
Vincent Seguin's avatar
Vincent Seguin committed
211 212 213 214
    }	
    return( i_default );
}

215
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
216
 * main_GetPszVariable: get the string value of an environment variable
217
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
218
 * This function is used to read some default parameters in modules.
219
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
220 221 222 223 224 225 226
char * main_GetPszVariable( char *psz_name, char *psz_default )
{
    char *psz_env;

    psz_env = getenv( psz_name );
    if( psz_env )
    {
227
        return( psz_env );
Vincent Seguin's avatar
Vincent Seguin committed
228
    }
229
    return( psz_default );
Vincent Seguin's avatar
Vincent Seguin committed
230 231
}

232
/*****************************************************************************
233
 * main_PutPszVariable: set the string value of an environment variable
234
 *****************************************************************************
235 236 237
 * This function is used to set some default parameters in modules. The use of
 * this function will cause some memory leak: since some systems use the pointer
 * passed to putenv to store the environment string, it can't be freed.
238
 *****************************************************************************/
239 240 241 242 243 244 245
void main_PutPszVariable( char *psz_name, char *psz_value )
{
    char *psz_env;

    psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
    if( psz_env == NULL )
    {
246
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
247 248 249 250 251 252 253
    }
    else
    {
        sprintf( psz_env, "%s=%s", psz_name, psz_value );
        if( putenv( psz_env ) )
        {
            intf_ErrMsg("error: %s\n", strerror(errno));
254
        }
255 256 257
    }
}

258
/*****************************************************************************
259
 * main_PutIntVariable: set the integer value of an environment variable
260
 *****************************************************************************
261 262 263
 * This function is used to set some default parameters in modules. The use of
 * this function will cause some memory leak: since some systems use the pointer
 * passed to putenv to store the environment string, it can't be freed.
264
 *****************************************************************************/
265 266
void main_PutIntVariable( char *psz_name, int i_value )
{
267
    char psz_value[ 256 ];                               /* buffer for value */
268

269 270
    sprintf(psz_value, "%d", i_value );
    main_PutPszVariable( psz_name, psz_value );
271 272
}

Michel Kaempf's avatar
Michel Kaempf committed
273 274
/* following functions are local */

275
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
276
 * SetDefaultConfiguration: set default options
277
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
278
 * This function is called by GetConfiguration before command line is parsed.
Vincent Seguin's avatar
Vincent Seguin committed
279 280 281
 * It sets all the default values required later by the program. At this stage,
 * most structure are not yet allocated, so initialization must be done using
 * environment.
282
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
283
static void SetDefaultConfiguration( void )
Michel Kaempf's avatar
Michel Kaempf committed
284 285
{
    /*
Vincent Seguin's avatar
Vincent Seguin committed
286
     * All features are activated by default
Michel Kaempf's avatar
Michel Kaempf committed
287
     */
Vincent Seguin's avatar
Vincent Seguin committed
288 289 290
    p_main->b_audio  = 1;
    p_main->b_video  = 1;
    p_main->b_vlans  = 1;
Michel Kaempf's avatar
Michel Kaempf committed
291 292
}

293
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
294
 * GetConfiguration: parse command line
295
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
296 297
 * Parse command line and configuration file for configuration. If the inline
 * help is requested, the function Usage() is called and the function returns
298
 * -1 (causing main() to exit). The messages interface is initialized at this
Vincent Seguin's avatar
Vincent Seguin committed
299 300
 * stage, but most structures are not allocated, so only environment should
 * be used.
301
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
302
static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
Michel Kaempf's avatar
Michel Kaempf committed
303
{
Vincent Seguin's avatar
Vincent Seguin committed
304
    int c, i_opt;
Michel Kaempf's avatar
Michel Kaempf committed
305 306

    /* Set default configuration and copy arguments */
Vincent Seguin's avatar
Vincent Seguin committed
307 308
    p_main->i_argc    = i_argc;
    p_main->ppsz_argv = ppsz_argv;
309
    p_main->ppsz_env  = ppsz_env;
Vincent Seguin's avatar
Vincent Seguin committed
310
    SetDefaultConfiguration();
Michel Kaempf's avatar
Michel Kaempf committed
311

Vincent Seguin's avatar
Vincent Seguin committed
312
    /* Parse command line options */
Michel Kaempf's avatar
Michel Kaempf committed
313 314 315 316 317
    opterr = 0;
    while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
    {
        switch( c )
        {
318 319
        /* General/common options */
        case 'h':                                              /* -h, --help */
Michel Kaempf's avatar
Michel Kaempf committed
320 321 322
            Usage();
            return( -1 );
            break;
323
        case 'v':                                           /* -v, --version */
Vincent Seguin's avatar
Vincent Seguin committed
324 325
            Version();
            return( -1 );
326
            break;
Michel Kaempf's avatar
Michel Kaempf committed
327 328

        /* Audio options */
329
        case OPT_NOAUDIO:                                       /* --noaudio */
Vincent Seguin's avatar
Vincent Seguin committed
330
	    p_main->b_audio = 0;
Michel Kaempf's avatar
Michel Kaempf committed
331
            break;
332
        case OPT_STEREO:                                         /* --stereo */
333
            main_PutIntVariable( AOUT_STEREO_VAR, 1 );
Michel Kaempf's avatar
Michel Kaempf committed
334
            break;
335
        case OPT_MONO:                                             /* --mono */
336
            main_PutIntVariable( AOUT_STEREO_VAR, 0 );
Michel Kaempf's avatar
Michel Kaempf committed
337 338 339
            break;

        /* Video options */
340
        case OPT_NOVIDEO:                                       /* --novideo */
Vincent Seguin's avatar
Vincent Seguin committed
341
            p_main->b_video = 0;
342 343
            break;
        case OPT_DISPLAY:                                       /* --display */
344
            main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
345 346
            break;
        case OPT_WIDTH:                                           /* --width */
347
            main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
348 349 350 351 352 353
            break;
        case OPT_HEIGHT:                                         /* --height */
            main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
            break;

        case 'g':                                         /* -g, --grayscale */
354
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
355 356
            break;
        case OPT_COLOR:                                           /* --color */
357
            main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
358
            break;
Michel Kaempf's avatar
Michel Kaempf committed
359

360
        /* Input options */
361
        case OPT_NOVLANS:                                       /* --novlans */
Vincent Seguin's avatar
Vincent Seguin committed
362
            p_main->b_vlans = 0;
363 364
            break;
        case OPT_SERVER:                                         /* --server */
365
            main_PutPszVariable( INPUT_SERVER_VAR, optarg );
366 367
            break;
        case OPT_PORT:                                             /* --port */
368
            main_PutPszVariable( INPUT_PORT_VAR, optarg );
369 370
            break;
	
Michel Kaempf's avatar
Michel Kaempf committed
371
        /* Internal error: unknown option */
372
        case '?':
Michel Kaempf's avatar
Michel Kaempf committed
373 374 375 376 377 378 379
        default:
            intf_ErrMsg("intf error: unknown option '%s'\n", ppsz_argv[optind - 1]);
            return( EINVAL );
            break;
        }
    }

Vincent Seguin's avatar
Vincent Seguin committed
380 381 382 383 384
    /* Parse command line parameters - no check is made for these options */
    for( i_opt = optind; i_opt < i_argc; i_opt++ )
    {
	putenv( ppsz_argv[ i_opt ] );
    }
Michel Kaempf's avatar
Michel Kaempf committed
385 386 387
    return( 0 );
}

388
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
389
 * Usage: print program usage
390
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
391
 * Print a short inline help. Message interface is initialized at this stage.
392
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
393 394
static void Usage( void )
{
Vincent Seguin's avatar
Vincent Seguin committed
395 396
    intf_Msg(COPYRIGHT_MESSAGE "\n");

Vincent Seguin's avatar
Vincent Seguin committed
397
    /* Usage */
398
    intf_Msg("usage: vlc [options...] [parameters]\n" );
Vincent Seguin's avatar
Vincent Seguin committed
399 400

    /* Options */
Vincent Seguin's avatar
Vincent Seguin committed
401
    intf_Msg("Options:\n" \
402 403
             "  -h, --help, -v, --version         \tprint usage or version\n" \
             "  --noaudio, --novideo              \tdisable audio/video\n" \
404 405 406 407 408 409
             "  --stereo, --mono                  \tstereo/mono audio\n" \
             "  --display <display>               \tdisplay string\n" \
             "  --width <w>, --height <h>         \tdisplay dimensions\n" \
             "  -g, --grayscale, --color          \tgrayscale/color video\n" \
             "  --novlans                         \tdisable vlans\n" \
             "  --server <host>, --port <port>    \tvideo server adress\n" \
Michel Kaempf's avatar
Michel Kaempf committed
410 411
             );

Vincent Seguin's avatar
Vincent Seguin committed
412 413
    /* Interface parameters */
    intf_Msg("Interface parameters:\n" \
414
	     "  " INTF_INIT_SCRIPT_VAR "=<filename>             \tinitialization script\n" \
415
             "  " INTF_CHANNELS_VAR "=<filename>            \tchannels list\n"\
Vincent Seguin's avatar
Vincent Seguin committed
416 417 418 419
	     );

    /* Audio parameters */
    intf_Msg("Audio parameters:\n" \
420 421 422
             "  " AOUT_DSP_VAR "=<filename>              \tdsp device path\n" \
             "  " AOUT_STEREO_VAR "={1|0}                \tstereo or mono output\n" \
             "  " AOUT_RATE_VAR "=<rate>             \toutput rate\n" \
Vincent Seguin's avatar
Vincent Seguin committed
423 424 425 426
	     );

    /* Video parameters */
    intf_Msg("Video parameters:\n" \
427 428 429 430 431
             "  " VOUT_DISPLAY_VAR "=<display name>      \tdisplay used\n" \
             "  " VOUT_WIDTH_VAR "=<width>               \tdisplay width\n" \
             "  " VOUT_HEIGHT_VAR "=<height>             \tdislay height\n" \
             "  " VOUT_FB_DEV_VAR "=<filename>           \tframebuffer device path\n" \
             "  " VOUT_GRAYSCALE_VAR "={1|0}             \tgrayscale or color output\n" \
432
	     );
Vincent Seguin's avatar
Vincent Seguin committed
433

434 435 436 437 438 439
    /* Input parameters */
    intf_Msg("Input parameters:\n" \
             "  " INPUT_SERVER_VAR "=<hostname>          \tvideo server\n" \
             "  " INPUT_PORT_VAR "=<port>            \tvideo server port\n" \
	     "  " INPUT_IFACE_VAR "=<interface>          \tnetwork interface\n" \
             "  " INPUT_VLAN_SERVER_VAR "=<hostname>     \tvlan server\n" \
440
             "  " INPUT_VLAN_PORT_VAR "=<port>           \tvlan server port\n"\
Vincent Seguin's avatar
Vincent Seguin committed
441
	     );
Michel Kaempf's avatar
Michel Kaempf committed
442 443
}

444
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
445
 * Version: print complete program version
446
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
447
 * Print complete program version and build number.
448
 *****************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
449 450 451 452 453 454 455 456 457 458
static void Version( void )
{
    intf_Msg(VERSION_MESSAGE "\n\n");
    intf_Msg("This is free software; see the documentation or contact <videolan@via.ecp.fr>\n" \
             "for use and copying conditions.\n" \
             "\n" \
             "This software is protected by the international copyright laws, and is\n" \
             "provided without any warranty, including the implied warranties of\n" \
             "merchantibility and fitness for a particular purpose.\n" \
            );
459
}
Vincent Seguin's avatar
Vincent Seguin committed
460

461
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
462
 * InitSignalHandler: system signal handler initialization
463
 *****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
464
 * Set the signal handlers. SIGTERM is not intercepted, because we need at
465
 * at least a method to kill the program when all other methods failed, and
Michel Kaempf's avatar
Michel Kaempf committed
466
 * when we don't want to use SIGKILL.
467
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
468 469
static void InitSignalHandler( void )
{
Vincent Seguin's avatar
Vincent Seguin committed
470 471 472 473
    /* Termination signals */
    signal( SIGHUP,  SignalHandler );
    signal( SIGINT,  SignalHandler );
    signal( SIGQUIT, SignalHandler );
Michel Kaempf's avatar
Michel Kaempf committed
474 475
}

476
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
477
 * SignalHandler: system signal handler
478
 *****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
479 480
 * This function is called when a signal is received by the program. It tries to
 * end the program in a clean way.
481
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
482 483
static void SignalHandler( int i_signal )
{
484
    /* Once a signal has been trapped, the termination sequence will be armed and
Vincent Seguin's avatar
Vincent Seguin committed
485 486 487 488 489 490 491 492 493
     * following signals will be ignored to avoid sending messages to an interface
     * having been destroyed */
    signal( SIGHUP,  SIG_IGN );
    signal( SIGINT,  SIG_IGN );
    signal( SIGQUIT, SIG_IGN );

    /* Acknowledge the signal received */
    intf_ErrMsgImm("intf: signal %d received\n", i_signal );

494
    /* Try to terminate everything - this is done by requesting the end of the
Vincent Seguin's avatar
Vincent Seguin committed
495 496 497 498
     * interface thread */
    p_main->p_intf->b_die = 1;
}

Michel Kaempf's avatar
Michel Kaempf committed
499 500