1 /**
2  * \file async.c
3  * \brief Async notification helpers
4  * \author Abramo Bagnara <abramo@alsa-project.org>
5  * \date 2001
6  */
7 /*
8  *  Async notification helpers
9  *  Copyright (c) 2001 by Abramo Bagnara <abramo@alsa-project.org>
10  *
11  *   This library is free software; you can redistribute it and/or modify
12  *   it under the terms of the GNU Lesser General Public License as
13  *   published by the Free Software Foundation; either version 2.1 of
14  *   the License, or (at your option) any later version.
15  *
16  *   This program is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU Lesser General Public License for more details.
20  *
21  *   You should have received a copy of the GNU Lesser General Public
22  *   License along with this library; if not, write to the Free Software
23  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
24  *
25  */
26 
27 #include "pcm/pcm_local.h"
28 #include "control/control_local.h"
29 #include <signal.h>
30 
31 static struct sigaction previous_action;
32 #define MAX_SIG_FUNCTION_CODE 10 /* i.e. SIG_DFL SIG_IGN SIG_HOLD et al */
33 
34 #ifdef SND_ASYNC_RT_SIGNAL
35 /** async signal number */
36 static int snd_async_signo;
37 
38 void snd_async_init(void) __attribute__ ((constructor));
39 
snd_async_init(void)40 void snd_async_init(void)
41 {
42 	snd_async_signo = __libc_allocate_rtsig(0);
43 	if (snd_async_signo < 0) {
44 		SNDERR("Unable to find a RT signal to use for snd_async");
45 		exit(1);
46 	}
47 }
48 #else
49 /** async signal number */
50 static const int snd_async_signo = SIGIO;
51 #endif
52 
53 static LIST_HEAD(snd_async_handlers);
54 
snd_async_handler(int signo ATTRIBUTE_UNUSED,siginfo_t * siginfo,void * context ATTRIBUTE_UNUSED)55 static void snd_async_handler(int signo ATTRIBUTE_UNUSED, siginfo_t *siginfo, void *context ATTRIBUTE_UNUSED)
56 {
57 #if defined(__DragonFly__) || defined(__FreeBSD__)
58 	/* XXX XXX XXX */
59 	struct list_head *i;
60 	list_for_each(i, &snd_async_handlers) {
61 		snd_async_handler_t *h = list_entry(i, snd_async_handler_t, glist);
62 		if (h->callback)
63 			h->callback(h);
64 	}
65 #else
66 	int fd;
67 	struct list_head *i;
68 	//assert(siginfo->si_code == SI_SIGIO);
69 	if (signo == SIGIO
70 	 && (unsigned long)(previous_action.sa_sigaction) > MAX_SIG_FUNCTION_CODE)
71 		previous_action.sa_sigaction(signo, siginfo, context);
72 	fd = siginfo->si_fd;
73 	list_for_each(i, &snd_async_handlers) {
74 		snd_async_handler_t *h = list_entry(i, snd_async_handler_t, glist);
75 		if (h->fd == fd && h->callback)
76 			h->callback(h);
77 	}
78 #endif
79 }
80 
81 /**
82  * \brief Registers an async handler.
83  * \param handler The function puts the pointer to the new async handler
84  *                object at the address specified by \p handler.
85  * \param fd The file descriptor to be associated with the callback.
86  * \param callback The async callback function.
87  * \param private_data Private data for the async callback function.
88  * \result Zero if successful, otherwise a negative error code.
89  *
90  * This function associates the callback function with the given file,
91  * and saves this association in a \c snd_async_handler_t object.
92  *
93  * Whenever the \c SIGIO signal is raised for the file \p fd, the callback
94  * function will be called with its parameter pointing to the async handler
95  * object returned by this function.
96  *
97  * The ALSA \c sigaction handler for the \c SIGIO signal automatically
98  * multiplexes the notifications to the registered async callbacks.
99  * However, the application is responsible for instructing the device driver
100  * to generate the \c SIGIO signal.
101  *
102  * The \c SIGIO signal may have been replaced with another signal,
103  * see #snd_async_handler_get_signo.
104  *
105  * When the async handler isn't needed anymore, you must delete it with
106  * #snd_async_del_handler.
107  *
108  * \see snd_async_add_pcm_handler, snd_async_add_ctl_handler
109  */
snd_async_add_handler(snd_async_handler_t ** handler,int fd,snd_async_callback_t callback,void * private_data)110 int snd_async_add_handler(snd_async_handler_t **handler, int fd,
111 			  snd_async_callback_t callback, void *private_data)
112 {
113 	snd_async_handler_t *h;
114 	int was_empty;
115 	assert(handler);
116 	h = malloc(sizeof(*h));
117 	if (!h)
118 		return -ENOMEM;
119 	h->fd = fd;
120 	h->callback = callback;
121 	h->private_data = private_data;
122 	was_empty = list_empty(&snd_async_handlers);
123 	list_add_tail(&h->glist, &snd_async_handlers);
124 	INIT_LIST_HEAD(&h->hlist);
125 	*handler = h;
126 	if (was_empty) {
127 		int err;
128 		struct sigaction act;
129 		memset(&act, 0, sizeof(act));
130 		act.sa_flags = SA_RESTART | SA_SIGINFO;
131 		act.sa_sigaction = snd_async_handler;
132 		sigemptyset(&act.sa_mask);
133 		assert(!previous_action.sa_sigaction);
134 		err = sigaction(snd_async_signo, &act, &previous_action);
135 		if (err < 0) {
136 			SYSERR("sigaction");
137 			return -errno;
138 		}
139 	}
140 	return 0;
141 }
142 
143 /**
144  * \brief Deletes an async handler.
145  * \param handler Handle of the async handler to delete.
146  * \result Zero if successful, otherwise a negative error code.
147  */
snd_async_del_handler(snd_async_handler_t * handler)148 int snd_async_del_handler(snd_async_handler_t *handler)
149 {
150 	int err = 0;
151 	int was_empty = list_empty(&snd_async_handlers);
152 	assert(handler);
153 	list_del(&handler->glist);
154 	if (!was_empty
155 	 && list_empty(&snd_async_handlers)) {
156 		err = sigaction(snd_async_signo, &previous_action, NULL);
157 		if (err < 0) {
158 			SYSERR("sigaction");
159 			return -errno;
160 		}
161 		memset(&previous_action, 0, sizeof(previous_action));
162 	}
163 	if (handler->type == SND_ASYNC_HANDLER_GENERIC)
164 		goto _end;
165 	if (!list_empty(&handler->hlist))
166 		list_del(&handler->hlist);
167 	if (!list_empty(&handler->hlist))
168 		goto _end;
169 	switch (handler->type) {
170 #ifdef BUILD_PCM
171 	case SND_ASYNC_HANDLER_PCM:
172 		err = snd_pcm_async(handler->u.pcm, -1, 1);
173 		break;
174 #endif
175 	case SND_ASYNC_HANDLER_CTL:
176 		err = snd_ctl_async(handler->u.ctl, -1, 1);
177 		break;
178 	default:
179 		assert(0);
180 	}
181  _end:
182 	free(handler);
183 	return err;
184 }
185 
186 /**
187  * \brief Returns the signal number assigned to an async handler.
188  * \param handler Handle to an async handler.
189  * \result The signal number if successful, otherwise a negative error code.
190  *
191  * The signal number for async handlers usually is \c SIGIO,
192  * but wizards can redefine it to a realtime signal
193  * when compiling the ALSA library.
194  */
snd_async_handler_get_signo(snd_async_handler_t * handler)195 int snd_async_handler_get_signo(snd_async_handler_t *handler)
196 {
197 	assert(handler);
198 	return snd_async_signo;
199 }
200 
201 /**
202  * \brief Returns the file descriptor assigned to an async handler.
203  * \param handler Handle to an async handler.
204  * \result The file descriptor if successful, otherwise a negative error code.
205  */
snd_async_handler_get_fd(snd_async_handler_t * handler)206 int snd_async_handler_get_fd(snd_async_handler_t *handler)
207 {
208 	assert(handler);
209 	return handler->fd;
210 }
211 
212 /**
213  * \brief Returns the private data assigned to an async handler.
214  * \param handler Handle to an async handler.
215  * \result The \c private_data value registered with the async handler.
216  */
snd_async_handler_get_callback_private(snd_async_handler_t * handler)217 void *snd_async_handler_get_callback_private(snd_async_handler_t *handler)
218 {
219 	assert(handler);
220 	return handler->private_data;
221 }
222 
223