summaryrefslogtreecommitdiff
path: root/test/timetest.c
blob: 18c054692750882b5f3b97fb69822e1ed384f73d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 *  Copyright (C) 2003,2007 Wolfgang Hommel
 *
 *  This file is part of the FakeTime Preload Library.
 *
 *  The FakeTime Preload Library 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.
 *
 *  The FakeTime Preload Library 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 the FakeTime Preload Library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sys/timeb.h>

#ifndef __APPLE__
#ifdef FAKE_STAT
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#else
#include <unistd.h>
#endif

#ifndef __APPLE__
#include <pthread.h>
#include <errno.h>
#include <signal.h>

#define VERBOSE 0

#define SIG SIGUSR1

static void
handler(int sig, siginfo_t *si, void *uc)
{
  /* Note: calling printf() from a signal handler is not
     strictly correct, since printf() is not async-signal-safe;
     see signal(7) */

  if ((si == NULL) || (si != uc))
  {
    printf("Caught signal %d\n", sig);
  }
}

void* pthread_test(void* args)
{
  pthread_mutex_t fakeMutex = PTHREAD_MUTEX_INITIALIZER;
  pthread_cond_t fakeCond = PTHREAD_COND_INITIALIZER;

  pthread_cond_t monotonic_cond;
  pthread_condattr_t attr;

  struct timespec timeToWait, now;
  int rt;

  args = args; // silence compiler warning about unused argument

  clock_gettime(CLOCK_REALTIME, &now);
  timeToWait.tv_sec = now.tv_sec+1;
  timeToWait.tv_nsec = now.tv_nsec;

  printf("pthread_cond_timedwait: CLOCK_REALTIME test\n");
  printf("(Intentionally sleeping 1 second...)\n");
  fflush(stdout);

  pthread_mutex_lock(&fakeMutex);
  rt = pthread_cond_timedwait(&fakeCond, &fakeMutex, &timeToWait);
  if (rt != ETIMEDOUT)
  {
    printf("pthread_cond_timedwait failed\n");
    pthread_mutex_unlock(&fakeMutex);
    exit(EXIT_FAILURE);
  }
  pthread_mutex_unlock(&fakeMutex);


  pthread_condattr_init(&attr);
  pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
  pthread_cond_init(&monotonic_cond, &attr);

  clock_gettime(CLOCK_MONOTONIC, &now);
  timeToWait.tv_sec = now.tv_sec+1;
  timeToWait.tv_nsec = now.tv_nsec;

  printf("pthread_cond_timedwait: CLOCK_MONOTONIC test\n");
  printf("(Intentionally sleeping 1 second...)\n");
  printf("(If this test hangs for more than a few seconds, please report\n your glibc version and system details as FORCE_MONOTONIC_FIX\n issue at https://github.com/wolfcw/libfaketime)\n");
  fflush(stdout);

  pthread_mutex_lock(&fakeMutex);
  rt = pthread_cond_timedwait(&monotonic_cond, &fakeMutex, &timeToWait);
  if (rt != ETIMEDOUT)
  {
    printf("pthread_cond_timedwait failed\n");
    pthread_mutex_unlock(&fakeMutex);
    exit(EXIT_FAILURE);
  }
  pthread_mutex_unlock(&fakeMutex);

  pthread_cond_destroy(&monotonic_cond);

  return NULL;
}

#endif

int main (int argc, char **argv)
{
    time_t now;
    struct timeb tb;
    struct timeval tv;
#ifndef __APPLE__
    struct timespec ts;
    timer_t timerid1 = 0, timerid2;
    struct sigevent sev;
    struct itimerspec its;
    sigset_t mask;
    struct sigaction sa;
#endif
#ifndef __APPLE__
#ifdef FAKE_STAT
    struct stat buf;
#endif
#endif

/* silence compiler warnings */
printf("%s", 0 == 1 ? argv[0] : "");

#ifndef __APPLE__
    pthread_t thread;
    void *ret;

    pthread_create(&thread, NULL, pthread_test, NULL);
    pthread_join(thread, &ret);

    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = handler;
    sigemptyset(&sa.sa_mask);
    if (sigaction(SIGUSR1, &sa, NULL) == -1)
    {
      perror("sigaction");
      exit(EXIT_FAILURE);
    }

    /* Block timer signal temporarily */
    printf("Blocking signal %d\n", SIGUSR1);
    sigemptyset(&mask);
    sigaddset(&mask, SIGUSR1);
    if (sigprocmask(SIG_SETMASK, &mask, NULL) == -1)
    {
      perror("sigaction");
      exit(EXIT_FAILURE);
    }

    /* Create the timer */
    sev.sigev_notify = SIGEV_SIGNAL;
    sev.sigev_signo = SIGUSR1;
    sev.sigev_value.sival_ptr = &timerid1;
    if (timer_create(CLOCK_REALTIME, &sev, &timerid1) == -1)
    {
      perror("timer_create");
      exit(EXIT_FAILURE);
    }

    /* Start timer1 */

    /* start timer ticking after one second */
    its.it_value.tv_sec = 1;
    its.it_value.tv_nsec = 0;
    /* fire in every 0.3 seconds */
    its.it_interval.tv_sec = 0;
    its.it_interval.tv_nsec = 300000000;

    if (timer_settime(timerid1, 0, &its, NULL) == -1)
    {
      perror("timer_settime");
      exit(EXIT_FAILURE);
    }

    sev.sigev_value.sival_ptr = &timerid2;
    if (timer_create(CLOCK_REALTIME, &sev, &timerid2) == -1)
    {
      perror("timer_create");
      exit(EXIT_FAILURE);
    }

    /* Start timer2 */

    clock_gettime(CLOCK_REALTIME, &its.it_value);
    /* start timer ticking after one second */
    its.it_value.tv_sec += 3;
    /* fire once */
    its.it_interval.tv_sec = 0;
    its.it_interval.tv_nsec = 0;

    if (timer_settime(timerid2, TIMER_ABSTIME, &its, NULL) == -1)
    {
      perror("timer_settime");
      exit(EXIT_FAILURE);
    }
#endif

    time(&now);
    printf("time()         : Current date and time: %s", ctime(&now));
    printf("time(NULL)     : Seconds since Epoch  : %u\n", (unsigned int)time(NULL));

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
    ftime(&tb);
#pragma GCC diagnostic pop
    printf("ftime()        : Current date and time: %s", ctime(&tb.time));

    printf("(Intentionally sleeping 2 seconds...)\n");
    fflush(stdout);
    if (argc < 3)
    {
        sleep(1);
        usleep(1000000);
    }

    gettimeofday(&tv, NULL);
    printf("gettimeofday() : Current date and time: %s", ctime(&tv.tv_sec));

#ifndef __APPLE__
    if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
    {
      perror("sigprocmask");
      exit(EXIT_FAILURE);
    }

    clock_gettime(CLOCK_REALTIME, &ts);
    printf("clock_gettime(): Current date and time: %s", ctime(&ts.tv_sec));

    int timer_getoverrun_timerid1 = timer_getoverrun(timerid1);
    if (timer_getoverrun_timerid1 != 3)
    {
#ifdef __GNU__
        printf("(Timer overruns are assumed to be fine on Hurd)\n");
#else
        printf("timer_getoverrun(timerid1) FAILED, must be 3 but got: %d\n", timer_getoverrun_timerid1);
#endif
    }

    timer_gettime(timerid1, &its);
    if (VERBOSE == 1)
    {
        printf("timer_gettime(timerid1, &its); its = {{%ld, %ld}, {%ld, %ld}}}\n",
                (long)its.it_interval.tv_sec, (long)its.it_interval.tv_nsec,
                (long)its.it_value.tv_sec, (long)its.it_value.tv_nsec);
    }

    int timer_getoverrun_timerid2 = timer_getoverrun(timerid2);
    if (timer_getoverrun_timerid2 != 0)
    {
        printf("timer_getoverrun(timerid2) FAILED, must be 0 but got: %d\n", timer_getoverrun_timerid2);
    }

    timer_gettime(timerid2, &its);
    if (VERBOSE == 1)
    {
        printf("timer_gettime(timerid2, &its); its = {{%ld, %ld}, {%ld, %ld}}}\n",
            (long)its.it_interval.tv_sec, (long)its.it_interval.tv_nsec,
            (long)its.it_value.tv_sec, (long)its.it_value.tv_nsec);
    }
#endif

#ifndef __APPLE__
#ifdef FAKE_STAT
    lstat(argv[0], &buf);
    printf("stat(): mod. time of file '%s': %s", argv[0], ctime(&buf.st_mtime));
#endif
#endif

    return 0;
}

/*
 * Editor modelines
 *
 * Local variables:
 * c-basic-offset: 2
 * tab-width: 2
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=2 tabstop=2 expandtab:
 * :indentSize=2:tabSize=2:noTabs=true:
 */

/* eof */