1 /*****************************************************************************
2 * vlc.c: the VLC player
3 *****************************************************************************
4 * Copyright (C) 1998-2008 the VideoLAN team
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 *****************************************************************************/
39 extern void LocaleFree (const char *);
40 extern char *FromLocale (const char *);
47 /*****************************************************************************
48 * main: parse command line, start interface and spawn threads.
49 *****************************************************************************/
50 int main( int i_argc, const char *ppsz_argv[] )
53 /* The so-called POSIX-compliant MacOS X is not.
54 * SIGPIPE fires even when it is blocked in all threads! */
55 signal (SIGPIPE, SIG_IGN);
58 #ifndef ALLOW_RUN_AS_ROOT
61 fprintf (stderr, "VLC is not supposed to be run as root. Sorry.\n"
62 "If you need to use real-time priorities and/or privileged TCP ports\n"
63 "you can use %s-wrapper (make sure it is Set-UID root and\n"
64 "cannot be run by non-trusted users first).\n", ppsz_argv[0]);
69 setlocale (LC_ALL, "");
72 /* This clutters OSX GUI error logs */
73 fprintf( stderr, "VLC media player %s\n", libvlc_get_version() );
78 /* Activate malloc checking routines to detect heap corruptions. */
79 putenv( (char*)"MALLOC_CHECK_=2" );
81 /* Disable the ugly Gnome crash dialog so that we properly segfault */
82 putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
86 /* Synchronously intercepted POSIX signals.
88 * In a threaded program such as VLC, the only sane way to handle signals
89 * is to block them in all thread but one - this is the only way to
90 * predict which thread will receive them. If any piece of code depends
91 * on delivery of one of this signal it is intrinsically not thread-safe
92 * and MUST NOT be used in VLC, whether we like it or not.
93 * There is only one exception: if the signal is raised with
94 * pthread_kill() - we do not use this in LibVLC but some pthread
95 * implementations use them internally. You should really use conditions
96 * for thread synchronization anyway.
98 * Signal that request a clean shutdown, and force an unclean shutdown
99 * if they are triggered again 2+ seconds later.
100 * We have to handle SIGTERM cleanly because of daemon mode.
101 * Note that we set the signals after the vlc_create call. */
102 static const int sigs[] = {
103 SIGINT, SIGHUP, SIGQUIT, SIGTERM,
104 /* Signals that cause a no-op:
105 * - SIGPIPE might happen with sockets and would crash VLC. It MUST be
106 * blocked by any LibVLC-dependent application, not just VLC.
107 * - SIGCHLD comes after exec*() (such as httpd CGI support) and must
108 * be dequeued to cleanup zombie processes.
115 for (unsigned i = 0; i < sizeof (sigs) / sizeof (sigs[0]); i++)
116 sigaddset (&set, sigs[i]);
118 /* Block all these signals */
119 pthread_sigmask (SIG_BLOCK, &set, NULL);
120 sigdelset (&set, SIGPIPE);
121 sigdelset (&set, SIGCHLD);
123 /* Note that FromLocale() can be used before libvlc is initialized */
124 const char *argv[i_argc + 3];
127 argv[argc++] = "--no-ignore-config";
129 argv[argc++] = FromLocale ("--plugin-path="TOP_BUILDDIR"/modules");
133 argv[argc++] = FromLocale ("--http-src="TOP_SRCDIR"/share/http");
137 for (int i = 1; i < i_argc; i++)
138 if ((argv[argc++] = FromLocale (ppsz_argv[i])) == NULL)
142 libvlc_exception_t ex, dummy;
143 libvlc_exception_init (&ex);
144 libvlc_exception_init (&dummy);
146 /* Initialize libvlc */
147 libvlc_instance_t *vlc = libvlc_new (argc, argv, &ex);
151 libvlc_add_intf (vlc, "signals", &ex);
152 if (libvlc_exception_raised (&ex))
154 libvlc_exception_clear (&ex);
155 pthread_sigmask (SIG_UNBLOCK, &set, NULL);
157 #if !defined (HAVE_MAEMO)
158 libvlc_add_intf (vlc, "globalhotkeys,none", &ex);
160 libvlc_exception_clear (&ex);
161 libvlc_add_intf (vlc, NULL, &ex);
162 libvlc_playlist_play (vlc, -1, 0, NULL, &dummy);
165 if (libvlc_exception_raised (&ex))
166 fprintf( stderr, "%s\n", libvlc_errmsg() );
167 libvlc_release (vlc);
170 for (int i = 1; i < argc; i++)
171 LocaleFree (argv[i]);
173 return vlc == NULL || libvlc_exception_raised (&ex);