]> git.sesse.net Git - vlc/blob - src/vlc.c
* ./src/misc/cpu.c: libvlc now plays nice with SIGILL and restores the
[vlc] / src / vlc.c
1 /*****************************************************************************
2  * vlc.c: the vlc player
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  * $Id: vlc.c,v 1.9 2002/08/19 11:13:45 sam Exp $
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *          Lots of other people, see the libvlc AUTHORS file
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 #include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
28 #include <stdio.h>                                              /* fprintf() */
29 #include <stdlib.h>                                  /* putenv(), strtol(),  */
30 #include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
31
32 #include <vlc/vlc.h>
33
34 /*****************************************************************************
35  * Local prototypes.
36  *****************************************************************************/
37 #ifndef WIN32
38 static void SigHandler  ( int i_signal );
39 #endif
40
41 /*****************************************************************************
42  * main: parse command line, start interface and spawn threads
43  *****************************************************************************/
44 int main( int i_argc, char *ppsz_argv[] )
45 {
46     vlc_error_t err;
47
48     fprintf( stderr, COPYRIGHT_MESSAGE "\n" );
49
50 #ifdef SYS_LINUX
51 #   ifdef DEBUG
52     /* Activate malloc checking routines to detect heap corruptions. */
53     putenv( "MALLOC_CHECK_=2" );
54
55     /* Disable the ugly Gnome crash dialog so that we properly segfault */
56     putenv( "GNOME_DISABLE_CRASH_DIALOG=1" );
57 #   endif
58 #endif
59
60     /* Create a libvlc structure */
61     err = vlc_create();
62     if( err != VLC_SUCCESS )
63     {
64         return err;
65     }
66
67 #ifndef WIN32
68     /* Set the signal handlers. SIGTERM is not intercepted, because we need at
69      * least one method to kill the program when all other methods failed, and
70      * when we don't want to use SIGKILL.
71      * Note that we set the signals after the vlc_create call. */
72     signal( SIGINT,  SigHandler );
73     signal( SIGHUP,  SigHandler );
74     signal( SIGQUIT, SigHandler );
75
76     /* Other signals */
77     signal( SIGALRM, SIG_IGN );
78     signal( SIGPIPE, SIG_IGN );
79 #endif
80
81     /* Initialize libvlc */
82     err = vlc_init( i_argc, ppsz_argv );
83     if( err != VLC_SUCCESS )
84     {
85         vlc_destroy();
86         return err;
87     }
88
89     /* Run libvlc, in non-blocking mode */
90     err = vlc_run();
91
92     /* Add background interfaces */
93 #if 0
94     { int i; for( i=10; i--; ) vlc_add_intf( NULL, "dummy", 0 ); }
95     vlc_add_intf( NULL, "dummy", VLC_FALSE );
96     vlc_add_intf( NULL, "logger", VLC_FALSE );
97     vlc_add_intf( NULL, "xosd", VLC_FALSE );
98     vlc_add_intf( NULL, "gtk", VLC_FALSE );
99     vlc_add_intf( NULL, "kde", VLC_FALSE );
100     vlc_add_intf( "rc", VLC_FALSE );
101 #endif
102
103     /* Add a blocking interface and keep the return value */
104     err = vlc_add_intf( NULL, VLC_TRUE );
105
106     /* Finish the interface */
107     vlc_stop();
108
109     /* Finish all threads */
110     vlc_end();
111
112     /* Destroy the libvlc structure */
113     vlc_destroy();
114
115     return err;
116 }
117
118 #ifndef WIN32
119 /*****************************************************************************
120  * SigHandler: system signal handler
121  *****************************************************************************
122  * This function is called when a fatal signal is received by the program.
123  * It tries to end the program in a clean way.
124  *****************************************************************************/
125 static void SigHandler( int i_signal )
126 {
127     static mtime_t abort_time = 0;
128     static volatile vlc_bool_t b_die = VLC_FALSE;
129
130     /* Once a signal has been trapped, the termination sequence will be
131      * armed and subsequent signals will be ignored to avoid sending signals
132      * to a libvlc structure having been destroyed */
133
134     if( !b_die )
135     {
136         b_die = VLC_TRUE;
137         abort_time = mdate();
138
139         fprintf( stderr, "signal %d received, terminating vlc - do it "
140                          "again in case it gets stuck\n", i_signal );
141
142         /* Acknowledge the signal received */
143         vlc_die();
144     }
145     else if( mdate() > abort_time + 1000000 )
146     {
147         /* If user asks again 1 second later, die badly */
148         signal( SIGINT,  SIG_DFL );
149         signal( SIGHUP,  SIG_DFL );
150         signal( SIGQUIT, SIG_DFL );
151         signal( SIGALRM, SIG_DFL );
152         signal( SIGPIPE, SIG_DFL );
153
154         fprintf( stderr, "user insisted too much, dying badly\n" );
155
156         exit( 1 );
157     }
158 }
159 #endif
160