Home | History | Annotate | Line # | Download | only in kernel
t_time_arith.c revision 1.1
      1  1.1  riastrad /*	$NetBSD: t_time_arith.c,v 1.1 2024/12/22 23:25:15 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2024 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
      8  1.1  riastrad  * modification, are permitted provided that the following conditions
      9  1.1  riastrad  * are met:
     10  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     11  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     12  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  riastrad  */
     28  1.1  riastrad 
     29  1.1  riastrad #include <sys/cdefs.h>
     30  1.1  riastrad __RCSID("$NetBSD: t_time_arith.c,v 1.1 2024/12/22 23:25:15 riastradh Exp $");
     31  1.1  riastrad 
     32  1.1  riastrad #include <sys/timearith.h>
     33  1.1  riastrad 
     34  1.1  riastrad #include <atf-c.h>
     35  1.1  riastrad #include <errno.h>
     36  1.1  riastrad #include <limits.h>
     37  1.1  riastrad #include <setjmp.h>
     38  1.1  riastrad #include <signal.h>
     39  1.1  riastrad #include <stdbool.h>
     40  1.1  riastrad #include <stdint.h>
     41  1.1  riastrad #include <stdio.h>
     42  1.1  riastrad #include <stdlib.h>
     43  1.1  riastrad #include <string.h>
     44  1.1  riastrad #include <time.h>
     45  1.1  riastrad #include <unistd.h>
     46  1.1  riastrad #include <util.h>
     47  1.1  riastrad 
     48  1.1  riastrad #include "h_macros.h"
     49  1.1  riastrad 
     50  1.1  riastrad enum { HZ = 100 };
     51  1.1  riastrad 
     52  1.1  riastrad int hz = HZ;
     53  1.1  riastrad int tick = 1000000/HZ;
     54  1.1  riastrad 
     55  1.1  riastrad static sig_atomic_t jmp_en;
     56  1.1  riastrad static int jmp_sig;
     57  1.1  riastrad static jmp_buf jmp;
     58  1.1  riastrad 
     59  1.1  riastrad static void
     60  1.1  riastrad handle_signal(int signo)
     61  1.1  riastrad {
     62  1.1  riastrad 	const int errno_save = errno;
     63  1.1  riastrad 	char buf[32];
     64  1.1  riastrad 
     65  1.1  riastrad 	snprintf_ss(buf, sizeof(buf), "signal %d\n", signo);
     66  1.1  riastrad 	(void)write(STDERR_FILENO, buf, strlen(buf));
     67  1.1  riastrad 
     68  1.1  riastrad 	errno = errno_save;
     69  1.1  riastrad 
     70  1.1  riastrad 	if (jmp_en) {
     71  1.1  riastrad 		jmp_sig = signo;
     72  1.1  riastrad 		jmp_en = 0;
     73  1.1  riastrad 		longjmp(jmp, 1);
     74  1.1  riastrad 	} else {
     75  1.1  riastrad 		raise_default_signal(signo);
     76  1.1  riastrad 	}
     77  1.1  riastrad }
     78  1.1  riastrad 
     79  1.1  riastrad const struct itimer_transition {
     80  1.1  riastrad 	struct itimerspec	it_time;
     81  1.1  riastrad 	struct timespec		it_now;
     82  1.1  riastrad 	struct timespec		it_next;
     83  1.1  riastrad 	int			it_overruns;
     84  1.1  riastrad 	const char		*it_xfail;
     85  1.1  riastrad } itimer_transitions[] = {
     86  1.1  riastrad 	/*
     87  1.1  riastrad 	 * Fired more than one interval early -- treat clock as wound
     88  1.1  riastrad 	 * backwards, not counting overruns.  Advance by somewhere
     89  1.1  riastrad 	 * between one and two intervals from now.
     90  1.1  riastrad 	 */
     91  1.1  riastrad 	[0] = {{.it_value = {3,0}, .it_interval = {1,0}},
     92  1.1  riastrad 	       {0,1}, {2,0}, 0,
     93  1.1  riastrad 	       /* 1.709551617 */
     94  1.1  riastrad 	       "PR kern/58925: itimer(9) responds erratically to clock wound back"},
     95  1.1  riastrad 	[1] = {{.it_value = {3,0}, .it_interval = {1,0}},
     96  1.1  riastrad 	       {0,500000000}, {2,0}, 0,
     97  1.1  riastrad 	       /* 1.709551615 */
     98  1.1  riastrad 	       "PR kern/58925: itimer(9) responds erratically to clock wound back"},
     99  1.1  riastrad 	[2] = {{.it_value = {3,0}, .it_interval = {1,0}},
    100  1.1  riastrad 	       {0,999999999}, {2,0}, 0,
    101  1.1  riastrad 	       /* 2.709551613 */
    102  1.1  riastrad 	       "PR kern/58925: itimer(9) responds erratically to clock wound back"},
    103  1.1  riastrad 	[3] = {{.it_value = {3,0}, .it_interval = {1,0}},
    104  1.1  riastrad 	       {1,0}, {2,0}, 0,
    105  1.1  riastrad 	       /* 2.709551615 */
    106  1.1  riastrad 	       "PR kern/58925: itimer(9) responds erratically to clock wound back"},
    107  1.1  riastrad 	[4] = {{.it_value = {3,0}, .it_interval = {1,0}},
    108  1.1  riastrad 	       {1,1}, {3,0}, 0,
    109  1.1  riastrad 	       /* 2.709551617 */
    110  1.1  riastrad 	       "PR kern/58925: itimer(9) responds erratically to clock wound back"},
    111  1.1  riastrad 	[5] = {{.it_value = {3,0}, .it_interval = {1,0}},
    112  1.1  riastrad 	       {1,500000000}, {3,0}, 0,
    113  1.1  riastrad 	       /* 2.709551615 */
    114  1.1  riastrad 	       "PR kern/58925: itimer(9) responds erratically to clock wound back"},
    115  1.1  riastrad 	[6] = {{.it_value = {3,0}, .it_interval = {1,0}},
    116  1.1  riastrad 	       {1,999999999}, {3,0}, 0,
    117  1.1  riastrad 	       /* 3.709551613 */
    118  1.1  riastrad 	       "PR kern/58925: itimer(9) responds erratically to clock wound back"},
    119  1.1  riastrad 
    120  1.1  riastrad 	/*
    121  1.1  riastrad 	 * Fired exactly one interval early.  Treat this too as clock
    122  1.1  riastrad 	 * wound backwards.
    123  1.1  riastrad 	 */
    124  1.1  riastrad 	[7] = {{.it_value = {3,0}, .it_interval = {1,0}},
    125  1.1  riastrad 	       {2,0}, {3,0}, 0,
    126  1.1  riastrad 	       /* 3.709551615 */
    127  1.1  riastrad 	       "PR kern/58925: itimer(9) responds erratically to clock wound back"},
    128  1.1  riastrad 
    129  1.1  riastrad 	/*
    130  1.1  riastrad 	 * Fired less than one interval early -- callouts and real-time
    131  1.1  riastrad 	 * clock might not be perfectly synced, counted as zero
    132  1.1  riastrad 	 * overruns.  Advance by one interval from the scheduled time.
    133  1.1  riastrad 	 */
    134  1.1  riastrad 	[8] = {{.it_value = {3,0}, .it_interval = {1,0}},
    135  1.1  riastrad 	       {2,1}, {4,0}, 0,
    136  1.1  riastrad 	       /* 3.000000001 */
    137  1.1  riastrad 	       "PR kern/58925: itimer(9) responds erratically to clock wound back"},
    138  1.1  riastrad 	[9] = {{.it_value = {3,0}, .it_interval = {1,0}},
    139  1.1  riastrad 	       {2,500000000}, {4,0}, 0,
    140  1.1  riastrad 	       /* 3.999999999 */
    141  1.1  riastrad 	       "PR kern/58925: itimer(9) responds erratically to clock wound back"},
    142  1.1  riastrad 	[10] = {{.it_value = {3,0}, .it_interval = {1,0}},
    143  1.1  riastrad 		{2,999999999}, {4,0}, 0,
    144  1.1  riastrad 		/* 4.999999997 */
    145  1.1  riastrad 		"PR kern/58925: itimer(9) responds erratically to clock wound back"},
    146  1.1  riastrad 
    147  1.1  riastrad 	/*
    148  1.1  riastrad 	 * Fired exactly on time.  Advance by one interval.
    149  1.1  riastrad 	 */
    150  1.1  riastrad 	[11] = {{.it_value = {3,0}, .it_interval = {1,0}},
    151  1.1  riastrad 		{3,0}, {4,0}, 0, NULL},
    152  1.1  riastrad 
    153  1.1  riastrad 	/*
    154  1.1  riastrad 	 * Fired late by less than one interval -- callouts and
    155  1.1  riastrad 	 * real-time clock might not be prefectly synced, counted as
    156  1.1  riastrad 	 * zero overruns.  Advance by one interval from the scheduled
    157  1.1  riastrad 	 * time (even if it's very close to a full interval).
    158  1.1  riastrad 	 */
    159  1.1  riastrad 	[12] = {{.it_value = {3,0}, .it_interval = {1,0}},
    160  1.1  riastrad 		{3,1}, {4,0}, 0, NULL},
    161  1.1  riastrad 	[14] = {{.it_value = {3,0}, .it_interval = {1,0}},
    162  1.1  riastrad 		{3,500000000}, {4,0}, 0, NULL},
    163  1.1  riastrad 	[15] = {{.it_value = {3,0}, .it_interval = {1,0}},
    164  1.1  riastrad 		{3,999999999}, {4,0}, 0, NULL},
    165  1.1  riastrad 
    166  1.1  riastrad 	/*
    167  1.1  riastrad 	 * Fired late by exactly one interval -- treat it as overrun.
    168  1.1  riastrad 	 *
    169  1.1  riastrad 	 * XXX ...or treat it as not overrun?  wat
    170  1.1  riastrad 	 */
    171  1.1  riastrad 	[16] = {{.it_value = {3,0}, .it_interval = {1,0}},
    172  1.1  riastrad 		{4,0}, {4,0}, 0, NULL},
    173  1.1  riastrad 
    174  1.1  riastrad 	/*
    175  1.1  riastrad 	 * Fired late by more than one interval but less than two --
    176  1.1  riastrad 	 * overrun.
    177  1.1  riastrad 	 */
    178  1.1  riastrad 	[17] = {{.it_value = {3,0}, .it_interval = {1,0}},
    179  1.1  riastrad 		{4,1}, {5,0}, 1,
    180  1.1  riastrad 		/* 4.000000000, overruns=0 */
    181  1.1  riastrad 		"PR kern/58927: itimer(9): overrun accounting is broken"},
    182  1.1  riastrad 	[18] = {{.it_value = {3,0}, .it_interval = {1,0}},
    183  1.1  riastrad 		{4,500000000}, {5,0}, 1,
    184  1.1  riastrad 		/* 4.000000000, overruns=0 */
    185  1.1  riastrad 		"PR kern/58927: itimer(9): overrun accounting is broken"},
    186  1.1  riastrad 	[19] = {{.it_value = {3,0}, .it_interval = {1,0}},
    187  1.1  riastrad 		{4,999999999}, {5,0}, 1,
    188  1.1  riastrad 		/* 4.000000000, overruns=0 */
    189  1.1  riastrad 		"PR kern/58927: itimer(9): overrun accounting is broken"},
    190  1.1  riastrad 
    191  1.1  riastrad 	/*
    192  1.1  riastrad 	 * Fired late by exactly two intervals -- two overruns.
    193  1.1  riastrad 	 */
    194  1.1  riastrad 	[20] = {{.it_value = {3,0}, .it_interval = {1,0}},
    195  1.1  riastrad 		{5,0}, {6,0}, 2,
    196  1.1  riastrad 		/* 4.000000000, overruns=0 */
    197  1.1  riastrad 		"PR kern/58927: itimer(9): overrun accounting is broken"},
    198  1.1  riastrad 
    199  1.1  riastrad 	/*
    200  1.1  riastrad 	 * Fired late by more intervals plus slop, up to 32.
    201  1.1  riastrad 	 *
    202  1.1  riastrad 	 * XXX Define DELAYTIMER_MAX so we can write it in terms of
    203  1.1  riastrad 	 * that.
    204  1.1  riastrad 	 */
    205  1.1  riastrad 	[21] = {{.it_value = {3,0}, .it_interval = {1,0}},
    206  1.1  riastrad 		{13,123456789}, {14,0}, 10,
    207  1.1  riastrad 		/* 4.000000000, overruns=0 */
    208  1.1  riastrad 		"PR kern/58927: itimer(9): overrun accounting is broken"},
    209  1.1  riastrad 	[22] = {{.it_value = {3,0}, .it_interval = {1,0}},
    210  1.1  riastrad 		{34,999999999}, {32,0}, 32,
    211  1.1  riastrad 		/* 4.000000000, overruns=0 */
    212  1.1  riastrad 		"PR kern/58927: itimer(9): overrun accounting is broken"},
    213  1.1  riastrad 
    214  1.1  riastrad 	/*
    215  1.1  riastrad 	 * Fired late by roughly INT_MAX intervals.
    216  1.1  riastrad 	 */
    217  1.1  riastrad 	[23] = {{.it_value = {3,0}, .it_interval = {1,0}},
    218  1.1  riastrad 		{(time_t)3 + INT_MAX - 1, 0},
    219  1.1  riastrad 		{(time_t)3 + INT_MAX, 0},
    220  1.1  riastrad 		INT_MAX,
    221  1.1  riastrad 		/* 4.000000000, overruns=0 */
    222  1.1  riastrad 		"PR kern/58927: itimer(9): overrun accounting is broken"},
    223  1.1  riastrad 	[24] = {{.it_value = {3,0}, .it_interval = {1,0}},
    224  1.1  riastrad 		{(time_t)3 + INT_MAX, 0},
    225  1.1  riastrad 		{(time_t)3 + INT_MAX + 1, 0},
    226  1.1  riastrad 		INT_MAX,
    227  1.1  riastrad 		/* 4.000000000, overruns=0 */
    228  1.1  riastrad 		"PR kern/58926: itimer(9) integer overflow in overrun counting"},
    229  1.1  riastrad 	[25] = {{.it_value = {3,0}, .it_interval = {1,0}},
    230  1.1  riastrad 		{(time_t)3 + INT_MAX + 1, 0},
    231  1.1  riastrad 		{(time_t)3 + INT_MAX + 2, 0},
    232  1.1  riastrad 		INT_MAX,
    233  1.1  riastrad 		/* 4.000000000, overruns=0 */
    234  1.1  riastrad 		"PR kern/58926: itimer(9) integer overflow in overrun counting"},
    235  1.1  riastrad 
    236  1.1  riastrad 	/* (2^63 - 1) ns */
    237  1.1  riastrad 	[26] = {{.it_value = {3,0}, .it_interval = {9223372036,854775807}},
    238  1.1  riastrad 		{3,1}, {9223372039,854775807}, 0, NULL},
    239  1.1  riastrad 	/* 2^63 ns */
    240  1.1  riastrad 	[27] = {{.it_value = {3,0}, .it_interval = {9223372036,854775808}},
    241  1.1  riastrad 		{3,1}, {9223372039,854775808}, 0, NULL},
    242  1.1  riastrad 	/* (2^63 + 1) ns */
    243  1.1  riastrad 	[28] = {{.it_value = {3,0}, .it_interval = {9223372036,854775809}},
    244  1.1  riastrad 		{3,1}, {9223372039,854775809}, 0, NULL},
    245  1.1  riastrad 
    246  1.1  riastrad 	/*
    247  1.1  riastrad 	 * Overflows -- we should (XXX but currently don't) reject
    248  1.1  riastrad 	 * intervals of at least 2^64 nanoseconds up front, since this
    249  1.1  riastrad 	 * is more time than it is reasonable to wait (more than 584
    250  1.1  riastrad 	 * years).
    251  1.1  riastrad 	 */
    252  1.1  riastrad 
    253  1.1  riastrad 	/* (2^64 - 1) ns */
    254  1.1  riastrad 	[29] = {{.it_value = {3,0}, .it_interval = {18446744073,709551615}},
    255  1.1  riastrad 		{2,999999999}, {0,0}, 0,
    256  1.1  riastrad 		"PR kern/58922: itimer(9): arithmetic overflow"},
    257  1.1  riastrad 	/* 2^64 ns */
    258  1.1  riastrad 	[30] = {{.it_value = {3,0}, .it_interval = {18446744073,709551616}},
    259  1.1  riastrad 		{2,999999999}, {0,0}, 0,
    260  1.1  riastrad 		"PR kern/58922: itimer(9): arithmetic overflow"},
    261  1.1  riastrad 	/* (2^64 + 1) ns */
    262  1.1  riastrad 	[31] = {{.it_value = {3,0}, .it_interval = {18446744073,709551617}},
    263  1.1  riastrad 		{2,999999999}, {0,0}, 0,
    264  1.1  riastrad 		"PR kern/58922: itimer(9): arithmetic overflow"},
    265  1.1  riastrad 
    266  1.1  riastrad 	/* (2^63 - 1) us */
    267  1.1  riastrad 	[32] = {{.it_value = {3,0}, .it_interval = {9223372036854,775807}},
    268  1.1  riastrad 		{2,999999999}, {0,0}, 0,
    269  1.1  riastrad 		"PR kern/58922: itimer(9): arithmetic overflow"},
    270  1.1  riastrad 	/* 2^63 us */
    271  1.1  riastrad 	[33] = {{.it_value = {3,0}, .it_interval = {9223372036854,775808}},
    272  1.1  riastrad 		{2,999999999}, {0,0}, 0,
    273  1.1  riastrad 		"PR kern/58922: itimer(9): arithmetic overflow"},
    274  1.1  riastrad 	/* (2^63 + 1) us */
    275  1.1  riastrad 	[34] = {{.it_value = {3,0}, .it_interval = {9223372036854,775809}},
    276  1.1  riastrad 		{2,999999999}, {0,0}, 0,
    277  1.1  riastrad 		"PR kern/58922: itimer(9): arithmetic overflow"},
    278  1.1  riastrad 
    279  1.1  riastrad 	/* (2^64 - 1) us */
    280  1.1  riastrad 	[35] = {{.it_value = {3,0}, .it_interval = {18446744073709,551615}},
    281  1.1  riastrad 		{2,999999999}, {0,0}, 0,
    282  1.1  riastrad 		"PR kern/58922: itimer(9): arithmetic overflow"},
    283  1.1  riastrad 	/* 2^64 us */
    284  1.1  riastrad 	[36] = {{.it_value = {3,0}, .it_interval = {18446744073709,551616}},
    285  1.1  riastrad 		{2,999999999}, {0,0}, 0,
    286  1.1  riastrad 		"PR kern/58922: itimer(9): arithmetic overflow"},
    287  1.1  riastrad 	/* (2^64 + 1) us */
    288  1.1  riastrad 	[37] = {{.it_value = {3,0}, .it_interval = {18446744073709,551617}},
    289  1.1  riastrad 		{2,999999999}, {0,0}, 0,
    290  1.1  riastrad 		"PR kern/58922: itimer(9): arithmetic overflow"},
    291  1.1  riastrad 
    292  1.1  riastrad 	/* (2^63 - 1) ms */
    293  1.1  riastrad 	[38] = {{.it_value = {3,0}, .it_interval = {9223372036854775,807}},
    294  1.1  riastrad 		{2,999999999}, {0,0}, 0,
    295  1.1  riastrad 		"PR kern/58922: itimer(9): arithmetic overflow"},
    296  1.1  riastrad 	/* 2^63 ms */
    297  1.1  riastrad 	[39] = {{.it_value = {3,0}, .it_interval = {9223372036854775,808}},
    298  1.1  riastrad 		{2,999999999}, {0,0}, 0,
    299  1.1  riastrad 		"PR kern/58922: itimer(9): arithmetic overflow"},
    300  1.1  riastrad 	/* (2^63 + 1) ms */
    301  1.1  riastrad 	[40] = {{.it_value = {3,0}, .it_interval = {9223372036854775,809}},
    302  1.1  riastrad 		{2,999999999}, {0,0}, 0,
    303  1.1  riastrad 		"PR kern/58922: itimer(9): arithmetic overflow"},
    304  1.1  riastrad 
    305  1.1  riastrad 	/* (2^64 - 1) ms */
    306  1.1  riastrad 	[41] = {{.it_value = {3,0}, .it_interval = {18446744073709551,615}},
    307  1.1  riastrad 		{2,999999999}, {0,0}, 0,
    308  1.1  riastrad 		"PR kern/58922: itimer(9): arithmetic overflow"},
    309  1.1  riastrad 	/* 2^64 ms */
    310  1.1  riastrad 	[42] = {{.it_value = {3,0}, .it_interval = {18446744073709551,616}},
    311  1.1  riastrad 		{2,999999999}, {0,0}, 0,
    312  1.1  riastrad 		"PR kern/58922: itimer(9): arithmetic overflow"},
    313  1.1  riastrad 	/* (2^64 + 1) ms */
    314  1.1  riastrad 	[43] = {{.it_value = {3,0}, .it_interval = {18446744073709551,617}},
    315  1.1  riastrad 		{2,999999999}, {0,0}, 0,
    316  1.1  riastrad 		"PR kern/58922: itimer(9): arithmetic overflow"},
    317  1.1  riastrad 
    318  1.1  riastrad 	/* invalid intervals */
    319  1.1  riastrad 	[44] = {{.it_value = {3,0}, .it_interval = {-1,0}},
    320  1.1  riastrad 		{3,1}, {0,0}, 0, NULL},
    321  1.1  riastrad 	[45] = {{.it_value = {3,0}, .it_interval = {0,-1}},
    322  1.1  riastrad 		{3,1}, {0,0}, 0, NULL},
    323  1.1  riastrad 	[46] = {{.it_value = {3,0}, .it_interval = {0,1000000000}},
    324  1.1  riastrad 		{3,1}, {0,0}, 0, NULL},
    325  1.1  riastrad };
    326  1.1  riastrad 
    327  1.1  riastrad ATF_TC(itimer_transitions);
    328  1.1  riastrad ATF_TC_HEAD(itimer_transitions, tc)
    329  1.1  riastrad {
    330  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
    331  1.1  riastrad 	    "Tests interval timer transitions");
    332  1.1  riastrad }
    333  1.1  riastrad ATF_TC_BODY(itimer_transitions, tc)
    334  1.1  riastrad {
    335  1.1  riastrad 	volatile unsigned i;
    336  1.1  riastrad 
    337  1.1  riastrad 	REQUIRE_LIBC(signal(SIGFPE, handle_signal), SIG_ERR);
    338  1.1  riastrad 	REQUIRE_LIBC(signal(SIGABRT, handle_signal), SIG_ERR);
    339  1.1  riastrad 
    340  1.1  riastrad 	for (i = 0; i < __arraycount(itimer_transitions); i++) {
    341  1.1  riastrad 		struct itimer_transition it = itimer_transitions[i];
    342  1.1  riastrad 		struct timespec next;
    343  1.1  riastrad 		int overruns;
    344  1.1  riastrad 		volatile bool aborted = true;
    345  1.1  riastrad 		volatile bool expect_abort = false;
    346  1.1  riastrad 
    347  1.1  riastrad 		fprintf(stderr, "case %u\n", i);
    348  1.1  riastrad 
    349  1.1  riastrad 		if (it.it_xfail)
    350  1.1  riastrad 			atf_tc_expect_fail("%s", it.it_xfail);
    351  1.1  riastrad 
    352  1.1  riastrad 		if (itimespecfix(&it.it_time.it_value) != 0 ||
    353  1.1  riastrad 		    itimespecfix(&it.it_time.it_interval) != 0) {
    354  1.1  riastrad 			fprintf(stderr, "rejected by itimerspecfix\n");
    355  1.1  riastrad 			expect_abort = true;
    356  1.1  riastrad 		}
    357  1.1  riastrad 
    358  1.1  riastrad 		if (setjmp(jmp) == 0) {
    359  1.1  riastrad 			jmp_en = 1;
    360  1.1  riastrad 			itimer_transition(&it.it_time, &it.it_now,
    361  1.1  riastrad 			    &next, &overruns);
    362  1.1  riastrad 			jmp_en = 0;
    363  1.1  riastrad 			aborted = false;
    364  1.1  riastrad 		}
    365  1.1  riastrad 		ATF_CHECK(!jmp_en);
    366  1.1  riastrad 		jmp_en = 0;	/* paranoia */
    367  1.1  riastrad 		if (expect_abort) {
    368  1.1  riastrad 			fprintf(stderr, "expected abort\n");
    369  1.1  riastrad 			ATF_CHECK_MSG(aborted,
    370  1.1  riastrad 			    "[%u] missing invariant assertion", i);
    371  1.1  riastrad 			ATF_CHECK_MSG(jmp_sig == SIGABRT,
    372  1.1  riastrad 			    "[%u] missing invariant assertion", i);
    373  1.1  riastrad 		} else {
    374  1.1  riastrad 			ATF_CHECK_MSG(!aborted, "[%u] raised signal %d: %s", i,
    375  1.1  riastrad 			    jmp_sig, strsignal(jmp_sig));
    376  1.1  riastrad 		}
    377  1.1  riastrad 
    378  1.1  riastrad 		ATF_CHECK_MSG((next.tv_sec == it.it_next.tv_sec &&
    379  1.1  riastrad 			next.tv_nsec == it.it_next.tv_nsec),
    380  1.1  riastrad 		    "[%u] periodic intervals of %lld.%09d from %lld.%09d"
    381  1.1  riastrad 		    " last expired at %lld.%09d:"
    382  1.1  riastrad 		    " next expiry at %lld.%09d, expected %lld.%09d", i,
    383  1.1  riastrad 		    (long long)it.it_time.it_interval.tv_sec,
    384  1.1  riastrad 		    (int)it.it_time.it_interval.tv_nsec,
    385  1.1  riastrad 		    (long long)it.it_time.it_value.tv_sec,
    386  1.1  riastrad 		    (int)it.it_time.it_value.tv_nsec,
    387  1.1  riastrad 		    (long long)it.it_now.tv_sec, (int)it.it_now.tv_nsec,
    388  1.1  riastrad 		    (long long)next.tv_sec, (int)next.tv_nsec,
    389  1.1  riastrad 		    (long long)it.it_next.tv_sec, (int)it.it_next.tv_nsec);
    390  1.1  riastrad 		ATF_CHECK_EQ_MSG(overruns, it.it_overruns,
    391  1.1  riastrad 		    "[%u] periodic intervals of %lld.%09d from %lld.%09d"
    392  1.1  riastrad 		    " last expired at %lld.%09d:"
    393  1.1  riastrad 		    " overruns %d, expected %d", i,
    394  1.1  riastrad 		    (long long)it.it_time.it_interval.tv_sec,
    395  1.1  riastrad 		    (int)it.it_time.it_interval.tv_nsec,
    396  1.1  riastrad 		    (long long)it.it_time.it_value.tv_sec,
    397  1.1  riastrad 		    (int)it.it_time.it_value.tv_nsec,
    398  1.1  riastrad 		    (long long)it.it_now.tv_sec, (int)it.it_now.tv_nsec,
    399  1.1  riastrad 		    overruns, it.it_overruns);
    400  1.1  riastrad 
    401  1.1  riastrad 		if (it.it_xfail)
    402  1.1  riastrad 			atf_tc_expect_pass();
    403  1.1  riastrad 	}
    404  1.1  riastrad }
    405  1.1  riastrad 
    406  1.1  riastrad ATF_TP_ADD_TCS(tp)
    407  1.1  riastrad {
    408  1.1  riastrad 
    409  1.1  riastrad 	ATF_TP_ADD_TC(tp, itimer_transitions);
    410  1.1  riastrad 
    411  1.1  riastrad 	return atf_no_error();
    412  1.1  riastrad }
    413  1.1  riastrad 
    414