Aggiunto esempio di gestione di segnali con la semantica BSD per il restart
[gapil.git] / sources / SigHand.c
index 89585e3cc63c3f3e485aca5a4017fae8288414f3..3fb588a99458b1903cded34effa155f5159b6504 100644 (file)
@@ -22,7 +22,7 @@
  *
  * Author: S. Piccardi Dec. 2002
  *
- * $Id: SigHand.c,v 1.5 2003/05/02 09:55:14 piccardi Exp $
+ * $Id: SigHand.c,v 1.6 2003/05/06 11:29:16 piccardi Exp $
  *
  *****************************************************************************/
 #include <errno.h>                               /* error simbol definitions */
 inline SigFunc * Signal(int signo, SigFunc *func) 
 {
     struct sigaction new_handl, old_handl;
-    new_handl.sa_handler = func;                      /* set signal handler */
+    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 */
+    if (sigemptyset(&new_handl.sa_mask)!=0){ /* initialize signal set */
         return SIG_ERR;
     }
-    new_handl.sa_flags=0;                            /* init to 0 all flags */
+    new_handl.sa_flags=0;                    /* init to 0 all flags */
     /* change action for signo signal */
-    if (sigaction(signo,&new_handl,&old_handl)){ 
+    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 callind 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 sigaction structure
+ */
+inline SigFunc * SignalRestart(int signo, SigFunc *func) 
+{
+    struct sigaction new_handl, old_handl;
+    new_handl.sa_handler = func;             /* set signal handler */
+    new_handl.sa_flags = SA_RESTART;         /* restart system call */
+    /* 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);
+}
+
+
 /* 
  * Functions: HandSigCHLD
  * Generic handler for SIGCHLD signal
  * 
  * Simone Piccardi Dec. 2002
- * $Id: SigHand.c,v 1.5 2003/05/02 09:55:14 piccardi Exp $
+ * $Id: SigHand.c,v 1.6 2003/05/06 11:29:16 piccardi Exp $
  */
 void HandSigCHLD(int sig)
 {