rpm 5.2.1

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

rpmsq.c
Go to the documentation of this file.
1
5 #include "system.h"
6
7 #if defined(__LCLINT__)
8 #define _BITS_SIGTHREAD_H /* XXX avoid __sigset_t heartburn. */
9
10 /*@-incondefs -protoparammatch@*/
11 /*@-exportheader@*/
12 /*@constant int SA_SIGINFO@*/
13 extern int sighold(int sig)
14 /*@globals errno, systemState @*/;
15 extern int sigignore(int sig)
16 /*@globals errno, systemState @*/;
17 extern int sigpause(int sig)
18 /*@globals errno, systemState @*/;
19 extern int sigrelse(int sig)
20 /*@globals errno, systemState @*/;
21 extern void (*sigset(int sig, void (*disp)(int)))(int)
22 /*@globals errno, systemState @*/;
23
24 struct qelem;
25 extern void __insque(struct qelem * __elem, struct qelem * __prev)
26 /*@modifies __elem, __prev @*/;
27 extern void __remque(struct qelem * __elem)
28 /*@modifies __elem @*/;
29
30 extern pthread_t pthread_self(void)
31 /*@*/;
32 extern int pthread_equal(pthread_t t1, pthread_t t2)
33 /*@*/;
34
35 extern int pthread_create(/*@out@*/ pthread_t *restrict thread,
36 const pthread_attr_t *restrict attr,
37 void *(*start_routine)(void*), void *restrict arg)
38 /*@modifies *thread @*/;
39 extern int pthread_join(pthread_t thread, /*@out@*/ void **value_ptr)
40 /*@modifies *value_ptr @*/;
41
42 extern int pthread_setcancelstate(int state, /*@out@*/ int *oldstate)
43 /*@globals internalState @*/
44 /*@modifies *oldstate, internalState @*/;
45 extern int pthread_setcanceltype(int type, /*@out@*/ int *oldtype)
46 /*@globals internalState @*/
47 /*@modifies *oldtype, internalState @*/;
48 extern void pthread_testcancel(void)
49 /*@globals internalState @*/
50 /*@modifies internalState @*/;
51 extern void pthread_cleanup_pop(int execute)
52 /*@globals internalState @*/
53 /*@modifies internalState @*/;
54 extern void pthread_cleanup_push(void (*routine)(void*), void *arg)
55 /*@globals internalState @*/
56 /*@modifies internalState @*/;
57 extern void _pthread_cleanup_pop(/*@out@*/ struct _pthread_cleanup_buffer *__buffer, int execute)
58 /*@globals internalState @*/
59 /*@modifies internalState @*/;
60 extern void _pthread_cleanup_push(/*@out@*/ struct _pthread_cleanup_buffer *__buffer, void (*routine)(void*), /*@out@*/ void *arg)
61 /*@globals internalState @*/
62 /*@modifies internalState @*/;
63
64 extern int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
65 /*@globals errno, internalState @*/
66 /*@modifies *attr, errno, internalState @*/;
67 extern int pthread_mutexattr_init(/*@out@*/ pthread_mutexattr_t *attr)
68 /*@globals errno, internalState @*/
69 /*@modifies *attr, errno, internalState @*/;
70
71 int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict attr,
72 /*@out@*/ int *restrict type)
73 /*@modifies *type @*/;
74 int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
75 /*@globals errno, internalState @*/
76 /*@modifies *attr, errno, internalState @*/;
77
78 extern int pthread_mutex_destroy(pthread_mutex_t *mutex)
79 /*@modifies *mutex @*/;
80 extern int pthread_mutex_init(/*@out@*/ pthread_mutex_t *restrict mutex,
81 /*@null@*/ const pthread_mutexattr_t *restrict attr)
82 /*@globals errno, internalState @*/
83 /*@modifies *mutex, errno, internalState @*/;
84
85 extern int pthread_mutex_lock(pthread_mutex_t *mutex)
86 /*@globals errno @*/
87 /*@modifies *mutex, errno @*/;
88 extern int pthread_mutex_trylock(pthread_mutex_t *mutex)
89 /*@globals errno @*/
90 /*@modifies *mutex, errno @*/;
91 extern int pthread_mutex_unlock(pthread_mutex_t *mutex)
92 /*@globals errno @*/
93 /*@modifies *mutex, errno @*/;
94
95 extern int pthread_cond_destroy(pthread_cond_t *cond)
96 /*@modifies *cond @*/;
97 extern int pthread_cond_init(/*@out@*/ pthread_cond_t *restrict cond,
98 const pthread_condattr_t *restrict attr)
99 /*@globals errno, internalState @*/
100 /*@modifies *cond, errno, internalState @*/;
101
102 extern int pthread_cond_timedwait(pthread_cond_t *restrict cond,
103 pthread_mutex_t *restrict mutex,
104 const struct timespec *restrict abstime)
105 /*@modifies *cond, *mutex @*/;
106 extern int pthread_cond_wait(pthread_cond_t *restrict cond,
107 pthread_mutex_t *restrict mutex)
108 /*@modifies *cond, *mutex @*/;
109 extern int pthread_cond_broadcast(pthread_cond_t *cond)
110 /*@globals errno, internalState @*/
111 /*@modifies *cond, errno, internalState @*/;
112 extern int pthread_cond_signal(pthread_cond_t *cond)
113 /*@globals errno, internalState @*/
114 /*@modifies *cond, errno, internalState @*/;
115
116 /*@=exportheader@*/
117 /*@=incondefs =protoparammatch@*/
118 #endif
119
120 #include <signal.h>
121 #if !defined(__QNX__)
122 # include <sys/signal.h>
123 #endif
124 #include <sys/wait.h>
125 #include <search.h>
126
127 /* portability fallback for sighold(3) */
128 #if !defined(HAVE_SIGHOLD) && defined(HAVE_SIGPROCMASK) && defined(HAVE_SIGADDSET)
129 static int __RPM_sighold(int sig)
130 {
131 sigset_t set;
132 if (sigprocmask(SIG_SETMASK, NULL, &set) < 0)
133 return -1;
134 if (sigaddset(&set, sig) < 0)
135 return -1;
136 return sigprocmask(SIG_SETMASK, &set, NULL);
137 }
138 #define sighold(sig) __RPM_sighold(sig)
139 #endif
140
141 /* portability fallback for sigrelse(3) */
142 #if !defined(HAVE_SIGRELSE) && defined(HAVE_SIGPROCMASK) && defined(HAVE_SIGDELSET)
143 static int __RPM_sigrelse(int sig)
144 {
145 sigset_t set;
146 if (sigprocmask(SIG_SETMASK, NULL, &set) < 0)
147 return -1;
148 if (sigdelset(&set, sig) < 0)
149 return -1;
150 return sigprocmask(SIG_SETMASK, &set, NULL);
151 }
152 #define sigrelse(sig) __RPM_sigrelse(sig)
153 #endif
154
155 /* portability fallback for sigpause(3) */
156 #if !defined(HAVE_SIGPAUSE) && defined(HAVE_SIGEMPTYSET) && defined(HAVE_SIGADDSET) && defined(HAVE_SIGSUSPEND)
157 static int __RPM_sigpause(int sig)
158 {
159 sigset_t set;
160 if (sigemptyset(&set) < 0)
161 return -1;
162 if (sigaddset(&set, sig) < 0)
163 return -1;
164 return sigsuspend(&set);
165 }
166 #define sigpause(sig) __RPM_sigpause(sig)
167 #endif
168
169 /* portability fallback for insque(3)/remque(3) */
170 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__QNX__)
171 struct qelem {
172 struct qelem *q_forw;
173 struct qelem *q_back;
174 };
175
176 static void __insque(struct qelem * elem, struct qelem * pred)
177 {
178 elem -> q_forw = pred -> q_forw;
179 pred -> q_forw -> q_back = elem;
180 elem -> q_back = pred;
181 pred -> q_forw = elem;
182 }
183 #define insque(_e, _p) __insque((_e), (_p))
184
185 static void __remque(struct qelem * elem)
186 {
187 elem -> q_forw -> q_back = elem -> q_back;
188 elem -> q_back -> q_forw = elem -> q_forw;
189 }
190 #define remque(_e) __remque(_e)
191 #endif
192
193 #if defined(WITH_PTHREADS)
194
195 # if !defined(__QNX__)
196 /* XXX suggested in bugzilla #159024 */
197 # if PTHREAD_MUTEX_DEFAULT != PTHREAD_MUTEX_NORMAL
198 # error RPM expects PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL
199 # endif
200 # endif
201
202 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
203 /*@unchecked@*/
204 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_MUTEX_INITIALIZER;
205 #else
206 /*@unchecked@*/
207 /*@-type@*/
208 static pthread_mutex_t rpmsigTbl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
209 /*@=type@*/
210 #endif
211
212 /*@-macromatchname@*/
213 #define DO_LOCK() /*@-retvalint@*/pthread_mutex_lock(&rpmsigTbl_lock)/*@=retvalint@*/;
214 #define DO_UNLOCK() pthread_mutex_unlock(&rpmsigTbl_lock);
215 #define INIT_LOCK() \
216 { pthread_mutexattr_t attr; \
217 (void) pthread_mutexattr_init(&attr); \
218 (void) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
219 (void) pthread_mutex_init (&rpmsigTbl_lock, &attr); \
220 (void) pthread_mutexattr_destroy(&attr); \
221 rpmsigTbl_sigchld->active = 0; \
222 }
223 #define ADD_REF(__tbl) (__tbl)->active++
224 #define SUB_REF(__tbl) --(__tbl)->active
225 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr) \
226 (void) pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, (__oldtypeptr));\
227 pthread_cleanup_push((__handler), (__arg));
228 #define CLEANUP_RESET(__execute, __oldtype) \
229 pthread_cleanup_pop(__execute); \
230 (void) pthread_setcanceltype ((__oldtype), &(__oldtype));
231
232 #define SAME_THREAD(_a, _b) pthread_equal(((pthread_t)_a), ((pthread_t)_b))
233 /*@-macromatchname@*/
234
235 #define ME() __tid2vp(pthread_self())
236 /*@shared@*/
237 static void *__tid2vp(pthread_t tid)
238 /*@*/
239 {
240 union { pthread_t tid; /*@shared@*/ void *vp; } u;
241 u.tid = tid;
242 return u.vp;
243 }
244
245 #else
246
247 /*@-macromatchname@*/
248 #define DO_LOCK() (0)
249 #define DO_UNLOCK() (0)
250 #define INIT_LOCK()
251 #define ADD_REF(__tbl) /*@-noeffect@*/ (0) /*@=noeffect@*/
252 #define SUB_REF(__tbl) /*@-noeffect@*/ (0) /*@=noeffect@*/
253 #define CLEANUP_HANDLER(__handler, __arg, __oldtypeptr)
254 #define CLEANUP_RESET(__execute, __oldtype)
255
256 #define SAME_THREAD(_a, _b) (42)
257 /*@=macromatchname@*/
258
259 #define ME() __pid2vp(getpid())
260 /*@shared@*/
261 static void *__pid2vp(pid_t pid)
262 /*@*/
263 {
264 union { pid_t pid; /*@shared@*/ void *vp; } u;
265 u.pid = pid;
266 return u.vp;
267 }
268
269 #endif /* WITH_PTHREADS */
270
271 #define _RPMSQ_INTERNAL
272 #include <rpmsq.h>
273
274 #include "debug.h"
275
276 #define _RPMSQ_DEBUG 0
277 /*@unchecked@*/
278 int _rpmsq_debug = _RPMSQ_DEBUG;
279
280 /* XXX __OpenBSD__ insque(3) needs rock->q_forw initialized. */
281 /*@unchecked@*/
282 /*@-fullinitblock @*/
283 static struct rpmsqElem rpmsqRock = { .q_forw = &rpmsqRock };
284 /*@=fullinitblock @*/
285
286 /*@-compmempass@*/
287 /*@unchecked@*/
288 rpmsq rpmsqQueue = &rpmsqRock;
289 /*@=compmempass@*/
290
291 int rpmsqInsert(void * elem, void * prev)
292 {
293 rpmsq sq = (rpmsq) elem;
294 int ret = -1;
295
296 if (sq != NULL) {
297 #ifdef _RPMSQ_DEBUG
298 if (_rpmsq_debug)
299 fprintf(stderr, " Insert(%p): %p\n", ME(), sq);
300 #endif
301 ret = sighold(SIGCHLD);
302 if (ret == 0) {
303 sq->child = 0;
304 sq->reaped = 0;
305 sq->status = 0;
306 /* ==> Set to 1 to catch SIGCHLD, set to 0 to use waitpid(2). */
307 sq->reaper = 1;
308 sq->pipes[0] = sq->pipes[1] = -1;
309
310 sq->id = ME();
311 /*@-noeffect@*/
312 insque(elem, (prev != NULL ? prev : rpmsqQueue));
313 /*@=noeffect@*/
314 ret = sigrelse(SIGCHLD);
315 }
316 }
317 return ret;
318 }
319
320 int rpmsqRemove(void * elem)
321 {
322 rpmsq sq = (rpmsq) elem;
323 int ret = -1;
324
325 if (elem != NULL) {
326
327 #ifdef _RPMSQ_DEBUG
328 if (_rpmsq_debug)
329 fprintf(stderr, " Remove(%p): %p\n", ME(), sq);
330 #endif
331 ret = sighold (SIGCHLD);
332 if (ret == 0) {
333 /*@-noeffect@*/
334 remque(elem);
335 /*@=noeffect@*/
336 sq->id = NULL;
337 if (sq->pipes[1] > 0) ret = close(sq->pipes[1]);
338 if (sq->pipes[0] > 0) ret = close(sq->pipes[0]);
339 sq->pipes[0] = sq->pipes[1] = -1;
340 #ifdef NOTYET /* rpmpsmWait debugging message needs */
341 sq->status = 0;
342 sq->reaped = 0;
343 sq->child = 0;
344 #endif
345 ret = sigrelse(SIGCHLD);
346 }
347 }
348 return ret;
349 }
350
351 /*@unchecked@*/
352 sigset_t rpmsqCaught;
353
354 /*@unchecked@*/
355 /*@-fullinitblock@*/
356 static struct rpmsig_s {
357 int signum;
358 void (*handler) (int signum, void * info, void * context);
359 int active;
360 struct sigaction oact;
361 } rpmsigTbl[] = {
362 { SIGINT, rpmsqAction },
363 #define rpmsigTbl_sigint (&rpmsigTbl[0])
364 { SIGQUIT, rpmsqAction },
365 #define rpmsigTbl_sigquit (&rpmsigTbl[1])
366 { SIGCHLD, rpmsqAction },
367 #define rpmsigTbl_sigchld (&rpmsigTbl[2])
368 { SIGHUP, rpmsqAction },
369 #define rpmsigTbl_sighup (&rpmsigTbl[3]) /* XXX unused */
370 { SIGTERM, rpmsqAction },
371 #define rpmsigTbl_sigterm (&rpmsigTbl[4]) /* XXX unused */
372 { SIGPIPE, rpmsqAction },
373 #define rpmsigTbl_sigpipe (&rpmsigTbl[5]) /* XXX unused */
374
375 #ifdef NOTYET /* XXX todo++ */
376 #if defined(SIGXCPU)
377 { SIGXCPU, rpmsqAction },
378 #define rpmsigTbl_sigxcpu (&rpmsigTbl[6]) /* XXX unused */
379 #endif
380 #if defined(SIGXFSZ)
381 { SIGXFSZ, rpmsqAction },
382 #define rpmsigTbl_sigxfsz (&rpmsigTbl[7]) /* XXX unused */
383 #endif
384 #endif
385
386 { -1, NULL },
387 };
388 /*@=fullinitblock@*/
389
390 void rpmsqAction(int signum, /*@unused@*/ void * info,
391 /*@unused@*/ void * context)
392 {
393 int save = errno;
394 rpmsig tbl;
395
396 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
397 if (tbl->signum != signum)
398 continue;
399
400 (void) sigaddset(&rpmsqCaught, signum);
401
402 switch (signum) {
403 case SIGCHLD:
404 while (1) {
405 rpmsq sq;
406 int status = 0;
407 pid_t reaped = waitpid(0, &status, WNOHANG);
408
409 /* XXX errno set to ECHILD/EINVAL/EINTR. */
410 if (reaped <= 0)
411 /*@innerbreak@*/ break;
412
413 /* XXX insque(3)/remque(3) are dequeue, not ring. */
414 for (sq = rpmsqQueue->q_forw;
415 sq != NULL && sq != rpmsqQueue;
416 sq = sq->q_forw)
417 {
418 int ret;
419
420 if (sq->child != reaped)
421 /*@innercontinue@*/ continue;
422 sq->reaped = reaped;
423 sq->status = status;
424
425 ret = close(sq->pipes[1]); sq->pipes[1] = -1;
426
427 /*@innerbreak@*/ break;
428 }
429 }
430 /*@switchbreak@*/ break;
431 default:
432 /*@switchbreak@*/ break;
433 }
434 break;
435 }
436 errno = save;
437 }
438
439 int rpmsqEnable(int signum, /*@null@*/ rpmsqAction_t handler)
440 /*@globals rpmsigTbl @*/
441 /*@modifies rpmsigTbl @*/
442 {
443 int tblsignum = (signum >= 0 ? signum : -signum);
444 struct sigaction sa;
445 rpmsig tbl;
446 int ret = -1;
447 int xx;
448
449 xx = DO_LOCK ();
450 if (rpmsqQueue->id == NULL)
451 rpmsqQueue->id = ME();
452 for (tbl = rpmsigTbl; tbl->signum >= 0; tbl++) {
453 if (tblsignum != tbl->signum)
454 continue;
455
456 if (signum >= 0) { /* Enable. */
457 if (ADD_REF(tbl) <= 0) {
458 (void) sigdelset(&rpmsqCaught, tbl->signum);
459
460 /* XXX Don't set a signal handler if already SIG_IGN */
461 (void) sigaction(tbl->signum, NULL, &tbl->oact);
462 if (tbl->oact.sa_handler == SIG_IGN)
463 continue;
464
465 (void) sigemptyset (&sa.sa_mask);
466 sa.sa_flags = SA_SIGINFO;
467 #if defined(__LCLINT__) /* XXX glibc has union to track handler prototype. */
468 sa.sa_handler = (handler != NULL ? handler : tbl->handler);
469 #else
470 sa.sa_sigaction = (void *) (handler != NULL ? handler : tbl->handler);
471 #endif
472 if (sigaction(tbl->signum, &sa, &tbl->oact) < 0) {
473 xx = SUB_REF(tbl);
474 break;
475 }
476 tbl->active = 1; /* XXX just in case */
477 if (handler != NULL)
478 tbl->handler = handler;
479 }
480 } else { /* Disable. */
481 if (SUB_REF(tbl) <= 0) {
482 if (sigaction(tbl->signum, &tbl->oact, NULL) < 0)
483 break;
484 tbl->active = 0; /* XXX just in case */
485 tbl->handler = (handler != NULL ? handler : rpmsqAction);
486 }
487 }
488 ret = tbl->active;
489 break;
490 }
491 xx = DO_UNLOCK ();
492 return ret;
493 }
494
495 pid_t rpmsqFork(rpmsq sq)
496 {
497 pid_t pid;
498 int xx;
499
500 if (sq->reaper) {
501 xx = rpmsqInsert(sq, NULL);
502 #ifdef _RPMSQ_DEBUG
503 if (_rpmsq_debug)
504 fprintf(stderr, " Enable(%p): %p\n", ME(), sq);
505 #endif
506 xx = rpmsqEnable(SIGCHLD, NULL);
507 }
508
509 xx = pipe(sq->pipes);
510
511 xx = sighold(SIGCHLD);
512
513 pid = fork();
514 if (pid < (pid_t) 0) { /* fork failed. */
515 sq->child = (pid_t)-1;
516 xx = close(sq->pipes[0]);
517 xx = close(sq->pipes[1]);
518 sq->pipes[0] = sq->pipes[1] = -1;
519 goto out;
520 } else if (pid == (pid_t) 0) { /* Child. */
521 int yy;
522
523 /* Block to permit parent time to wait. */
524 xx = close(sq->pipes[1]);
525 if (sq->reaper)
526 xx = (int)read(sq->pipes[0], &yy, sizeof(yy));
527 xx = close(sq->pipes[0]);
528 sq->pipes[0] = sq->pipes[1] = -1;
529
530 #ifdef _RPMSQ_DEBUG
531 if (_rpmsq_debug)
532 fprintf(stderr, " Child(%p): %p child %d\n", ME(), sq, (int)getpid());
533 #endif
534
535 } else { /* Parent. */
536
537 sq->child = pid;
538
539 #ifdef _RPMSQ_DEBUG
540 if (_rpmsq_debug)
541 fprintf(stderr, " Parent(%p): %p child %d\n", ME(), sq, (int)sq->child);
542 #endif
543
544 }
545
546 out:
547 xx = sigrelse(SIGCHLD);
548 return sq->child;
549 }
550
557 static int rpmsqWaitUnregister(rpmsq sq)
558 /*@globals fileSystem, internalState @*/
559 /*@modifies sq, fileSystem, internalState @*/
560 {
561 int nothreads = 0;
562 int ret = 0;
563 int xx;
564
565 assert(sq->reaper);
566 /* Protect sq->reaped from handler changes. */
567 ret = sighold(SIGCHLD);
568
569 /* Start the child, linux often runs child before parent. */
570 if (sq->pipes[0] >= 0)
571 xx = close(sq->pipes[0]);
572 if (sq->pipes[1] >= 0)
573 xx = close(sq->pipes[1]);
574
575 /* Re-initialize the pipe to receive SIGCHLD receipt confirmation. */
576 xx = pipe(sq->pipes);
577
578 /* Put a stopwatch on the time spent waiting to measure performance gain. */
579 (void) rpmswEnter(&sq->op, -1);
580
581 /* Wait for handler to receive SIGCHLD. */
582 /*@-infloops@*/
583 while (ret == 0 && sq->reaped != sq->child) {
584 if (nothreads)
585 /* Note that sigpause re-enables SIGCHLD. */
586 ret = sigpause(SIGCHLD);
587 else {
588 xx = sigrelse(SIGCHLD);
589
590 /* Signal handler does close(sq->pipes[1]) triggering 0b EOF read */
591 if (read(sq->pipes[0], &xx, sizeof(xx)) == 0) {
592 xx = close(sq->pipes[0]); sq->pipes[0] = -1;
593 ret = 1;
594 }
595
596 xx = sighold(SIGCHLD);
597 }
598 }
599 /*@=infloops@*/
600
601 /* Accumulate stopwatch time spent waiting, potential performance gain. */
602 sq->ms_scriptlets += rpmswExit(&sq->op, -1)/1000;
603
604 xx = sigrelse(SIGCHLD);
605
606 #ifdef _RPMSQ_DEBUG
607 if (_rpmsq_debug)
608 fprintf(stderr, " Wake(%p): %p child %d reaper %d ret %d\n", ME(), sq, (int)sq->child, sq->reaper, ret);
609 #endif
610
611 /* Remove processed SIGCHLD item from queue. */
612 xx = rpmsqRemove(sq);
613
614 /* Disable SIGCHLD handler on refcount == 0. */
615 xx = rpmsqEnable(-SIGCHLD, NULL);
616 #ifdef _RPMSQ_DEBUG
617 if (_rpmsq_debug)
618 fprintf(stderr, " Disable(%p): %p\n", ME(), sq);
619 #endif
620
621 return ret;
622 }
623
624 pid_t rpmsqWait(rpmsq sq)
625 {
626
627 #ifdef _RPMSQ_DEBUG
628 if (_rpmsq_debug)
629 fprintf(stderr, " Wait(%p): %p child %d reaper %d\n", ME(), sq, (int)sq->child, sq->reaper);
630 #endif
631
632 if (sq->reaper) {
633 (void) rpmsqWaitUnregister(sq);
634 } else {
635 pid_t reaped;
636 int status;
637 do {
638 reaped = waitpid(sq->child, &status, 0);
639 } while (reaped >= 0 && reaped != sq->child);
640 sq->reaped = reaped;
641 sq->status = status;
642 #ifdef _RPMSQ_DEBUG
643 if (_rpmsq_debug)
644 fprintf(stderr, " Waitpid(%p): %p child %d reaped %d\n", ME(), sq, (int)sq->child, (int)sq->reaped);
645 #endif
646 }
647
648 #ifdef _RPMSQ_DEBUG
649 if (_rpmsq_debug)
650 fprintf(stderr, " Fini(%p): %p child %d status 0x%x\n", ME(), sq, (int)sq->child, sq->status);
651 #endif
652
653 return sq->reaped;
654 }
655
656 void * rpmsqThread(void * (*start) (void * arg), void * arg)
657 {
658 #if defined(WITH_PTHREADS)
659 pthread_t pth;
660 int ret;
661
662 ret = pthread_create(&pth, NULL, start, arg);
663 return (ret == 0 ? (void *)pth : NULL);
664 #else
665 (void) start;
666 (void) arg;
667 return NULL;
668 #endif
669 }
670
671 int rpmsqJoin(void * thread)
672 {
673 #if defined(WITH_PTHREADS)
674 pthread_t pth = (pthread_t) thread;
675 if (thread == NULL)
676 return EINVAL;
677 return pthread_join(pth, NULL);
678 #else
679 (void) thread;
680 return EINVAL;
681 #endif
682 }
683
684 int rpmsqThreadEqual(void * thread)
685 {
686 #if defined(WITH_PTHREADS)
687 pthread_t t1 = (pthread_t) thread;
688 pthread_t t2 = pthread_self();
689 return pthread_equal(t1, t2);
690 #else
691 (void) thread;
692 return 0;
693 #endif
694 }
695
699 #if defined(WITH_PTHREADS)
700 static void
701 sigchld_cancel (void *arg)
702 /*@globals rpmsigTbl, fileSystem, internalState @*/
703 /*@modifies rpmsigTbl, fileSystem, internalState @*/
704 {
705 pid_t child = *(pid_t *) arg;
706 pid_t result;
707 int xx;
708
709 xx = kill(child, SIGKILL);
710
711 do {
712 result = waitpid(child, NULL, 0);
713 } while (result == (pid_t)-1 && errno == EINTR);
714
715 xx = DO_LOCK ();
716 if (SUB_REF (rpmsigTbl_sigchld) == 0) {
717 xx = rpmsqEnable(-SIGQUIT, NULL);
718 xx = rpmsqEnable(-SIGINT, NULL);
719 }
720 xx = DO_UNLOCK ();
721 }
722 #endif
723
727 int
728 rpmsqExecve (const char ** argv)
729 /*@globals rpmsigTbl @*/
730 /*@modifies rpmsigTbl @*/
731 {
732 #if defined(WITH_PTHREADS)
733 int oldtype;
734 #endif
735 int status = -1;
736 pid_t pid = 0;
737 pid_t result;
738 sigset_t newMask, oldMask;
739 rpmsq sq = memset(alloca(sizeof(*sq)), 0, sizeof(*sq));
740 int xx;
741
742 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
743 INIT_LOCK ();
744 #endif
745
746 xx = DO_LOCK ();
747 if (ADD_REF (rpmsigTbl_sigchld) == 0) {
748 if (rpmsqEnable(SIGINT, NULL) < 0) {
749 xx = SUB_REF (rpmsigTbl_sigchld);
750 goto out;
751 }
752 if (rpmsqEnable(SIGQUIT, NULL) < 0) {
753 xx = SUB_REF (rpmsigTbl_sigchld);
754 goto out_restore_sigint;
755 }
756 }
757 xx = DO_UNLOCK ();
758
759 (void) sigemptyset (&newMask);
760 (void) sigaddset (&newMask, SIGCHLD);
761 if (sigprocmask (SIG_BLOCK, &newMask, &oldMask) < 0) {
762 xx = DO_LOCK ();
763 if (SUB_REF (rpmsigTbl_sigchld) == 0)
764 goto out_restore_sigquit_and_sigint;
765 goto out;
766 }
767
768 /*@-sysunrecog@*/
769 CLEANUP_HANDLER(sigchld_cancel, &pid, &oldtype);
770 /*@=sysunrecog@*/
771
772 pid = fork ();
773 if (pid < (pid_t) 0) { /* fork failed. */
774 goto out;
775 } else if (pid == (pid_t) 0) { /* Child. */
776
777 /* Restore the signals. */
778 (void) sigaction (SIGINT, &rpmsigTbl_sigint->oact, NULL);
779 (void) sigaction (SIGQUIT, &rpmsigTbl_sigquit->oact, NULL);
780 (void) sigprocmask (SIG_SETMASK, &oldMask, NULL);
781
782 /* Reset rpmsigTbl lock and refcnt. */
783 INIT_LOCK ();
784
785 (void) execve (argv[0], (char *const *) argv, environ);
786 _exit (127);
787 } else { /* Parent. */
788 do {
789 result = waitpid(pid, &status, 0);
790 } while (result == (pid_t)-1 && errno == EINTR);
791 if (result != pid)
792 status = -1;
793 }
794
795 CLEANUP_RESET(0, oldtype);
796
797 xx = DO_LOCK ();
798 if ((SUB_REF (rpmsigTbl_sigchld) == 0 &&
799 (rpmsqEnable(-SIGINT, NULL) < 0 || rpmsqEnable (-SIGQUIT, NULL) < 0))
800 || sigprocmask (SIG_SETMASK, &oldMask, NULL) != 0)
801 {
802 status = -1;
803 }
804 goto out;
805
806 out_restore_sigquit_and_sigint:
807 xx = rpmsqEnable(-SIGQUIT, NULL);
808 out_restore_sigint:
809 xx = rpmsqEnable(-SIGINT, NULL);
810 out:
811 xx = DO_UNLOCK ();
812 return status;
813 }