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_timerfdfork.c:
22 * Program to test timerfd behaviour across fork's
24 * Author: Simone Piccardi
27 * Usage: testtimerfdfork -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 <sys/timerfd.h> /* timerfd */
40 #include <sys/epoll.h> /* Linux epoll interface */
46 /* Help printing routine */
50 #define MAX_EPOLL_EV 10
52 int main(int argc, char *argv[])
55 * Variables definition
57 int i, n, nread, fd, epfd;
59 struct epoll_event epev, events[MAX_EPOLL_EV];
60 struct itimerspec expiring;
63 * Input section: decode command line parameters
66 opterr = 0; /* don't want writing to stderr */
67 while ( (i = getopt(argc, argv, "h")) != -1) {
72 case 'h': /* help option */
73 printf("Wrong -h option use\n");
77 case '?': /* unrecognized options */
78 printf("Unrecognized options -%c\n",optopt);
80 default: /* should not reached */
84 /* ***********************************************************
86 * Options processing completed
90 * ***********************************************************/
91 /* There must be 0 remaing arguments */
92 if ( (argc-optind) != 0 ) {
93 printf("From %d arguments, removed %d options\n", argc, optind);
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");
107 if ((epfd=epoll_create(5)) < 0)
108 die("Failing on epoll_create");
110 epev.events = EPOLLIN;
111 if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &epev))
112 die("Failing in epoll_ctl");
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))) {
125 die("signalfd read error");
129 if (nread != sizeof(expirated)) {
130 printf("Error on timer data read, '\n");
133 printf("Expired %llu times\n", expirated);
135 printf("in child\n");
137 printf("in father\n");
146 * routine to print usage info and exit
149 printf("Program testtimerfdfork : test timerfd across fork \n");
151 printf(" testtimerfdfork [-h] \n");
152 printf(" -h print this help\n");
157 * Print error message and exit routine
159 void die(char * mess) {