Use setenv(), safer and cleaner than putenv()
[vlc/solaris.git] / bin / vlc.c
blobb99b5aab5762d81855473772d90a2f65c985d03d
1 /*****************************************************************************
2 * vlc.c: the VLC player
3 *****************************************************************************
4 * Copyright (C) 1998-2008 the VideoLAN team
5 * $Id$
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 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc/vlc.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <locale.h>
37 #ifdef __APPLE__
38 #include <string.h>
39 #endif
42 /* Explicit HACK */
43 extern void LocaleFree (const char *);
44 extern char *FromLocale (const char *);
45 extern void vlc_enable_override (void);
47 #include <signal.h>
48 #include <time.h>
49 #include <pthread.h>
50 #include <unistd.h>
51 #include <dlfcn.h>
53 #ifdef HAVE_MAEMO
54 static void dummy_handler (int signum)
56 (void) signum;
58 #endif
60 /*****************************************************************************
61 * main: parse command line, start interface and spawn threads.
62 *****************************************************************************/
63 int main( int i_argc, const char *ppsz_argv[] )
65 /* The so-called POSIX-compliant MacOS X reportedly processes SIGPIPE even
66 * if it is blocked in all thread. Also some libraries want SIGPIPE blocked
67 * as they have no clue about signal masks.
68 * Note: this is NOT an excuse for not protecting against SIGPIPE. If
69 * LibVLC runs outside of VLC, we cannot rely on this code snippet. */
70 signal (SIGPIPE, SIG_IGN);
72 #ifdef HAVE_SETENV
73 # ifndef NDEBUG
74 /* Activate malloc checking routines to detect heap corruptions. */
75 setenv ("MALLOC_CHECK_", "2", 1);
77 /* Disable the ugly Gnome crash dialog so that we properly segfault */
78 setenv ("GNOME_DISABLE_CRASH_DIALOG", "1", 1);
79 # endif
81 /* Make Xlib hide visuals with an alphachannel. Ensure that Qt4 will not
82 * use the alpha channel for the embedded video window. */
83 setenv ("XLIB_SKIP_ARGB_VISUALS", "1", 1);
85 /* Clear the X.Org startup notification ID. Otherwise the UI might try to
86 * change the environment while the process is multi-threaded. That could
87 * crash. Screw you X.Org. Next time write a thread-safe specification. */
88 unsetenv ("DESKTOP_STARTUP_ID");
89 #endif
91 #ifndef ALLOW_RUN_AS_ROOT
92 if (geteuid () == 0)
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;
100 #endif
102 setlocale (LC_ALL, "");
104 #ifndef __APPLE__
105 /* This clutters OSX GUI error logs */
106 fprintf( stderr, "VLC media player %s (revision %s)\n",
107 libvlc_get_version(), libvlc_get_changeset() );
108 #endif
110 /* Synchronously intercepted POSIX signals.
112 * In a threaded program such as VLC, the only sane way to handle signals
113 * is to block them in all threads but one - this is the only way to
114 * predict which thread will receive them. If any piece of code depends
115 * on delivery of one of this signal it is intrinsically not thread-safe
116 * and MUST NOT be used in VLC, whether we like it or not.
117 * There is only one exception: if the signal is raised with
118 * pthread_kill() - we do not use this in LibVLC but some pthread
119 * implementations use them internally. You should really use conditions
120 * for thread synchronization anyway.
122 * Signal that request a clean shutdown, and force an unclean shutdown
123 * if they are triggered again 2+ seconds later.
124 * We have to handle SIGTERM cleanly because of daemon mode.
125 * Note that we set the signals after the vlc_create call. */
126 static const int sigs[] = {
127 SIGINT, SIGHUP, SIGQUIT, SIGTERM,
128 /* Signals that cause a no-op:
129 * - SIGPIPE might happen with sockets and would crash VLC. It MUST be
130 * blocked by any LibVLC-dependent application, not just VLC.
131 * - SIGCHLD comes after exec*() (such as httpd CGI support) and must
132 * be dequeued to cleanup zombie processes.
134 SIGPIPE, SIGCHLD
137 sigset_t set;
138 sigemptyset (&set);
139 for (unsigned i = 0; i < sizeof (sigs) / sizeof (sigs[0]); i++)
140 sigaddset (&set, sigs[i]);
141 #ifdef HAVE_MAEMO
142 sigaddset (&set, SIGRTMIN);
144 struct sigaction act = { .sa_handler = dummy_handler, };
145 sigaction (SIGRTMIN, &act, NULL);
147 #endif
149 /* Block all these signals */
150 pthread_sigmask (SIG_BLOCK, &set, NULL);
151 sigdelset (&set, SIGPIPE);
152 sigdelset (&set, SIGCHLD);
154 /* Note that FromLocale() can be used before libvlc is initialized */
155 const char *argv[i_argc + 3];
156 int argc = 0;
158 argv[argc++] = "--no-ignore-config";
159 #ifdef TOP_BUILDDIR
160 argv[argc++] = FromLocale ("--plugin-path="TOP_BUILDDIR"/modules");
161 #endif
162 #ifdef TOP_SRCDIR
163 argv[argc++] = FromLocale ("--data-path="TOP_SRCDIR"/share");
164 #endif
166 int i = 1;
167 #ifdef __APPLE__
168 /* When VLC.app is run by double clicking in Mac OS X, the 2nd arg
169 * is the PSN - process serial number (a unique PID-ish thingie)
170 * still ok for real Darwin & when run from command line
171 * for example -psn_0_9306113 */
172 if(i_argc >= 2 && !strncmp( ppsz_argv[1] , "-psn" , 4 ))
173 i = 2;
174 #endif
175 for (; i < i_argc; i++)
176 if ((argv[argc++] = FromLocale (ppsz_argv[i])) == NULL)
177 return 1; // BOOM!
178 argv[argc] = NULL;
180 vlc_enable_override ();
182 /* Initialize libvlc */
183 libvlc_instance_t *vlc = libvlc_new (argc, argv);
185 if (vlc != NULL)
187 if (libvlc_add_intf (vlc, "signals"))
188 pthread_sigmask (SIG_UNBLOCK, &set, NULL);
189 #if !defined (HAVE_MAEMO)
190 libvlc_add_intf (vlc, "globalhotkeys,none");
191 #endif
192 if (libvlc_add_intf (vlc, NULL) == 0)
194 libvlc_playlist_play (vlc, -1, 0, NULL);
195 libvlc_wait (vlc);
197 libvlc_release (vlc);
200 for (int i = 1; i < argc; i++)
201 LocaleFree (argv[i]);
203 #ifdef RTLD_NOLOAD
204 /* Avoid crash in KIO scheduler cleanup. */
205 /* This is ugly, but we get way too many crash reports due to this. */
206 if (dlopen ("libkfilemodule.so", RTLD_LAZY|RTLD_LOCAL|RTLD_NOLOAD) != NULL)
208 fprintf (stderr, "KFile plugin present. Unclean shutdown!\n");
209 _exit (0);
211 #endif
212 return 0;