]> git.sesse.net Git - vlc/blob - bin/vlc.c
Work around cleanup handler bug in kfile plugin
[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
46 #include <signal.h>
47 #include <time.h>
48 #include <pthread.h>
49 #include <unistd.h>
50 #include <dlfcn.h>
51
52 /*****************************************************************************
53  * main: parse command line, start interface and spawn threads.
54  *****************************************************************************/
55 int main( int i_argc, const char *ppsz_argv[] )
56 {
57 #ifdef __APPLE__
58     /* The so-called POSIX-compliant MacOS X is not. 
59      * SIGPIPE fires even when it is blocked in all threads! */
60     signal (SIGPIPE, SIG_IGN);
61 #endif
62
63 #ifndef ALLOW_RUN_AS_ROOT
64     if (geteuid () == 0)
65     {
66         fprintf (stderr, "VLC is not supposed to be run as root. Sorry.\n"
67         "If you need to use real-time priorities and/or privileged TCP ports\n"
68         "you can use %s-wrapper (make sure it is Set-UID root and\n"
69         "cannot be run by non-trusted users first).\n", ppsz_argv[0]);
70         return 1;
71     }
72 #endif
73
74     setlocale (LC_ALL, "");
75
76 #ifndef __APPLE__
77     /* This clutters OSX GUI error logs */
78     fprintf( stderr, "VLC media player %s (revision %s)\n",
79              libvlc_get_version(), libvlc_get_changeset() );
80 #endif
81
82 #ifdef HAVE_PUTENV
83 #   ifndef NDEBUG
84     /* Activate malloc checking routines to detect heap corruptions. */
85     putenv( (char*)"MALLOC_CHECK_=2" );
86
87     /* Disable the ugly Gnome crash dialog so that we properly segfault */
88     putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
89 #   endif
90 #endif
91
92     /* Synchronously intercepted POSIX signals.
93      *
94      * In a threaded program such as VLC, the only sane way to handle signals
95      * is to block them in all threads but one - this is the only way to
96      * predict which thread will receive them. If any piece of code depends
97      * on delivery of one of this signal it is intrinsically not thread-safe
98      * and MUST NOT be used in VLC, whether we like it or not.
99      * There is only one exception: if the signal is raised with
100      * pthread_kill() - we do not use this in LibVLC but some pthread
101      * implementations use them internally. You should really use conditions
102      * for thread synchronization anyway.
103      *
104      * Signal that request a clean shutdown, and force an unclean shutdown
105      * if they are triggered again 2+ seconds later.
106      * We have to handle SIGTERM cleanly because of daemon mode.
107      * Note that we set the signals after the vlc_create call. */
108     static const int sigs[] = {
109         SIGINT, SIGHUP, SIGQUIT, SIGTERM,
110     /* Signals that cause a no-op:
111      * - SIGPIPE might happen with sockets and would crash VLC. It MUST be
112      *   blocked by any LibVLC-dependent application, not just VLC.
113      * - SIGCHLD comes after exec*() (such as httpd CGI support) and must
114      *   be dequeued to cleanup zombie processes.
115      */
116         SIGPIPE, SIGCHLD
117     };
118
119     sigset_t set;
120     sigemptyset (&set);
121     for (unsigned i = 0; i < sizeof (sigs) / sizeof (sigs[0]); i++)
122         sigaddset (&set, sigs[i]);
123
124     /* Block all these signals */
125     pthread_sigmask (SIG_BLOCK, &set, NULL);
126     sigdelset (&set, SIGPIPE);
127     sigdelset (&set, SIGCHLD);
128
129     /* Note that FromLocale() can be used before libvlc is initialized */
130     const char *argv[i_argc + 3];
131     int argc = 0;
132
133     argv[argc++] = "--no-ignore-config";
134 #ifdef TOP_BUILDDIR
135     argv[argc++] = FromLocale ("--plugin-path="TOP_BUILDDIR"/modules");
136 #endif
137 #ifdef TOP_SRCDIR
138     argv[argc++] = FromLocale ("--data-path="TOP_SRCDIR"/share");
139 #endif
140
141     int i = 1;
142 #ifdef __APPLE__
143     /* When VLC.app is run by double clicking in Mac OS X, the 2nd arg
144      * is the PSN - process serial number (a unique PID-ish thingie)
145      * still ok for real Darwin & when run from command line
146      * for example -psn_0_9306113 */
147     if(i_argc >= 2 && !strncmp( ppsz_argv[1] , "-psn" , 4 ))
148         i = 2;
149 #endif
150     for (; i < i_argc; i++)
151         if ((argv[argc++] = FromLocale (ppsz_argv[i])) == NULL)
152             return 1; // BOOM!
153     argv[argc] = NULL;
154
155     /* Initialize libvlc */
156     libvlc_instance_t *vlc = libvlc_new (argc, argv);
157
158     if (vlc != NULL)
159     {
160         if (libvlc_add_intf (vlc, "signals"))
161             pthread_sigmask (SIG_UNBLOCK, &set, NULL);
162 #if !defined (HAVE_MAEMO)
163         libvlc_add_intf (vlc, "globalhotkeys,none");
164 #endif
165         if (libvlc_add_intf (vlc, NULL) == 0)
166         {
167             libvlc_playlist_play (vlc, -1, 0, NULL);
168             libvlc_wait (vlc);
169         }
170         libvlc_release (vlc);
171     }
172
173     for (int i = 1; i < argc; i++)
174         LocaleFree (argv[i]);
175
176 #ifdef RTLD_NOLOAD
177     /* Avoid crash in KIO scheduler cleanup. */
178     /* This is ugly, but we get way too many crash reports due to this. */
179     if (dlopen ("libkfilemodule.so", RTLD_LAZY|RTLD_LOCAL|RTLD_NOLOAD) != NULL)
180     {
181         fprintf (stderr, "KFile plugin present. Unclean shutdown!\n");
182         _exit (0);
183     }
184 #endif
185     return 0;
186 }