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