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