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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
/*@-unrecog@*/
/** \ingroup rpmio
* \file rpmio/rpmsq.c
*/
#include "system.h"
#if defined(__LCLINT__)
struct qelem;
/*@-exportheader@*/
extern void insque(struct qelem * __elem, struct qelem * __prev)
/*@modifies __elem, prev @*/;
extern void remque(struct qelem * __elem)
/*@modifies __elem @*/;
/*@=exportheader@*/
#endif
#include <signal.h>
#include <sys/signal.h>
#include <sys/wait.h>
#include <search.h>
#if defined(HAVE_PTHREAD_H) && !defined(__LCLINT__)
#include <pthread.h>
/*@unchecked@*/
static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
#define DO_LOCK() pthread_mutex_lock(&rpmsigTbl_lock);
#define DO_UNLOCK() pthread_mutex_unlock(&rpmsigTbl_lock);
#define INIT_LOCK() \
{ pthread_mutexattr_t attr; \
pthread_mutexattr_init(&attr); \
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
pthread_mutex_init (&rpmsigTbl_lock, &attr); \
pthread_mutexattr_destroy(&attr); \
rpmsigTbl_sigchld->active = 0; \
}
#define ADD_REF(__tbl) (__tbl)->active++
#define SUB_REF(__tbl) --(__tbl)->active
#define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr) \
pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, (__oldtypeptr)); \
pthread_cleanup_push((__handler), (__arg));
#define CLEANUP_RESET(__execute, __oldtype) \
pthread_cleanup_pop(__execute); \
pthread_setcanceltype ((__oldtype), &(__oldtype));
#define SAME_THREAD(_a, _b) pthread_equal(((pthread_t)_a), ((pthread_t)_b))
#define ME() ((void *)pthread_self())
#else
#define DO_LOCK()
#define DO_UNLOCK()
#define INIT_LOCK()
#define ADD_REF(__tbl) /*@-noeffect@*/ (0) /*@=noeffect@*/
#define SUB_REF(__tbl) /*@-noeffect@*/ (0) /*@=noeffect@*/
#define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr)
#define CLEANUP_RESET(__execute, __oldtype)
#define SAME_THREAD(_a, _b) (42)
#define ME() (((void *)getpid()))
#endif /* HAVE_PTHREAD_H */
#include <rpmsq.h>
#include "debug.h"
#define _RPMSQ_DEBUG 0
/*@unchecked@*/
int _rpmsq_debug = _RPMSQ_DEBUG;
/*@unchecked@*/
static struct rpmsqElem rpmsqRock;
/*@-compmempass@*/
/*@unchecked@*/
rpmsq rpmsqQueue = &rpmsqRock;
/*@=compmempass@*/
/*@-mustmod@*/
int rpmsqInsert(void * elem, /*@unused@*/ void * prev)
{
rpmsq sq = (rpmsq) elem;
int ret = -1;
if (sq != NULL) {
#ifdef _RPMSQ_DEBUG
/*@-modfilesys@*/
if (_rpmsq_debug)
fprintf(stderr, " Insert(%p): %p\n", ME(), sq);
/*@=modfilesys@*/
#endif
ret = sighold(SIGCHLD);
if (ret == 0) {
sq->child = 0;
sq->reaped = 0;
sq->status = 0;
sq->reaper = 1;
/*@-bounds@*/
sq->pipes[0] = sq->pipes[1] = -1;
/*@=bounds@*/
/*@-unqualifiedtrans@*/
sq->id = ME();
/*@=unqualifiedtrans@*/
ret = pthread_mutex_init(&sq->mutex, NULL);
ret = pthread_cond_init(&sq->cond, NULL);
#if !defined(__LCLINT__) /* XXX FIXME */
insque(elem, (prev ? prev : rpmsqQueue));
#endif
ret = sigrelse(SIGCHLD);
}
}
return ret;
}
/*@=mustmod@*/
int rpmsqRemove(void * elem)
{
rpmsq sq = (rpmsq) elem;
int ret = -1;
if (elem != NULL) {
#ifdef _RPMSQ_DEBUG
/*@-modfilesys@*/
if (_rpmsq_debug)
fprintf(stderr, " Remove(%p): %p\n", ME(), sq);
/*@=modfilesys@*/
#endif
ret = sighold (SIGCHLD);
if (ret == 0) {
remque(elem);
ret = pthread_cond_destroy(&sq->cond);
ret = pthread_mutex_destroy(&sq->mutex);
sq->id = NULL;
/*@-bounds@*/
if (sq->pipes[1]) ret = close(sq->pipes[1]);
if (sq->pipes[0]) ret = close(sq->pipes[0]);
sq->pipes[0] = sq->pipes[1] = -1;
/*@=bounds@*/
#ifdef NOTYET /* rpmpsmWait debugging message needs */
sq->reaper = 1;
sq->status = 0;
sq->reaped = 0;
sq->child = 0;
#endif
ret = sigrelse(SIGCHLD);
}
}
return ret;
}
/*@unchecked@*/
sigset_t rpmsqCaught;
/*@unchecked@*/
/*@-fullinitblock@*/
static struct rpmsig_s {
int signum;
void (*handler) (int signum, void * info, void * context);
int active;
struct sigaction oact;
} rpmsigTbl[] = {
{ SIGINT, rpmsqAction },
#define rpmsigTbl_sigint (&rpmsigTbl[0])
{ SIGQUIT, rpmsqAction },
#define rpmsigTbl_sigquit (&rpmsigTbl[1])
{ SIGCHLD, rpmsqAction },
#define rpmsigTbl_sigchld (&rpmsigTbl[2])
{ SIGHUP, rpmsqAction },
#define rpmsigTbl_sighup (&rpmsigTbl[3])
{ SIGTERM, rpmsqAction },
#define rpmsigTbl_sigterm (&rpmsigTbl[4])
{ SIGPIPE, rpmsqAction },
#define rpmsigTbl_sigpipe (&rpmsigTbl[5])
{ -1, NULL },
};
/*@=fullinitblock@*/
/*@-incondefs@*/
void rpmsqAction(int signum,
/*@unused@*/ void * info, /*@unused@*/ void * context)
/*@globals rpmsqQueue @*/
/*@modifies rpmsqQueue @*/
{
int save = errno;
rpmsig tbl;
for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
if (tbl->signum != signum)
continue;
(void) sigaddset(&rpmsqCaught, signum);
switch (signum) {
case SIGCHLD:
while (1) {
rpmsq sq;
int status = 0;
pid_t reaped = waitpid(0, &status, WNOHANG);
/* XXX errno set to ECHILD/EINVAL/EINTR. */
if (reaped <= 0)
/*@innerbreak@*/ break;
/* XXX insque(3)/remque(3) are dequeue, not ring. */
for (sq = rpmsqQueue->q_forw;
sq != NULL && sq != rpmsqQueue;
sq = sq->q_forw)
{
if (sq->child != reaped)
/*@innercontinue@*/ continue;
sq->reaped = reaped;
sq->status = status;
#if defined(HAVE_PTHREAD_H) && !defined(__LCLINT__)
(void) pthread_cond_signal(&sq->cond);
#endif
/*@innerbreak@*/ break;
}
}
/*@switchbreak@*/ break;
default:
/*@switchbreak@*/ break;
}
break;
}
errno = save;
}
/*@=incondefs@*/
int rpmsqEnable(int signum, /*@null@*/ rpmsqAction_t handler)
/*@globals rpmsigTbl @*/
/*@modifies rpmsigTbl @*/
{
int tblsignum = (signum >= 0 ? signum : -signum);
struct sigaction sa;
rpmsig tbl;
int ret = -1;
DO_LOCK ();
#if !defined(__LCLINT__)
if (rpmsqQueue->id == NULL)
rpmsqQueue->id = ME();
#endif
for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
if (tblsignum != tbl->signum)
continue;
if (signum >= 0) { /* Enable. */
if (ADD_REF(tbl) <= 0) {
(void) sigdelset(&rpmsqCaught, tbl->signum);
(void) sigemptyset (&sa.sa_mask);
/*@-compdef -type @*/
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = (handler != NULL ? handler : tbl->handler);
if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
SUB_REF(tbl);
break;
}
/*@=compdef =type @*/
tbl->active = 1; /* XXX just in case */
if (handler != NULL)
tbl->handler = handler;
}
} else { /* Disable. */
if (SUB_REF(tbl) <= 0) {
if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
break;
tbl->active = 0; /* XXX just in case */
tbl->handler = (handler != NULL ? handler : rpmsqAction);
}
}
ret = tbl->active;
break;
}
DO_UNLOCK ();
return ret;
}
pid_t rpmsqFork(rpmsq sq)
{
pid_t pid;
int xx;
if (sq->reaper) {
xx = rpmsqInsert(sq, NULL);
#ifdef _RPMSQ_DEBUG
/*@-modfilesys@*/
if (_rpmsq_debug)
fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
/*@=modfilesys@*/
#endif
xx = rpmsqEnable(SIGCHLD, NULL);
}
xx = pipe(sq->pipes);
xx = sighold(SIGCHLD);
pid = fork();
if (pid < (pid_t) 0) { /* fork failed. */
/*@-bounds@*/
xx = close(sq->pipes[0]);
xx = close(sq->pipes[1]);
sq->pipes[0] = sq->pipes[1] = -1;
/*@=bounds@*/
goto out;
} else if (pid == (pid_t) 0) { /* Child. */
int yy;
/* Block to permit parent to wait. */
/*@-bounds@*/
xx = close(sq->pipes[1]);
xx = read(sq->pipes[0], &yy, sizeof(yy));
xx = close(sq->pipes[0]);
sq->pipes[0] = sq->pipes[1] = -1;
/*@=bounds@*/
#ifdef _RPMSQ_DEBUG
/*@-modfilesys@*/
if (_rpmsq_debug)
fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, getpid());
/*@=modfilesys@*/
#endif
} else { /* Parent. */
sq->child = pid;
#ifdef _RPMSQ_DEBUG
/*@-modfilesys@*/
if (_rpmsq_debug)
fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, sq->child);
/*@=modfilesys@*/
#endif
}
out:
xx = sigrelse(SIGCHLD);
return sq->child;
}
/**
* Wait for child process to be reaped, and unregister SIGCHLD handler.
* @param sq scriptlet queue element
* @return 0 on success
*/
static int rpmsqWaitUnregister(rpmsq sq)
/*@globals fileSystem, internalState @*/
/*@modifies sq, fileSystem, internalState @*/
{
int same_thread = 0;
int ret = 0;
int xx;
if (same_thread)
ret = sighold(SIGCHLD);
else
ret = pthread_mutex_lock(&sq->mutex);
/* Start the child. */
/*@-bounds@*/
if (sq->pipes[0] >= 0)
xx = close(sq->pipes[0]);
if (sq->pipes[1] >= 0)
xx = close(sq->pipes[1]);
sq->pipes[0] = sq->pipes[1] = -1;
/*@=bounds@*/
(void) rpmswEnter(&sq->op, -1);
/*@-infloops@*/
while (ret == 0 && sq->reaped != sq->child) {
if (same_thread)
ret = sigpause(SIGCHLD);
else
ret = pthread_cond_wait(&sq->cond, &sq->mutex);
}
/*@=infloops@*/
sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
if (same_thread)
xx = sigrelse(SIGCHLD);
else
xx = pthread_mutex_unlock(&sq->mutex);
#ifdef _RPMSQ_DEBUG
/*@-modfilesys@*/
if (_rpmsq_debug)
fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, sq->child, sq->reaper, ret);
/*@=modfilesys@*/
#endif
xx = rpmsqRemove(sq);
xx = rpmsqEnable(-SIGCHLD, NULL);
#ifdef _RPMSQ_DEBUG
/*@-modfilesys@*/
if (_rpmsq_debug)
fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
/*@=modfilesys@*/
#endif
return ret;
}
pid_t rpmsqWait(rpmsq sq)
{
#ifdef _RPMSQ_DEBUG
/*@-modfilesys@*/
if (_rpmsq_debug)
fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, sq->child, sq->reaper);
/*@=modfilesys@*/
#endif
if (sq->reaper) {
(void) rpmsqWaitUnregister(sq);
} else {
pid_t reaped;
int status;
do {
reaped = waitpid(sq->child, &status, 0);
} while (reaped >= 0 && reaped != sq->child);
sq->reaped = reaped;
sq->status = status;
#ifdef _RPMSQ_DEBUG
/*@-modfilesys@*/
if (_rpmsq_debug)
fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, sq->child, sq->reaped);
/*@=modfilesys@*/
#endif
}
#ifdef _RPMSQ_DEBUG
/*@-modfilesys@*/
if (_rpmsq_debug)
fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, sq->child, sq->status);
/*@=modfilesys@*/
#endif
return sq->reaped;
}
int rpmsqThread(void * (*start) (void * arg), void * arg)
{
pthread_t pth;
int ret;
ret = pthread_create(&pth, NULL, start, arg);
if (ret == 0) {
#if 0
fprintf(stderr, " Thread(%p): %p\n", ME(), pth);
#endif
ret = pthread_join(pth, NULL);
}
return ret;
}
/**
* SIGCHLD cancellation handler.
*/
static void
sigchld_cancel (void *arg)
/*@globals fileSystem, internalState @*/
/*@modifies fileSystem, internalState @*/
{
pid_t child = *(pid_t *) arg;
pid_t result;
(void) kill(child, SIGKILL);
do {
result = waitpid(child, NULL, 0);
} while (result == (pid_t)-1 && errno == EINTR);
DO_LOCK ();
if (SUB_REF (rpmsigTbl_sigchld) == 0) {
(void) rpmsqEnable(-SIGQUIT, NULL);
(void) rpmsqEnable(-SIGINT, NULL);
}
DO_UNLOCK ();
}
/**
* Execute a command, returning its status.
*/
/*@-bounds@*/
int
rpmsqExecve (const char ** argv)
{
int oldtype;
int status = -1;
pid_t pid;
pid_t result;
sigset_t newMask, oldMask;
rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
DO_LOCK ();
if (ADD_REF (rpmsigTbl_sigchld) == 0) {
if (rpmsqEnable(SIGINT, NULL) < 0) {
SUB_REF (rpmsigTbl_sigchld);
goto out;
}
if (rpmsqEnable(SIGQUIT, NULL) < 0) {
SUB_REF (rpmsigTbl_sigchld);
goto out_restore_sigint;
}
}
DO_UNLOCK ();
(void) sigemptyset (&newMask);
(void) sigaddset (&newMask, SIGCHLD);
if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
DO_LOCK ();
if (SUB_REF (rpmsigTbl_sigchld) == 0)
goto out_restore_sigquit_and_sigint;
goto out;
}
CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
pid = fork ();
if (pid < (pid_t) 0) { /* fork failed. */
goto out;
} else if (pid == (pid_t) 0) { /* Child. */
/* Restore the signals. */
(void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
(void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
(void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
/* Reset rpmsigTbl lock and refcnt. */
INIT_LOCK ();
(void) execve (argv[0], (char *const *) argv, environ);
_exit (127);
} else { /* Parent. */
do {
result = waitpid(pid, &status, 0);
} while (result == (pid_t)-1 && errno == EINTR);
if (result != pid)
status = -1;
}
CLEANUP_RESET(0, oldtype);
DO_LOCK ();
if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
(rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
|| sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
{
status = -1;
}
goto out;
out_restore_sigquit_and_sigint:
(void) rpmsqEnable(-SIGQUIT, NULL);
out_restore_sigint:
(void) rpmsqEnable(-SIGINT, NULL);
out:
DO_UNLOCK ();
return status;
}
/*@=bounds@*/
/*@=unrecog@*/
|