Fix missing lock in 440126cfb7322950d14f7029dd424c4f4bd41ad
[vlc/asuraparaju-public.git] / bin / vlc.c
blob2b287f163e177db89bdcdba86bd2326d1300b646
1 /*****************************************************************************
2 * vlc.c: the VLC player
3 *****************************************************************************
4 * Copyright (C) 1998-2008 the VideoLAN team
5 * $Id$
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
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.
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.
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 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
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>
43 #ifdef __APPLE__
44 #include <string.h>
45 #endif
48 /* Explicit HACK */
49 extern void LocaleFree (const char *);
50 extern char *FromLocale (const char *);
51 extern void vlc_enable_override (void);
53 #ifdef HAVE_MAEMO
54 static void dummy_handler (int signum)
56 (void) signum;
58 #endif
60 static bool signal_ignored (int signum)
62 struct sigaction sa;
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;
70 static void vlc_kill (void *data)
72 pthread_t *ps = data;
74 pthread_kill (*ps, SIGTERM);
78 /*****************************************************************************
79 * main: parse command line, start interface and spawn threads.
80 *****************************************************************************/
81 int main( int i_argc, const char *ppsz_argv[] )
83 /* The so-called POSIX-compliant MacOS X reportedly processes SIGPIPE even
84 * if it is blocked in all thread. Also some libraries want SIGPIPE blocked
85 * as they have no clue about signal masks.
86 * Note: this is NOT an excuse for not protecting against SIGPIPE. If
87 * LibVLC runs outside of VLC, we cannot rely on this code snippet. */
88 signal (SIGPIPE, SIG_IGN);
89 /* Restore default for SIGCHLD in case parent ignores it. */
90 signal (SIGCHLD, SIG_DFL);
92 #ifdef HAVE_SETENV
93 # ifndef NDEBUG
94 /* Activate malloc checking routines to detect heap corruptions. */
95 setenv ("MALLOC_CHECK_", "2", 1);
97 /* Disable the ugly Gnome crash dialog so that we properly segfault */
98 setenv ("GNOME_DISABLE_CRASH_DIALOG", "1", 1);
99 # endif
101 /* Clear the X.Org startup notification ID. Otherwise the UI might try to
102 * change the environment while the process is multi-threaded. That could
103 * crash. Screw you X.Org. Next time write a thread-safe specification. */
104 unsetenv ("DESKTOP_STARTUP_ID");
105 #endif
107 #ifndef ALLOW_RUN_AS_ROOT
108 if (geteuid () == 0)
110 fprintf (stderr, "VLC is not supposed to be run as root. Sorry.\n"
111 "If you need to use real-time priorities and/or privileged TCP ports\n"
112 "you can use %s-wrapper (make sure it is Set-UID root and\n"
113 "cannot be run by non-trusted users first).\n", ppsz_argv[0]);
114 return 1;
116 #endif
118 setlocale (LC_ALL, "");
120 #ifndef __APPLE__
121 /* This clutters OSX GUI error logs */
122 fprintf( stderr, "VLC media player %s (revision %s)\n",
123 libvlc_get_version(), libvlc_get_changeset() );
124 #endif
126 sigset_t set;
128 sigemptyset (&set);
129 /* Synchronously intercepted POSIX signals.
131 * In a threaded program such as VLC, the only sane way to handle signals
132 * is to block them in all threads but one - this is the only way to
133 * predict which thread will receive them. If any piece of code depends
134 * on delivery of one of this signal it is intrinsically not thread-safe
135 * and MUST NOT be used in VLC, whether we like it or not.
136 * There is only one exception: if the signal is raised with
137 * pthread_kill() - we do not use this in LibVLC but some pthread
138 * implementations use them internally. You should really use conditions
139 * for thread synchronization anyway.
141 * Signal that request a clean shutdown, and force an unclean shutdown
142 * if they are triggered again 2+ seconds later.
143 * We have to handle SIGTERM cleanly because of daemon mode. */
144 sigaddset (&set, SIGINT);
145 sigaddset (&set, SIGHUP);
146 sigaddset (&set, SIGQUIT);
147 sigaddset (&set, SIGTERM);
149 /* Signals that cause a no-op:
150 * - SIGPIPE might happen with sockets and would crash VLC. It MUST be
151 * blocked by any LibVLC-dependent application, not just VLC.
152 * - SIGCHLD comes after exec*() (such as httpd CGI support) and must
153 * be dequeued to cleanup zombie processes.
155 sigaddset (&set, SIGPIPE);
156 sigaddset (&set, SIGCHLD);
158 #ifdef HAVE_MAEMO
159 sigaddset (&set, SIGRTMIN);
161 struct sigaction act = { .sa_handler = dummy_handler, };
162 sigaction (SIGRTMIN, &act, NULL);
164 #endif
165 /* Block all these signals */
166 pthread_sigmask (SIG_BLOCK, &set, NULL);
168 /* Note that FromLocale() can be used before libvlc is initialized */
169 const char *argv[i_argc + 4];
170 int argc = 0;
172 argv[argc++] = "--no-ignore-config";
173 argv[argc++] = "--user-agent=\"VLC media player\"";
174 #ifdef TOP_BUILDDIR
175 argv[argc++] = FromLocale ("--plugin-path="TOP_BUILDDIR"/modules");
176 #endif
177 #ifdef TOP_SRCDIR
178 argv[argc++] = FromLocale ("--data-path="TOP_SRCDIR"/share");
179 #endif
181 int i = 1;
182 #ifdef __APPLE__
183 /* When VLC.app is run by double clicking in Mac OS X, the 2nd arg
184 * is the PSN - process serial number (a unique PID-ish thingie)
185 * still ok for real Darwin & when run from command line
186 * for example -psn_0_9306113 */
187 if(i_argc >= 2 && !strncmp( ppsz_argv[1] , "-psn" , 4 ))
188 i = 2;
189 #endif
190 for (; i < i_argc; i++)
191 if ((argv[argc++] = FromLocale (ppsz_argv[i])) == NULL)
192 return 1; // BOOM!
193 argv[argc] = NULL;
195 vlc_enable_override ();
197 /* Initialize libvlc */
198 libvlc_instance_t *vlc = libvlc_new (argc, argv);
199 if (vlc == NULL)
200 goto out;
202 #if !defined (HAVE_MAEMO)
203 libvlc_add_intf (vlc, "globalhotkeys,none");
204 #endif
205 if (libvlc_add_intf (vlc, NULL))
206 goto out;
208 libvlc_playlist_play (vlc, -1, 0, NULL);
210 /* Wait for a termination signal */
211 pthread_t self = pthread_self ();
212 libvlc_set_exit_handler (vlc, vlc_kill, &self);
214 if (signal_ignored (SIGHUP)) /* <- needed to handle nohup properly */
215 sigdelset (&set, SIGHUP);
216 sigdelset (&set, SIGPIPE);
218 int signum;
220 sigwait (&set, &signum);
221 while (signum == SIGCHLD);
223 /* Cleanup */
224 out:
225 if (vlc != NULL)
226 libvlc_release (vlc);
227 for (int i = 2; i < argc; i++)
228 LocaleFree (argv[i]);
230 #ifdef RTLD_NOLOAD
231 /* Avoid crash in KIO scheduler cleanup. */
232 /* This is ugly, but we get way too many crash reports due to this. */
233 if (dlopen ("libkio.so.5", RTLD_LAZY|RTLD_LOCAL|RTLD_NOLOAD) != NULL)
235 fprintf (stderr, "KIO present. Unclean shutdown!\n"
236 " (see http://bugs.kde.org/show_bug.cgi?id=234484 for details)\n");
237 _exit (0);
239 #endif
240 return 0;