53d185a08f50be41176c66ac1683fa2c2e840b0e
[gapil.git] / sources / SigHand.c
1 /* SigHand.c
2  * 
3  * Copyright (C) 2002 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /*****************************************************************************
20  *
21  * File SigHand.c: define a set of functions for signal manipulation 
22  *
23  * Author: S. Piccardi Dec. 2002
24  *
25  * $Id: SigHand.c,v 1.2 2002/12/03 22:30:11 piccardi Exp $
26  *
27  *****************************************************************************/
28 #include <errno.h>                               /* error simbol definitions */
29 #include <stdio.h>                                 /* standard I/O functions */
30 #include <signal.h>                          /* signal handling declarations */
31 #include <sys/types.h>
32 #include <sys/wait.h>
33
34 #include "Gapil.h"
35 #include "macros.h"
36
37 /*
38  * Function Signal
39  * Initialize a signal handler.
40  * To enable the signal handling a process we need to tell it to
41  * kernel; this is done writing all needed info to a sigaction structure
42  * named sigact, and then callind sigaction() system call passing the
43  * information stored in the sigact structure variable.
44  *
45  * Input:  the signal to handle 
46  *         the signal handler function
47  * Return: the previous sigaction structure
48  */
49 inline SigFunc * Signal(int signo, SigFunc *func) 
50 {
51     struct sigaction new_handl, old_handl;
52     new_handl.sa_handler=func;
53     /* clear signal mask: no signal blocked during execution of func */
54     if (sigemptyset(&new_handl.sa_mask)!=0){        /* initialize signal set */
55         perror("cannot initializes the signal set to empty");   /* see mess. */
56         exit(1);
57     }
58     new_handl.sa_flags=0;                  /* init to 0 all flags */
59     /* change action for signo signal */
60     if (sigaction(signo,&new_handl,&old_handl)){ 
61         perror("sigaction failed on signal action setting");
62         exit(1);
63     }
64     return (old_handl.sa_handler);
65 }
66 /* 
67  * Functions: HandSigCHLD
68  * Generic handler for SIGCHLD signal
69  * 
70  * Simone Piccardi Dec. 2002
71  * $Id: SigHand.c,v 1.2 2002/12/03 22:30:11 piccardi Exp $
72  */
73 void HandSigCHLD(int sig)
74 {
75     int errno_save;
76     int status;
77     pid_t pid;
78     /* save errno current value */
79     errno_save = errno;
80     /* loop until no */
81     do {
82         errno = 0;
83         pid = waitpid(WAIT_ANY, &status, WNOHANG);
84         if (pid > 0) {
85             debug("child %d terminated with status %x\n", pid, status);
86         }
87     } while ((pid > 0) && (errno == EINTR));
88     /* restore errno value*/
89     errno = errno_save;
90     /* return */
91     return;
92 }