1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/*
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <gpxe/process.h>
#include <gpxe/async.h>
/** @file
*
* Asynchronous operations
*
*/
/**
* Name signal
*
* @v signal Signal number
* @ret name Name of signal
*/
static inline __attribute__ (( always_inline )) const char *
signal_name ( enum signal signal ) {
switch ( signal ) {
case SIGCHLD: return "SIGCHLD";
case SIGKILL: return "SIGKILL";
case SIGUPDATE: return "SIGUPDATE";
default: return "SIG<UNKNOWN>";
}
}
/**
* Initialise an asynchronous operation
*
* @v async Asynchronous operation
* @v aop Asynchronous operation operations to use
* @v parent Parent asynchronous operation, or NULL
* @ret aid Asynchronous operation ID
*
* It is valid to create an asynchronous operation with no parent
* operation; see async_init_orphan().
*/
aid_t async_init ( struct async *async, struct async_operations *aop,
struct async *parent ) {
static aid_t aid = 1;
/* Assign identifier. Negative IDs are used to indicate
* errors, so avoid assigning them.
*/
++aid;
aid &= ( ( ~( ( aid_t ) 0 ) ) >> 1 );
DBGC ( async, "ASYNC %p (type %p) initialising as", async, aop );
if ( parent ) {
DBGC ( async, " child of ASYNC %p", parent );
} else {
DBGC ( async, " orphan" );
}
DBGC ( async, " with ID %ld\n", aid );
assert ( async != NULL );
assert ( aop != NULL );
/* Add to hierarchy */
if ( parent ) {
async->parent = parent;
list_add ( &async->siblings, &parent->children );
}
INIT_LIST_HEAD ( &async->children );
/* Initialise fields */
async->rc = -EINPROGRESS;
async->completed = 0;
async->total = 0;
async->aop = aop;
async->aid = aid;
return async->aid;
}
/**
* SIGCHLD 'ignore' handler
*
* @v async Asynchronous operation
* @v signal Signal received
*/
static void async_ignore_sigchld ( struct async *async, enum signal signal ) {
aid_t waited_aid;
assert ( async != NULL );
assert ( signal == SIGCHLD );
/* Reap the child */
waited_aid = async_wait ( async, NULL, 0 );
assert ( waited_aid >= 0 );
}
/**
* 'Ignore' signal handler
*
* @v async Asynchronous operation
* @v signal Signal received
*/
void async_ignore_signal ( struct async *async, enum signal signal ) {
DBGC ( async, "ASYNC %p using ignore handler for %s\n",
async, signal_name ( signal ) );
assert ( async != NULL );
switch ( signal ) {
case SIGCHLD:
async_ignore_sigchld ( async, signal );
break;
case SIGKILL:
case SIGUPDATE:
default:
/* Nothing to do */
break;
}
}
/**
* Default signal handler
*
* @v async Asynchronous operation
* @v signal Signal received
*/
static void async_default_signal ( struct async *async, enum signal signal ) {
DBGC ( async, "ASYNC %p using default handler for %s\n",
async, signal_name ( signal ) );
assert ( async != NULL );
switch ( signal ) {
case SIGCHLD:
case SIGKILL:
case SIGUPDATE:
default:
/* Nothing to do */
break;
}
}
/**
* Send signal to asynchronous operation
*
* @v async Asynchronous operation
* @v signal Signal to send
*/
void async_signal ( struct async *async, enum signal signal ) {
signal_handler_t handler;
DBGC ( async, "ASYNC %p receiving %s\n",
async, signal_name ( signal ) );
assert ( async != NULL );
assert ( async->aop != NULL );
assert ( signal < SIGMAX );
handler = async->aop->signal[signal];
if ( handler ) {
/* Use the asynchronous operation's signal handler */
handler ( async, signal );
} else {
/* Use the default handler */
async_default_signal ( async, signal );
}
}
/**
* Send signal to all child asynchronous operations
*
* @v async Asynchronous operation
* @v signal Signal to send
*/
void async_signal_children ( struct async *async, enum signal signal ) {
struct async *child;
struct async *tmp;
assert ( async != NULL );
list_for_each_entry_safe ( child, tmp, &async->children, siblings ) {
async_signal ( child, signal );
}
}
/**
* Mark asynchronous operation as complete
*
* @v async Asynchronous operation
* @v rc Return status code
*
* An asynchronous operation should call this once it has completed.
* After calling async_done(), it must be prepared to be reaped by
* having its reap() method called.
*/
void async_done ( struct async *async, int rc ) {
DBGC ( async, "ASYNC %p completing with status %d (%s)\n",
async, rc, strerror ( rc ) );
assert ( async != NULL );
assert ( async->parent != NULL );
assert ( rc != -EINPROGRESS );
/* Store return status code */
async->rc = rc;
/* Send SIGCHLD to parent. Guard against NULL pointer dereferences */
if ( async->parent )
async_signal ( async->parent, SIGCHLD );
}
/**
* Reap default handler
*
* @v async Asynchronous operation
*/
static void async_reap_default ( struct async *async ) {
DBGC ( async, "ASYNC %p ignoring REAP\n", async );
assert ( async != NULL );
/* Nothing to do */
}
/**
* Wait for any child asynchronous operation to complete
*
* @v child Child asynchronous operation
* @v rc Child exit status to fill in, or NULL
* @v block Block waiting for child operation to complete
* @ret aid Asynchronous operation ID, or -1 on error
*/
aid_t async_wait ( struct async *async, int *rc, int block ) {
struct async *child;
aid_t child_aid;
int dummy_rc;
DBGC ( async, "ASYNC %p performing %sblocking wait%s\n", async,
( block ? "" : "non-" ), ( rc ? "" : " (ignoring status)" ) );
assert ( async != NULL );
/* Avoid multiple tests for "if ( rc )" */
if ( ! rc )
rc = &dummy_rc;
while ( 1 ) {
/* Return immediately if we have no children */
if ( list_empty ( &async->children ) ) {
DBGC ( async, "ASYNC %p has no more children\n",
async );
*rc = -ECHILD;
return -1;
}
/* Look for a completed child */
list_for_each_entry ( child, &async->children, siblings ) {
if ( child->rc == -EINPROGRESS )
continue;
/* Found a completed child */
*rc = child->rc;
child_aid = child->aid;
DBGC ( async, "ASYNC %p reaping child ASYNC %p (ID "
"%ld), exit status %d (%s)\n", async, child,
child_aid, child->rc, strerror ( child->rc ) );
/* Reap the child */
assert ( child->aop != NULL );
assert ( list_empty ( &child->children ) );
/* Unlink from operations hierarchy */
list_del ( &child->siblings );
child->parent = NULL;
/* Release all resources */
if ( child->aop->reap ) {
child->aop->reap ( child );
} else {
async_reap_default ( child );
}
return child_aid;
}
/* Return immediately if non-blocking */
if ( ! block ) {
*rc = -EINPROGRESS;
return -1;
}
/* Allow processes to run */
step();
}
}
/**
* Default asynchronous operations
*
* The default is to ignore SIGCHLD (i.e. to automatically reap
* children) and to use the default handler (i.e. do nothing) for all
* other signals.
*/
struct async_operations default_async_operations = {
.signal = {
[SIGCHLD] = SIG_IGN,
},
};
/**
* Default asynchronous operations for orphan asynchronous operations
*
* The default for orphan asynchronous operations is to do nothing for
* SIGCHLD (i.e. to not automatically reap children), on the
* assumption that you're probably creating the orphan solely in order
* to async_wait() on it.
*/
struct async_operations orphan_async_operations = {
.signal = {
[SIGCHLD] = SIG_DFL,
},
};
|