Changeset 2fa6c9ce280b037c058eed166bfc51e7c70c5d14

Show
Ignore:
Timestamp:
19/08/02 13:13:45 (6 years ago)
Author:
Sam Hocevar <sam@videolan.org>
git-committer:
Sam Hocevar <sam@videolan.org> 1029755625 +0000
git-parent:

[9c669a9f8f98c6ff6cf5c72168b485fc1ab21967]

git-author:
Sam Hocevar <sam@videolan.org> 1029755625 +0000
Message:
  • ./src/misc/cpu.c: libvlc now plays nice with SIGILL and restores the
    signal handler to its previous value after use.
  • ./src/libvlc.c: moved signal handling to vlc.c.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • include/vlc/vlc.h

    r005be13 r2fa6c9c  
    33 ***************************************************************************** 
    44 * Copyright (C) 1998, 1999, 2000 VideoLAN 
    5  * $Id: vlc.h,v 1.8 2002/08/14 17:06:53 sam Exp $ 
     5 * $Id: vlc.h,v 1.9 2002/08/19 11:13:44 sam Exp $ 
    66 * 
    77 * This program is free software; you can redistribute it and/or modify 
     
    115115vlc_error_t     vlc_init         ( int, char *[] ); 
    116116vlc_error_t     vlc_run          ( void ); 
     117vlc_error_t     vlc_die          ( void ); 
    117118vlc_error_t     vlc_stop         ( void ); 
    118119vlc_error_t     vlc_end          ( void ); 
     
    130131vlc_error_t     vlc_init_r       ( vlc_t *, int, char *[] ); 
    131132vlc_error_t     vlc_run_r        ( vlc_t * ); 
     133vlc_error_t     vlc_die_r        ( vlc_t * ); 
    132134vlc_error_t     vlc_stop_r       ( vlc_t * ); 
    133135vlc_error_t     vlc_end_r        ( vlc_t * ); 
  • src/libvlc.c

    r3dff442 r2fa6c9c  
    33 ***************************************************************************** 
    44 * Copyright (C) 1998-2002 VideoLAN 
    5  * $Id: libvlc.c,v 1.27 2002/08/18 13:49:20 sam Exp $ 
     5 * $Id: libvlc.c,v 1.28 2002/08/19 11:13:45 sam Exp $ 
    66 * 
    77 * Authors: Vincent Seguin <seguin@via.ecp.fr> 
     
    3838#include <string.h>                                            /* strerror() */ 
    3939#include <stdlib.h>                                                /* free() */ 
    40 #include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */ 
    4140 
    4241#include <vlc/vlc.h> 
     
    103102static void Version       ( void ); 
    104103 
    105 #ifndef WIN32 
    106 static void InitSignalHandler   ( void ); 
    107 static void FatalSignalHandler  ( int i_signal ); 
    108 #endif 
    109  
    110104#ifdef WIN32 
    111105static void ShowConsole   ( void ); 
     
    116110 ***************************************************************************** 
    117111 * This function allocates a vlc_t structure and returns NULL in case of 
    118  * failure. Also, the thread system and the signal handlers are initialized. 
     112 * failure. Also, the thread system is initialized. 
    119113 *****************************************************************************/ 
    120114vlc_error_t vlc_create( void ) 
    121115{ 
    122     vlc_t * p_vlc = vlc_create_r(); 
    123     return p_vlc ? VLC_SUCCESS : VLC_EGENERIC; 
     116    vlc_t * p_vlc; 
     117    vlc_bool_t b_failed = VLC_FALSE; 
     118 
     119    /* This gives us a rather good protection against concurrent calls, but 
     120     * an additional check will be necessary for complete thread safety. */ 
     121    if( i_vlc ) 
     122    { 
     123        return VLC_EGENERIC; 
     124    } 
     125 
     126    p_vlc = vlc_create_r(); 
     127 
     128    if( p_vlc == NULL ) 
     129    { 
     130        return VLC_EGENERIC; 
     131    } 
     132 
     133    /* We have created an object, which ensures us that p_global_lock has 
     134     * been properly initialized. We can now atomically check that we are 
     135     * the only p_vlc object. */ 
     136    vlc_mutex_lock( p_vlc->p_global_lock ); 
     137    if( i_vlc != 1 ) 
     138    { 
     139        b_failed = VLC_TRUE; 
     140    } 
     141    vlc_mutex_unlock( p_vlc->p_global_lock ); 
     142 
     143    /* There can be only one */ 
     144    if( b_failed ) 
     145    { 
     146        vlc_destroy_r( p_vlc ); 
     147        return VLC_EGENERIC; 
     148    } 
     149 
     150    return VLC_SUCCESS; 
    124151} 
    125152 
     
    149176    vlc_mutex_init( p_vlc, &p_vlc->config_lock ); 
    150177    vlc_mutex_init( p_vlc, &p_vlc->structure_lock ); 
    151  
    152     /* Set signal handling policy for all threads */ 
    153 #ifndef WIN32 
    154     InitSignalHandler( ); 
    155 #endif 
    156178 
    157179    /* Store our newly allocated structure in the global list */ 
     
    181203vlc_error_t vlc_init( int i_argc, char *ppsz_argv[] ) 
    182204{ 
    183     return vlc_init_r( ( i_vlc == 1 ) ? *pp_vlc : NULL, i_argc, ppsz_argv ); 
     205    return vlc_init_r( ( i_vlc == 1 ) ? pp_vlc[0] : NULL, i_argc, ppsz_argv ); 
    184206} 
    185207 
     
    197219        return VLC_ESTATUS; 
    198220    } 
    199  
    200     fprintf( stderr, COPYRIGHT_MESSAGE "\n" ); 
    201221 
    202222    /* Guess what CPU we have */ 
     
    512532vlc_error_t vlc_run( void ) 
    513533{ 
    514     return vlc_run_r( ( i_vlc == 1 ) ? *pp_vlc : NULL ); 
     534    return vlc_run_r( ( i_vlc == 1 ) ? pp_vlc[0] : NULL ); 
    515535} 
    516536 
     
    540560vlc_error_t vlc_add_intf( const char *psz_module, vlc_bool_t b_block ) 
    541561{ 
    542     return vlc_add_intf_r( ( i_vlc == 1 ) ? *pp_vlc : NULL, 
     562    return vlc_add_intf_r( ( i_vlc == 1 ) ? pp_vlc[0] : NULL, 
    543563                           psz_module, b_block ); 
    544564} 
     
    603623vlc_error_t vlc_stop( void ) 
    604624{ 
    605     return vlc_stop_r( ( i_vlc == 1 ) ? *pp_vlc : NULL ); 
     625    return vlc_stop_r( ( i_vlc == 1 ) ? pp_vlc[0] : NULL ); 
    606626} 
    607627 
     
    680700vlc_error_t vlc_end( void ) 
    681701{ 
    682     return vlc_end_r( ( i_vlc == 1 ) ? *pp_vlc : NULL ); 
     702    return vlc_end_r( ( i_vlc == 1 ) ? pp_vlc[0] : NULL ); 
    683703} 
    684704 
     
    738758vlc_error_t vlc_destroy( void ) 
    739759{ 
    740     return vlc_destroy_r( ( i_vlc == 1 ) ? *pp_vlc : NULL ); 
     760    return vlc_destroy_r( ( i_vlc == 1 ) ? pp_vlc[0] : NULL ); 
    741761} 
    742762 
     
    803823} 
    804824 
     825/***************************************************************************** 
     826 * vlc_die: ask vlc to die. 
     827 ***************************************************************************** 
     828 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other 
     829 * task. It is your duty to call vlc_end and vlc_destroy afterwards. 
     830 *****************************************************************************/ 
     831vlc_error_t vlc_die( void ) 
     832{ 
     833    return vlc_die_r( ( i_vlc == 1 ) ? pp_vlc[0] : NULL ); 
     834} 
     835 
     836vlc_error_t vlc_die_r( vlc_t *p_vlc ) 
     837{ 
     838    if( !p_vlc ) 
     839    { 
     840        fprintf( stderr, "error: invalid status (!EXIST)\n" ); 
     841        return VLC_ESTATUS; 
     842    } 
     843 
     844    p_vlc->b_die = VLC_TRUE; 
     845 
     846    return VLC_SUCCESS; 
     847} 
     848 
     849/***************************************************************************** 
     850 * vlc_status: return the current vlc status. 
     851 ***************************************************************************** 
     852 * This function returns the current value of p_vlc->i_status. 
     853 *****************************************************************************/ 
    805854vlc_status_t vlc_status( void ) 
    806855{ 
    807     return vlc_status_r( ( i_vlc == 1 ) ? *pp_vlc : NULL ); 
     856    return vlc_status_r( ( i_vlc == 1 ) ? pp_vlc[0] : NULL ); 
    808857} 
    809858 
     
    818867} 
    819868 
     869/***************************************************************************** 
     870 * vlc_add_target: adds a target for playing. 
     871 ***************************************************************************** 
     872 * This function adds psz_target to the current playlist. If a playlist does 
     873 * not exist, it will create one. 
     874 *****************************************************************************/ 
    820875vlc_error_t vlc_add_target( const char *psz_target, int i_mode, int i_pos ) 
    821876{ 
    822     return vlc_add_target_r( ( i_vlc == 1 ) ? *pp_vlc : NULL, 
     877    return vlc_add_target_r( ( i_vlc == 1 ) ? pp_vlc[0] : NULL, 
    823878                             psz_target, i_mode, i_pos ); 
    824879} 
     
    11781233#endif 
    11791234 
    1180 #ifndef WIN32 
    1181 /***************************************************************************** 
    1182  * InitSignalHandler: system signal handler initialization 
    1183  ***************************************************************************** 
    1184  * Set the signal handlers. SIGTERM is not intercepted, because we need at 
    1185  * at least a method to kill the program when all other methods failed, and 
    1186  * when we don't want to use SIGKILL. 
    1187  *****************************************************************************/ 
    1188 static void InitSignalHandler( void ) 
    1189 { 
    1190     /* Termination signals */ 
    1191     signal( SIGINT,  FatalSignalHandler ); 
    1192     signal( SIGHUP,  FatalSignalHandler ); 
    1193     signal( SIGQUIT, FatalSignalHandler ); 
    1194  
    1195     /* Other signals */ 
    1196     signal( SIGALRM, SIG_IGN ); 
    1197     signal( SIGPIPE, SIG_IGN ); 
    1198 } 
    1199  
    1200 /***************************************************************************** 
    1201  * FatalSignalHandler: system signal handler 
    1202  ***************************************************************************** 
    1203  * This function is called when a fatal signal is received by the program. 
    1204  * It tries to end the program in a clean way. 
    1205  *****************************************************************************/ 
    1206 static void FatalSignalHandler( int i_signal ) 
    1207 { 
    1208     static mtime_t abort_time = 0; 
    1209     static volatile vlc_bool_t b_die = VLC_FALSE; 
    1210     int i_index; 
    1211  
    1212     /* Once a signal has been trapped, the termination sequence will be 
    1213      * armed and following signals will be ignored to avoid sending messages 
    1214      * to an interface having been destroyed */ 
    1215  
    1216     if( !b_die ) 
    1217     { 
    1218         b_die = VLC_TRUE; 
    1219         abort_time = mdate(); 
    1220  
    1221         fprintf( stderr, "signal %d received, terminating libvlc - do it " 
    1222                          "again in case your process gets stuck\n", i_signal ); 
    1223  
    1224         /* Try to terminate everything - this is done by requesting the end of 
    1225          * all the p_vlc structures */ 
    1226         for( i_index = 0 ; i_index < i_vlc ; i_index++ ) 
    1227         { 
    1228             /* Acknowledge the signal received */ 
    1229             pp_vlc[ i_index ]->b_die = VLC_TRUE; 
    1230         } 
    1231     } 
    1232     else if( mdate() > abort_time + 1000000 ) 
    1233     { 
    1234         /* If user asks again 1 second later, die badly */ 
    1235         signal( SIGINT,  SIG_IGN ); 
    1236         signal( SIGHUP,  SIG_IGN ); 
    1237         signal( SIGQUIT, SIG_IGN ); 
    1238  
    1239         fprintf( stderr, "user insisted too much, dying badly\n" ); 
    1240  
    1241         exit( 1 ); 
    1242     } 
    1243 } 
    1244 #endif 
    1245  
  • src/misc/cpu.c

    rb9e9cb4 r2fa6c9c  
    33 ***************************************************************************** 
    44 * Copyright (C) 1998-2002 VideoLAN 
    5  * $Id: cpu.c,v 1.4 2002/06/07 14:30:41 sam Exp $ 
     5 * $Id: cpu.c,v 1.5 2002/08/19 11:13:45 sam Exp $ 
    66 * 
    77 * Authors: Samuel Hocevar <sam@zoy.org> 
     
    4444 * Local prototypes 
    4545 *****************************************************************************/ 
    46 static void IllegalSignalHandler( int i_signal ); 
     46static void SigHandler   ( int ); 
     47static u32  Capabilities ( vlc_object_t * ); 
    4748 
    4849/***************************************************************************** 
     
    5657 
    5758/***************************************************************************** 
    58  * CPUCapabilities: list the processors MMX support and other capabilities 
     59 * CPUCapabilities: get the CPU capabilities 
     60 ***************************************************************************** 
     61 * This function is a wrapper around Capabilities(). 
     62 *****************************************************************************/ 
     63u32 __CPUCapabilities( vlc_object_t *p_this ) 
     64
     65    u32 i_capabilities; 
     66 
     67    vlc_mutex_lock( p_this->p_vlc->p_global_lock ); 
     68    i_capabilities = Capabilities( p_this ); 
     69    vlc_mutex_unlock( p_this->p_vlc->p_global_lock ); 
     70     
     71    return i_capabilities; 
     72
     73 
     74/***************************************************************************** 
     75 * Capabilities: list the processors MMX support and other capabilities 
    5976 ***************************************************************************** 
    6077 * This function is called to list extensions the CPU may have. 
    6178 *****************************************************************************/ 
    62 u32 __CPUCapabilities( vlc_object_t *p_this ) 
     79static u32 Capabilities( vlc_object_t *p_this ) 
    6380{ 
    6481    volatile u32 i_capabilities = CPU_CAPABILITY_NONE; 
     
    113130                     : "cc" ); 
    114131 
     132#   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) 
     133    sighandler_t pf_sigill = signal( SIGILL, SigHandler ); 
     134#   endif 
     135 
    115136    i_capabilities |= CPU_CAPABILITY_FPU; 
    116  
    117 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) 
    118     signal( SIGILL, IllegalSignalHandler ); 
    119 #   endif 
    120137 
    121138    /* test for a 486 CPU */ 
     
    139156    { 
    140157#   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) 
    141         signal( SIGILL, NULL ); 
     158        signal( SIGILL, pf_sigill ); 
    142159#   endif 
    143160        return i_capabilities; 
     
    152169    { 
    153170#   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) 
    154         signal( SIGILL, NULL ); 
     171        signal( SIGILL, pf_sigill ); 
    155172#   endif 
    156173        return i_capabilities; 
     
    170187    { 
    171188#   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) 
    172         signal( SIGILL, NULL ); 
     189        signal( SIGILL, pf_sigill ); 
    173190#   endif 
    174191        return i_capabilities; 
     
    186203        i_illegal = 0; 
    187204 
    188         vlc_mutex_lock( p_this->p_vlc->p_global_lock ); 
    189205        if( setjmp( env ) == 0 ) 
    190206        { 
     
    192208            __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : ); 
    193209        } 
    194         vlc_mutex_unlock( p_this->p_vlc->p_global_lock ); 
    195210 
    196211        if( i_illegal == 0 ) 
     
    207222    { 
    208223#   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) 
    209         signal( SIGILL, NULL ); 
     224        signal( SIGILL, pf_sigill ); 
    210225#   endif 
    211226        return i_capabilities; 
     
    221236        i_illegal = 0; 
    222237 
    223         vlc_mutex_lock( p_this->p_vlc->p_global_lock ); 
    224238        if( setjmp( env ) == 0 ) 
    225239        { 
     
    227241            __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : ); 
    228242        } 
    229         vlc_mutex_unlock( p_this->p_vlc->p_global_lock ); 
    230243 
    231244        if( i_illegal == 0 ) 
     
    242255 
    243256#   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) 
    244     signal( SIGILL, NULL ); 
     257    signal( SIGILL, pf_sigill ); 
    245258#   endif 
    246259    return i_capabilities; 
     
    248261#elif defined( __powerpc__ ) 
    249262 
     263#   ifdef CAN_COMPILE_ALTIVEC 
     264    sighandler_t pf_sigill = signal( SIGILL, SigHandler ); 
     265 
    250266    i_capabilities |= CPU_CAPABILITY_FPU; 
    251267 
    252 #   ifdef CAN_COMPILE_ALTIVEC 
    253     signal( SIGILL, IllegalSignalHandler ); 
    254  
    255268    i_illegal = 0; 
    256269 
    257     vlc_mutex_lock( p_this->p_vlc->p_global_lock ); 
    258270    if( setjmp( env ) == 0 ) 
    259271    { 
     
    263275                      : "r" (-1)); 
    264276    } 
    265     vlc_mutex_unlock( p_this->p_vlc->p_global_lock ); 
    266277 
    267278    if( i_illegal == 0 ) 
     
    270281    } 
    271282 
    272     signal( SIGILL, NULL ); 
     283    signal( SIGILL, pf_sigill ); 
    273284#   endif 
    274285 
     
    288299 
    289300/***************************************************************************** 
    290  * IllegalSignalHandler: system signal handler 
     301 * SigHandler: system signal handler 
    291302 ***************************************************************************** 
    292303 * This function is called when an illegal instruction signal is received by 
    293304 * the program. We use this function to test OS and CPU capabilities 
    294305 *****************************************************************************/ 
    295 static void IllegalSignalHandler( int i_signal ) 
     306static void SigHandler( int i_signal ) 
    296307{ 
    297308    /* Acknowledge the signal received */ 
  • src/vlc.c

    r7689bc9 r2fa6c9c  
    33 ***************************************************************************** 
    44 * Copyright (C) 1998-2001 VideoLAN 
    5  * $Id: vlc.c,v 1.8 2002/08/08 00:35:11 sam Exp $ 
     5 * $Id: vlc.c,v 1.9 2002/08/19 11:13:45 sam Exp $ 
    66 * 
    77 * Authors: Vincent Seguin <seguin@via.ecp.fr> 
    88 *          Samuel Hocevar <sam@zoy.org> 
    99 *          Gildas Bazin <gbazin@netcourrier.com> 
     10 *          Lots of other people, see the libvlc AUTHORS file 
    1011 * 
    1112 * This program is free software; you can redistribute it and/or modify 
     
    2324 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. 
    2425 *****************************************************************************/ 
     26 
    2527#include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */ 
    2628#include <stdio.h>                                              /* fprintf() */ 
    2729#include <stdlib.h>                                  /* putenv(), strtol(),  */ 
     30#include <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */ 
    2831 
    2932#include <vlc/vlc.h> 
    3033 
    3134/***************************************************************************** 
     35 * Local prototypes. 
     36 *****************************************************************************/ 
     37#ifndef WIN32 
     38static void SigHandler  ( int i_signal ); 
     39#endif 
     40 
     41/***************************************************************************** 
    3242 * main: parse command line, start interface and spawn threads 
    3343 *****************************************************************************/ 
    34 int main(int i_argc, char *ppsz_argv[]
     44int main( int i_argc, char *ppsz_argv[]
    3545{ 
    3646    vlc_error_t err; 
     47 
     48    fprintf( stderr, COPYRIGHT_MESSAGE "\n" ); 
    3749 
    3850#ifdef SYS_LINUX 
     
    4658#endif 
    4759 
    48     /* Create the vlc structure */ 
     60    /* Create a libvlc structure */ 
    4961    err = vlc_create(); 
    5062    if( err != VLC_SUCCESS ) 
     
    5365    } 
    5466 
    55     /* Initialize vlc */ 
     67#ifndef WIN32 
     68    /* Set the signal handlers. SIGTERM is not intercepted, because we need at 
     69     * least one method to kill the program when all other methods failed, and 
     70     * when we don't want to use SIGKILL. 
     71     * Note that we set the signals after the vlc_create call. */ 
     72    signal( SIGINT,  SigHandler ); 
     73    signal( SIGHUP,  SigHandler ); 
     74    signal( SIGQUIT, SigHandler ); 
     75 
     76    /* Other signals */ 
     77    signal( SIGALRM, SIG_IGN ); 
     78    signal( SIGPIPE, SIG_IGN ); 
     79#endif 
     80 
     81    /* Initialize libvlc */ 
    5682    err = vlc_init( i_argc, ppsz_argv ); 
    5783    if( err != VLC_SUCCESS ) 
     
    6187    } 
    6288 
    63     /* Run vlc, in non-blocking mode */ 
     89    /* Run libvlc, in non-blocking mode */ 
    6490    err = vlc_run(); 
    6591 
     
    84110    vlc_end(); 
    85111 
    86     /* Destroy the vlc structure */ 
     112    /* Destroy the libvlc structure */ 
    87113    vlc_destroy(); 
    88114 
     
    90116} 
    91117 
     118#ifndef WIN32 
     119/***************************************************************************** 
     120 * SigHandler: system signal handler 
     121 ***************************************************************************** 
     122 * This function is called when a fatal signal is received by the program. 
     123 * It tries to end the program in a clean way. 
     124 *****************************************************************************/ 
     125static void SigHandler( int i_signal ) 
     126{ 
     127    static mtime_t abort_time = 0; 
     128    static volatile vlc_bool_t b_die = VLC_FALSE; 
     129 
     130    /* Once a signal has been trapped, the termination sequence will be 
     131     * armed and subsequent signals will be ignored to avoid sending signals 
     132     * to a libvlc structure having been destroyed */ 
     133 
     134    if( !b_die ) 
     135    { 
     136        b_die = VLC_TRUE; 
     137        abort_time = mdate(); 
     138 
     139        fprintf( stderr, "signal %d received, terminating vlc - do it " 
     140                         "again in case it gets stuck\n", i_signal ); 
     141 
     142        /* Acknowledge the signal received */ 
     143        vlc_die(); 
     144    } 
     145    else if( mdate() > abort_time + 1000000 ) 
     146    { 
     147        /* If user asks again 1 second later, die badly */ 
     148        signal( SIGINT,  SIG_DFL ); 
     149        signal( SIGHUP,  SIG_DFL ); 
     150        signal( SIGQUIT, SIG_DFL ); 
     151        signal( SIGALRM, SIG_DFL ); 
     152        signal( SIGPIPE, SIG_DFL ); 
     153 
     154        fprintf( stderr, "user insisted too much, dying badly\n" ); 
     155 
     156        exit( 1 ); 
     157    } 
     158} 
     159#endif 
     160