1 /* sigio.c -- Support for SIGIO handler installation and removal
2 * Created: Thu Jun 3 15:39:18 1999 by faith@precisioninsight.com
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
29 * Copyright (c) 2002 by The XFree86 Project, Inc.
31 * Permission is hereby granted, free of charge, to any person obtaining a
32 * copy of this software and associated documentation files (the "Software"),
33 * to deal in the Software without restriction, including without limitation
34 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35 * and/or sell copies of the Software, and to permit persons to whom the
36 * Software is furnished to do so, subject to the following conditions:
38 * The above copyright notice and this permission notice shall be included in
39 * all copies or substantial portions of the Software.
41 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
44 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
45 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
46 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
47 * OTHER DEALINGS IN THE SOFTWARE.
49 * Except as contained in this notice, the name of the copyright holder(s)
50 * and author(s) shall not be used in advertising or otherwise to promote
51 * the sale, use or other dealings in this Software without prior written
52 * authorization from the copyright holder(s) and author(s).
55 #ifdef HAVE_XORG_CONFIG_H
56 #include <xorg-config.h>
62 #include "xf86_OSlib.h"
70 /* MAXDEVICES represents the maximimum number of input devices usable
71 * at the same time plus one entry for DRM support.
73 #define MAX_FUNCS (MAXDEVICES + 1)
78 typedef struct _xf86SigIOFunc
{
79 void (*f
) (int, void *);
84 static Xf86SigIOFunc xf86SigIOFuncs
[MAX_FUNCS
];
85 static int xf86SigIOMax
;
86 static int xf86SigIOMaxFd
;
87 static fd_set xf86SigIOMask
;
90 * SIGIO gives no way of discovering which fd signalled, select
99 int save_errno
= errno
; /* do not clobber the global errno */
102 inSignalContext
= TRUE
;
104 ready
= xf86SigIOMask
;
107 SYSCALL(r
= select(xf86SigIOMaxFd
, &ready
, 0, 0, &to
));
108 for (i
= 0; r
> 0 && i
< xf86SigIOMax
; i
++)
109 if (xf86SigIOFuncs
[i
].f
&& FD_ISSET(xf86SigIOFuncs
[i
].fd
, &ready
)) {
110 (*xf86SigIOFuncs
[i
].f
) (xf86SigIOFuncs
[i
].fd
,
111 xf86SigIOFuncs
[i
].closure
);
115 xf86Msg(X_ERROR
, "SIGIO %d descriptors not handled\n", r
);
117 /* restore global errno */
120 inSignalContext
= FALSE
;
128 if (fstat(fd
, &buf
) < 0)
130 return S_ISFIFO(buf
.st_mode
);
134 xf86InstallSIGIOHandler(int fd
, void (*f
) (int, void *), void *closure
)
137 struct sigaction osa
;
139 int installed
= FALSE
;
141 if (!xf86Info
.useSIGIO
)
144 for (i
= 0; i
< MAX_FUNCS
; i
++) {
145 if (!xf86SigIOFuncs
[i
].f
) {
150 if (fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) | O_ASYNC
) == -1) {
151 xf86Msg(X_WARNING
, "fcntl(%d, O_ASYNC): %s\n",
152 fd
, strerror(errno
));
155 if (fcntl(fd
, F_SETOWN
, getpid()) == -1) {
156 xf86Msg(X_WARNING
, "fcntl(%d, F_SETOWN): %s\n",
157 fd
, strerror(errno
));
164 #ifdef I_SETSIG /* System V Streams - used on Solaris for input devices */
165 if (!installed
&& isastream(fd
)) {
166 if (ioctl(fd
, I_SETSIG
, S_INPUT
| S_ERROR
| S_HANGUP
) == -1) {
167 xf86Msg(X_WARNING
, "fcntl(%d, I_SETSIG): %s\n",
168 fd
, strerror(errno
));
179 sigemptyset(&sa
.sa_mask
);
180 sigaddset(&sa
.sa_mask
, SIGIO
);
182 sa
.sa_handler
= xf86SIGIO
;
183 sigaction(SIGIO
, &sa
, &osa
);
184 xf86SigIOFuncs
[i
].fd
= fd
;
185 xf86SigIOFuncs
[i
].closure
= closure
;
186 xf86SigIOFuncs
[i
].f
= f
;
187 if (i
>= xf86SigIOMax
)
188 xf86SigIOMax
= i
+ 1;
189 if (fd
>= xf86SigIOMaxFd
)
190 xf86SigIOMaxFd
= fd
+ 1;
191 FD_SET(fd
, &xf86SigIOMask
);
195 /* Allow overwriting of the closure and callback */
196 else if (xf86SigIOFuncs
[i
].fd
== fd
) {
197 xf86SigIOFuncs
[i
].closure
= closure
;
198 xf86SigIOFuncs
[i
].f
= f
;
206 xf86RemoveSIGIOHandler(int fd
)
209 struct sigaction osa
;
215 if (!xf86Info
.useSIGIO
)
221 for (i
= 0; i
< MAX_FUNCS
; i
++) {
222 if (xf86SigIOFuncs
[i
].f
) {
223 if (xf86SigIOFuncs
[i
].fd
== fd
) {
224 xf86SigIOFuncs
[i
].f
= 0;
225 xf86SigIOFuncs
[i
].fd
= 0;
226 xf86SigIOFuncs
[i
].closure
= 0;
227 FD_CLR(fd
, &xf86SigIOMask
);
232 if (xf86SigIOFuncs
[i
].fd
>= maxfd
)
233 maxfd
= xf86SigIOFuncs
[i
].fd
+ 1;
239 fcntl(fd
, F_SETFL
, fcntl(fd
, F_GETFL
) & ~O_ASYNC
);
243 if (ioctl(fd
, I_SETSIG
, 0) == -1) {
244 xf86Msg(X_WARNING
, "fcntl(%d, I_SETSIG, 0): %s\n",
245 fd
, strerror(errno
));
250 xf86SigIOMaxFd
= maxfd
;
252 sigemptyset(&sa
.sa_mask
);
253 sigaddset(&sa
.sa_mask
, SIGIO
);
255 sa
.sa_handler
= SIG_IGN
;
256 sigaction(SIGIO
, &sa
, &osa
);
265 return OsBlockSIGIO();
269 xf86UnblockSIGIO(int wasset
)
275 xf86AssertBlockedSIGIO(char *where
)
280 sigprocmask(SIG_BLOCK
, &set
, &old
);
281 if (!sigismember(&old
, SIGIO
))
282 xf86Msg(X_ERROR
, "SIGIO not blocked at %s\n", where
);
285 /* XXX This is a quick hack for the benefit of xf86SetSilkenMouse() */
288 xf86SIGIOSupported(void)