]> git.sesse.net Git - vlc/blob - src/vlc.c
c75c2f377ec7e66d9ae5b15da54060e980bdeac9
[vlc] / src / vlc.c
1 /*****************************************************************************
2  * vlc.c: the VLC player
3  *****************************************************************************
4  * Copyright (C) 1998-2008 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 # include <signal.h>
43 # include <time.h>
44 # include <pthread.h>
45
46 static void *SigHandler (void *set);
47
48 /*****************************************************************************
49  * main: parse command line, start interface and spawn threads.
50  *****************************************************************************/
51 int main( int i_argc, const char *ppsz_argv[] )
52 {
53     int i_ret;
54
55     setlocale (LC_ALL, "");
56
57 #ifndef __APPLE__
58     /* This clutters OSX GUI error logs */
59     fprintf( stderr, "VLC media player %s\n", libvlc_get_version() );
60 #endif
61
62 #ifdef HAVE_PUTENV
63 #   ifndef NDEBUG
64     /* Activate malloc checking routines to detect heap corruptions. */
65     putenv( (char*)"MALLOC_CHECK_=2" );
66 #       ifdef __APPLE__
67     putenv( (char*)"MallocErrorAbort=crash_my_baby_crash" );
68 #       endif
69
70     /* Disable the ugly Gnome crash dialog so that we properly segfault */
71     putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
72 #   endif
73 #endif
74
75 #if defined (HAVE_GETEUID) && !defined (SYS_BEOS)
76     /* FIXME: rootwrap (); */
77 #endif
78
79     /* Synchronously intercepted POSIX signals.
80      *
81      * In a threaded program such as VLC, the only sane way to handle signals
82      * is to block them in all thread but one - this is the only way to
83      * predict which thread will receive them. If any piece of code depends
84      * on delivery of one of this signal it is intrinsically not thread-safe
85      * and MUST NOT be used in VLC, whether we like it or not.
86      * There is only one exception: if the signal is raised with
87      * pthread_kill() - we do not use this in LibVLC but some pthread
88      * implementations use them internally. You should really use conditions
89      * for thread synchronization anyway.
90      *
91      * Signal that request a clean shutdown, and force an unclean shutdown
92      * if they are triggered again 2+ seconds later.
93      * We have to handle SIGTERM cleanly because of daemon mode.
94      * Note that we set the signals after the vlc_create call. */
95     static const int exitsigs[] = { SIGINT, SIGHUP, SIGQUIT, SIGTERM };
96     /* Signals that cause a no-op:
97      * - SIGALRM should not happen, but lets stay on the safe side.
98      * - SIGPIPE might happen with sockets and would crash VLC. It MUST be
99      *   blocked by any LibVLC-dependent application, in addition to VLC.
100      * - SIGCHLD is comes after exec*() (such as httpd CGI support) and must
101      *   be dequeued to cleanup zombie processes.
102      */
103     static const int dummysigs[] = { SIGALRM, SIGPIPE, SIGCHLD };
104
105     sigset_t set;
106     pthread_t sigth;
107
108     sigemptyset (&set);
109     for (unsigned i = 0; i < sizeof (exitsigs) / sizeof (exitsigs[0]); i++)
110         sigaddset (&set, exitsigs[i]);
111     for (unsigned i = 0; i < sizeof (dummysigs) / sizeof (dummysigs[0]); i++)
112         sigaddset (&set, dummysigs[i]);
113
114     /* Block all these signals */
115     pthread_sigmask (SIG_BLOCK, &set, NULL);
116
117     for (unsigned i = 0; i < sizeof (dummysigs) / sizeof (dummysigs[0]); i++)
118         sigdelset (&set, dummysigs[i]);
119
120     pthread_create (&sigth, NULL, SigHandler, &set);
121
122     /* Note that FromLocale() can be used before libvlc is initialized */
123     for (int i = 0; i < i_argc; i++)
124         if ((ppsz_argv[i] = FromLocale (ppsz_argv[i])) == NULL)
125             return 1; // BOOM!
126
127     libvlc_exception_t ex;
128     libvlc_exception_init (&ex);
129
130     /* Initialize libvlc */
131     int i_argc_real = i_argc ? i_argc - 1 : 0;
132     const char **ppsz_argv_real = i_argc ? &ppsz_argv[1] : ppsz_argv;
133     libvlc_instance_t *vlc = libvlc_new (i_argc_real, ppsz_argv_real, &ex);
134
135     if (vlc != NULL)
136     {
137         libvlc_run_interface (vlc, NULL, &ex);
138         libvlc_release (vlc);
139     }
140     i_ret = libvlc_exception_raised (&ex);
141     libvlc_exception_clear (&ex);
142
143     for (int i = 0; i < i_argc; i++)
144         LocaleFree (ppsz_argv[i]);
145
146     pthread_cancel (sigth);
147 # ifdef __APPLE__
148     /* In Mac OS X up to 10.4.8 sigwait (among others) is not a pthread
149      * cancellation point, so we throw a dummy quit signal to end
150      * sigwait() in the sigth thread */
151     pthread_kill (sigth, SIGQUIT);
152 # endif
153     pthread_join (sigth, NULL);
154
155     return i_ret;
156 }
157
158 /*****************************************************************************
159  * SigHandler: system signal handler
160  *****************************************************************************
161  * This thread receives all handled signals synchronously.
162  * It tries to end the program in a clean way.
163  *****************************************************************************/
164 static void *SigHandler (void *data)
165 {
166     const sigset_t *exitset = (sigset_t *)data;
167     sigset_t fullset;
168     time_t abort_time = 0;
169
170     pthread_sigmask (SIG_BLOCK, exitset, &fullset);
171
172     for (;;)
173     {
174         int i_signal, state;
175         if( sigwait (&fullset, &i_signal) != 0 )
176             continue;
177
178 #ifdef __APPLE__
179         /* In Mac OS X up to 10.4.8 sigwait (among others) is not a pthread
180          * cancellation point */
181         pthread_testcancel();
182 #endif
183
184         if (!sigismember (exitset, i_signal))
185             continue; /* Ignore "dummy" signals */
186
187         /* Once a signal has been trapped, the termination sequence will be
188          * armed and subsequent signals will be ignored to avoid sending
189          * signals to a libvlc structure having been destroyed */
190
191         pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
192         if (abort_time == 0 || time (NULL) > abort_time)
193         {
194             time (&abort_time);
195             abort_time += 2;
196
197             fprintf (stderr, "signal %d received, terminating vlc - do it "
198                             "again quickly in case it gets stuck\n", i_signal);
199             //VLC_Die( 0 );
200         }
201         else /* time (NULL) <= abort_time */
202         {
203             /* If user asks again more than 2 seconds later, die badly */
204             pthread_sigmask (SIG_UNBLOCK, exitset, NULL);
205             fprintf (stderr, "user insisted too much, dying badly\n");
206 #ifdef __APPLE__
207             /* On Mac OS X, use exit(-1) as it doesn't trigger
208              * backtrace generation, whereas abort() does.
209              * The backtrace generation trigger a Crash Dialog
210              * And takes way too much time, which is not what
211              * we want. */
212             exit (-1);
213 #else
214             abort ();
215 #endif
216         }
217         pthread_setcancelstate (state, NULL);
218     }
219     /* Never reached */
220 }