1 /*****************************************************************************
2 * vlc.c: the VLC player
3 *****************************************************************************
4 * Copyright (C) 1998-2008 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
39 extern void LocaleFree (const char *);
40 extern char *FromLocale (const char *);
46 static void *SigHandler (void *set);
48 /*****************************************************************************
49 * main: parse command line, start interface and spawn threads.
50 *****************************************************************************/
51 int main( int i_argc, const char *ppsz_argv[] )
55 setlocale (LC_ALL, "");
58 /* This clutters OSX GUI error logs */
59 fprintf( stderr, "VLC media player %s\n", libvlc_get_version() );
64 /* Activate malloc checking routines to detect heap corruptions. */
65 putenv( (char*)"MALLOC_CHECK_=2" );
67 putenv( (char*)"MallocErrorAbort=crash_my_baby_crash" );
70 /* Disable the ugly Gnome crash dialog so that we properly segfault */
71 putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
75 #if defined (HAVE_GETEUID) && !defined (SYS_BEOS)
76 /* FIXME: rootwrap (); */
79 /* Synchronously intercepted POSIX signals.
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.
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.
103 static const int dummysigs[] = { SIGALRM, SIGPIPE, SIGCHLD };
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]);
114 /* Block all these signals */
115 pthread_sigmask (SIG_BLOCK, &set, NULL);
117 for (unsigned i = 0; i < sizeof (dummysigs) / sizeof (dummysigs[0]); i++)
118 sigdelset (&set, dummysigs[i]);
120 pthread_create (&sigth, NULL, SigHandler, &set);
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)
127 libvlc_exception_t ex;
128 libvlc_exception_init (&ex);
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);
137 libvlc_run_interface (vlc, NULL, &ex);
138 libvlc_release (vlc);
140 i_ret = libvlc_exception_raised (&ex);
141 libvlc_exception_clear (&ex);
143 for (int i = 0; i < i_argc; i++)
144 LocaleFree (ppsz_argv[i]);
146 pthread_cancel (sigth);
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);
153 pthread_join (sigth, NULL);
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)
166 const sigset_t *exitset = (sigset_t *)data;
168 time_t abort_time = 0;
170 pthread_sigmask (SIG_BLOCK, exitset, &fullset);
175 if( sigwait (&fullset, &i_signal) != 0 )
179 /* In Mac OS X up to 10.4.8 sigwait (among others) is not a pthread
180 * cancellation point */
181 pthread_testcancel();
184 if (!sigismember (exitset, i_signal))
185 continue; /* Ignore "dummy" signals */
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 */
191 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
192 if (abort_time == 0 || time (NULL) > abort_time)
197 fprintf (stderr, "signal %d received, terminating vlc - do it "
198 "again quickly in case it gets stuck\n", i_signal);
201 else /* time (NULL) <= abort_time */
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");
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
217 pthread_setcancelstate (state, NULL);