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