contrib: rust: freeze the Rust version
[vlc.git] / bin / vlc.c
blob268922a6175d11c0b0d3228a05dd22ed6a311848
1 /*****************************************************************************
2 * vlc.c: the VLC player
3 *****************************************************************************
4 * Copyright (C) 1998-2013 the VideoLAN team
6 * Authors: Vincent Seguin <seguin@via.ecp.fr>
7 * Samuel Hocevar <sam@zoy.org>
8 * Gildas Bazin <gbazin@videolan.org>
9 * Derk-Jan Hartman <hartman at videolan dot org>
10 * Lots of other people, see the libvlc AUTHORS file
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc/vlc.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <stdbool.h>
35 #include <string.h>
36 #include <locale.h>
37 #include <signal.h>
38 #ifdef HAVE_PTHREAD_H
39 # include <pthread.h>
40 #endif
41 #include <unistd.h>
43 #ifdef __OS2__
44 # include <iconv.h>
46 # define pthread_t int
47 # define pthread_self() _gettid()
49 static char *FromSystem(const void *str)
51 iconv_t handle = iconv_open ("UTF-8", "");
52 if (handle == (iconv_t)(-1))
53 return NULL;
55 size_t str_len = strlen (str);
56 char *out = NULL;
57 for (unsigned mul = 4; mul < 8; mul++)
59 size_t in_size = str_len;
60 const char *in = str;
61 size_t out_max = mul * str_len;
62 char *tmp = out = malloc (1 + out_max);
63 if (!out)
64 break;
66 if (iconv (handle, &in, &in_size, &tmp, &out_max) != (size_t)(-1)) {
67 *tmp = '\0';
68 break;
70 free(out);
71 out = NULL;
73 if (errno != E2BIG)
74 break;
76 iconv_close(handle);
77 return out;
79 #endif
81 extern void vlc_enable_override (void);
83 static bool signal_ignored (int signum)
85 struct sigaction sa;
87 if (sigaction (signum, NULL, &sa))
88 return false;
89 return ((sa.sa_flags & SA_SIGINFO)
90 ? (void *)sa.sa_sigaction : (void *)sa.sa_handler) == SIG_IGN;
93 static void vlc_kill (void *data)
95 #ifndef __OS2__
96 pthread_t *ps = data;
98 pthread_kill (*ps, SIGTERM);
99 #else
100 // send a signal to the main thread
101 kill (getpid(), SIGTERM);
102 #endif
105 static void exit_timeout (int signum)
107 (void) signum;
108 signal (SIGINT, SIG_DFL);
111 /*****************************************************************************
112 * main: parse command line, start interface and spawn threads.
113 *****************************************************************************/
114 int main(int argc, const char *argv[])
116 /* The so-called POSIX-compliant MacOS X reportedly processes SIGPIPE even
117 * if it is blocked in all thread.
118 * Note: this is NOT an excuse for not protecting against SIGPIPE. If
119 * LibVLC runs outside of VLC, we cannot rely on this code snippet. */
120 signal (SIGPIPE, SIG_IGN);
121 /* Restore SIGCHLD in case our parent process ignores it. */
122 signal (SIGCHLD, SIG_DFL);
124 #ifndef NDEBUG
125 /* Activate malloc checking routines to detect heap corruptions. */
126 setenv ("MALLOC_CHECK_", "2", 1);
128 /* Disable the ugly Gnome crash dialog so that we properly segfault */
129 setenv ("GNOME_DISABLE_CRASH_DIALOG", "1", 1);
130 #endif
132 #ifdef TOP_BUILDDIR
133 setenv ("VLC_PLUGIN_PATH", TOP_BUILDDIR"/modules", 1);
134 setenv ("VLC_DATA_PATH", TOP_SRCDIR"/share", 1);
135 setenv ("VLC_LIB_PATH", TOP_BUILDDIR"/modules", 1);
136 #endif
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");
143 #ifndef ALLOW_RUN_AS_ROOT
144 if (geteuid () == 0)
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", argv[0]);
150 return 1;
152 #endif
154 setlocale (LC_ALL, "");
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());
162 sigset_t set;
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).
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.
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);
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.
191 sigaddset (&set, SIGPIPE);
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);
200 /* Block all these signals */
201 pthread_t self = pthread_self ();
202 pthread_sigmask (SIG_SETMASK, &set, NULL);
204 const char *args[argc + 3];
205 int count = 0;
207 args[count++] = "--no-ignore-config";
208 args[count++] = "--media-library";
209 #ifdef HAVE_DBUS
210 args[count++] = "--dbus";
211 #endif
213 #ifdef __OS2__
214 for (int i = 1; i < argc; i++)
215 if ((args[count++] = FromSystem(argv[i])) == NULL)
217 fprintf (stderr, "Converting '%s' to UTF-8 failed.\n", argv[i]);
218 return 1;
220 #else
221 memcpy(args + count, argv + 1, (argc - 1) * sizeof (*argv));
222 count += (argc - 1);
223 #endif
224 args[count] = NULL;
226 vlc_enable_override ();
228 /* Initialize libvlc */
229 libvlc_instance_t *vlc = libvlc_new(count, args);
230 if (vlc == NULL)
231 return 1;
233 int ret = 1;
234 libvlc_set_exit_handler (vlc, vlc_kill, &self);
235 libvlc_set_app_id (vlc, "org.VideoLAN.VLC", PACKAGE_VERSION, PACKAGE_NAME);
236 libvlc_set_user_agent (vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
238 libvlc_add_intf (vlc, "hotkeys,none");
239 #if !defined (__OS2__)
240 libvlc_add_intf (vlc, "globalhotkeys,none");
241 #endif
242 if (libvlc_add_intf (vlc, NULL))
244 fprintf(stderr, "%s: cannot start any interface. Exiting.\n", argv[0]);
245 goto out;
248 libvlc_playlist_play (vlc);
250 /* Qt insists on catching SIGCHLD via signal handler. To work around that,
251 * unblock it after all our child threads are created. */
252 sigdelset (&set, SIGCHLD);
253 pthread_sigmask (SIG_SETMASK, &set, NULL);
255 /* Do not dequeue SIGHUP if it is ignored (nohup) */
256 if (signal_ignored (SIGHUP))
257 sigdelset (&set, SIGHUP);
258 /* Ignore SIGPIPE */
259 sigdelset (&set, SIGPIPE);
261 int signum;
262 sigwait (&set, &signum);
264 /* Restore default signal behaviour after 3 seconds */
265 sigemptyset (&set);
266 sigaddset (&set, SIGINT);
267 sigaddset (&set, SIGALRM);
268 signal (SIGINT, SIG_IGN);
269 signal (SIGALRM, exit_timeout);
270 pthread_sigmask (SIG_UNBLOCK, &set, NULL);
271 alarm (3);
273 ret = 0;
274 /* Cleanup */
275 out:
276 libvlc_release (vlc);
277 #ifdef __OS2__
278 for (int i = count - argc + 1; i < count; i++)
279 free(args[i]);
280 #endif
281 return ret;