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