34ceaedd1b317c0d29bf31397a2a7e5718ad54f0
[gapil.git] / sources / test_timerfdfork.c
1 /* test_timerfdfork.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_timerfdfork.c: 
22  * Program to test timerfd behaviour across fork's
23  *
24  * Author: Simone Piccardi
25  * Apr. 2011
26  *
27  * Usage: testtimerfdfork -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 <sys/timerfd.h> /* timerfd */
40 #include <sys/epoll.h>   /* Linux epoll interface */
41 // #include <time.h>
42
43 #include "macros.h"
44 #include "Gapil.h"
45
46 /* Help printing routine */
47 void usage(void);
48 void die(char *);
49
50 #define MAX_EPOLL_EV 10
51
52 int main(int argc, char *argv[])
53 {
54 /* 
55  * Variables definition  
56  */
57     int i, n, nread, fd, epfd;
58     pid_t pid;
59     struct epoll_event epev, events[MAX_EPOLL_EV];
60     struct itimerspec expiring;
61     uint64_t expirated;
62     /*
63      * Input section: decode command line parameters 
64      * Use getopt function
65      */
66     opterr = 0;  /* don't want writing to stderr */
67     while ( (i = getopt(argc, argv, "h")) != -1) {
68         switch (i) {
69         /* 
70          * Handling options 
71          */ 
72         case 'h':   /* help option */
73             printf("Wrong -h option use\n");
74             usage();
75             return -1;
76             break;
77         case '?':   /* unrecognized options */
78             printf("Unrecognized options -%c\n",optopt);
79             usage();
80         default:    /* should not reached */
81             usage();
82         }
83     }
84     /* ***********************************************************
85      * 
86      *           Options processing completed
87      *
88      *                Main code beginning
89      * 
90      * ***********************************************************/
91     /* There must be 0 remaing arguments */
92     if ( (argc-optind) != 0 )  {
93         printf("From %d arguments, removed %d options\n", argc, optind);
94         usage();
95     }
96     /* timerfd setup */
97     fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
98     expiring.it_interval.tv_sec=1;
99     expiring.it_interval.tv_nsec=0;
100     expiring.it_value.tv_sec=5;
101     expiring.it_value.tv_nsec=0;
102     if (timerfd_settime(fd, 0, &expiring, NULL)) {
103         die("Cannot set timer");
104     }
105     pid = fork();
106     /* epoll setup */
107     if ((epfd=epoll_create(5)) < 0)
108         die("Failing on epoll_create");
109     epev.data.fd = fd;
110     epev.events = EPOLLIN;
111     if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &epev))
112         die("Failing in epoll_ctl"); 
113     /* main loop */
114     //while (1) {
115         if ((n=epoll_wait(epfd, events, MAX_EPOLL_EV, -1)) < 0) 
116             die("error on epoll_wait");
117         debug("Got %i events\n", n);
118         /* loop on epoll events */
119         for (i=0; i<n; i++) {
120             if (events[i].data.fd == fd) {             // if timer expired
121                 printf("Timer expired:\n");
122                 while(nread=read(fd, &expirated, sizeof(expirated))) {
123                     if (nread < 0) {
124                         if (errno != EAGAIN) 
125                             die("signalfd read error");
126                         else 
127                             break;
128                     }
129                     if (nread != sizeof(expirated)) {
130                         printf("Error on timer data read, '\n");
131                         continue;
132                     }
133                     printf("Expired %llu times\n", expirated);
134                     if (pid == 0) {
135                         printf("in child\n");
136                     } else {
137                         printf("in father\n");
138                     }
139                 }
140             }
141         }
142     // }
143     return 0;
144 }
145 /*
146  * routine to print usage info and exit
147  */
148 void usage(void) {
149     printf("Program testtimerfdfork : test timerfd across fork  \n");
150     printf("Usage:\n");
151     printf("  testtimerfdfork [-h]  \n");
152     printf("  -h           print this help\n");
153     
154     exit(1);
155 }
156 /*
157  * Print error message and exit routine
158  */
159 void die(char * mess) {
160     perror(mess);
161     exit(-1);
162 }
163