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 *****************************************************************************/
46 # define pthread_t int
47 # define pthread_self() _gettid()
48 # define pthread_sigmask(h, i, o) sigprocmask(h, i, o)
50 static char *FromSystem(const void *str
)
52 iconv_t handle
= iconv_open ("UTF-8", "");
53 if (handle
== (iconv_t
)(-1))
56 size_t str_len
= strlen (str
);
58 for (unsigned mul
= 4; mul
< 8; mul
++)
60 size_t in_size
= str_len
;
62 size_t out_max
= mul
* str_len
;
63 char *tmp
= out
= malloc (1 + out_max
);
67 if (iconv (handle
, &in
, &in_size
, &tmp
, &out_max
) != (size_t)(-1)) {
82 extern void vlc_enable_override (void);
84 static bool signal_ignored (int signum
)
88 if (sigaction (signum
, NULL
, &sa
))
90 return ((sa
.sa_flags
& SA_SIGINFO
)
91 ? (void *)sa
.sa_sigaction
: (void *)sa
.sa_handler
) == SIG_IGN
;
94 static void vlc_kill (void *data
)
99 pthread_kill (*ps
, SIGTERM
);
101 // send a signal to the main thread
102 kill (getpid(), SIGTERM
);
106 static void exit_timeout (int signum
)
109 signal (SIGINT
, SIG_DFL
);
112 /*****************************************************************************
113 * main: parse command line, start interface and spawn threads.
114 *****************************************************************************/
115 int main(int argc
, const char *argv
[])
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
);
126 /* Activate malloc checking routines to detect heap corruptions. */
127 setenv ("MALLOC_CHECK_", "2", 1);
129 /* Disable the ugly Gnome crash dialog so that we properly segfault */
130 setenv ("GNOME_DISABLE_CRASH_DIALOG", "1", 1);
134 setenv ("VLC_PLUGIN_PATH", TOP_BUILDDIR
"/modules", 1);
135 setenv ("VLC_DATA_PATH", TOP_SRCDIR
"/share", 1);
136 setenv ("VLC_LIB_PATH", TOP_BUILDDIR
"/modules", 1);
139 /* Clear the X.Org startup notification ID. Otherwise the UI might try to
140 * change the environment while the process is multi-threaded. That could
141 * crash. Screw you X.Org. Next time write a thread-safe specification. */
142 unsetenv ("DESKTOP_STARTUP_ID");
144 #ifndef ALLOW_RUN_AS_ROOT
147 fprintf (stderr
, "VLC is not supposed to be run as root. Sorry.\n"
148 "If you need to use real-time priorities and/or privileged TCP ports\n"
149 "you can use %s-wrapper (make sure it is Set-UID root and\n"
150 "cannot be run by non-trusted users first).\n", argv
[0]);
155 setlocale (LC_ALL
, "");
157 if (isatty (STDERR_FILENO
))
158 /* This message clutters error logs. It is printed only on a TTY.
159 * Fortunately, LibVLC prints version info with -vv anyway. */
160 fprintf (stderr
, "VLC media player %s (revision %s)\n",
161 libvlc_get_version(), libvlc_get_changeset());
166 /* VLC uses sigwait() to dequeue interesting signals.
167 * For this to work, those signals must be blocked in all threads,
168 * including the thread calling sigwait() (see the man page for details).
170 * There are two advantages to sigwait() over traditional signal handlers:
171 * - delivery is synchronous: no need to worry about async-safety,
172 * - EINTR is not generated: other threads need not handle that error.
173 * That being said, some LibVLC programs do not use sigwait(). Therefore
174 * EINTR must still be handled cleanly, notably from poll() calls.
176 * Signals that request a clean shutdown, and force an unclean shutdown
177 * if they are triggered again 2+ seconds later.
178 * We have to handle SIGTERM cleanly because of daemon mode. */
179 sigaddset (&set
, SIGINT
);
180 sigaddset (&set
, SIGHUP
);
181 sigaddset (&set
, SIGQUIT
);
182 sigaddset (&set
, SIGTERM
);
184 /* SIGPIPE can happen and would crash the process. On modern systems,
185 * the MSG_NOSIGNAL flag protects socket write operations against SIGPIPE.
186 * But we still need to block SIGPIPE when:
187 * - writing to pipes,
188 * - using write() instead of send() for code not specific to sockets.
189 * LibVLC code assumes that SIGPIPE is blocked. Other LibVLC applications
190 * shall block it (or handle it somehow) too.
192 sigaddset (&set
, SIGPIPE
);
194 /* SIGCHLD must be dequeued to clean up zombie child processes.
195 * Furthermore the handler must not be set to SIG_IGN (see above).
196 * We cannot pragmatically handle EINTR, short reads and short writes
197 * in every code paths (including underlying libraries). So we just
198 * block SIGCHLD in all threads, and dequeue it below. */
199 sigaddset (&set
, SIGCHLD
);
201 /* Block all these signals */
202 pthread_t self
= pthread_self ();
203 pthread_sigmask (SIG_SETMASK
, &set
, NULL
);
205 const char *args
[argc
+ 3];
208 args
[count
++] = "--no-ignore-config";
209 args
[count
++] = "--media-library";
211 args
[count
++] = "--dbus";
215 for (int i
= 1; i
< argc
; i
++)
216 if ((args
[count
++] = FromSystem(argv
[i
])) == NULL
)
218 fprintf (stderr
, "Converting '%s' to UTF-8 failed.\n", argv
[i
]);
222 memcpy(args
+ count
, argv
+ 1, (argc
- 1) * sizeof (*argv
));
227 vlc_enable_override ();
229 /* Initialize libvlc */
230 libvlc_instance_t
*vlc
= libvlc_new(count
, args
);
235 libvlc_set_exit_handler (vlc
, vlc_kill
, &self
);
236 libvlc_set_app_id (vlc
, "org.VideoLAN.VLC", PACKAGE_VERSION
, PACKAGE_NAME
);
237 libvlc_set_user_agent (vlc
, "VLC media player", "VLC/"PACKAGE_VERSION
);
239 if (libvlc_add_intf (vlc
, NULL
))
241 fprintf(stderr
, "%s: cannot start any interface. Exiting.\n", argv
[0]);
245 libvlc_playlist_play (vlc
);
247 /* Qt insists on catching SIGCHLD via signal handler. To work around that,
248 * unblock it after all our child threads are created. */
249 sigdelset (&set
, SIGCHLD
);
250 pthread_sigmask (SIG_SETMASK
, &set
, NULL
);
252 /* Do not dequeue SIGHUP if it is ignored (nohup) */
253 if (signal_ignored (SIGHUP
))
254 sigdelset (&set
, SIGHUP
);
256 sigdelset (&set
, SIGPIPE
);
259 sigwait (&set
, &signum
);
261 /* Restore default signal behaviour after 3 seconds */
263 sigaddset (&set
, SIGINT
);
264 sigaddset (&set
, SIGALRM
);
265 signal (SIGINT
, SIG_IGN
);
266 signal (SIGALRM
, exit_timeout
);
267 pthread_sigmask (SIG_UNBLOCK
, &set
, NULL
);
273 libvlc_release (vlc
);
275 for (int i
= count
- argc
+ 1; i
< count
; i
++)