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