]> git.sesse.net Git - vlc/blob - modules/control/signals.c
Plugins: include vlc_common.h directly instead of vlc/vlc.h
[vlc] / modules / control / signals.c
1 /*****************************************************************************
2  * signals.c : signals handler module for vlc
3  *****************************************************************************
4  * Copyright (C) 2008 RĂ©mi Denis-Courmont
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <pthread.h>
26 #include <signal.h>
27 #include <time.h>
28
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_interface.h>
32
33 static int  Open (vlc_object_t *);
34 static void Close (vlc_object_t *);
35 static void Run (intf_thread_t *);
36 static void *SigThread (void *);
37
38 vlc_module_begin ();
39     set_shortname (N_("Signals"));
40     set_category (CAT_INTERFACE);
41     set_subcategory (SUBCAT_INTERFACE_CONTROL);
42     set_description (N_("POSIX signals handling interface"));
43     set_capability ("interface", 0);
44     set_callbacks (Open, Close);
45 vlc_module_end ();
46
47 struct intf_sys_t
48 {
49     pthread_t       thread;
50     int             signum;
51 };
52
53 static int Open (vlc_object_t *obj)
54 {
55     intf_thread_t *intf = (intf_thread_t *)obj;
56     intf_sys_t *p_sys = malloc (sizeof (*p_sys));
57
58     if (p_sys == NULL)
59         return VLC_ENOMEM;
60
61     p_sys->signum = 0;
62     intf->p_sys = p_sys;
63
64     if (pthread_create (&p_sys->thread, NULL, SigThread, obj))
65     {
66         free (p_sys);
67         intf->p_sys = NULL;
68         return VLC_ENOMEM;
69     }
70
71     intf->pf_run = Run;
72     return 0;
73 }
74
75 static void Close (vlc_object_t *obj)
76 {
77     intf_thread_t *intf = (intf_thread_t *)obj;
78     intf_sys_t *p_sys = intf->p_sys;
79
80     pthread_cancel (p_sys->thread);
81 #ifdef __APPLE__
82    /* In Mac OS X up to 10.5 sigwait (among others) is not a pthread
83     * cancellation point, so we throw a dummy quit signal to end
84     * sigwait() in the sigth thread */
85     pthread_kill (p_sys->thread, SIGQUIT);
86 # endif
87     pthread_join (p_sys->thread, NULL);
88     free (p_sys);
89 }
90
91 static void *SigThread (void *data)
92 {
93     intf_thread_t *obj = data;
94     intf_sys_t *p_sys = obj->p_sys;
95     sigset_t set;
96
97     sigemptyset (&set);
98     sigaddset (&set, SIGHUP);
99     sigaddset (&set, SIGINT);
100     sigaddset (&set, SIGQUIT);
101     sigaddset (&set, SIGTERM);
102
103     sigaddset (&set, SIGCHLD);
104
105     for (;;)
106     {
107         int signum;
108
109         sigwait (&set, &signum);
110
111 #ifdef __APPLE__
112         /* In Mac OS X up to 10.5 sigwait (among others) is not a pthread
113          * cancellation point */
114         pthread_testcancel();
115 #endif
116
117         vlc_object_lock (obj);
118         p_sys->signum = signum;
119         vlc_object_signal_unlocked (obj);
120         vlc_object_unlock (obj);
121     }
122 }
123
124 static void Run (intf_thread_t *obj)
125 {
126     intf_sys_t *p_sys = obj->p_sys;
127
128     vlc_object_lock (obj);
129     while (vlc_object_alive (obj))
130     {
131         switch (p_sys->signum)
132         {
133             case SIGINT:
134             case SIGHUP:
135             case SIGTERM:
136             case SIGQUIT:
137                 msg_Err (obj, "Caught %s signal, exiting...",
138                          strsignal (p_sys->signum));
139                 goto out;
140         }
141         vlc_object_wait (obj);
142     }
143
144 out:
145     vlc_object_unlock (obj);
146     vlc_object_kill (obj->p_libvlc);
147 }