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