Created
February 16, 2015 14:13
-
-
Save lethean/446cea944b7441228298 to your computer and use it in GitHub Desktop.
Detect time change with timerfd_settime() in Linux.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/timerfd.h> | |
#include <sys/timerfd.h> | |
#include <time.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <stdint.h> /* Definition of uint64_t */ | |
#ifndef TFD_TIMER_CANCEL_ON_SET | |
# define TFD_TIMER_CANCEL_ON_SET (1 << 1) | |
#endif | |
#define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1) | |
#define handle_error(msg) \ | |
do { perror(msg); exit(EXIT_FAILURE); } while (0) | |
int | |
main(int argc, char *argv[]) | |
{ | |
/* We only care for the cancellation event, hence we set the | |
* timeout to the latest possible value. */ | |
struct itimerspec new_value = { | |
.it_value.tv_sec = TIME_T_MAX, | |
}; | |
uint64_t exp; | |
int fd; | |
int s; | |
fd = timerfd_create(CLOCK_REALTIME, 0); | |
if (fd == -1) | |
handle_error("timerfd_create"); | |
if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &new_value, NULL) == -1) | |
handle_error("timerfd_settime"); | |
while (1) { | |
s = read(fd, &exp, sizeof(uint64_t)); | |
printf("time has been changed\n"); | |
} | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment