1 /*****************************************************************************
2 * signals.c : signals handler module for vlc
3 *****************************************************************************
4 * Copyright (C) 2008 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
30 #include <vlc_interface.h>
32 static int Open (vlc_object_t *);
33 static void Close (vlc_object_t *);
34 static void *SigThread (void *);
37 set_shortname (N_("Signals"))
38 set_category (CAT_INTERFACE)
39 set_subcategory (SUBCAT_INTERFACE_CONTROL)
40 set_description (N_("POSIX signals handling interface"))
41 set_capability ("interface", 0)
42 set_callbacks (Open, Close)
50 static int Open (vlc_object_t *obj)
52 intf_thread_t *intf = (intf_thread_t *)obj;
53 intf_sys_t *p_sys = malloc (sizeof (*p_sys));
60 if (vlc_clone (&p_sys->thread, SigThread, obj, VLC_THREAD_PRIORITY_LOW))
71 static void Close (vlc_object_t *obj)
73 intf_thread_t *intf = (intf_thread_t *)obj;
74 intf_sys_t *p_sys = intf->p_sys;
76 vlc_cancel (p_sys->thread);
78 /* In Mac OS X up to 10.5 sigwait (among others) is not a pthread
79 * cancellation point, so we throw a dummy quit signal to end
80 * sigwait() in the sigth thread */
81 pthread_kill (p_sys->thread, SIGQUIT);
83 vlc_join (p_sys->thread, NULL);
87 static bool ignored (int signum)
91 if (sigaction (signum, NULL, &sa))
93 return ((sa.sa_flags & SA_SIGINFO)
94 ? (void *)sa.sa_sigaction : (void *)sa.sa_handler) == SIG_IGN;
97 static void *SigThread (void *data)
99 intf_thread_t *obj = data;
104 if (!ignored (SIGHUP)) /* <- needed to handle nohup properly */
105 sigaddset (&set, SIGHUP);
106 sigaddset (&set, SIGINT);
107 sigaddset (&set, SIGQUIT);
108 sigaddset (&set, SIGTERM);
110 sigaddset (&set, SIGCHLD);
114 while (sigwait (&set, &signum));
117 /* In Mac OS X up to 10.5 sigwait (among others) is not a pthread
118 * cancellation point */
122 while (signum == SIGCHLD);
124 msg_Err (obj, "Caught %s signal, exiting...", strsignal (signum));
125 libvlc_Quit (obj->p_libvlc);
127 /* After 3 seconds, fallback to normal signal handling */
128 msleep (3 * CLOCK_FREQ);
129 pthread_sigmask (SIG_UNBLOCK, &set, NULL);