]> git.sesse.net Git - vlc/blobdiff - modules/control/signals.c
ParseExecute: robustify and cleanup
[vlc] / modules / control / signals.c
index deb098409dc5c8dc3625fa126f6249d434a06169..5cb6f369b94437bc5a6c552a3829464ea5b54e11 100644 (file)
 # include "config.h"
 #endif
 
-#include <pthread.h>
 #include <signal.h>
 #include <time.h>
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_interface.h>
 
 static int  Open (vlc_object_t *);
 static void Close (vlc_object_t *);
-static void Run (intf_thread_t *);
 static void *SigThread (void *);
 
-vlc_module_begin ();
-    set_shortname (N_("Signals"));
-    set_category (CAT_INTERFACE);
-    set_subcategory (SUBCAT_INTERFACE_CONTROL);
-    set_description (N_("POSIX signals handling interface"));
-    set_capability ("interface", 0);
-    set_callbacks (Open, Close);
-vlc_module_end ();
+vlc_module_begin ()
+    set_shortname (N_("Signals"))
+    set_category (CAT_INTERFACE)
+    set_subcategory (SUBCAT_INTERFACE_CONTROL)
+    set_description (N_("POSIX signals handling interface"))
+    set_capability ("interface", 0)
+    set_callbacks (Open, Close)
+vlc_module_end ()
 
 struct intf_sys_t
 {
-    pthread_t       thread;
-    int             signum;
+    vlc_thread_t    thread;
 };
 
 static int Open (vlc_object_t *obj)
@@ -58,17 +55,16 @@ static int Open (vlc_object_t *obj)
     if (p_sys == NULL)
         return VLC_ENOMEM;
 
-    p_sys->signum = 0;
     intf->p_sys = p_sys;
 
-    if (pthread_create (&p_sys->thread, NULL, SigThread, obj))
+    if (vlc_clone (&p_sys->thread, SigThread, obj, VLC_THREAD_PRIORITY_LOW))
     {
         free (p_sys);
         intf->p_sys = NULL;
         return VLC_ENOMEM;
     }
 
-    intf->pf_run = Run;
+    intf->pf_run = NULL;
     return 0;
 }
 
@@ -77,71 +73,60 @@ static void Close (vlc_object_t *obj)
     intf_thread_t *intf = (intf_thread_t *)obj;
     intf_sys_t *p_sys = intf->p_sys;
 
-    pthread_cancel (p_sys->thread);
+    vlc_cancel (p_sys->thread);
 #ifdef __APPLE__
    /* In Mac OS X up to 10.5 sigwait (among others) is not a pthread
     * cancellation point, so we throw a dummy quit signal to end
     * sigwait() in the sigth thread */
     pthread_kill (p_sys->thread, SIGQUIT);
 # endif
-    pthread_join (p_sys->thread, NULL);
+    vlc_join (p_sys->thread, NULL);
     free (p_sys);
 }
 
+static bool ignored (int signum)
+{
+    struct sigaction sa;
+
+    if (sigaction (signum, NULL, &sa))
+        return false;
+    return ((sa.sa_flags & SA_SIGINFO)
+            ? (void *)sa.sa_sigaction : (void *)sa.sa_handler) == SIG_IGN;
+}
+
 static void *SigThread (void *data)
 {
     intf_thread_t *obj = data;
-    intf_sys_t *p_sys = obj->p_sys;
     sigset_t set;
+    int signum;
 
     sigemptyset (&set);
-    sigaddset (&set, SIGHUP);
+    if (!ignored (SIGHUP)) /* <- needed to handle nohup properly */
+        sigaddset (&set, SIGHUP);
     sigaddset (&set, SIGINT);
     sigaddset (&set, SIGQUIT);
     sigaddset (&set, SIGTERM);
 
     sigaddset (&set, SIGCHLD);
 
-    for (;;)
+    do
     {
-        int signum;
-
-        sigwait (&set, &signum);
+        while (sigwait (&set, &signum));
 
 #ifdef __APPLE__
         /* In Mac OS X up to 10.5 sigwait (among others) is not a pthread
          * cancellation point */
-        pthread_testcancel();
+        vlc_testcancel();
 #endif
-
-        vlc_object_lock (obj);
-        p_sys->signum = signum;
-        vlc_object_signal_unlocked (obj);
-        vlc_object_unlock (obj);
     }
-}
-
-static void Run (intf_thread_t *obj)
-{
-    intf_sys_t *p_sys = obj->p_sys;
+    while (signum == SIGCHLD);
 
-    vlc_object_lock (obj);
-    while (vlc_object_alive (obj))
-    {
-        switch (p_sys->signum)
-        {
-            case SIGINT:
-            case SIGHUP:
-            case SIGTERM:
-            case SIGQUIT:
-                msg_Err (obj, "Caught %s signal, exiting...",
-                         strsignal (p_sys->signum));
-                goto out;
-        }
-        vlc_object_wait (obj);
-    }
+    msg_Err (obj, "Caught %s signal, exiting...", strsignal (signum));
+    libvlc_Quit (obj->p_libvlc);
 
-out:
-    vlc_object_unlock (obj);
-    vlc_object_kill (obj->p_libvlc);
+    /* After 3 seconds, fallback to normal signal handling */
+    msleep (3 * CLOCK_FREQ);
+    pthread_sigmask (SIG_UNBLOCK, &set, NULL);
+    for (;;)
+        pause ();
 }