aboutsummaryrefslogtreecommitdiff
path: root/rt/tst-timer4.c
blob: 50654357fc004abfb94377d917abf5ca638ad448 (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/* Tests for POSIX timer implementation.
   Copyright (C) 2004-2021 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Jakub Jelinek <jakub@redhat.com>, 2004

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1 of the
   License, or (at your option) any later version.

   The GNU C 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; see the file COPYING.LIB.  If
   not, see <https://www.gnu.org/licenses/>.  */

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <stdint.h>
#if _POSIX_THREADS && defined SA_SIGINFO
# include <pthread.h>

# ifndef TEST_CLOCK
#  define TEST_CLOCK		CLOCK_REALTIME
# endif

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

timer_t timer_none, timer_sig1, timer_sig2, timer_thr1, timer_thr2;

int thr1_cnt, thr1_err;
union sigval thr1_sigval;
struct timespec thr1_ts;

static void
thr1 (union sigval sigval)
{
  pthread_mutex_lock (&lock);
  thr1_err = clock_gettime (TEST_CLOCK, &thr1_ts);
  if (thr1_cnt >= 5)
    {
      struct itimerspec it = { };
      thr1_err |= timer_settime (timer_thr1, 0, &it, NULL);
    }
  thr1_sigval = sigval;
  ++thr1_cnt;
  pthread_cond_signal (&cond);
  pthread_mutex_unlock (&lock);
}

int thr2_cnt, thr2_err;
union sigval thr2_sigval;
size_t thr2_guardsize;
struct timespec thr2_ts;

static void
thr2 (union sigval sigval)
{
  pthread_attr_t nattr;
  int err = 0;
  size_t guardsize = -1;
  int ret = pthread_getattr_np (pthread_self (), &nattr);
  if (ret)
    {
      errno = ret;
      printf ("*** pthread_getattr_np failed: %m\n");
      err = 1;
    }
  else
    {
      ret = pthread_attr_getguardsize (&nattr, &guardsize);
      if (ret)
        {
          errno = ret;
          printf ("*** pthread_attr_getguardsize failed: %m\n");
          err = 1;
        }
      if (pthread_attr_destroy (&nattr) != 0)
        {
          puts ("*** pthread_attr_destroy failed");
          err = 1;
        }
    }
  pthread_mutex_lock (&lock);
  thr2_err = clock_gettime (TEST_CLOCK, &thr2_ts) | err;
  if (thr2_cnt >= 5)
    {
      struct itimerspec it = { };
      thr2_err |= timer_settime (timer_thr2, 0, &it, NULL);
    }
  thr2_sigval = sigval;
  ++thr2_cnt;
  thr2_guardsize = guardsize;
  pthread_cond_signal (&cond);
  pthread_mutex_unlock (&lock);
}

volatile int sig1_cnt, sig1_err;
volatile union sigval sig1_sigval;
struct timespec sig1_ts;

static void
sig1_handler (int sig, siginfo_t *info, void *ctx)
{
  int err = 0;
  if (sig != SIGRTMIN) err |= 1 << 0;
  if (info->si_signo != SIGRTMIN) err |= 1 << 1;
  if (info->si_code != SI_TIMER) err |= 1 << 2;
  if (clock_gettime (TEST_CLOCK, &sig1_ts) != 0)
    err |= 1 << 3;
  if (sig1_cnt >= 5)
    {
      struct itimerspec it = { };
      if (timer_settime (timer_sig1, 0, &it, NULL))
	err |= 1 << 4;
    }
  sig1_err |= err;
  sig1_sigval = info->si_value;
  ++sig1_cnt;
}

volatile int sig2_cnt, sig2_err;
volatile union sigval sig2_sigval;
struct timespec sig2_ts;

static void
sig2_handler (int sig, siginfo_t *info, void *ctx)
{
  int err = 0;
  if (sig != SIGRTMIN + 1) err |= 1 << 0;
  if (info->si_signo != SIGRTMIN + 1) err |= 1 << 1;
  if (info->si_code != SI_TIMER) err |= 1 << 2;
  if (clock_gettime (TEST_CLOCK, &sig2_ts) != 0)
    err |= 1 << 3;
  if (sig2_cnt >= 5)
    {
      struct itimerspec it = { };
      if (timer_settime (timer_sig2, 0, &it, NULL))
	err |= 1 << 4;
    }
  sig2_err |= err;
  sig2_sigval = info->si_value;
  ++sig2_cnt;
}

/* Check if end is later or equal to start + nsec.  */
static int
check_ts (const char *name, const struct timespec *start,
	  const struct timespec *end, long msec)
{
  struct timespec ts = *start;

  ts.tv_sec += msec / 1000000;
  ts.tv_nsec += (msec % 1000000) * 1000;
  if (ts.tv_nsec >= 1000000000)
    {
      ++ts.tv_sec;
      ts.tv_nsec -= 1000000000;
    }
  if (end->tv_sec < ts.tv_sec
      || (end->tv_sec == ts.tv_sec && end->tv_nsec < ts.tv_nsec))
    {
      printf ("\
*** timer %s invoked too soon: %ld.%09jd instead of expected %ld.%09jd\n",
	      name, (long) end->tv_sec, (intmax_t) end->tv_nsec,
	      (long) ts.tv_sec, (intmax_t) ts.tv_nsec);
      return 1;
    }
  else
    return 0;
}

#define TEST_FUNCTION do_test ()
static int
do_test (void)
{
  int result = 0;

#ifdef TEST_CLOCK_MISSING
  const char *missing = TEST_CLOCK_MISSING (TEST_CLOCK);
  if (missing != NULL)
    {
      printf ("%s missing, skipping test\n", missing);
      return 0;
    }
#endif

  struct timespec ts;
  if (clock_gettime (TEST_CLOCK, &ts) != 0)
    {
      printf ("*** clock_gettime failed: %m\n");
      result = 1;
    }
  else
    printf ("clock_gettime returned timespec = { %ld, %jd }\n",
	    (long) ts.tv_sec, (intmax_t) ts.tv_nsec);

  if (clock_getres (TEST_CLOCK, &ts) != 0)
    {
      printf ("*** clock_getres failed: %m\n");
      result = 1;
    }
  else
    printf ("clock_getres returned timespec = { %ld, %jd }\n",
	    (long) ts.tv_sec, (intmax_t) ts.tv_nsec);

  struct sigevent ev;
  memset (&ev, 0x11, sizeof (ev));
  ev.sigev_notify = SIGEV_NONE;
  if (timer_create (TEST_CLOCK, &ev, &timer_none) != 0)
    {
      printf ("*** timer_create for timer_none failed: %m\n");
      return 1;
    }

  struct sigaction sa = { .sa_sigaction = sig1_handler,
			  .sa_flags = SA_SIGINFO };
  sigemptyset (&sa.sa_mask);
  sigaction (SIGRTMIN, &sa, NULL);
  sa.sa_sigaction = sig2_handler;
  sigaction (SIGRTMIN + 1, &sa, NULL);

  memset (&ev, 0x22, sizeof (ev));
  ev.sigev_notify = SIGEV_SIGNAL;
  ev.sigev_signo = SIGRTMIN;
  ev.sigev_value.sival_ptr = &ev;
  if (timer_create (TEST_CLOCK, &ev, &timer_sig1) != 0)
    {
      printf ("*** timer_create for timer_sig1 failed: %m\n");
      return 1;
    }

  memset (&ev, 0x33, sizeof (ev));
  ev.sigev_notify = SIGEV_SIGNAL;
  ev.sigev_signo = SIGRTMIN + 1;
  ev.sigev_value.sival_int = 163;
  if (timer_create (TEST_CLOCK, &ev, &timer_sig2) != 0)
    {
      printf ("*** timer_create for timer_sig2 failed: %m\n");
      return 1;
    }

  memset (&ev, 0x44, sizeof (ev));
  ev.sigev_notify = SIGEV_THREAD;
  ev.sigev_notify_function = thr1;
  ev.sigev_notify_attributes = NULL;
  ev.sigev_value.sival_ptr = &ev;
  if (timer_create (TEST_CLOCK, &ev, &timer_thr1) != 0)
    {
      printf ("*** timer_create for timer_thr1 failed: %m\n");
      return 1;
    }

  pthread_attr_t nattr;
  if (pthread_attr_init (&nattr)
      || pthread_attr_setguardsize (&nattr, 0))
    {
      puts ("*** pthread_attr_t setup failed");
      result = 1;
    }

  memset (&ev, 0x55, sizeof (ev));
  ev.sigev_notify = SIGEV_THREAD;
  ev.sigev_notify_function = thr2;
  ev.sigev_notify_attributes = &nattr;
  ev.sigev_value.sival_int = 111;
  if (timer_create (TEST_CLOCK, &ev, &timer_thr2) != 0)
    {
      printf ("*** timer_create for timer_thr2 failed: %m\n");
      return 1;
    }

  int ret = timer_getoverrun (timer_thr1);
  if (ret != 0)
    {
      if (ret == -1)
	printf ("*** timer_getoverrun failed: %m\n");
      else
	printf ("*** timer_getoverrun returned %d != 0\n", ret);
      result = 1;
    }

  struct itimerspec it;
  it.it_value.tv_sec = 0;
  it.it_value.tv_nsec = -26;
  it.it_interval.tv_sec = 0;
  it.it_interval.tv_nsec = 0;
  if (timer_settime (timer_sig1, 0, &it, NULL) == 0)
    {
      puts ("*** timer_settime with negative tv_nsec unexpectedly succeeded");
      result = 1;
    }
  else if (errno != EINVAL)
    {
      printf ("*** timer_settime with negative tv_nsec did not fail with "
	      "EINVAL: %m\n");
      result = 1;
    }

  it.it_value.tv_nsec = 100000;
  it.it_interval.tv_nsec = 1000000000;
  if (timer_settime (timer_sig2, 0, &it, NULL) == 0)
    {
      puts ("\
*** timer_settime with tv_nsec 1000000000 unexpectedly succeeded");
      result = 1;
    }
  else if (errno != EINVAL)
    {
      printf ("*** timer_settime with tv_nsec 1000000000 did not fail with "
	      "EINVAL: %m\n");
      result = 1;
    }

#if 0
  it.it_value.tv_nsec = 0;
  it.it_interval.tv_nsec = -26;
  if (timer_settime (timer_thr1, 0, &it, NULL) != 0)
    {
      printf ("\
!!! timer_settime with it_value 0 it_interval invalid failed: %m\n");
      /* FIXME: is this mandated by POSIX?
      result = 1; */
    }

  it.it_interval.tv_nsec = 3000000000;
  if (timer_settime (timer_thr2, 0, &it, NULL) != 0)
    {
      printf ("\
!!! timer_settime with it_value 0 it_interval invalid failed: %m\n");
      /* FIXME: is this mandated by POSIX?
      result = 1; */
    }
#endif

  struct timespec startts;
  if (clock_gettime (TEST_CLOCK, &startts) != 0)
    {
      printf ("*** clock_gettime failed: %m\n");
      result = 1;
    }

  it.it_value.tv_nsec = 100000000;
  it.it_interval.tv_nsec = 0;
  if (timer_settime (timer_none, 0, &it, NULL) != 0)
    {
      printf ("*** timer_settime timer_none failed: %m\n");
      result = 1;
    }

  it.it_value.tv_nsec = 200000000;
  if (timer_settime (timer_thr1, 0, &it, NULL) != 0)
    {
      printf ("*** timer_settime timer_thr1 failed: %m\n");
      result = 1;
    }

  it.it_value.tv_nsec = 300000000;
  if (timer_settime (timer_thr2, 0, &it, NULL) != 0)
    {
      printf ("*** timer_settime timer_thr2 failed: %m\n");
      result = 1;
    }

  it.it_value.tv_nsec = 400000000;
  if (timer_settime (timer_sig1, 0, &it, NULL) != 0)
    {
      printf ("*** timer_settime timer_sig1 failed: %m\n");
      result = 1;
    }

  it.it_value.tv_nsec = 500000000;
  if (TEMP_FAILURE_RETRY (timer_settime (timer_sig2, 0, &it, NULL)) != 0)
    {
      printf ("*** timer_settime timer_sig2 failed: %m\n");
      result = 1;
    }

  pthread_mutex_lock (&lock);
  while (thr1_cnt == 0 || thr2_cnt == 0)
    pthread_cond_wait (&cond, &lock);
  pthread_mutex_unlock (&lock);

  while (sig1_cnt == 0 || sig2_cnt == 0)
    {
      ts.tv_sec = 0;
      ts.tv_nsec = 100000000;
      nanosleep (&ts, NULL);
    }

  pthread_mutex_lock (&lock);

  if (thr1_cnt != 1)
    {
      printf ("*** thr1 not called exactly once, but %d times\n", thr1_cnt);
      result = 1;
    }
  else if (thr1_err)
    {
      puts ("*** an error occurred in thr1");
      result = 1;
    }
  else if (thr1_sigval.sival_ptr != &ev)
    {
      printf ("*** thr1_sigval.sival_ptr %p != %p\n",
	      thr1_sigval.sival_ptr, &ev);
      result = 1;
    }
  else if (check_ts ("thr1", &startts, &thr1_ts, 200000))
    result = 1;

  if (thr2_cnt != 1)
    {
      printf ("*** thr2 not called exactly once, but %d times\n", thr2_cnt);
      result = 1;
    }
  else if (thr2_err)
    {
      puts ("*** an error occurred in thr2");
      result = 1;
    }
  else if (thr2_sigval.sival_int != 111)
    {
      printf ("*** thr2_sigval.sival_ptr %d != 111\n", thr2_sigval.sival_int);
      result = 1;
    }
  else if (check_ts ("thr2", &startts, &thr2_ts, 300000))
    result = 1;
  else if (thr2_guardsize != 0)
    {
      printf ("*** thr2 guardsize %zd != 0\n", thr2_guardsize);
      result = 1;
    }

  pthread_mutex_unlock (&lock);

  if (sig1_cnt != 1)
    {
      printf ("*** sig1 not called exactly once, but %d times\n", sig1_cnt);
      result = 1;
    }
  else if (sig1_err)
    {
      printf ("*** errors occurred in sig1 handler %x\n", sig1_err);
      result = 1;
    }
  else if (sig1_sigval.sival_ptr != &ev)
    {
      printf ("*** sig1_sigval.sival_ptr %p != %p\n",
	      sig1_sigval.sival_ptr, &ev);
      result = 1;
    }
  else if (check_ts ("sig1", &startts, &sig1_ts, 400000))
    result = 1;

  if (sig2_cnt != 1)
    {
      printf ("*** sig2 not called exactly once, but %d times\n", sig2_cnt);
      result = 1;
    }
  else if (sig2_err)
    {
      printf ("*** errors occurred in sig2 handler %x\n", sig2_err);
      result = 1;
    }
  else if (sig2_sigval.sival_int != 163)
    {
      printf ("*** sig2_sigval.sival_ptr %d != 163\n", sig2_sigval.sival_int);
      result = 1;
    }
  else if (check_ts ("sig2", &startts, &sig2_ts, 500000))
    result = 1;

  if (timer_gettime (timer_none, &it) != 0)
    {
      printf ("*** timer_gettime timer_none failed: %m\n");
      result = 1;
    }
  else if (it.it_value.tv_sec || it.it_value.tv_nsec
	   || it.it_interval.tv_sec || it.it_interval.tv_nsec)
    {
      printf ("\
*** timer_gettime timer_none returned { %ld.%09jd, %ld.%09jd }\n",
	      (long) it.it_value.tv_sec, (intmax_t) it.it_value.tv_nsec,
	      (long) it.it_interval.tv_sec, (intmax_t) it.it_interval.tv_nsec);
      result = 1;
    }

  if (clock_gettime (TEST_CLOCK, &startts) != 0)
    {
      printf ("*** clock_gettime failed: %m\n");
      result = 1;
    }

  it.it_value.tv_sec = 1;
  it.it_value.tv_nsec = 0;
  it.it_interval.tv_sec = 0;
  it.it_interval.tv_nsec = 100000000;
  if (timer_settime (timer_none, 0, &it, NULL) != 0)
    {
      printf ("*** timer_settime timer_none failed: %m\n");
      result = 1;
    }

  it.it_value.tv_nsec = 100000000;
  it.it_interval.tv_nsec = 200000000;
  if (timer_settime (timer_thr1, 0, &it, NULL) != 0)
    {
      printf ("*** timer_settime timer_thr1 failed: %m\n");
      result = 1;
    }

  it.it_value.tv_nsec = 200000000;
  it.it_interval.tv_nsec = 300000000;
  if (timer_settime (timer_thr2, 0, &it, NULL) != 0)
    {
      printf ("*** timer_settime timer_thr2 failed: %m\n");
      result = 1;
    }

  it.it_value.tv_nsec = 300000000;
  it.it_interval.tv_nsec = 400000000;
  if (timer_settime (timer_sig1, 0, &it, NULL) != 0)
    {
      printf ("*** timer_settime timer_sig1 failed: %m\n");
      result = 1;
    }

  it.it_value.tv_nsec = 400000000;
  it.it_interval.tv_nsec = 500000000;
  if (TEMP_FAILURE_RETRY (timer_settime (timer_sig2, 0, &it, NULL)) != 0)
    {
      printf ("*** timer_settime timer_sig2 failed: %m\n");
      result = 1;
    }

  pthread_mutex_lock (&lock);
  while (thr1_cnt < 6 || thr2_cnt < 6)
    pthread_cond_wait (&cond, &lock);
  pthread_mutex_unlock (&lock);

  while (sig1_cnt < 6 || sig2_cnt < 6)
    {
      ts.tv_sec = 0;
      ts.tv_nsec = 100000000;
      nanosleep (&ts, NULL);
    }

  pthread_mutex_lock (&lock);

  if (thr1_err)
    {
      puts ("*** an error occurred in thr1");
      result = 1;
    }
  else if (check_ts ("thr1", &startts, &thr1_ts, 1100000 + 4 * 200000))
    result = 1;

  if (thr2_err)
    {
      puts ("*** an error occurred in thr2");
      result = 1;
    }
  else if (check_ts ("thr2", &startts, &thr2_ts, 1200000 + 4 * 300000))
    result = 1;
  else if (thr2_guardsize != 0)
    {
      printf ("*** thr2 guardsize %zd != 0\n", thr2_guardsize);
      result = 1;
    }

  pthread_mutex_unlock (&lock);

  if (sig1_err)
    {
      printf ("*** errors occurred in sig1 handler %x\n", sig1_err);
      result = 1;
    }
  else if (check_ts ("sig1", &startts, &sig1_ts, 1300000 + 4 * 400000))
    result = 1;

  if (sig2_err)
    {
      printf ("*** errors occurred in sig2 handler %x\n", sig2_err);
      result = 1;
    }
  else if (check_ts ("sig2", &startts, &sig2_ts, 1400000 + 4 * 500000))
    result = 1;

  if (timer_gettime (timer_none, &it) != 0)
    {
      printf ("*** timer_gettime timer_none failed: %m\n");
      result = 1;
    }
  else if (it.it_interval.tv_sec || it.it_interval.tv_nsec != 100000000)
    {
      printf ("\
!!! second timer_gettime timer_none returned it_interval %ld.%09jd\n",
	      (long) it.it_interval.tv_sec, (intmax_t) it.it_interval.tv_nsec);
      /* FIXME: For now disabled.
      result = 1; */
    }

  if (timer_delete (timer_none) != 0)
    {
      printf ("*** timer_delete for timer_none failed: %m\n");
      result = 1;
    }

  if (timer_delete (timer_sig1) != 0)
    {
      printf ("*** timer_delete for timer_sig1 failed: %m\n");
      result = 1;
    }

  if (timer_delete (timer_sig2) != 0)
    {
      printf ("*** timer_delete for timer_sig2 failed: %m\n");
      result = 1;
    }

  if (timer_delete (timer_thr1) != 0)
    {
      printf ("*** timer_delete for timer_thr1 failed: %m\n");
      result = 1;
    }

  if (timer_delete (timer_thr2) != 0)
    {
      printf ("*** timer_delete for timer_thr2 failed: %m\n");
      result = 1;
    }
  return result;
}

#elif defined TEST_CLOCK_MISSING
/* This just ensures that any functions called in TEST_CLOCK_MISSING
   are not diagnosed as unused.  */
# define TEST_FUNCTION (TEST_CLOCK_MISSING (TEST_CLOCK), 0)
#else
# define TEST_FUNCTION 0
#endif

#include "../test-skeleton.c"