X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2FSigHand.c;h=605493d1f2f6779c7200f589e125658c2f98cc64;hp=12a7c5b4bf1ab04d73655277cfd0462064ecd9ab;hb=c2e762abed93fe970c6c4d019a8bfe95fadb4efa;hpb=6483a787322c614bc6282a0bf0ee001f1bf54b44 diff --git a/sources/SigHand.c b/sources/SigHand.c index 12a7c5b..605493d 100644 --- a/sources/SigHand.c +++ b/sources/SigHand.c @@ -1,17 +1,33 @@ +/* SigHand.c + * + * Copyright (C) 2002 Simone Piccardi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ /***************************************************************************** * * File SigHand.c: define a set of functions for signal manipulation * * Author: S. Piccardi Dec. 2002 * - * $Id: SigHand.c,v 1.1 2002/12/03 11:06:05 piccardi Exp $ - * *****************************************************************************/ -#include /* error simbol definitions */ -#include /* standard I/O functions */ -#include /* signal handling declarations */ -#include -#include +#include /* error definitions and routines */ +#include /* standard I/O library */ +#include /* signal constants, types and functions */ +#include /* primitive system data types */ +#include /* process termination constants and functions */ #include "Gapil.h" #include "macros.h" @@ -21,36 +37,118 @@ * Initialize a signal handler. * To enable the signal handling a process we need to tell it to * kernel; this is done writing all needed info to a sigaction structure - * named sigact, and then callind sigaction() system call passing the + * named sigact, and then calling sigaction() system call passing the * information stored in the sigact structure variable. * * Input: the signal to handle * the signal handler function - * Return: the previous sigaction structure + * Return: the previous signal handler */ -inline SigFunc * Signal(int signo, SigFunc *func) +inline SigHandler * Signal(int signo, SigHandler *func) { struct sigaction new_handl, old_handl; - new_handl.sa_handler=func; + new_handl.sa_flags=0; /* init to 0 all flags */ + new_handl.sa_handler = func; /* set signal handler */ /* clear signal mask: no signal blocked during execution of func */ - if (sigemptyset(&new_handl.sa_mask)!=0){ /* initialize signal set */ - perror("cannot initializes the signal set to empty"); /* see mess. */ - exit(1); + if (sigemptyset(&new_handl.sa_mask)!=0){ /* initialize signal set */ + return SIG_ERR; } - new_handl.sa_flags=0; /* init to 0 all flags */ /* change action for signo signal */ - if (sigaction(signo,&new_handl,&old_handl)){ - perror("sigaction failed on signal action setting"); - exit(1); + if (sigaction(signo, &new_handl, &old_handl)){ + return SIG_ERR; } return (old_handl.sa_handler); } + +/* + * Function SignalRestart + * Initialize a signal handler. + * To enable the signal handling a process we need to tell it to + * kernel; this is done writing all needed info to a sigaction structure + * named sigact, and then calling sigaction() system call passing the + * information stored in the sigact structure variable. + * This version enable BSD semantics with SA_RESTART + * + * Input: the signal to handle + * the signal handler function + * Return: the previous signal handler + */ +inline SigHandler * SignalRestart(int signo, SigHandler *func) +{ + struct sigaction new_handl, old_handl; + new_handl.sa_flags = SA_RESTART; /* restart system call */ + new_handl.sa_handler = func; /* set signal handler */ + /* clear signal mask: no signal blocked during execution of func */ + if (sigemptyset(&new_handl.sa_mask)!=0){ /* initialize signal set */ + return SIG_ERR; + } + /* change action for signo signal */ + if (sigaction(signo, &new_handl, &old_handl)){ + return SIG_ERR; + } + return (old_handl.sa_handler); +} + +/* + * Function Action + * Initialize a sa_sigaction signal handler. + * To enable the signal handling a process we need to tell it to + * kernel; this is done writing all needed info to a sigaction structure + * named sigact, and then calling sigaction() system call passing the + * information stored in the sigact structure variable. + * + * Input: the signal to handle + * the signal handler function (sa_sigaction type) + * Return: the previous signal handler + */ +inline SigAction * Action(int signo, SigAction *func) +{ + struct sigaction new_handl, old_handl; + new_handl.sa_flags=SA_SIGINFO; /* we use sa_sigaction handler */ + new_handl.sa_sigaction = func; /* set signal handler */ + /* clear signal mask: no signal blocked during execution of func */ + if (sigemptyset(&new_handl.sa_mask)!=0){ /* initialize signal set */ + return NULL; + } + /* change action for signo signal */ + if (sigaction(signo, &new_handl, &old_handl)){ + return NULL; + } + return (old_handl.sa_sigaction); +} +/* + * Function Action + * Initialize a sa_sigaction signal handler. + * To enable the signal handling a process we need to tell it to + * kernel; this is done writing all needed info to a sigaction structure + * named sigact, and then calling sigaction() system call passing the + * information stored in the sigact structure variable. + * + * Input: the signal to handle + * the signal handler function (sa_sigaction type) + * Return: the previous signal handler + */ +inline SigAction * ActionRestart(int signo, SigAction *func) +{ + struct sigaction new_handl, old_handl; + new_handl.sa_flags=SA_SIGINFO|SA_RESTART;/* flag setup */ + new_handl.sa_sigaction = func; /* set signal handler */ + /* clear signal mask: no signal blocked during execution of func */ + if (sigemptyset(&new_handl.sa_mask)!=0){ /* initialize signal set */ + return NULL; + } + /* change action for signo signal */ + if (sigaction(signo, &new_handl, &old_handl)){ + return NULL; + } + return (old_handl.sa_sigaction); +} + /* * Functions: HandSigCHLD - * Generic handler for SIGCHLD signal + * Generic simple handler for SIGCHLD signal * * Simone Piccardi Dec. 2002 - * $Id: SigHand.c,v 1.1 2002/12/03 11:06:05 piccardi Exp $ */ void HandSigCHLD(int sig) { @@ -63,10 +161,10 @@ void HandSigCHLD(int sig) do { errno = 0; pid = waitpid(WAIT_ANY, &status, WNOHANG); - if (pid > 0) { - debug("child %d terminated with status %x\n", pid, status); - } - } while ((pid > 0) && (errno == EINTR)); +// if (pid > 0) { +// debug("child %d terminated with status %x\n", pid, status); +// } + } while (pid > 0); /* restore errno value*/ errno = errno_save; /* return */