XMMS2
signal_unix.c
Go to the documentation of this file.
1 /* XMMS2 - X Music Multiplexer System
2  * Copyright (C) 2003-2011 XMMS2 Team
3  *
4  * PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  */
16 
17 
18 
19 
20 /** @file
21  * Takes care of unix-signals.
22  */
23 
24 
25 #include "xmmspriv/xmms_signal.h"
27 #include "xmms/xmms_log.h"
28 #include "xmms/xmms_object.h"
29 
30 #include <stdlib.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <string.h>
34 #include <glib.h>
35 #include <unistd.h>
36 
37 static sigset_t osignals;
38 
39 static gpointer
40 sigwaiter (gpointer data)
41 {
42  xmms_object_t *obj = (xmms_object_t *) data;
44  sigset_t signals;
45  int caught;
46 
47  xmms_set_thread_name ("x2 sig waiter");
48 
49  sigemptyset(&signals);
50  sigaddset (&signals, SIGINT);
51  sigaddset (&signals, SIGTERM);
52 
53  while (1337) {
54  sigwait (&signals, &caught);
55 
56  switch (caught){
57  case SIGINT:
58  case SIGTERM:
59  pthread_sigmask (SIG_UNBLOCK, &signals, NULL);
60 
61  xmms_log_info ("Bye!");
62 
64  memset (&arg, 0, sizeof (arg));
65  arg.args = xmmsv_new_list ();
66  xmms_error_reset (&arg.error);
68  xmmsv_unref (arg.args);
69  break;
70  }
71  }
72 
73  return 0;
74 }
75 
76 void
78 {
79  sigset_t signals;
80 
81  sigemptyset(&signals);
82 
83  sigaddset (&signals, SIGHUP);
84  sigaddset (&signals, SIGTERM);
85  sigaddset (&signals, SIGINT);
86 
87  pthread_sigmask (SIG_BLOCK, &signals, &osignals);
88 
89  /* Thanks to bug #8533731 in CoreServices on Mac OS X, calling
90  * FindComponent/AudioComponentNext in the CoreAudio output
91  * plugin will cause SIGPIPE to be unblocked. To solve this
92  * we have to fend off SIGPIPE here instead of via sigmask.
93  * Doesn't affect the behavior on other platforms.
94  */
95  signal (SIGPIPE, SIG_IGN);
96 }
97 
98 void
100 {
101  pthread_sigmask (SIG_SETMASK, &osignals, NULL);
102 }
103 
104 void
106 {
107  g_thread_create (sigwaiter, obj, FALSE, NULL);
108 }
void xmms_set_thread_name(const gchar *name)
void xmmsv_unref(xmmsv_t *val)
Decreases the references for the xmmsv_t When the number of references reaches 0 it will be freed...
Definition: value.c:303
void xmms_signal_restore(void)
Definition: signal_unix.c:99
void xmms_object_cmd_arg_init(xmms_object_cmd_arg_t *arg)
Initialize a command argument.
Definition: object.c:236
void xmms_signal_block(void)
Definition: signal_unix.c:77
xmmsv_t * xmmsv_new_list(void)
Allocates a new list xmmsv_t.
Definition: value.c:250
void xmms_object_cmd_call(xmms_object_t *object, guint cmdid, xmms_object_cmd_arg_t *arg)
Call a command with argument.
Definition: object.c:338
#define xmms_log_info(fmt,...)
Definition: xmms_log.h:34
xmms_error_t error
Definition: xmms_object.h:72
void xmms_signal_init(xmms_object_t *obj)
Definition: signal_unix.c:105