]> git.sesse.net Git - vlc/blob - bin/vlc.c
Call XInitThreads early enough
[vlc] / bin / 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 #ifdef HAVE_X11_XLIB_H
37 # include <X11/Xlib.h>
38 #endif
39
40 /* Explicit HACK */
41 extern void LocaleFree (const char *);
42 extern char *FromLocale (const char *);
43
44 #include <signal.h>
45 #include <time.h>
46 #include <pthread.h>
47 #include <unistd.h>
48
49 /*****************************************************************************
50  * main: parse command line, start interface and spawn threads.
51  *****************************************************************************/
52 int main( int i_argc, const char *ppsz_argv[] )
53 {
54     int i_ret;
55
56 #ifndef ALLOW_RUN_AS_ROOT
57     if (geteuid () == 0)
58     {
59         fprintf (stderr, "VLC is not supposed to be run as root. Sorry.\n"
60         "If you need to use real-time priorities and/or privileged TCP ports\n"
61         "you can use %s-wrapper (make sure it is Set-UID root and\n"
62         "cannot be run by non-trusted users first).\n", ppsz_argv[0]);
63         return 1;
64     }
65 #endif
66
67     setlocale (LC_ALL, "");
68
69 #ifndef __APPLE__
70     /* This clutters OSX GUI error logs */
71     fprintf( stderr, "VLC media player %s\n", libvlc_get_version() );
72 #endif
73
74 #ifdef HAVE_PUTENV
75 #   ifndef NDEBUG
76     /* Activate malloc checking routines to detect heap corruptions. */
77     putenv( (char*)"MALLOC_CHECK_=2" );
78 #       ifdef __APPLE__
79     putenv( (char*)"MallocErrorAbort=crash_my_baby_crash" );
80 #       endif
81
82     /* Disable the ugly Gnome crash dialog so that we properly segfault */
83     putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
84 #   endif
85 #endif
86
87 #ifdef HAVE_X11_XLIB_H
88     /* Initialize Xlib thread support. */
89     if (!XInitThreads ())
90     {
91         fputs ("VLC requires a thread-safe Xlib. Sorry.\n", stderr);
92         return 1;
93     }
94 #endif
95
96     /* Synchronously intercepted POSIX signals.
97      *
98      * In a threaded program such as VLC, the only sane way to handle signals
99      * is to block them in all thread but one - this is the only way to
100      * predict which thread will receive them. If any piece of code depends
101      * on delivery of one of this signal it is intrinsically not thread-safe
102      * and MUST NOT be used in VLC, whether we like it or not.
103      * There is only one exception: if the signal is raised with
104      * pthread_kill() - we do not use this in LibVLC but some pthread
105      * implementations use them internally. You should really use conditions
106      * for thread synchronization anyway.
107      *
108      * Signal that request a clean shutdown, and force an unclean shutdown
109      * if they are triggered again 2+ seconds later.
110      * We have to handle SIGTERM cleanly because of daemon mode.
111      * Note that we set the signals after the vlc_create call. */
112     static const int sigs[] = {
113         SIGINT, SIGHUP, SIGQUIT, SIGTERM,
114     /* Signals that cause a no-op:
115      * - SIGPIPE might happen with sockets and would crash VLC. It MUST be
116      *   blocked by any LibVLC-dependent application, not just VLC.
117      * - SIGCHLD comes after exec*() (such as httpd CGI support) and must
118      *   be dequeued to cleanup zombie processes.
119      */
120         SIGPIPE, SIGCHLD
121     };
122
123     sigset_t set;
124     sigemptyset (&set);
125     for (unsigned i = 0; i < sizeof (sigs) / sizeof (sigs[0]); i++)
126         sigaddset (&set, sigs[i]);
127
128     /* Block all these signals */
129     pthread_sigmask (SIG_BLOCK, &set, NULL);
130     sigdelset (&set, SIGPIPE);
131     sigdelset (&set, SIGCHLD);
132
133     /* Note that FromLocale() can be used before libvlc is initialized */
134     const char *argv[i_argc + 3];
135     int argc = 0;
136
137 #ifdef TOP_BUILDDIR
138     argv[argc++] = FromLocale ("--plugin-path="TOP_BUILDDIR"/modules");
139 #endif
140 #ifdef TOP_SRCDIR
141 # ifdef ENABLE_HTTPD
142     argv[argc++] = FromLocale ("--http-src="TOP_SRCDIR"/share/http");
143 # endif
144 #endif
145
146     for (int i = 1; i < i_argc; i++)
147         if ((argv[argc++] = FromLocale (ppsz_argv[i])) == NULL)
148             return 1; // BOOM!
149
150     libvlc_exception_t ex, dummy;
151     libvlc_exception_init (&ex);
152     libvlc_exception_init (&dummy);
153
154     /* Initialize libvlc */
155     libvlc_instance_t *vlc = libvlc_new (argc, argv, &ex);
156
157     if (vlc != NULL)
158     {
159         libvlc_add_intf (vlc, "signals", &ex);
160         if (libvlc_exception_raised (&ex))
161         {
162             libvlc_exception_clear (&ex);
163             pthread_sigmask (SIG_UNBLOCK, &set, NULL);
164         }
165         libvlc_add_intf (vlc, NULL, &ex);
166         libvlc_playlist_play (vlc, -1, 0, NULL, &dummy);
167         libvlc_wait (vlc);
168         libvlc_release (vlc);
169     }
170     i_ret = libvlc_exception_raised (&ex);
171     if( i_ret )
172         fprintf( stderr, "%s\n", libvlc_exception_get_message( &ex));
173
174     libvlc_exception_clear (&ex);
175     libvlc_exception_clear (&dummy);
176
177     for (int i = 0; i < argc; i++)
178         LocaleFree (argv[i]);
179
180     return i_ret;
181 }