]> git.sesse.net Git - vlc/blob - src/vlc.c
Fix a hang when quitting vlc on Mac OS X. This hang is due to the signal thread that...
[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 <vlc/vlc.h>
31 #include <stdio.h>                                              /* fprintf() */
32 #include <stdlib.h>                                  /* putenv(), strtol(),  */
33
34
35 /*****************************************************************************
36  * Local prototypes.
37  *****************************************************************************/
38 #ifdef WIN32
39 #include <windows.h>
40 extern void __wgetmainargs(int *argc, wchar_t ***wargv, wchar_t ***wenviron,
41                            int expand_wildcards, int *startupinfo);
42 static inline void Kill(void) { }
43 #else
44
45 # include <signal.h>
46 # include <time.h>
47 # include <pthread.h>
48
49 static void Kill (void);
50 static void *SigHandler (void *set);
51 #endif
52
53 /*****************************************************************************
54  * main: parse command line, start interface and spawn threads.
55  *****************************************************************************/
56 int main( int i_argc, char *ppsz_argv[] )
57 {
58     int i_ret;
59
60 #ifndef __APPLE__
61     /* This clutters OSX GUI error logs */
62     fprintf( stderr, "VLC media player %s\n", VLC_Version() );
63 #endif
64
65 #ifdef HAVE_PUTENV
66 #   ifdef DEBUG
67     /* Activate malloc checking routines to detect heap corruptions. */
68     putenv( (char*)"MALLOC_CHECK_=2" );
69
70     /* Disable the ugly Gnome crash dialog so that we properly segfault */
71     putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
72 #   endif
73
74     /* If the user isn't using VLC_VERBOSE, set it to 0 by default */
75     if( getenv( "VLC_VERBOSE" ) == NULL )
76     {
77         putenv( (char *)"VLC_VERBOSE=0" );
78     }
79 #endif
80
81 #if defined (HAVE_GETEUID) && !defined (SYS_BEOS)
82     /* FIXME: rootwrap (); */
83 #endif
84
85     /* Create a libvlc structure */
86     i_ret = VLC_Create();
87     if( i_ret < 0 )
88     {
89         return i_ret;
90     }
91
92 #if !defined(WIN32) && !defined(UNDER_CE)
93     /* Synchronously intercepted signals. Thy request a clean shutdown,
94      * and force an unclean shutdown if they are triggered again 2+ seconds
95      * later. We have to handle SIGTERM cleanly because of daemon mode.
96      * Note that we set the signals after the vlc_create call. */
97     static const int sigs[] = { SIGINT, SIGHUP, SIGQUIT, SIGTERM };
98     /* Ignored signals */
99     static const int ignored[] = { SIGALRM, SIGPIPE };
100
101     sigset_t set;
102     pthread_t sigth;
103
104     sigemptyset (&set);
105     for (unsigned i = 0; i < sizeof (sigs) / sizeof (sigs[0]); i++)
106         sigaddset (&set, sigs[i]);
107     for (unsigned i = 0; i < sizeof (ignored) / sizeof (ignored[0]); i++)
108         sigaddset (&set, ignored[i]);
109
110     /* Block all these signals */
111     pthread_sigmask (SIG_BLOCK, &set, NULL);
112
113     for (unsigned i = 0; i < sizeof (ignored) / sizeof (ignored[0]); i++)
114         sigdelset (&set, ignored[i]);
115
116     pthread_create (&sigth, NULL, SigHandler, &set);
117 #endif
118
119 #ifdef WIN32
120     /* Replace argv[1..n] with unicode for Windows NT and above */
121     if( GetVersion() < 0x80000000 )
122     {
123         wchar_t **wargv, **wenvp;
124         int i,i_wargc;
125         int si = { 0 };
126         __wgetmainargs(&i_wargc, &wargv, &wenvp, 0, &si);
127
128         for( i = 1; i < i_wargc; i++ )
129         {
130             int len = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL);
131             if( len > 0 )
132             {
133                 if( len > 1 ) {
134                     char *utf8arg = (char *)malloc(len);
135                     if( NULL != utf8arg )
136                     {
137                         WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, utf8arg, len, NULL, NULL);
138                         ppsz_argv[i] = utf8arg;
139                     }
140                     else
141                     {
142                         /* failed!, quit */
143                         return -1;
144                     }
145                 }
146                 else
147                 {
148                     ppsz_argv[i] = "";
149                 }
150             }
151             else
152             {
153                 /* failed!, quit */
154                 return -1;
155             }
156         }
157     }
158 #endif
159
160     /* Initialize libvlc */
161     i_ret = VLC_Init( 0, i_argc, ppsz_argv );
162     if( i_ret < 0 )
163     {
164         VLC_Destroy( 0 );
165         return i_ret == VLC_EEXITSUCCESS ? 0 : i_ret;
166     }
167
168     i_ret = VLC_AddIntf( 0, NULL, VLC_TRUE, VLC_TRUE );
169
170     /* Finish the threads */
171     VLC_CleanUp( 0 );
172
173     Kill ();
174
175     /* Destroy the libvlc structure */
176     VLC_Destroy( 0 );
177
178 #if !defined(WIN32) && !defined(UNDER_CE)
179     pthread_cancel (sigth);
180 # ifdef __APPLE__
181     /* In Mac OS X up to 10.4.8 sigwait (among others) is not a pthread
182      * cancellation point, so we throw a dummy quit signal to end
183      * sigwait() in the sigth thread */
184     pthread_kill (sigth, SIGQUIT);
185 # endif
186     pthread_join (sigth, NULL);
187 #endif
188
189     return i_ret;
190 }
191
192 #if !defined(WIN32) && !defined(UNDER_CE)
193 /*****************************************************************************
194  * SigHandler: system signal handler
195  *****************************************************************************
196  * This thread receives all handled signals synchronously.
197  * It tries to end the program in a clean way.
198  *****************************************************************************/
199 static void *SigHandler (void *data)
200 {
201     const sigset_t *set = (sigset_t *)data;
202     time_t abort_time = 0;
203     vlc_bool_t b_die = VLC_FALSE;
204
205     for (;;)
206     {
207         int i_signal, state;
208         (void)sigwait (set, &i_signal);
209
210 #ifdef __APPLE__
211         /* In Mac OS X up to 10.4.8 sigwait (among others) is not a pthread
212          * cancellation point */
213         pthread_testcancel();
214 #endif
215
216         /* Once a signal has been trapped, the termination sequence will be
217          * armed and subsequent signals will be ignored to avoid sending
218          * signals to a libvlc structure having been destroyed */
219
220         pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
221         if (!b_die)
222         {
223             b_die = VLC_TRUE;
224             abort_time = time (NULL);
225
226             fprintf (stderr, "signal %d received, terminating vlc - do it "
227                             "again in case it gets stuck\n", i_signal);
228
229             /* Acknowledge the signal received */
230             Kill ();
231         }
232         else if( time( NULL ) > abort_time + 2 )
233         {
234             /* If user asks again 1 or 2 seconds later, die badly */
235             pthread_sigmask (SIG_UNBLOCK, set, NULL);
236             fprintf (stderr, "user insisted too much, dying badly\n");
237             abort ();
238         }
239         pthread_setcancelstate (state, NULL);
240     }
241     /* Never reached */
242 }
243
244
245 static void KillOnce (void)
246 {
247     VLC_Die (0);
248 }
249
250
251 static void Kill (void)
252 {
253     static pthread_once_t once = PTHREAD_ONCE_INIT;
254     pthread_once (&once, KillOnce);
255 }
256
257 #endif
258
259 #if defined(UNDER_CE)
260 #   if defined( _MSC_VER ) && defined( UNDER_CE )
261 #       include "vlc_common.h"
262 #   endif
263 /*****************************************************************************
264  * WinMain: parse command line, start interface and spawn threads. (WinCE only)
265  *****************************************************************************/
266 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
267                     LPTSTR lpCmdLine, int nCmdShow )
268 {
269     char **argv, psz_cmdline[MAX_PATH];
270     int argc, i_ret;
271
272     WideCharToMultiByte( CP_ACP, 0, lpCmdLine, -1,
273                          psz_cmdline, MAX_PATH, NULL, NULL );
274
275     argv = vlc_parse_cmdline( psz_cmdline, &argc );
276     argv = realloc( argv, (argc + 1) * sizeof(char *) );
277     if( !argv ) return -1;
278
279     if( argc ) memmove( argv + 1, argv, argc * sizeof(char *) );
280     argv[0] = ""; /* Fake program path */
281
282     i_ret = main( argc + 1, argv );
283
284     /* No need to free the argv memory */
285     return i_ret;
286 }
287 #endif