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