Varie correzioni, completata revisione capitolo sull'I/O su file
[gapil.git] / sources / test_signalfd.c
1 /* test_signalfd.c
2  * 
3  * Copyright (C) 2011 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 test_signalfd.c: 
22  * Program to test signalfd behaviour on fd close
23  *
24  * Author: Simone Piccardi
25  * Jan. 2011
26  *
27  * Usage: test_signalfd -h give all info's
28  *
29  ****************************************************************/
30 /* 
31  * Include needed headers
32  */
33 #define _GNU_SOURCE
34 #include <errno.h>       /* error definitions and routines */ 
35 #include <stdlib.h>      /* C standard library */
36 #include <unistd.h>      /* unix standard library */
37 #include <stdio.h>       /* standard I/O library */
38 #include <string.h>      /* C strings library */
39 #include <signal.h>      /* signal constants, types and functions */
40 #include <sys/signalfd.h>/* Linux signalfd interface */
41 #include <time.h>
42
43 #include "macros.h"
44 #include "Gapil.h"
45
46 /* Subroutines declaration */
47 void usage(void);
48 void die(char *);
49 void HandSIGINT(int signo);
50
51 int main(int argc, char *argv[])
52 {
53 /* 
54  * Variables definition  
55  */
56     int i, n, nomask=0, testwrite=0, sigfd;
57     time_t t;
58     struct signalfd_siginfo siginf;
59     sigset_t sigmask;
60     char buffer[] = "test write";
61    /*
62      * Input section: decode command line parameters 
63      * Use getopt function
64      */
65     opterr = 0;  /* don't want writing to stderr */
66     while ( (i = getopt(argc, argv, "hnw")) != -1) {
67         switch (i) {
68         /* 
69          * Handling options 
70          */ 
71         case 'h':   /* help option */
72             printf("Wrong -h option use\n");
73             usage();
74             return -1;
75             break;
76         case 'n':   /* no mask option */
77             nomask = 1;
78             break;
79         case 'w':   /* testwrite option */
80             testwrite = 1;
81             break;
82         case '?':   /* unrecognized options */
83             printf("Unrecognized options -%c\n",optopt);
84             usage();
85         default:    /* should not reached */
86             usage();
87         }
88     }
89     /* ***********************************************************
90      * 
91      *           Options processing completed
92      *
93      *                Main code beginning
94      * 
95      * ***********************************************************/
96     Signal(SIGINT, HandSIGINT);
97     if (nomask != 1) {
98         sigemptyset(&sigmask);
99         sigaddset(&sigmask, SIGINT);
100         if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1)   // block signals
101             die("Failing in singal masking");
102     }
103     if ((sigfd=signalfd(-1, &sigmask, SFD_NONBLOCK)) == -1) // take a signalfd
104         die("Failing in signalfd");
105     printf("Signalfd armed\n");
106     if (testwrite) {
107         if ( (n=write(sigfd, buffer, sizeof(buffer))) < 0)
108             perror("write on signal fd error");
109         else
110             printf("write successfully %d bytes\n", n);     
111     }
112     /* raise signal */
113     if (raise(SIGINT) != 0)
114         die("Failing in sending SIGINT");
115     t = time(NULL);
116     printf("Lanciato SIGINT, %s\n", ctime(&t));
117     sleep(2);
118     close(sigfd);
119     if (sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == -1)   // block signals
120         die("Failing in signal unmasking");
121     sleep(10);
122     printf("ERROR:SHOULD BE END IN THE SIGNAL HANDLER\n");
123     return 0;
124 }
125 /*
126  * routine to print usage info and exit
127  */
128 void usage(void) {
129     printf("Program testsignalfd : test signalfd on close \n");
130     printf("Usage:\n");
131     printf("  testsignalfd [-h] file mode \n");
132     printf("  -h           print this help\n");
133     
134     exit(1);
135 }
136 /*
137  * Print error message and exit routine
138  */
139 void die(char * mess) {
140     perror(mess);
141     exit(-1);
142 }
143 /*
144  * Signal Handler to manage termination
145  */
146 void HandSIGINT(int signo) {
147     time_t t;
148     debug("Terminated by %s\n", strsignal(signo));
149     t = time(NULL);
150     printf("Handler executed %s\n", ctime(&t));
151     exit(0);
152 }