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[] )
54 #ifndef ALLOW_RUN_AS_ROOT
57 fprintf (stderr, "VLC is not supposed to be run as root. Sorry.\n"
58 "If you need to use real-time priorities and/or privileged TCP ports\n"
59 "you can use %s-wrapper (make sure it is Set-UID root first and\n"
60 "cannot be run by non-trusted users first).\n", ppsz_argv[0]);
65 setlocale (LC_ALL, "");
68 /* This clutters OSX GUI error logs */
69 fprintf( stderr, "VLC media player %s\n", libvlc_get_version() );
74 /* Activate malloc checking routines to detect heap corruptions. */
75 putenv( (char*)"MALLOC_CHECK_=2" );
77 putenv( (char*)"MallocErrorAbort=crash_my_baby_crash" );
80 /* Disable the ugly Gnome crash dialog so that we properly segfault */
81 putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
85 /* Synchronously intercepted POSIX signals.
87 * In a threaded program such as VLC, the only sane way to handle signals
88 * is to block them in all thread but one - this is the only way to
89 * predict which thread will receive them. If any piece of code depends
90 * on delivery of one of this signal it is intrinsically not thread-safe
91 * and MUST NOT be used in VLC, whether we like it or not.
92 * There is only one exception: if the signal is raised with
93 * pthread_kill() - we do not use this in LibVLC but some pthread
94 * implementations use them internally. You should really use conditions
95 * for thread synchronization anyway.
97 * Signal that request a clean shutdown, and force an unclean shutdown
98 * if they are triggered again 2+ seconds later.
99 * We have to handle SIGTERM cleanly because of daemon mode.
100 * Note that we set the signals after the vlc_create call. */
101 static const int sigs[] = {
102 SIGINT, SIGHUP, SIGQUIT, SIGTERM,
103 /* Signals that cause a no-op:
104 * - SIGPIPE might happen with sockets and would crash VLC. It MUST be
105 * blocked by any LibVLC-dependent application, not just VLC.
106 * - SIGCHLD comes after exec*() (such as httpd CGI support) and must
107 * be dequeued to cleanup zombie processes.
114 for (unsigned i = 0; i < sizeof (sigs) / sizeof (sigs[0]); i++)
115 sigaddset (&set, sigs[i]);
117 /* Block all these signals */
118 pthread_sigmask (SIG_BLOCK, &set, NULL);
119 sigdelset (&set, SIGPIPE);
120 sigdelset (&set, SIGCHLD);
122 /* Note that FromLocale() can be used before libvlc is initialized */
123 for (int i = 0; i < i_argc; i++)
124 if ((ppsz_argv[i] = FromLocale (ppsz_argv[i])) == NULL)
127 libvlc_exception_t ex, dummy;
128 libvlc_exception_init (&ex);
129 libvlc_exception_init (&dummy);
131 /* Initialize libvlc */
132 int i_argc_real = i_argc ? i_argc - 1 : 0;
133 const char **ppsz_argv_real = i_argc ? &ppsz_argv[1] : ppsz_argv;
134 libvlc_instance_t *vlc = libvlc_new (i_argc_real, ppsz_argv_real, &ex);
138 libvlc_add_intf (vlc, "signals", &ex);
139 if (libvlc_exception_raised (&ex))
141 libvlc_exception_clear (&ex);
142 pthread_sigmask (SIG_UNBLOCK, &set, NULL);
144 libvlc_add_intf (vlc, NULL, &ex);
145 libvlc_playlist_play (vlc, -1, 0, NULL, &dummy);
147 libvlc_release (vlc);
149 i_ret = libvlc_exception_raised (&ex);
151 fprintf( stderr, "%s\n", libvlc_exception_get_message( &ex));
153 libvlc_exception_clear (&ex);
154 libvlc_exception_clear (&dummy);
156 for (int i = 0; i < i_argc; i++)
157 LocaleFree (ppsz_argv[i]);