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