]> git.sesse.net Git - vlc/blob - bin/darwinvlc.c
bin: derive a special Darwin version from vlc.c
[vlc] / bin / darwinvlc.c
1 /*****************************************************************************
2  * darwinvlc.c: the darwin-specific 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 extern void vlc_enable_override (void);
45
46 static bool signal_ignored (int signum)
47 {
48     struct sigaction sa;
49
50     if (sigaction (signum, NULL, &sa))
51         return false;
52     return ((sa.sa_flags & SA_SIGINFO)
53             ? (void *)sa.sa_sigaction : (void *)sa.sa_handler) == SIG_IGN;
54 }
55
56 static void vlc_kill (void *data)
57 {
58     pthread_t *ps = data;
59     pthread_kill (*ps, SIGTERM);
60 }
61
62 static void exit_timeout (int signum)
63 {
64     (void) signum;
65     signal (SIGINT, SIG_DFL);
66 }
67
68 /*****************************************************************************
69  * main: parse command line, start interface and spawn threads.
70  *****************************************************************************/
71 int main( int i_argc, const char *ppsz_argv[] )
72 {
73     /* The so-called POSIX-compliant MacOS X reportedly processes SIGPIPE even
74      * if it is blocked in all thread.
75      * Note: this is NOT an excuse for not protecting against SIGPIPE. If
76      * LibVLC runs outside of VLC, we cannot rely on this code snippet. */
77     signal (SIGPIPE, SIG_IGN);
78     /* Restore SIGCHLD in case our parent process ignores it. */
79     signal (SIGCHLD, SIG_DFL);
80
81 #ifndef NDEBUG
82     /* Activate malloc checking routines to detect heap corruptions. */
83     setenv ("MALLOC_CHECK_", "2", 1);
84 #endif
85
86 #ifdef TOP_BUILDDIR
87     setenv ("VLC_PLUGIN_PATH", TOP_BUILDDIR"/modules", 1);
88     setenv ("VLC_DATA_PATH", TOP_SRCDIR"/share", 1);
89 #endif
90
91 #ifndef ALLOW_RUN_AS_ROOT
92     if (geteuid () == 0)
93     {
94         fprintf (stderr, "VLC is not supposed to be run as root. Sorry.\n"
95         "If you need to use real-time priorities and/or privileged TCP ports\n"
96         "you can use %s-wrapper (make sure it is Set-UID root and\n"
97         "cannot be run by non-trusted users first).\n", ppsz_argv[0]);
98         return 1;
99     }
100 #endif
101
102     setlocale (LC_ALL, "");
103
104     if (isatty (STDERR_FILENO))
105         /* This message clutters error logs. It is printed only on a TTY.
106          * Fortunately, LibVLC prints version info with -vv anyway. */
107         fprintf (stderr, "VLC media player %s (revision %s)\n",
108                  libvlc_get_version(), libvlc_get_changeset());
109
110     sigset_t set;
111
112     sigemptyset (&set);
113     /* VLC uses sigwait() to dequeue interesting signals.
114      * For this to work, those signals must be blocked in all threads,
115      * including the thread calling sigwait() (see the man page for details).
116      *
117      * There are two advantages to sigwait() over traditional signal handlers:
118      *  - delivery is synchronous: no need to worry about async-safety,
119      *  - EINTR is not generated: other threads need not handle that error.
120      * That being said, some LibVLC programs do not use sigwait(). Therefore
121      * EINTR must still be handled cleanly, notably from poll() calls.
122      *
123      * Signals that request a clean shutdown, and force an unclean shutdown
124      * if they are triggered again 2+ seconds later.
125      * We have to handle SIGTERM cleanly because of daemon mode. */
126     sigaddset (&set, SIGINT);
127     sigaddset (&set, SIGHUP);
128     sigaddset (&set, SIGQUIT);
129     sigaddset (&set, SIGTERM);
130
131     /* SIGPIPE can happen and would crash the process. On modern systems,
132      * the MSG_NOSIGNAL flag protects socket write operations against SIGPIPE.
133      * But we still need to block SIGPIPE when:
134      *  - writing to pipes,
135      *  - using write() instead of send() for code not specific to sockets.
136      * LibVLC code assumes that SIGPIPE is blocked. Other LibVLC applications
137      * shall block it (or handle it somehow) too.
138      */
139     sigaddset (&set, SIGPIPE);
140
141     /* SIGCHLD must be dequeued to clean up zombie child processes.
142      * Furthermore the handler must not be set to SIG_IGN (see above).
143      * We cannot pragmatically handle EINTR, short reads and short writes
144      * in every code paths (including underlying libraries). So we just
145      * block SIGCHLD in all threads, and dequeue it below. */
146     sigaddset (&set, SIGCHLD);
147
148     /* Block all these signals */
149     pthread_t self = pthread_self ();
150     pthread_sigmask (SIG_SETMASK, &set, NULL);
151
152     const char *argv[i_argc + 2];
153     int argc = 0;
154
155     argv[argc++] = "--no-ignore-config";
156     argv[argc++] = "--media-library";
157     ppsz_argv++; i_argc--; /* skip executable path */
158
159     /* When VLC.app is run by double clicking in Mac OS X, the 2nd arg
160      * is the PSN - process serial number (a unique PID-ish thingie)
161      * still ok for real Darwin & when run from command line
162      * for example -psn_0_9306113 */
163     if (i_argc >= 1 && !strncmp (*ppsz_argv, "-psn" , 4))
164         ppsz_argv++, i_argc--;
165
166     memcpy (argv + argc, ppsz_argv, i_argc * sizeof (*argv));
167     argc += i_argc;
168     argv[argc] = NULL;
169
170     vlc_enable_override ();
171
172     /* Initialize libvlc */
173     libvlc_instance_t *vlc = libvlc_new (argc, argv);
174     if (vlc == NULL)
175         return 1;
176
177     int ret = 1;
178     libvlc_set_exit_handler (vlc, vlc_kill, &self);
179     libvlc_set_app_id (vlc, "org.VideoLAN.VLC", PACKAGE_VERSION, PACKAGE_NAME);
180     libvlc_set_user_agent (vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
181
182     libvlc_add_intf (vlc, "hotkeys,none");
183
184     if (libvlc_add_intf (vlc, NULL))
185         goto out;
186
187     libvlc_playlist_play (vlc, -1, 0, NULL);
188
189     /* Qt4 insists on catching SIGCHLD via signal handler. To work around that,
190      * unblock it after all our child threads are created. */
191     sigdelset (&set, SIGCHLD);
192     pthread_sigmask (SIG_SETMASK, &set, NULL);
193
194     /* Do not dequeue SIGHUP if it is ignored (nohup) */
195     if (signal_ignored (SIGHUP))
196         sigdelset (&set, SIGHUP);
197     /* Ignore SIGPIPE */
198     sigdelset (&set, SIGPIPE);
199
200     int signum;
201     sigwait (&set, &signum);
202
203     /* Restore default signal behaviour after 3 seconds */
204     sigemptyset (&set);
205     sigaddset (&set, SIGINT);
206     sigaddset (&set, SIGALRM);
207     signal (SIGINT, SIG_IGN);
208     signal (SIGALRM, exit_timeout);
209     pthread_sigmask (SIG_UNBLOCK, &set, NULL);
210     alarm (3);
211
212     ret = 0;
213     /* Cleanup */
214 out:
215     libvlc_release (vlc);
216
217     return ret;
218 }