Test program to look at Apache timount signal.
[gapil.git] / sources / testsignalcgi.c
1 /* testsignalcgi.c
2  * 
3  * Copyright (C) 2002 Simone Piccardi
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19 /****************************************************************
20  *
21  * Program testsignalcgi
22  * CGI to test timout signals sent by apache
23  *
24  * Author: Simone Piccardi
25  * May 2011
26  *
27  * Usage: cgi-bin for apache.
28  * Called by downloading something like:
29  * http://localhost/cgi-bin/testsinglacgi
30  *
31  ****************************************************************/
32 /* 
33  * Include needed headers
34  */
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 */
40 #include <time.h>
41
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);
46
47 FILE * file;
48 time_t t;
49
50 /* Program begin */
51 int main(int argc, char *argv[], char *envp[])
52 {
53 /*
54  * Variables definition  
55  */
56     char content[]="Content-type: text/html\n\n";
57     int i;
58     struct sigaction new_handl, old_handl;
59     /* write mime-type to stdout */ 
60     write(STDOUT_FILENO, content, strlen(content));
61     file = fopen("/tmp/testcgi.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(SIGIO,sig_hand);
71     Signal(SIGURG,sig_hand);
72     Signal(SIGABRT,sig_hand);
73     pause();
74     t = time(NULL);
75     fprintf(stdout, "Exit time: %s", ctime(&t));
76     fprintf(file, "Exit time: %s", ctime(&t));
77     exit(0);
78 }
79
80 /*
81  * Signal Handler to print signal
82  */
83 void sig_hand(int sig, siginfo_t * siginfo, void *ptr) {
84
85     /* just return to interrupt sigsuspend */
86     pid_t pid;
87     t = time(NULL);
88     fprintf(stdout, "Received time: %s", ctime(&t));
89     fprintf(file, "Received time: %s", ctime(&t));
90     fprintf(stdout, "Signal is: %i\n", sig);
91     pid=getpid();
92     fprintf(file, "My pid is: %i, sending pid %i, uid %i\n", 
93             pid,siginfo->si_pid,siginfo->si_uid);
94     fprintf(file, "Signal is: %i,%i, err %i, code %i\n", 
95             sig,siginfo->si_signo,siginfo->si_errno,siginfo->si_code);
96     sleep(1);
97     t = time(NULL);
98     fprintf(stdout, "Printed time: %s", ctime(&t));
99     fprintf(file, "Printed time: %s", ctime(&t));
100 }
101
102 inline SigFunc * Signal(int signo, SigFunc *func) 
103 {
104     struct sigaction new_handl, old_handl;
105     new_handl.sa_sigaction = func;             /* set signal handler */
106     /* clear signal mask: no signal blocked during execution of func */
107     sigemptyset(&new_handl.sa_mask);
108     new_handl.sa_flags=SA_SIGINFO; 
109     /* change action for signo signal */
110     sigaction(signo, &new_handl, &old_handl);
111     return (old_handl.sa_sigaction);
112 }