]> git.sesse.net Git - vlc/blob - src/vlc.c
Rework signal handling so that it is async-safe
[vlc] / src / vlc.c
1 /*****************************************************************************
2  * vlc.c: the vlc player
3  *****************************************************************************
4  * Copyright (C) 1998-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *          Derk-Jan Hartman <hartman at videolan dot org>
11  *          Lots of other people, see the libvlc AUTHORS file
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 #include "config.h"
29
30 #include <stdio.h>                                              /* fprintf() */
31 #include <stdlib.h>                                  /* putenv(), strtol(),  */
32 #ifdef HAVE_SIGNAL_H
33 #   include <signal.h>                            /* SIGHUP, SIGINT, SIGKILL */
34 #endif
35 #ifdef HAVE_TIME_H
36 #   include <time.h>                                               /* time() */
37 #endif
38 #ifdef HAVE_PTHREAD_H
39 #   include <pthread.h>
40 #endif
41
42 #include <vlc/vlc.h>
43
44 #ifdef WIN32
45 #include <windows.h>
46 extern void __wgetmainargs(int *argc, wchar_t ***wargv, wchar_t ***wenviron,
47                            int expand_wildcards, int *startupinfo);
48 #endif
49
50 /*****************************************************************************
51  * Local prototypes.
52  *****************************************************************************/
53 #if !defined(WIN32) && !defined(UNDER_CE)
54 static void *SigHandler  ( void *set );
55 #endif
56
57 /*****************************************************************************
58  * main: parse command line, start interface and spawn threads.
59  *****************************************************************************/
60 int main( int i_argc, char *ppsz_argv[] )
61 {
62     int i_ret;
63
64 #ifndef __APPLE__
65     /* This clutters OSX GUI error logs */
66     fprintf( stderr, "VLC media player %s\n", VLC_Version() );
67 #endif
68
69 #ifdef HAVE_PUTENV
70 #   ifdef DEBUG
71     /* Activate malloc checking routines to detect heap corruptions. */
72     putenv( (char*)"MALLOC_CHECK_=2" );
73
74     /* Disable the ugly Gnome crash dialog so that we properly segfault */
75     putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
76 #   endif
77
78     /* If the user isn't using VLC_VERBOSE, set it to 0 by default */
79     if( getenv( "VLC_VERBOSE" ) == NULL )
80     {
81         putenv( (char *)"VLC_VERBOSE=0" );
82     }
83 #endif
84
85 #if defined (HAVE_GETEUID) && !defined (SYS_BEOS)
86     /* FIXME: rootwrap (); */
87 #endif
88
89     /* Create a libvlc structure */
90     i_ret = VLC_Create();
91     if( i_ret < 0 )
92     {
93         return i_ret;
94     }
95
96 #if !defined(WIN32) && !defined(UNDER_CE)
97     /* Synchronously intercepted signals. Thy request a clean shutdown,
98      * and force an unclean shutdown if they are triggered again 2+ seconds
99      * later. We have to handle SIGTERM cleanly because of daemon mode.
100      * Note that we set the signals after the vlc_create call. */
101     static const int sigs[] = { SIGINT, SIGHUP, SIGQUIT, SIGTERM };
102     /* Ignored signals */
103     static const int ignored[] = { SIGALRM, SIGPIPE };
104
105     sigset_t set;
106     pthread_t sigth;
107
108     sigemptyset (&set);
109     for (unsigned i = 0; i < sizeof (sigs) / sizeof (sigs[0]); i++)
110         sigaddset (&set, sigs[i]);
111     for (unsigned i = 0; i < sizeof (ignored) / sizeof (ignored[0]); i++)
112         sigaddset (&set, ignored[i]);
113
114     /* Block all these signals */
115     pthread_sigmask (SIG_BLOCK, &set, NULL);
116
117     for (unsigned i = 0; i < sizeof (ignored) / sizeof (ignored[0]); i++)
118         sigdelset (&set, ignored[i]);
119
120     pthread_create (&sigth, NULL, SigHandler, &set);
121 #endif
122
123 #ifdef WIN32
124     /* Replace argv[1..n] with unicode for Windows NT and above */
125     if( GetVersion() < 0x80000000 )
126     {
127         wchar_t **wargv, **wenvp;
128         int i,i_wargc;
129         int si = { 0 };
130         __wgetmainargs(&i_wargc, &wargv, &wenvp, 0, &si);
131
132         for( i = 1; i < i_wargc; i++ )
133         {
134             int len = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL);
135             if( len > 0 )
136             {
137                 if( len > 1 ) {
138                     char *utf8arg = (char *)malloc(len);
139                     if( NULL != utf8arg )
140                     {
141                         WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, utf8arg, len, NULL, NULL);
142                         ppsz_argv[i] = utf8arg;
143                     }
144                     else
145                     {
146                         /* failed!, quit */
147                         return -1;
148                     }
149                 }
150                 else
151                 {
152                     ppsz_argv[i] = "";
153                 }
154             }
155             else
156             {
157                 /* failed!, quit */
158                 return -1;
159             }
160         }
161     }
162 #endif
163
164     /* Initialize libvlc */
165     i_ret = VLC_Init( 0, i_argc, ppsz_argv );
166     if( i_ret < 0 )
167     {
168         VLC_Destroy( 0 );
169         return i_ret == VLC_EEXITSUCCESS ? 0 : i_ret;
170     }
171
172     i_ret = VLC_AddIntf( 0, NULL, VLC_TRUE, VLC_TRUE );
173
174 #if !defined(WIN32) && !defined(UNDER_CE)
175     pthread_cancel (sigth);
176     pthread_join (sigth, NULL);
177 #endif
178
179     /* Finish the threads */
180     VLC_CleanUp( 0 );
181
182     /* Destroy the libvlc structure */
183     VLC_Destroy( 0 );
184
185     return i_ret;
186 }
187
188 #if !defined(WIN32) && !defined(UNDER_CE)
189 /*****************************************************************************
190  * SigHandler: system signal handler
191  *****************************************************************************
192  * This thread receives all handled signals synchronously.
193  * It tries to end the program in a clean way.
194  *****************************************************************************/
195 static void *SigHandler( void *data )
196 {
197     const sigset_t *set = (sigset_t *)data;
198     time_t abort_time = 0;
199     vlc_bool_t b_die = VLC_FALSE;
200
201     for (;;)
202     {
203         int i_signal, state;
204         (void)sigwait (set, &i_signal);
205
206         /* Once a signal has been trapped, the termination sequence will be
207          * armed and subsequent signals will be ignored to avoid sending
208          * signals to a libvlc structure having been destroyed */
209
210         pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
211         if( !b_die )
212         {
213             b_die = VLC_TRUE;
214             abort_time = time( NULL );
215
216             fprintf( stderr, "signal %d received, terminating vlc - do it "
217                             "again in case it gets stuck\n", i_signal );
218
219             /* Acknowledge the signal received */
220             VLC_Die( 0 );
221         }
222         else if( time( NULL ) > abort_time + 2 )
223         {
224             /* If user asks again 1 or 2 seconds later, die badly */
225             pthread_sigmask (SIG_UNBLOCK, set, NULL);
226             fprintf( stderr, "user insisted too much, dying badly\n" );
227             abort();
228         }
229         pthread_setcancelstate (state, NULL);
230     }
231     /* Never reached */
232 }
233 #endif
234
235 #if defined(UNDER_CE)
236 #   if defined( _MSC_VER ) && defined( UNDER_CE )
237 #       include "vlc_common.h"
238 #   endif
239 /*****************************************************************************
240  * WinMain: parse command line, start interface and spawn threads. (WinCE only)
241  *****************************************************************************/
242 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
243                     LPTSTR lpCmdLine, int nCmdShow )
244 {
245     char **argv, psz_cmdline[MAX_PATH];
246     int argc, i_ret;
247
248     WideCharToMultiByte( CP_ACP, 0, lpCmdLine, -1,
249                          psz_cmdline, MAX_PATH, NULL, NULL );
250
251     argv = vlc_parse_cmdline( psz_cmdline, &argc );
252     argv = realloc( argv, (argc + 1) * sizeof(char *) );
253     if( !argv ) return -1;
254
255     if( argc ) memmove( argv + 1, argv, argc * sizeof(char *) );
256     argv[0] = ""; /* Fake program path */
257
258     i_ret = main( argc + 1, argv );
259
260     /* No need to free the argv memory */
261     return i_ret;
262 }
263 #endif