1 /*****************************************************************************
2 * darwinvlc.c: the darwin-specific VLC player
3 *****************************************************************************
4 * Copyright (C) 1998-2013 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
44 extern void vlc_enable_override (void);
46 static bool signal_ignored (int signum)
50 if (sigaction (signum, NULL, &sa))
52 return ((sa.sa_flags & SA_SIGINFO)
53 ? (void *)sa.sa_sigaction : (void *)sa.sa_handler) == SIG_IGN;
56 static void vlc_kill (void *data)
59 pthread_kill (*ps, SIGTERM);
62 static void exit_timeout (int signum)
65 signal (SIGINT, SIG_DFL);
68 /*****************************************************************************
69 * main: parse command line, start interface and spawn threads.
70 *****************************************************************************/
71 int main( int i_argc, const char *ppsz_argv[] )
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);
82 /* Activate malloc checking routines to detect heap corruptions. */
83 setenv ("MALLOC_CHECK_", "2", 1);
87 setenv ("VLC_PLUGIN_PATH", TOP_BUILDDIR"/modules", 1);
88 setenv ("VLC_DATA_PATH", TOP_SRCDIR"/share", 1);
91 #ifndef ALLOW_RUN_AS_ROOT
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]);
102 setlocale (LC_ALL, "");
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());
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).
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.
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);
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.
139 sigaddset (&set, SIGPIPE);
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);
148 /* Block all these signals */
149 pthread_t self = pthread_self ();
150 pthread_sigmask (SIG_SETMASK, &set, NULL);
152 const char *argv[i_argc + 2];
155 argv[argc++] = "--no-ignore-config";
156 argv[argc++] = "--media-library";
157 ppsz_argv++; i_argc--; /* skip executable path */
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--;
166 memcpy (argv + argc, ppsz_argv, i_argc * sizeof (*argv));
170 vlc_enable_override ();
172 /* Initialize libvlc */
173 libvlc_instance_t *vlc = libvlc_new (argc, argv);
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);
182 libvlc_add_intf (vlc, "hotkeys,none");
184 if (libvlc_add_intf (vlc, NULL))
187 libvlc_playlist_play (vlc, -1, 0, NULL);
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);
194 /* Do not dequeue SIGHUP if it is ignored (nohup) */
195 if (signal_ignored (SIGHUP))
196 sigdelset (&set, SIGHUP);
198 sigdelset (&set, SIGPIPE);
201 sigwait (&set, &signum);
203 /* Restore default signal behaviour after 3 seconds */
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);
215 libvlc_release (vlc);