]> git.sesse.net Git - vlc/blob - src/vlc.c
Dequeue "dummy" signals
[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>
32 #include <stdlib.h>
33 #include <locale.h>
34
35
36 /* Explicit HACK */
37 extern void LocaleFree (const char *);
38 extern char *FromLocale (const char *);
39
40
41 /*****************************************************************************
42  * Local prototypes.
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 static inline void Kill(void) { }
49 #else
50
51 # include <signal.h>
52 # include <time.h>
53 # include <pthread.h>
54
55 static void Kill (void);
56 static void *SigHandler (void *set);
57 #endif
58
59 /*****************************************************************************
60  * main: parse command line, start interface and spawn threads.
61  *****************************************************************************/
62 int main( int i_argc, char *ppsz_argv[] )
63 {
64     int i_ret;
65
66     setlocale (LC_ALL, "");
67
68 #ifndef __APPLE__
69     /* This clutters OSX GUI error logs */
70     fprintf( stderr, "VLC media player %s\n", VLC_Version() );
71 #endif
72
73 #ifdef HAVE_PUTENV
74 #   ifdef DEBUG
75     /* Activate malloc checking routines to detect heap corruptions. */
76     putenv( (char*)"MALLOC_CHECK_=2" );
77
78     /* Disable the ugly Gnome crash dialog so that we properly segfault */
79     putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
80 #   endif
81
82     /* If the user isn't using VLC_VERBOSE, set it to 0 by default */
83     if( getenv( "VLC_VERBOSE" ) == NULL )
84     {
85         putenv( (char *)"VLC_VERBOSE=0" );
86     }
87 #endif
88
89 #if defined (HAVE_GETEUID) && !defined (SYS_BEOS)
90     /* FIXME: rootwrap (); */
91 #endif
92
93     /* Create a libvlc structure */
94     i_ret = VLC_Create();
95     if( i_ret < 0 )
96     {
97         return -i_ret;
98     }
99
100 #if !defined(WIN32) && !defined(UNDER_CE)
101     /* Synchronously intercepted POSIX signals.
102      *
103      * In a threaded program such as VLC, the only sane way to handle signals
104      * is to block them in all thread but one - this is the only way to
105      * predict which thread will receive them. If any piece of code depends
106      * on delivery of one of this signal it is intrinsically not thread-safe
107      * and MUST NOT be used in VLC, whether we like it or not.
108      * There is only one exception: if the signal is raised with
109      * pthread_kill() - we do not use this in LibVLC but some pthread
110      * implementations use them internally. You should really use conditions
111      * for thread synchronization anyway.
112      *
113      * Signal that request a clean shutdown, and force an unclean shutdown
114      * if they are triggered again 2+ seconds later.
115      * We have to handle SIGTERM cleanly because of daemon mode.
116      * Note that we set the signals after the vlc_create call. */
117     static const int exitsigs[] = { SIGINT, SIGHUP, SIGQUIT, SIGTERM };
118     /* Signals that cause a no-op:
119      * - SIGALRM should not happen, but lets stay on the safe side.
120      * - SIGPIPE might happen with sockets and would crash VLC. It MUST be
121      *   blocked by any LibVLC-dependant application, in addition to VLC.
122      * - SIGCHLD is comes after exec*() (such as httpd CGI support) and must
123      *   be dequeued to cleanup zombie processes.
124      */
125     static const int dummysigs[] = { SIGALRM, SIGPIPE, SIGCHLD };
126
127     sigset_t set;
128     pthread_t sigth;
129
130     sigemptyset (&set);
131     for (unsigned i = 0; i < sizeof (exitsigs) / sizeof (exitsigs[0]); i++)
132         sigaddset (&set, exitsigs[i]);
133     for (unsigned i = 0; i < sizeof (dummysigs) / sizeof (dummysigs[0]); i++)
134         sigaddset (&set, dummysigs[i]);
135
136     /* Block all these signals */
137     pthread_sigmask (SIG_BLOCK, &set, NULL);
138
139     for (unsigned i = 0; i < sizeof (dummysigs) / sizeof (dummysigs[0]); i++)
140         sigdelset (&set, dummysigs[i]);
141
142     pthread_create (&sigth, NULL, SigHandler, &set);
143 #endif
144
145 #ifdef WIN32
146     /* Replace argv[1..n] with unicode for Windows NT and above */
147     if( GetVersion() < 0x80000000 )
148     {
149         wchar_t **wargv, **wenvp;
150         int i,i_wargc;
151         int si = { 0 };
152         __wgetmainargs(&i_wargc, &wargv, &wenvp, 0, &si);
153
154         for( i = 0; i < i_wargc; i++ )
155         {
156             int len = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL);
157             if( len > 0 )
158             {
159                 if( len > 1 ) {
160                     char *utf8arg = (char *)malloc(len);
161                     if( NULL != utf8arg )
162                     {
163                         WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, utf8arg, len, NULL, NULL);
164                         ppsz_argv[i] = utf8arg;
165                     }
166                     else
167                     {
168                         /* failed!, quit */
169                         return 1;
170                     }
171                 }
172                 else
173                 {
174                     ppsz_argv[i] = strdup ("");
175                 }
176             }
177             else
178             {
179                 /* failed!, quit */
180                 return 1;
181             }
182         }
183     }
184     else
185 #endif
186     {
187         for (int i = 0; i < i_argc; i++)
188             if ((ppsz_argv[i] = FromLocale (ppsz_argv[i])) == NULL)
189                 return 1; // BOOM!
190     }
191
192     /* Initialize libvlc */
193     i_ret = VLC_Init( 0, i_argc, ppsz_argv );
194     if( i_ret < 0 )
195     {
196         VLC_Destroy( 0 );
197         return i_ret == VLC_EEXITSUCCESS ? 0 : -i_ret;
198     }
199
200     i_ret = VLC_AddIntf( 0, NULL, VLC_TRUE, VLC_TRUE );
201
202     /* Finish the threads */
203     VLC_CleanUp( 0 );
204
205     Kill ();
206
207     /* Destroy the libvlc structure */
208     VLC_Destroy( 0 );
209
210     for (int i = 0; i < i_argc; i++)
211         LocaleFree (ppsz_argv[i]);
212
213 #if !defined(WIN32) && !defined(UNDER_CE)
214     pthread_cancel (sigth);
215 # ifdef __APPLE__
216     /* In Mac OS X up to 10.4.8 sigwait (among others) is not a pthread
217      * cancellation point, so we throw a dummy quit signal to end
218      * sigwait() in the sigth thread */
219     pthread_kill (sigth, SIGQUIT);
220 # endif
221     pthread_join (sigth, NULL);
222 #endif
223
224     return -i_ret;
225 }
226
227 #if !defined(WIN32) && !defined(UNDER_CE)
228 /*****************************************************************************
229  * SigHandler: system signal handler
230  *****************************************************************************
231  * This thread receives all handled signals synchronously.
232  * It tries to end the program in a clean way.
233  *****************************************************************************/
234 static void *SigHandler (void *data)
235 {
236     const sigset_t *exitset = (sigset_t *)data;
237     sigset_t fullset;
238     time_t abort_time = 0;
239
240     pthread_sigmask (SIG_BLOCK, exitset, &fullset);
241
242     for (;;)
243     {
244         int i_signal, state;
245         (void)sigwait (&fullset, &i_signal);
246
247 #ifdef __APPLE__
248         /* In Mac OS X up to 10.4.8 sigwait (among others) is not a pthread
249          * cancellation point */
250         pthread_testcancel();
251 #endif
252
253         if (!sigismember (exitset, i_signal))
254             continue; /* Ignore "dummy" signals */
255
256         /* Once a signal has been trapped, the termination sequence will be
257          * armed and subsequent signals will be ignored to avoid sending
258          * signals to a libvlc structure having been destroyed */
259
260         pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
261         if (abort_time == 0)
262         {
263             time (&abort_time);
264             abort_time += 2;
265
266             fprintf (stderr, "signal %d received, terminating vlc - do it "
267                             "again in case it gets stuck\n", i_signal);
268
269             /* Acknowledge the signal received */
270             Kill ();
271         }
272         else
273         if (time (NULL) <= abort_time)
274         {
275             /* If user asks again more than 2 seconds later, die badly */
276             pthread_sigmask (SIG_UNBLOCK, exitset, NULL);
277             fprintf (stderr, "user insisted too much, dying badly\n");
278             abort ();
279         }
280         pthread_setcancelstate (state, NULL);
281     }
282     /* Never reached */
283 }
284
285
286 static void KillOnce (void)
287 {
288     VLC_Die (0);
289 }
290
291 static void Kill (void)
292 {
293     static pthread_once_t once = PTHREAD_ONCE_INIT;
294     pthread_once (&once, KillOnce);
295 }
296
297 #endif
298
299 #if defined(UNDER_CE)
300 #   if defined( _MSC_VER ) && defined( UNDER_CE )
301 #       include "vlc_common.h"
302 #   endif
303 /*****************************************************************************
304  * WinMain: parse command line, start interface and spawn threads. (WinCE only)
305  *****************************************************************************/
306 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
307                     LPTSTR lpCmdLine, int nCmdShow )
308 {
309     char **argv, psz_cmdline[MAX_PATH];
310     int argc, i_ret;
311
312     WideCharToMultiByte( CP_ACP, 0, lpCmdLine, -1,
313                          psz_cmdline, MAX_PATH, NULL, NULL );
314
315     argv = vlc_parse_cmdline( psz_cmdline, &argc );
316     argv = realloc( argv, (argc + 1) * sizeof(char *) );
317     if( !argv ) return -1;
318
319     if( argc ) memmove( argv + 1, argv, argc * sizeof(char *) );
320     argv[0] = ""; /* Fake program path */
321
322     i_ret = main( argc + 1, argv );
323
324     /* No need to free the argv memory */
325     return i_ret;
326 }
327 #endif