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 * Program testsignalcgi
22 * CGI to test timout signals sent by apache
24 * Author: Simone Piccardi
27 * Usage: cgi-bin for apache.
28 * Called by downloading something like:
29 * http://localhost/cgi-bin/testsinglacgi
31 ****************************************************************/
33 * Include needed headers
35 #include <unistd.h> /* unix standard library */
36 #include <stdio.h> /* standard I/O library */
37 #include <stdlib.h> /* C standard library */
38 #include <string.h> /* C strings library */
39 #include <signal.h> /* signal constants, types and functions */
42 void sig_hand(int, siginfo_t *, void *);
43 typedef void SigFunc(int, siginfo_t *, void *);
44 /* Function Signal: Initialize a signal handler. See SigHand.c */
45 SigFunc * Signal(int signo, SigFunc *func);
50 int main(int argc, char *argv[], char *envp[])
53 * Variables definition
55 char content[]="Content-type: text/html\n\n";
58 struct sigaction new_handl, old_handl;
59 /* write mime-type to stdout */
60 write(STDOUT_FILENO, content, strlen(content));
61 file = fopen("/tmp/testsignalcgi.out","w+");
62 Signal(SIGPIPE,sig_hand);
63 Signal(SIGQUIT,sig_hand);
64 Signal(SIGTERM,sig_hand);
65 Signal(SIGINT,sig_hand);
66 Signal(SIGHUP,sig_hand);
67 Signal(SIGUSR1,sig_hand);
68 Signal(SIGUSR2,sig_hand);
69 Signal(SIGIO,sig_hand);
70 Signal(SIGURG,sig_hand);
71 Signal(SIGABRT,sig_hand);
74 fprintf(file, "Exit time: %s", ctime(&t));
79 * Signal Handler to print signal
81 void sig_hand(int sig, siginfo_t * siginfo, void *ptr) {
83 /* just return to interrupt sigsuspend */
87 fprintf(file, "Received time: %s", ctime(&t));
89 fprintf(file, "My pid is: %i, sending pid %i, uid %i\n",
90 pid,siginfo->si_pid,siginfo->si_uid);
91 fprintf(file, "Signal is: %i,%i, err %i, code %i\n",
92 sig,siginfo->si_signo,siginfo->si_errno,siginfo->si_code);
95 fprintf(file, "Printed time: %s", ctime(&t));
98 inline SigFunc * Signal(int signo, SigFunc *func)
100 struct sigaction new_handl, old_handl;
101 new_handl.sa_sigaction = func; /* set signal handler */
102 /* clear signal mask: no signal blocked during execution of func */
103 sigemptyset(&new_handl.sa_mask);
104 new_handl.sa_flags=SA_SIGINFO;
105 /* change action for signo signal */
106 sigaction(signo, &new_handl, &old_handl);
107 return (old_handl.sa_sigaction);