3 * Copyright (C) 2011 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 test_signalfd.c:
22 * Program to test signalfd behaviour on fd close
24 * Author: Simone Piccardi
27 * Usage: test_signalfd -h give all info's
29 ****************************************************************/
31 * Include needed headers
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 */
46 /* Subroutines declaration */
49 void HandSIGINT(int signo);
51 int main(int argc, char *argv[])
54 * Variables definition
56 int i, n, nomask=0, testwrite=0, sigfd;
58 struct signalfd_siginfo siginf;
60 char buffer[] = "test write";
62 * Input section: decode command line parameters
65 opterr = 0; /* don't want writing to stderr */
66 while ( (i = getopt(argc, argv, "hnw")) != -1) {
71 case 'h': /* help option */
72 printf("Wrong -h option use\n");
76 case 'n': /* no mask option */
79 case 'w': /* testwrite option */
82 case '?': /* unrecognized options */
83 printf("Unrecognized options -%c\n",optopt);
85 default: /* should not reached */
89 /* ***********************************************************
91 * Options processing completed
95 * ***********************************************************/
96 Signal(SIGINT, HandSIGINT);
98 sigemptyset(&sigmask);
99 sigaddset(&sigmask, SIGINT);
100 if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) // block signals
101 die("Failing in singal masking");
103 if ((sigfd=signalfd(-1, &sigmask, SFD_NONBLOCK)) == -1) // take a signalfd
104 die("Failing in signalfd");
105 printf("Signalfd armed\n");
107 if ( (n=write(sigfd, buffer, sizeof(buffer))) < 0)
108 perror("write on signal fd error");
110 printf("write successfully %d bytes\n", n);
113 if (raise(SIGINT) != 0)
114 die("Failing in sending SIGINT");
116 printf("Lanciato SIGINT, %s\n", ctime(&t));
119 if (sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == -1) // block signals
120 die("Failing in signal unmasking");
122 printf("ERROR:SHOULD BE END IN THE SIGNAL HANDLER\n");
126 * routine to print usage info and exit
129 printf("Program testsignalfd : test signalfd on close \n");
131 printf(" testsignalfd [-h] file mode \n");
132 printf(" -h print this help\n");
137 * Print error message and exit routine
139 void die(char * mess) {
144 * Signal Handler to manage termination
146 void HandSIGINT(int signo) {
148 debug("Terminated by %s\n", strsignal(signo));
150 printf("Handler executed %s\n", ctime(&t));