3 * Copyright (C) 2002 Simone Piccardi
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.
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.
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.
19 /*****************************************************************************
21 * File SigHand.c: define a set of functions for signal manipulation
23 * Author: S. Piccardi Dec. 2002
25 * $Id: SigHand.c,v 1.7 2003/05/06 14:05:12 piccardi Exp $
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>
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.
45 * Input: the signal to handle
46 * the signal handler function
47 * Return: the previous sigaction structure
49 inline SigFunc * Signal(int signo, SigFunc *func)
51 struct sigaction new_handl, old_handl;
52 new_handl.sa_handler = func; /* set signal handler */
53 /* clear signal mask: no signal blocked during execution of func */
54 if (sigemptyset(&new_handl.sa_mask)!=0){ /* initialize signal set */
57 new_handl.sa_flags=0; /* init to 0 all flags */
58 /* change action for signo signal */
59 if (sigaction(signo, &new_handl, &old_handl)){
62 return (old_handl.sa_handler);
66 * Function SignalRestart
67 * Initialize a signal handler.
68 * To enable the signal handling a process we need to tell it to
69 * kernel; this is done writing all needed info to a sigaction structure
70 * named sigact, and then callind sigaction() system call passing the
71 * information stored in the sigact structure variable.
72 * This version enable BSD semantics with SA_RESTART
74 * Input: the signal to handle
75 * the signal handler function
76 * Return: the previous sigaction structure
78 inline SigFunc * SignalRestart(int signo, SigFunc *func)
80 struct sigaction new_handl, old_handl;
81 new_handl.sa_handler = func; /* set signal handler */
82 new_handl.sa_flags = SA_RESTART; /* restart system call */
83 /* clear signal mask: no signal blocked during execution of func */
84 if (sigemptyset(&new_handl.sa_mask)!=0){ /* initialize signal set */
87 /* change action for signo signal */
88 if (sigaction(signo, &new_handl, &old_handl)){
91 return (old_handl.sa_handler);
96 * Functions: HandSigCHLD
97 * Generic handler for SIGCHLD signal
99 * Simone Piccardi Dec. 2002
100 * $Id: SigHand.c,v 1.7 2003/05/06 14:05:12 piccardi Exp $
102 void HandSigCHLD(int sig)
107 /* save errno current value */
112 pid = waitpid(WAIT_ANY, &status, WNOHANG);
114 // debug("child %d terminated with status %x\n", pid, status);
116 } while ((pid > 0) && (errno == EINTR));
117 /* restore errno value*/