root/src/vlc.c
| Revision fa22e4435c9fc1d5d774567b150ba4fb587e2f1b, 5.4 kB (checked in by Rémi Denis-Courmont <rdenis@simphalempin.com>, 1 month ago) | |
|---|---|
| |
| Line | |
|---|---|
| 1 | /***************************************************************************** |
| 2 | * vlc.c: the VLC player |
| 3 | ***************************************************************************** |
| 4 | * Copyright (C) 1998-2008 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 <locale.h> |
| 36 | |
| 37 | |
| 38 | /* Explicit HACK */ |
| 39 | extern void LocaleFree (const char *); |
| 40 | extern char *FromLocale (const char *); |
| 41 | |
| 42 | #include <signal.h> |
| 43 | #include <time.h> |
| 44 | #include <pthread.h> |
| 45 | #include <unistd.h> |
| 46 | |
| 47 | /***************************************************************************** |
| 48 | * main: parse command line, start interface and spawn threads. |
| 49 | *****************************************************************************/ |
| 50 | int main( int i_argc, const char *ppsz_argv[] ) |
| 51 | { |
| 52 | int i_ret; |
| 53 | |
| 54 | if (geteuid () == 0) |
| 55 | { |
| 56 | fprintf (stderr, "VLC is not supposed to be run as root. Sorry.\n" |
| 57 | "If you need to use real-time priorities and/or privileged TCP ports\n" |
| 58 | "you can use %s-wrapper (make sure it is Set-UID root first and\n" |
| 59 | "cannot be run by non-trusted users first).\n", ppsz_argv[0]); |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | setlocale (LC_ALL, ""); |
| 64 | |
| 65 | #ifndef __APPLE__ |
| 66 | /* This clutters OSX GUI error logs */ |
| 67 | fprintf( stderr, "VLC media player %s\n", libvlc_get_version() ); |
| 68 | #endif |
| 69 | |
| 70 | #ifdef HAVE_PUTENV |
| 71 | # ifndef NDEBUG |
| 72 | /* Activate malloc checking routines to detect heap corruptions. */ |
| 73 | putenv( (char*)"MALLOC_CHECK_=2" ); |
| 74 | # ifdef __APPLE__ |
| 75 | putenv( (char*)"MallocErrorAbort=crash_my_baby_crash" ); |
| 76 | # endif |
| 77 | |
| 78 | /* Disable the ugly Gnome crash dialog so that we properly segfault */ |
| 79 | putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" ); |
| 80 | # endif |
| 81 | #endif |
| 82 | |
| 83 | #if defined (HAVE_GETEUID) && !defined (SYS_BEOS) |
| 84 | /* FIXME: rootwrap (); */ |
| 85 | #endif |
| 86 | |
| 87 | /* Synchronously intercepted POSIX signals. |
| 88 | * |
| 89 | * In a threaded program such as VLC, the only sane way to handle signals |
| 90 | * is to block them in all thread but one - this is the only way to |
| 91 | * predict which thread will receive them. If any piece of code depends |
| 92 | * on delivery of one of this signal it is intrinsically not thread-safe |
| 93 | * and MUST NOT be used in VLC, whether we like it or not. |
| 94 | * There is only one exception: if the signal is raised with |
| 95 | * pthread_kill() - we do not use this in LibVLC but some pthread |
| 96 | * implementations use them internally. You should really use conditions |
| 97 | * for thread synchronization anyway. |
| 98 | * |
| 99 | * Signal that request a clean shutdown, and force an unclean shutdown |
| 100 | * if they are triggered again 2+ seconds later. |
| 101 | * We have to handle SIGTERM cleanly because of daemon mode. |
| 102 | * Note that we set the signals after the vlc_create call. */ |
| 103 | static const int sigs[] = { |
| 104 | SIGINT, SIGHUP, SIGQUIT, SIGTERM, |
| 105 | /* Signals that cause a no-op: |
| 106 | * - SIGPIPE might happen with sockets and would crash VLC. It MUST be |
| 107 | * blocked by any LibVLC-dependent application, in addition to VLC. |
| 108 | * - SIGCHLD is comes after exec*() (such as httpd CGI support) and must |
| 109 | * be dequeued to cleanup zombie processes. |
| 110 | */ |
| 111 | SIGPIPE, SIGCHLD |
| 112 | }; |
| 113 | |
| 114 | sigset_t set; |
| 115 | sigemptyset (&set); |
| 116 | for (unsigned i = 0; i < sizeof (sigs) / sizeof (sigs[0]); i++) |
| 117 | sigaddset (&set, sigs[i]); |
| 118 | |
| 119 | /* Block all these signals */ |
| 120 | pthread_sigmask (SIG_BLOCK, &set, NULL); |
| 121 | |
| 122 | /* Note that FromLocale() can be used before libvlc is initialized */ |
| 123 | for (int i = 0; i < i_argc; i++) |
| 124 | if ((ppsz_argv[i] = FromLocale (ppsz_argv[i])) == NULL) |
| 125 | return 1; // BOOM! |
| 126 | |
| 127 | libvlc_exception_t ex, dummy; |
| 128 | libvlc_exception_init (&ex); |
| 129 | libvlc_exception_init (&dummy); |
| 130 | |
| 131 | /* Initialize libvlc */ |
| 132 | int i_argc_real = i_argc ? i_argc - 1 : 0; |
| 133 | const char **ppsz_argv_real = i_argc ? &ppsz_argv[1] : ppsz_argv; |
| 134 | libvlc_instance_t *vlc = libvlc_new (i_argc_real, ppsz_argv_real, &ex); |
| 135 | |
| 136 | if (vlc != NULL) |
| 137 | { |
| 138 | libvlc_add_intf (vlc, "signals", &dummy); |
| 139 | libvlc_add_intf (vlc, NULL, &ex); |
| 140 | libvlc_playlist_play (vlc, -1, 0, NULL, &dummy); |
| 141 | libvlc_wait (vlc); |
| 142 | libvlc_release (vlc); |
| 143 | } |
| 144 | i_ret = libvlc_exception_raised (&ex); |
| 145 | if( i_ret ) |
| 146 | fprintf( stderr, "%s\n", libvlc_exception_get_message( &ex)); |
| 147 | |
| 148 | libvlc_exception_clear (&ex); |
| 149 | libvlc_exception_clear (&dummy); |
| 150 | |
| 151 | for (int i = 0; i < i_argc; i++) |
| 152 | LocaleFree (ppsz_argv[i]); |
| 153 | |
| 154 | return i_ret; |
| 155 | } |
Note: See TracBrowser for help on using the browser.
