]> git.pond.sub.org Git - empserver/blob - src/lib/lwp/sig.c
(empth_init_signals): Don't catch SIGINT and SIGTERM.
[empserver] / src / lib / lwp / sig.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1994-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  sig.c: Wait for signals
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2006
32  */
33
34 #include <config.h>
35
36 #include <errno.h>
37 #include <signal.h>
38 #include "misc.h"
39 #include "lwp.h"
40 #include "lwpint.h"
41
42 /* Signals awaited by lwpSigWait() */
43 static sigset_t *LwpSigAwaited;
44
45 /*
46  * Signals from LwpSigAwaited catched so far
47  * Access only with signals blocked!
48  */
49 static sigset_t LwpSigCatched;
50
51 /* The thread waiting for signals in lwpSigWait() */
52 static struct lwpProc *LwpSigWaiter;
53
54 /* Where to return the signal number to the thread in lwpSigWait() */
55 static int *LwpSigPtr;
56
57 static void lwpCatchAwaitedSig(int);
58
59 /*
60  * Initialize waiting for signals in SET.
61  */
62 void
63 lwpInitSigWait(sigset_t *set)
64 {
65     struct sigaction act;
66     int i;
67
68     sigemptyset(&LwpSigCatched);
69
70     act.sa_flags = 0;
71     act.sa_mask = *set;
72     sigemptyset(&act.sa_mask);
73     act.sa_handler = lwpCatchAwaitedSig;
74     for (i = 0; i < NSIG; i++) {
75         if (sigismember(set, i))
76             sigaction(i, &act, NULL);
77     }
78 }
79
80 static void
81 lwpCatchAwaitedSig(int sig)
82 {
83     sigaddset(&LwpSigCatched, sig);
84 }
85
86 /*
87  * Test whether a signal from SET has been catched.
88  * If yes, delete that signal from the set of catched signals, and
89  * return its number.
90  * Else return 0.
91  */
92 static int
93 lwpGetSig(sigset_t *set)
94 {
95     sigset_t save;
96     int i;
97
98     sigprocmask(SIG_BLOCK, set, &save);
99     for (i = NSIG - 1; i > 0; i--) {
100         if (sigismember(set, i) && sigismember(&LwpSigCatched, i)) {
101             lwpStatus(LwpCurrent, "Got awaited signal %d", i);
102             sigdelset(&LwpSigCatched, i);
103             break;
104         }
105     }
106     sigprocmask(SIG_SETMASK, &save, NULL);
107     return i;
108 }
109
110 /*
111  * Wait until a signal from SET arrives.
112  * Assign its number to *SIG and return 0.
113  * If another thread is already waiting for signals, return EBUSY
114  * without waiting.
115  */
116 int
117 lwpSigWait(sigset_t *set, int *sig)
118 {
119     int res;
120
121     if (CANT_HAPPEN(LwpSigWaiter))
122         return EBUSY;
123     res = lwpGetSig(set);
124     if (res <= 0) {
125         lwpStatus(LwpCurrent, "Waiting for signals");
126         LwpSigAwaited = set;
127         LwpSigPtr = sig;
128         LwpSigWaiter = LwpCurrent;
129         lwpReschedule();
130         return 0;
131     }
132     *sig = res;
133     return 0;
134 }
135
136 /*
137  * Wake up the thread awaiting signals if one arrived.
138  * To be called from lwpReschedule().
139  */
140 void
141 lwpSigWakeup(void)
142 {
143     int res;
144
145     if (!LwpSigWaiter)
146         return;
147     res = lwpGetSig(LwpSigAwaited);
148     if (res > 0) {
149         *LwpSigPtr = res;
150         lwpReady(LwpSigWaiter);
151         LwpSigWaiter = NULL;
152     }
153 }