Varie correzioni, completata revisione capitolo sull'I/O su file
[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  *****************************************************************************/
26 #include <errno.h>       /* error definitions and routines */
27 #include <stdio.h>       /* standard I/O library */
28 #include <signal.h>      /* signal constants, types and functions */
29 #include <sys/types.h>   /* primitive system data types */
30 #include <sys/wait.h>    /* process termination constants and functions */
31
32 #include "Gapil.h"
33 #include "macros.h"
34
35 /*
36  * Function Signal
37  * Initialize a signal handler.
38  * To enable the signal handling a process we need to tell it to
39  * kernel; this is done writing all needed info to a sigaction structure
40  * named sigact, and then callind sigaction() system call passing the
41  * information stored in the sigact structure variable.
42  *
43  * Input:  the signal to handle 
44  *         the signal handler function
45  * Return: the previous sigaction structure
46  */
47 inline SigFunc * Signal(int signo, SigFunc *func) 
48 {
49     struct sigaction new_handl, old_handl;
50     new_handl.sa_handler = func;             /* set signal handler */
51     /* clear signal mask: no signal blocked during execution of func */
52     if (sigemptyset(&new_handl.sa_mask)!=0){ /* initialize signal set */
53         return SIG_ERR;
54     }
55     new_handl.sa_flags=0;                    /* init to 0 all flags */
56     /* change action for signo signal */
57     if (sigaction(signo, &new_handl, &old_handl)){ 
58         return SIG_ERR;
59     }
60     return (old_handl.sa_handler);
61 }
62
63 /*
64  * Function SignalRestart
65  * Initialize a signal handler.
66  * To enable the signal handling a process we need to tell it to
67  * kernel; this is done writing all needed info to a sigaction structure
68  * named sigact, and then callind sigaction() system call passing the
69  * information stored in the sigact structure variable.
70  * This version enable BSD semantics with SA_RESTART
71  *
72  * Input:  the signal to handle 
73  *         the signal handler function
74  * Return: the previous sigaction structure
75  */
76 inline SigFunc * SignalRestart(int signo, SigFunc *func) 
77 {
78     struct sigaction new_handl, old_handl;
79     new_handl.sa_handler = func;             /* set signal handler */
80     new_handl.sa_flags = SA_RESTART;         /* restart system call */
81     /* clear signal mask: no signal blocked during execution of func */
82     if (sigemptyset(&new_handl.sa_mask)!=0){ /* initialize signal set */
83         return SIG_ERR;
84     }
85     /* change action for signo signal */
86     if (sigaction(signo, &new_handl, &old_handl)){ 
87         return SIG_ERR;
88     }
89     return (old_handl.sa_handler);
90 }
91
92
93 /* 
94  * Functions: HandSigCHLD
95  * Generic handler for SIGCHLD signal
96  * 
97  * Simone Piccardi Dec. 2002
98  */
99 void HandSigCHLD(int sig)
100 {
101     int errno_save;
102     int status;
103     pid_t pid;
104     /* save errno current value */
105     errno_save = errno;
106     /* loop until no */
107     do {
108         errno = 0;
109         pid = waitpid(WAIT_ANY, &status, WNOHANG);
110 //      if (pid > 0) {
111 //          debug("child %d terminated with status %x\n", pid, status);
112 //      }
113     } while (pid > 0);
114     /* restore errno value*/
115     errno = errno_save;
116     /* return */
117     return;
118 }