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