12a7c5b4bf1ab04d73655277cfd0462064ecd9ab
[gapil.git] / sources / SigHand.c
1 /*****************************************************************************
2  *
3  * File SigHand.c: define a set of functions for signal manipulation 
4  *
5  * Author: S. Piccardi Dec. 2002
6  *
7  * $Id: SigHand.c,v 1.1 2002/12/03 11:06:05 piccardi Exp $
8  *
9  *****************************************************************************/
10 #include <errno.h>                               /* error simbol definitions */
11 #include <stdio.h>                                 /* standard I/O functions */
12 #include <signal.h>                          /* signal handling declarations */
13 #include <sys/types.h>
14 #include <sys/wait.h>
15
16 #include "Gapil.h"
17 #include "macros.h"
18
19 /*
20  * Function Signal
21  * Initialize a signal handler.
22  * To enable the signal handling a process we need to tell it to
23  * kernel; this is done writing all needed info to a sigaction structure
24  * named sigact, and then callind sigaction() system call passing the
25  * information stored in the sigact structure variable.
26  *
27  * Input:  the signal to handle 
28  *         the signal handler function
29  * Return: the previous sigaction structure
30  */
31 inline SigFunc * Signal(int signo, SigFunc *func) 
32 {
33     struct sigaction new_handl, old_handl;
34     new_handl.sa_handler=func;
35     /* clear signal mask: no signal blocked during execution of func */
36     if (sigemptyset(&new_handl.sa_mask)!=0){        /* initialize signal set */
37         perror("cannot initializes the signal set to empty");   /* see mess. */
38         exit(1);
39     }
40     new_handl.sa_flags=0;                  /* init to 0 all flags */
41     /* change action for signo signal */
42     if (sigaction(signo,&new_handl,&old_handl)){ 
43         perror("sigaction failed on signal action setting");
44         exit(1);
45     }
46     return (old_handl.sa_handler);
47 }
48 /* 
49  * Functions: HandSigCHLD
50  * Generic handler for SIGCHLD signal
51  * 
52  * Simone Piccardi Dec. 2002
53  * $Id: SigHand.c,v 1.1 2002/12/03 11:06:05 piccardi Exp $
54  */
55 void HandSigCHLD(int sig)
56 {
57     int errno_save;
58     int status;
59     pid_t pid;
60     /* save errno current value */
61     errno_save = errno;
62     /* loop until no */
63     do {
64         errno = 0;
65         pid = waitpid(WAIT_ANY, &status, WNOHANG);
66         if (pid > 0) {
67             debug("child %d terminated with status %x\n", pid, status);
68         }
69     } while ((pid > 0) && (errno == EINTR));
70     /* restore errno value*/
71     errno = errno_save;
72     /* return */
73     return;
74 }