©2004 by Marc J. Rochkind. All rights reserved. Portions marked "Open Source" may be copied under license.

 

Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

c9/sigmin.c

Go to the documentation of this file.
00001 /*
00002     Minimal defensive signal handling
00003     AUP2, Sec. 9.01.8
00004 
00005     Copyright 2003 by Marc J. Rochkind. All rights reserved.
00006     May be copied only for purposes and under conditions described
00007     on the Web page www.basepath.com/aup/copyright.htm.
00008 
00009     The Example Files are provided "as is," without any warranty;
00010     without even the implied warranty of merchantability or fitness
00011     for a particular purpose. The author and his publisher are not
00012     responsible for any damages, direct or incidental, resulting
00013     from the use or non-use of these Example Files.
00014 
00015     The Example Files may contain defects, and some contain deliberate
00016     coding mistakes that were included for educational reasons.
00017     You are responsible for determining if and how the Example Files
00018     are to be used.
00019 
00020 */
00021 #include "defs.h"
00022 /*[clean_up]*/
00023 static void clean_up(void)
00024 {
00025     /*
00026         Clean-up code goes here --
00027         must be async-signal-safe.
00028     */
00029 }
00030 
00031 static size_t strlen_safe(const char *s)
00032 {
00033     size_t n = 0;
00034     while (*s++ != '\0')
00035         n++;
00036     return n;
00037 }
00038 /*[handler]*/
00039 static void handler(int signum)
00040 {
00041     int i;
00042     struct {
00043         int signum;
00044         char *msg;
00045     } sigmsg[] = {
00046         { SIGTERM, "Termination signal" },
00047         { SIGBUS, "Access to undefined portion of a memory object" },
00048         { SIGFPE, "Erroneous arithmetic operation" },
00049         { SIGILL, "Illegal instruction" },
00050         { SIGSEGV, "Invalid memory reference" },
00051         { SIGSYS, "Bad system call" },
00052         { SIGXCPU, "CPU-time limit exceeded" },
00053         { SIGXFSZ, "File-size limit exceeded" },
00054         { 0, NULL}
00055     };
00056 
00057     clean_up();
00058     for (i = 0; sigmsg[i].signum > 0; i++)
00059         if (sigmsg[i].signum == signum) {
00060             (void)write(STDERR_FILENO, sigmsg[i].msg,
00061               strlen_safe(sigmsg[i].msg));
00062             (void)write(STDERR_FILENO, "\n", 1);
00063             break;
00064         }
00065     _exit(EXIT_FAILURE);
00066 }
00067 /*[handle_signals]*/
00068 static bool handle_signals(void)
00069 {
00070     sigset_t set;
00071     struct sigaction act;
00072 
00073     ec_neg1( sigfillset(&set) )
00074     ec_neg1( sigprocmask(SIG_SETMASK, &set, NULL) )
00075     memset(&act, 0, sizeof(act));
00076     ec_neg1( sigfillset(&act.sa_mask) )
00077     act.sa_handler = SIG_IGN;
00078     ec_neg1( sigaction(SIGHUP, &act, NULL) )
00079     ec_neg1( sigaction(SIGINT, &act, NULL) )
00080     ec_neg1( sigaction(SIGQUIT, &act, NULL) )
00081     ec_neg1( sigaction(SIGPIPE, &act, NULL) )
00082     act.sa_handler = handler;
00083     ec_neg1( sigaction(SIGTERM, &act, NULL) )
00084     ec_neg1( sigaction(SIGBUS, &act, NULL) )
00085     ec_neg1( sigaction(SIGFPE, &act, NULL) )
00086     ec_neg1( sigaction(SIGILL, &act, NULL) )
00087     ec_neg1( sigaction(SIGSEGV, &act, NULL) )
00088     ec_neg1( sigaction(SIGSYS, &act, NULL) )
00089     ec_neg1( sigaction(SIGXCPU, &act, NULL) )
00090     ec_neg1( sigaction(SIGXFSZ, &act, NULL) )
00091     ec_neg1( sigemptyset(&set) )
00092     ec_neg1( sigprocmask(SIG_SETMASK, &set, NULL) )
00093     return true;
00094 
00095 EC_CLEANUP_BGN
00096     return false;
00097 EC_CLEANUP_END
00098 }
00099 /*[]*/
00100 void process(void)
00101 {
00102     ec_false (handle_signals() )
00103     sleep(2);
00104     exit(EXIT_SUCCESS);
00105 
00106 EC_CLEANUP_BGN
00107     exit(EXIT_FAILURE);
00108 EC_CLEANUP_END
00109 }
00110 
00111 #define MAX_SIG 50
00112 
00113 int main(void)
00114 {
00115     sigset_t set;
00116     int x = 1, y;
00117 
00118     ec_neg1( sigfillset(&set) )
00119     ec_neg1( sigprocmask(SIG_SETMASK, &set, NULL) )
00120     y = x / 0;
00121 #if 0
00122     int signum;
00123     pid_t pid[MAX_SIG + 1];
00124     int status;
00125 
00126     for (signum = 1; signum <= MAX_SIG; signum++) {
00127         ec_neg1( pid[signum] = fork() )
00128         if (pid[signum] == 0)
00129             process();
00130         sleep(1);
00131         printf("\nSending signal %d...\n", signum);
00132         if (kill(pid[signum], signum) == -1)
00133             printf("\tkill failed\n");
00134         ec_neg1( waitpid(-1, &status, WUNTRACED) )
00135         if (WIFEXITED(status))
00136             printf("%ld exited normally\n", (long)pid[signum]);
00137         else if (WIFSIGNALED(status))
00138             printf("%ld exited with signal %d\n", (long)pid[signum],
00139               WTERMSIG(status));
00140         else
00141             printf("wait returned with other status\n");
00142     }
00143     for (signum = 1; signum <= MAX_SIG; signum++)
00144         (void)kill(pid[signum], SIGKILL);
00145 #endif
00146     exit(EXIT_SUCCESS);
00147 
00148 EC_CLEANUP_BGN
00149     exit(EXIT_FAILURE);
00150 EC_CLEANUP_END
00151 }
00152 /*[]*/

Generated on Fri Apr 23 10:57:03 2004 for AUP2 Example Source by doxygen 1.3.1