+/* test_timerfdfork.c
+ *
+ * Copyright (C) 2011 Simone Piccardi
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/****************************************************************
+ *
+ * Program test_timerfdfork.c:
+ * Program to test timerfd behaviour across fork's
+ *
+ * Author: Simone Piccardi
+ * Apr. 2011
+ *
+ * Usage: testtimerfdfork -h give all info's
+ *
+ ****************************************************************/
+/*
+ * Include needed headers
+ */
+#define _GNU_SOURCE
+#include <errno.h> /* error definitions and routines */
+#include <stdlib.h> /* C standard library */
+#include <unistd.h> /* unix standard library */
+#include <stdio.h> /* standard I/O library */
+#include <string.h> /* C strings library */
+#include <sys/timerfd.h> /* timerfd */
+// #include <time.h>
+
+/* Help printing routine */
+void usage(void);
+
+int main(int argc, char *argv[])
+{
+/*
+ * Variables definition
+ */
+ int i, fd;
+ struct itimerspec expiring;
+ /*
+ * Input section: decode command line parameters
+ * Use getopt function
+ */
+ opterr = 0; /* don't want writing to stderr */
+ while ( (i = getopt(argc, argv, "h")) != -1) {
+ switch (i) {
+ /*
+ * Handling options
+ */
+ case 'h': /* help option */
+ printf("Wrong -h option use\n");
+ usage();
+ return -1;
+ break;
+ case '?': /* unrecognized options */
+ printf("Unrecognized options -%c\n",optopt);
+ usage();
+ default: /* should not reached */
+ usage();
+ }
+ }
+ /* ***********************************************************
+ *
+ * Options processing completed
+ *
+ * Main code beginning
+ *
+ * ***********************************************************/
+ /* There must be 0 remaing arguments */
+ if ( (argc-optind) != 0 ) {
+ printf("From %d arguments, removed %d options\n", argc, optind);
+ usage();
+ }
+
+ fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
+ expiring.it_interval.tv_sec=1;
+ expiring.it_interval.tv_nsec=0;
+ expiring.it_value.tv_sec=5;
+ expiring.it_value.tv_nsec=0;
+ if (timerfd_settime(fd, 0, expiring, NULL)) {
+ perror("Cannot set timer");
+ }
+ return 0;
+}
+/*
+ * routine to print usage info and exit
+ */
+void usage(void) {
+ printf("Program testtimerfdfork : test timerfd across fork \n");
+ printf("Usage:\n");
+ printf(" testtimerfdfork [-h] \n");
+ printf(" -h print this help\n");
+
+ exit(1);
+}
+