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 *****************************************************************************/
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 */
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.
43 * Input: the signal to handle
44 * the signal handler function
45 * Return: the previous sigaction structure
47 inline SigFunc * Signal(int signo, SigFunc *func)
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 */
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)){
60 return (old_handl.sa_handler);
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
72 * Input: the signal to handle
73 * the signal handler function
74 * Return: the previous sigaction structure
76 inline SigFunc * SignalRestart(int signo, SigFunc *func)
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 */
85 /* change action for signo signal */
86 if (sigaction(signo, &new_handl, &old_handl)){
89 return (old_handl.sa_handler);
94 * Functions: HandSigCHLD
95 * Generic handler for SIGCHLD signal
97 * Simone Piccardi Dec. 2002
99 void HandSigCHLD(int sig)
104 /* save errno current value */
109 pid = waitpid(WAIT_ANY, &status, WNOHANG);
111 // debug("child %d terminated with status %x\n", pid, status);
114 /* restore errno value*/