]> git.sesse.net Git - vlc/blob - bin/vlc.c
Win32: add a --no-crashdump option
[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
38 /* Explicit HACK */
39 extern void LocaleFree (const char *);
40 extern char *FromLocale (const char *);
41
42 #include <signal.h>
43 #include <time.h>
44 #include <pthread.h>
45 #include <unistd.h>
46
47 /*****************************************************************************
48  * main: parse command line, start interface and spawn threads.
49  *****************************************************************************/
50 int main( int i_argc, const char *ppsz_argv[] )
51 {
52     int i_ret;
53
54 #ifndef ALLOW_RUN_AS_ROOT
55     if (geteuid () == 0)
56     {
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 and\n"
60         "cannot be run by non-trusted users first).\n", ppsz_argv[0]);
61         return 1;
62     }
63 #endif
64
65     setlocale (LC_ALL, "");
66
67 #ifndef __APPLE__
68     /* This clutters OSX GUI error logs */
69     fprintf( stderr, "VLC media player %s\n", libvlc_get_version() );
70 #endif
71
72 #ifdef HAVE_PUTENV
73 #   ifndef NDEBUG
74     /* Activate malloc checking routines to detect heap corruptions. */
75     putenv( (char*)"MALLOC_CHECK_=2" );
76 #       ifdef __APPLE__
77     putenv( (char*)"MallocErrorAbort=crash_my_baby_crash" );
78 #       endif
79
80     /* Disable the ugly Gnome crash dialog so that we properly segfault */
81     putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
82 #   endif
83 #endif
84
85     /* Synchronously intercepted POSIX signals.
86      *
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.
96      *
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.
108      */
109         SIGPIPE, SIGCHLD
110     };
111
112     sigset_t set;
113     sigemptyset (&set);
114     for (unsigned i = 0; i < sizeof (sigs) / sizeof (sigs[0]); i++)
115         sigaddset (&set, sigs[i]);
116
117     /* Block all these signals */
118     pthread_sigmask (SIG_BLOCK, &set, NULL);
119     sigdelset (&set, SIGPIPE);
120     sigdelset (&set, SIGCHLD);
121
122     /* Note that FromLocale() can be used before libvlc is initialized */
123     const char *argv[i_argc + 3];
124     int argc = 0;
125
126 #ifdef TOP_BUILDDIR
127     argv[argc++] = FromLocale ("--plugin-path="TOP_BUILDDIR"/modules");
128 #endif
129 #ifdef TOP_SRCDIR
130 # ifdef ENABLE_HTTPD
131     argv[argc++] = FromLocale ("--http-src="TOP_SRCDIR"/share/http");
132 # endif
133 #endif
134
135     for (int i = 1; i < i_argc; i++)
136         if ((argv[argc++] = FromLocale (ppsz_argv[i])) == NULL)
137             return 1; // BOOM!
138
139     libvlc_exception_t ex, dummy;
140     libvlc_exception_init (&ex);
141     libvlc_exception_init (&dummy);
142
143     /* Initialize libvlc */
144     libvlc_instance_t *vlc = libvlc_new (argc, argv, &ex);
145
146     if (vlc != NULL)
147     {
148         libvlc_add_intf (vlc, "signals", &ex);
149         if (libvlc_exception_raised (&ex))
150         {
151             libvlc_exception_clear (&ex);
152             pthread_sigmask (SIG_UNBLOCK, &set, NULL);
153         }
154         libvlc_add_intf (vlc, "globalhotkeys,none", &ex);
155         libvlc_add_intf (vlc, NULL, &ex);
156         libvlc_playlist_play (vlc, -1, 0, NULL, &dummy);
157         libvlc_wait (vlc);
158         libvlc_release (vlc);
159     }
160     i_ret = libvlc_exception_raised (&ex);
161     if( i_ret )
162         fprintf( stderr, "%s\n", libvlc_exception_get_message( &ex));
163
164     libvlc_exception_clear (&ex);
165     libvlc_exception_clear (&dummy);
166
167     for (int i = 0; i < argc; i++)
168         LocaleFree (argv[i]);
169
170     return i_ret;
171 }