signals.c 3.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*****************************************************************************
 * signals.c : signals handler module for vlc
 *****************************************************************************
 * Copyright (C) 2008 Rémi Denis-Courmont
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <pthread.h>
#include <signal.h>
#include <time.h>

#include <vlc/vlc.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 ();

struct intf_sys_t
{
    pthread_t       thread;
    int             signum;
};

static int Open (vlc_object_t *obj)
{
    intf_thread_t *intf = (intf_thread_t *)obj;
    intf_sys_t *p_sys = malloc (sizeof (*p_sys));

    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))
    {
        free (p_sys);
        intf->p_sys = NULL;
        return VLC_ENOMEM;
    }

    intf->pf_run = Run;
    return 0;
}

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);
81
82
83
84
85
86
#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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    pthread_join (p_sys->thread, NULL);
    free (p_sys);
}

static void *SigThread (void *data)
{
    intf_thread_t *obj = data;
    intf_sys_t *p_sys = obj->p_sys;
    sigset_t set;

    sigemptyset (&set);
    sigaddset (&set, SIGHUP);
    sigaddset (&set, SIGINT);
    sigaddset (&set, SIGQUIT);
    sigaddset (&set, SIGTERM);

    sigaddset (&set, SIGCHLD);

    for (;;)
    {
        int signum;

        sigwait (&set, &signum);

111
112
113
114
115
116
#ifdef __APPLE__
        /* In Mac OS X up to 10.5 sigwait (among others) is not a pthread
         * cancellation point */
        pthread_testcancel();
#endif

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
        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;

    vlc_object_lock (obj);
    do
    {
        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;
        }
    }
    while (!vlc_object_wait (obj));

out:
    vlc_object_unlock (obj);
    vlc_object_kill (obj->p_libvlc);
}