]> git.sesse.net Git - vlc/blob - bin/vlc.c
Qt is GPLv2+
[vlc] / bin / vlc.c
1 /*****************************************************************************
2  * vlc.c: the VLC player
3  *****************************************************************************
4  * Copyright (C) 1998-2013 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 <stdbool.h>
36 #include <string.h>
37 #include <locale.h>
38 #include <signal.h>
39 #ifdef HAVE_PTHREAD_H
40 # include <pthread.h>
41 #endif
42 #include <unistd.h>
43
44 #ifdef __OS2__
45 # include <iconv.h>
46
47 # define pthread_t      int
48 # define pthread_self() _gettid()
49
50 static char *FromSystem(const void *str)
51 {
52     iconv_t handle = iconv_open ("UTF-8", "");
53     if (handle == (iconv_t)(-1))
54         return NULL;
55
56     size_t str_len = strlen (str);
57     char *out = NULL;
58     for (unsigned mul = 4; mul < 8; mul++)
59     {
60         size_t in_size = str_len;
61         const char *in = str;
62         size_t out_max = mul * str_len;
63         char *tmp = out = malloc (1 + out_max);
64         if (!out)
65             break;
66
67         if (iconv (handle, &in, &in_size, &tmp, &out_max) != (size_t)(-1)) {
68             *tmp = '\0';
69             break;
70         }
71         free(out);
72         out = NULL;
73
74         if (errno != E2BIG)
75             break;
76     }
77     iconv_close(handle);
78     return out;
79 }
80 #endif
81
82 extern void vlc_enable_override (void);
83
84 static bool signal_ignored (int signum)
85 {
86     struct sigaction sa;
87
88     if (sigaction (signum, NULL, &sa))
89         return false;
90     return ((sa.sa_flags & SA_SIGINFO)
91             ? (void *)sa.sa_sigaction : (void *)sa.sa_handler) == SIG_IGN;
92 }
93
94 static void vlc_kill (void *data)
95 {
96 #ifndef __OS2__
97     pthread_t *ps = data;
98
99     pthread_kill (*ps, SIGTERM);
100 #else
101     // send a signal to the main thread
102     kill (getpid(), SIGTERM);
103 #endif
104 }
105
106 static void exit_timeout (int signum)
107 {
108     (void) signum;
109     signal (SIGINT, SIG_DFL);
110 }
111
112 /*****************************************************************************
113  * main: parse command line, start interface and spawn threads.
114  *****************************************************************************/
115 int main( int i_argc, const char *ppsz_argv[] )
116 {
117     /* The so-called POSIX-compliant MacOS X reportedly processes SIGPIPE even
118      * if it is blocked in all thread.
119      * Note: this is NOT an excuse for not protecting against SIGPIPE. If
120      * LibVLC runs outside of VLC, we cannot rely on this code snippet. */
121     signal (SIGPIPE, SIG_IGN);
122     /* Restore SIGCHLD in case our parent process ignores it. */
123     signal (SIGCHLD, SIG_DFL);
124
125 #ifndef NDEBUG
126     /* Activate malloc checking routines to detect heap corruptions. */
127     setenv ("MALLOC_CHECK_", "2", 1);
128
129     /* Disable the ugly Gnome crash dialog so that we properly segfault */
130     setenv ("GNOME_DISABLE_CRASH_DIALOG", "1", 1);
131 #endif
132
133 #ifdef TOP_BUILDDIR
134     setenv ("VLC_PLUGIN_PATH", TOP_BUILDDIR"/modules", 1);
135     setenv ("VLC_DATA_PATH", TOP_SRCDIR"/share", 1);
136 #endif
137
138     /* Clear the X.Org startup notification ID. Otherwise the UI might try to
139      * change the environment while the process is multi-threaded. That could
140      * crash. Screw you X.Org. Next time write a thread-safe specification. */
141     unsetenv ("DESKTOP_STARTUP_ID");
142
143 #ifndef ALLOW_RUN_AS_ROOT
144     if (geteuid () == 0)
145     {
146         fprintf (stderr, "VLC is not supposed to be run as root. Sorry.\n"
147         "If you need to use real-time priorities and/or privileged TCP ports\n"
148         "you can use %s-wrapper (make sure it is Set-UID root and\n"
149         "cannot be run by non-trusted users first).\n", ppsz_argv[0]);
150         return 1;
151     }
152 #endif
153
154     setlocale (LC_ALL, "");
155
156     if (isatty (STDERR_FILENO))
157         /* This message clutters error logs. It is printed only on a TTY.
158          * Fortunately, LibVLC prints version info with -vv anyway. */
159         fprintf (stderr, "VLC media player %s (revision %s)\n",
160                  libvlc_get_version(), libvlc_get_changeset());
161
162     sigset_t set;
163
164     sigemptyset (&set);
165     /* VLC uses sigwait() to dequeue interesting signals.
166      * For this to work, those signals must be blocked in all threads,
167      * including the thread calling sigwait() (see the man page for details).
168      *
169      * There are two advantages to sigwait() over traditional signal handlers:
170      *  - delivery is synchronous: no need to worry about async-safety,
171      *  - EINTR is not generated: other threads need not handle that error.
172      * That being said, some LibVLC programs do not use sigwait(). Therefore
173      * EINTR must still be handled cleanly, notably from poll() calls.
174      *
175      * Signals that request a clean shutdown, and force an unclean shutdown
176      * if they are triggered again 2+ seconds later.
177      * We have to handle SIGTERM cleanly because of daemon mode. */
178     sigaddset (&set, SIGINT);
179     sigaddset (&set, SIGHUP);
180     sigaddset (&set, SIGQUIT);
181     sigaddset (&set, SIGTERM);
182
183     /* SIGPIPE can happen and would crash the process. On modern systems,
184      * the MSG_NOSIGNAL flag protects socket write operations against SIGPIPE.
185      * But we still need to block SIGPIPE when:
186      *  - writing to pipes,
187      *  - using write() instead of send() for code not specific to sockets.
188      * LibVLC code assumes that SIGPIPE is blocked. Other LibVLC applications
189      * shall block it (or handle it somehow) too.
190      */
191     sigaddset (&set, SIGPIPE);
192
193     /* SIGCHLD must be dequeued to clean up zombie child processes.
194      * Furthermore the handler must not be set to SIG_IGN (see above).
195      * We cannot pragmatically handle EINTR, short reads and short writes
196      * in every code paths (including underlying libraries). So we just
197      * block SIGCHLD in all threads, and dequeue it below. */
198     sigaddset (&set, SIGCHLD);
199
200     /* Block all these signals */
201     pthread_t self = pthread_self ();
202     pthread_sigmask (SIG_SETMASK, &set, NULL);
203
204     const char *argv[i_argc + 2];
205     int argc = 0;
206
207     argv[argc++] = "--no-ignore-config";
208     argv[argc++] = "--media-library";
209     ppsz_argv++; i_argc--; /* skip executable path */
210
211 #ifdef __OS2__
212     for (int i = 0; i < i_argc; i++)
213         if ((argv[argc++] = FromSystem (ppsz_argv[i])) == NULL)
214         {
215             fprintf (stderr, "Converting '%s' to UTF-8 failed.\n",
216                      ppsz_argv[i]);
217             return 1;
218         }
219 #else
220     memcpy (argv + argc, ppsz_argv, i_argc * sizeof (*argv));
221     argc += i_argc;
222 #endif
223     argv[argc] = NULL;
224
225     vlc_enable_override ();
226
227     /* Initialize libvlc */
228     libvlc_instance_t *vlc = libvlc_new (argc, argv);
229     if (vlc == NULL)
230         return 1;
231
232     int ret = 1;
233     libvlc_set_exit_handler (vlc, vlc_kill, &self);
234     libvlc_set_app_id (vlc, "org.VideoLAN.VLC", PACKAGE_VERSION, PACKAGE_NAME);
235     libvlc_set_user_agent (vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
236
237     libvlc_add_intf (vlc, "hotkeys,none");
238 #if !defined (__OS2__)
239     libvlc_add_intf (vlc, "globalhotkeys,none");
240 #endif
241 #ifdef HAVE_DBUS
242     libvlc_add_intf (vlc, "dbus,none");
243 #endif
244     if (libvlc_add_intf (vlc, NULL))
245         goto out;
246
247     libvlc_playlist_play (vlc, -1, 0, NULL);
248
249     /* Qt4 insists on catching SIGCHLD via signal handler. To work around that,
250      * unblock it after all our child threads are created. */
251     sigdelset (&set, SIGCHLD);
252     pthread_sigmask (SIG_SETMASK, &set, NULL);
253
254     /* Do not dequeue SIGHUP if it is ignored (nohup) */
255     if (signal_ignored (SIGHUP))
256         sigdelset (&set, SIGHUP);
257     /* Ignore SIGPIPE */
258     sigdelset (&set, SIGPIPE);
259
260     int signum;
261     sigwait (&set, &signum);
262
263     /* Restore default signal behaviour after 3 seconds */
264     sigemptyset (&set);
265     sigaddset (&set, SIGINT);
266     sigaddset (&set, SIGALRM);
267     signal (SIGINT, SIG_IGN);
268     signal (SIGALRM, exit_timeout);
269     pthread_sigmask (SIG_UNBLOCK, &set, NULL);
270     alarm (3);
271
272     ret = 0;
273     /* Cleanup */
274 out:
275     libvlc_release (vlc);
276 #ifdef __OS2__
277     for (int i = argc - i_argc; i < argc; i++)
278         free (argv[i]);
279 #endif
280     return ret;
281 }