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