Main Page | Modules | Data Structures | File List | Data Fields | Related Pages

sig.c

00001 /***************************************************************************
00002  * CVSID: $Id: sig.c,v 1.3 2004/08/07 15:10:47 stefanb Exp $
00003  *
00004  * sig.c : Functions for signal handling
00005  *
00006  * Copyright (C) 2004 Stefan Bambach, <sbambach@gmx.net>
00007  *
00008  * Licensed under the GNU General Public License 2.0
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  **************************************************************************/
00025  
00026 #ifdef HAVE_CONFIG_H
00027 #  include <config.h>
00028 #endif
00029 
00030 #include <stdio.h>
00031 #include <signal.h>
00032 
00033 #include "sig.h"
00034 #include "dbug_mem.h"
00035 
00036 
00037 
00038 sb_signal_handler_t sb_signal_handler[MAX_SIGNALS];
00039 
00040 
00041 
00042 int
00043 sb_signal_init(void)
00044 {
00045     int i=0;
00046     
00047     for (i=0; i<MAX_SIGNALS; i++) {
00048         sb_signal_handler[i] = SIG_DFL;
00049     }
00050     
00051     return 0;
00052 }
00053 
00054 
00055 
00056 int
00057 sb_signal_is_initialized(void)
00058 {
00059     static int initialized = 0;
00060     
00061     if (initialized == 0) {
00062         if (sb_signal_init() == 0) {
00063             initialized = 1;
00064         }
00065     }
00066     
00067     return initialized;
00068 }
00069 
00070 
00071 
00072 int
00073 sb_signal_set_handler(int signum, sb_signal_handler_t handler)
00074 {
00075     sb_signal_handler_t old_handler = SIG_DFL;
00076     
00077     if (sb_signal_is_initialized() == 0) {
00078         return -1;
00079     }
00080 
00081     if (sb_signal_handler[signum] == SIG_DFL) {
00082         old_handler = signal(signum, handler);
00083         if (old_handler != SIG_ERR) {
00084             sb_signal_handler[signum] = old_handler;
00085             return 0;
00086         }
00087     }
00088     
00089     return -1;
00090 }
00091  
00092 
00093 
00094 int
00095 sb_signal_restore_handler(int signum)
00096 {
00097     if (sb_signal_is_initialized() == 0) {
00098         return -1;
00099     }
00100 
00101     if (sb_signal_handler[signum] != SIG_DFL) {
00102         if (signal(signum, sb_signal_handler[signum]) == SIG_ERR) {
00103             return -1;
00104         }
00105     }
00106     
00107     return 0;
00108 }
00109  
00110 
00111 
00112 int
00113 sb_signal_restore_all_handlers(void)
00114 {
00115     int signum=0;
00116     int error=0;
00117     
00118     if (sb_signal_is_initialized() == 0) {
00119         return -1;
00120     }
00121 
00122     for (signum=0; signum<MAX_SIGNALS; signum++) {
00123         if (sb_signal_restore_handler(signum) != 0) {
00124             error++;
00125         }
00126     }
00127     
00128     if (error > 0) {
00129         return -1;
00130     }
00131     
00132     return 0;
00133 }

Generated on Wed Mar 30 13:43:27 2005 for Mntd by  doxygen 1.3.9.1