]> git.sesse.net Git - vlc/blob - bin/vlc.c
034f0cae45d4af8ca1c839f8c86a38e249901270
[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
37 #ifdef __APPLE__
38 #include <string.h>
39 #endif
40
41
42 /* Explicit HACK */
43 extern void LocaleFree (const char *);
44 extern char *FromLocale (const char *);
45 extern void vlc_enable_override (void);
46
47 #include <signal.h>
48 #include <time.h>
49 #include <pthread.h>
50 #include <unistd.h>
51 #include <dlfcn.h>
52
53 #ifdef HAVE_MAEMO
54 static void dummy_handler (int signum)
55 {
56     (void) signum;
57 }
58 #endif
59
60 /*****************************************************************************
61  * main: parse command line, start interface and spawn threads.
62  *****************************************************************************/
63 int main( int i_argc, const char *ppsz_argv[] )
64 {
65     /* The so-called POSIX-compliant MacOS X reportedly processes SIGPIPE even
66      * if it is blocked in all thread. Also some libraries want SIGPIPE blocked
67      * as they have no clue about signal masks.
68      * Note: this is NOT an excuse for not protecting against SIGPIPE. If
69      * LibVLC runs outside of VLC, we cannot rely on this code snippet. */
70     signal (SIGPIPE, SIG_IGN);
71
72 #ifdef HAVE_PUTENV
73 # ifndef NDEBUG
74     /* Activate malloc checking routines to detect heap corruptions. */
75     putenv( (char*)"MALLOC_CHECK_=2" );
76
77     /* Disable the ugly Gnome crash dialog so that we properly segfault */
78     putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
79 # endif
80
81     /* Make Xlib hide visuals with an alphachannel. Ensure that Qt4 will not
82      * use the alpha channel for the embedded video window. */
83     putenv( (char *)"XLIB_SKIP_ARGB_VISUALS=1" );
84 #endif
85 #ifdef HAVE_SETENV
86     /* Clear the X.Org startup notification ID. Otherwise the UI might try to
87      * change the environment while the process is multi-threaded. That could
88      * crash. Screw you X.Org. Next time write a thread-safe specification. */
89     unsetenv ("DESKTOP_STARTUP_ID");
90 #endif
91
92 #ifndef ALLOW_RUN_AS_ROOT
93     if (geteuid () == 0)
94     {
95         fprintf (stderr, "VLC is not supposed to be run as root. Sorry.\n"
96         "If you need to use real-time priorities and/or privileged TCP ports\n"
97         "you can use %s-wrapper (make sure it is Set-UID root and\n"
98         "cannot be run by non-trusted users first).\n", ppsz_argv[0]);
99         return 1;
100     }
101 #endif
102
103     setlocale (LC_ALL, "");
104
105 #ifndef __APPLE__
106     /* This clutters OSX GUI error logs */
107     fprintf( stderr, "VLC media player %s (revision %s)\n",
108              libvlc_get_version(), libvlc_get_changeset() );
109 #endif
110
111     /* Synchronously intercepted POSIX signals.
112      *
113      * In a threaded program such as VLC, the only sane way to handle signals
114      * is to block them in all threads but one - this is the only way to
115      * predict which thread will receive them. If any piece of code depends
116      * on delivery of one of this signal it is intrinsically not thread-safe
117      * and MUST NOT be used in VLC, whether we like it or not.
118      * There is only one exception: if the signal is raised with
119      * pthread_kill() - we do not use this in LibVLC but some pthread
120      * implementations use them internally. You should really use conditions
121      * for thread synchronization anyway.
122      *
123      * Signal that request a clean shutdown, and force an unclean shutdown
124      * if they are triggered again 2+ seconds later.
125      * We have to handle SIGTERM cleanly because of daemon mode.
126      * Note that we set the signals after the vlc_create call. */
127     static const int sigs[] = {
128         SIGINT, SIGHUP, SIGQUIT, SIGTERM,
129     /* Signals that cause a no-op:
130      * - SIGPIPE might happen with sockets and would crash VLC. It MUST be
131      *   blocked by any LibVLC-dependent application, not just VLC.
132      * - SIGCHLD comes after exec*() (such as httpd CGI support) and must
133      *   be dequeued to cleanup zombie processes.
134      */
135         SIGPIPE, SIGCHLD
136     };
137
138     sigset_t set;
139     sigemptyset (&set);
140     for (unsigned i = 0; i < sizeof (sigs) / sizeof (sigs[0]); i++)
141         sigaddset (&set, sigs[i]);
142 #ifdef HAVE_MAEMO
143     sigaddset (&set, SIGRTMIN);
144     {
145         struct sigaction act = { .sa_handler = dummy_handler, };
146         sigaction (SIGRTMIN, &act, NULL);
147     }
148 #endif
149
150     /* Block all these signals */
151     pthread_sigmask (SIG_BLOCK, &set, NULL);
152     sigdelset (&set, SIGPIPE);
153     sigdelset (&set, SIGCHLD);
154
155     /* Note that FromLocale() can be used before libvlc is initialized */
156     const char *argv[i_argc + 3];
157     int argc = 0;
158
159     argv[argc++] = "--no-ignore-config";
160 #ifdef TOP_BUILDDIR
161     argv[argc++] = FromLocale ("--plugin-path="TOP_BUILDDIR"/modules");
162 #endif
163 #ifdef TOP_SRCDIR
164     argv[argc++] = FromLocale ("--data-path="TOP_SRCDIR"/share");
165 #endif
166
167     int i = 1;
168 #ifdef __APPLE__
169     /* When VLC.app is run by double clicking in Mac OS X, the 2nd arg
170      * is the PSN - process serial number (a unique PID-ish thingie)
171      * still ok for real Darwin & when run from command line
172      * for example -psn_0_9306113 */
173     if(i_argc >= 2 && !strncmp( ppsz_argv[1] , "-psn" , 4 ))
174         i = 2;
175 #endif
176     for (; i < i_argc; i++)
177         if ((argv[argc++] = FromLocale (ppsz_argv[i])) == NULL)
178             return 1; // BOOM!
179     argv[argc] = NULL;
180
181     vlc_enable_override ();
182
183     /* Initialize libvlc */
184     libvlc_instance_t *vlc = libvlc_new (argc, argv);
185
186     if (vlc != NULL)
187     {
188         if (libvlc_add_intf (vlc, "signals"))
189             pthread_sigmask (SIG_UNBLOCK, &set, NULL);
190 #if !defined (HAVE_MAEMO)
191         libvlc_add_intf (vlc, "globalhotkeys,none");
192 #endif
193         if (libvlc_add_intf (vlc, NULL) == 0)
194         {
195             libvlc_playlist_play (vlc, -1, 0, NULL);
196             libvlc_wait (vlc);
197         }
198         libvlc_release (vlc);
199     }
200
201     for (int i = 1; i < argc; i++)
202         LocaleFree (argv[i]);
203
204 #ifdef RTLD_NOLOAD
205     /* Avoid crash in KIO scheduler cleanup. */
206     /* This is ugly, but we get way too many crash reports due to this. */
207     if (dlopen ("libkio.so.5", RTLD_LAZY|RTLD_LOCAL|RTLD_NOLOAD) != NULL)
208     {
209         fprintf (stderr, "KIO present. Unclean shutdown!\n"
210            " (see http://bugs.kde.org/show_bug.cgi?id=234484 for details)\n");
211         _exit (0);
212     }
213 #endif
214     return 0;
215 }