Varie correzioni, completata revisione capitolo sull'I/O su file
[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
49 /* Program begin */
50 int main(int argc, char *argv[], char *envp[])
51 {
52 /*
53  * Variables definition  
54  */
55     char content[]="Content-type: text/html\n\n";
56     time_t t;
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/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);
72     pause();
73     t = time(NULL);
74     fprintf(file, "Exit time: %s", ctime(&t));
75     exit(0);
76 }
77
78 /*
79  * Signal Handler to print signal
80  */
81 void sig_hand(int sig, siginfo_t * siginfo, void *ptr) {
82
83     /* just return to interrupt sigsuspend */
84     pid_t pid;
85     time_t t;
86     t = time(NULL);
87     fprintf(file, "Received time: %s", ctime(&t));
88     pid=getpid();
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);
93     sleep(1);
94     t = time(NULL);
95     fprintf(file, "Printed time: %s", ctime(&t));
96 }
97
98 inline SigFunc * Signal(int signo, SigFunc *func) 
99 {
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);
108 }