Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /* Leap second stress test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  *              by: John Stultz (john.stultz@linaro.org)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *              (C) Copyright IBM 2012
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *              (C) Copyright 2013, 2015 Linaro Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *              Licensed under the GPLv2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *  This test signals the kernel to insert a leap second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *  every day at midnight GMT. This allows for stessing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *  kernel's leap-second behavior, as well as how well applications
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *  handle the leap-second discontinuity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *  Usage: leap-a-day [-s] [-i <num>]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *  Options:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *	-s:	Each iteration, set the date to 10 seconds before midnight GMT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *		This speeds up the number of leapsecond transitions tested,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *		but because it calls settimeofday frequently, advancing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *		time by 24 hours every ~16 seconds, it may cause application
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *		disruption.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *	-i:	Number of iterations to run (default: infinite)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *  Other notes: Disabling NTP prior to running this is advised, as the two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *		 may conflict in their commands to the kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *  To build:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *	$ gcc leap-a-day.c -o leap-a-day -lrt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *   This program is free software: you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  *   it under the terms of the GNU General Public License as published by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  *   the Free Software Foundation, either version 2 of the License, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  *   (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  *   This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  *   GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #include <sys/timex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #include <sys/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #include "../kselftest.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define NSEC_PER_SEC 1000000000ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define CLOCK_TAI 11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) time_t next_leap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) int error_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) /* returns 1 if a <= b, 0 otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static inline int in_order(struct timespec a, struct timespec b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (a.tv_sec < b.tv_sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (a.tv_sec > b.tv_sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	if (a.tv_nsec > b.tv_nsec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) struct timespec timespec_add(struct timespec ts, unsigned long long ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	ts.tv_nsec += ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	while (ts.tv_nsec >= NSEC_PER_SEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		ts.tv_nsec -= NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		ts.tv_sec++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	return ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) char *time_state_str(int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	case TIME_OK:	return "TIME_OK";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	case TIME_INS:	return "TIME_INS";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	case TIME_DEL:	return "TIME_DEL";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	case TIME_OOP:	return "TIME_OOP";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	case TIME_WAIT:	return "TIME_WAIT";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	case TIME_BAD:	return "TIME_BAD";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	return "ERROR";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) /* clear NTP time_status & time_state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) int clear_time_state(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	struct timex tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	 * We have to call adjtime twice here, as kernels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	 * prior to 6b1859dba01c7 (included in 3.5 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	 * -stable), had an issue with the state machine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	 * and wouldn't clear the STA_INS/DEL flag directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	tx.modes = ADJ_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	tx.status = STA_PLL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	ret = adjtimex(&tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	/* Clear maxerror, as it can cause UNSYNC to be set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	tx.modes = ADJ_MAXERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	tx.maxerror = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	ret = adjtimex(&tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	/* Clear the status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	tx.modes = ADJ_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	tx.status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	ret = adjtimex(&tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* Make sure we cleanup on ctrl-c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) void handler(int unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	clear_time_state();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) void sigalarm(int signo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	struct timex tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	tx.modes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	ret = adjtimex(&tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if (tx.time.tv_sec < next_leap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		printf("Error: Early timer expiration! (Should be %ld)\n", next_leap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		error_found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		printf("adjtimex: %10ld sec + %6ld us (%i)\t%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 					tx.time.tv_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 					tx.time.tv_usec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 					tx.tai,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 					time_state_str(ret));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (ret != TIME_WAIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		printf("Error: Timer seeing incorrect NTP state? (Should be TIME_WAIT)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		error_found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		printf("adjtimex: %10ld sec + %6ld us (%i)\t%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 					tx.time.tv_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 					tx.time.tv_usec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 					tx.tai,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 					time_state_str(ret));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /* Test for known hrtimer failure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) void test_hrtimer_failure(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	struct timespec now, target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	clock_gettime(CLOCK_REALTIME, &now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	target = timespec_add(now, NSEC_PER_SEC/2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &target, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	clock_gettime(CLOCK_REALTIME, &now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (!in_order(target, now)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		printf("ERROR: hrtimer early expiration failure observed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		error_found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	timer_t tm1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	struct itimerspec its1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	struct sigevent se;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	struct sigaction act;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	int signum = SIGRTMAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	int settime = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	int tai_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	int insert = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	int iterations = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	int opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	/* Process arguments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	while ((opt = getopt(argc, argv, "sti:")) != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		switch (opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		case 'w':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			printf("Only setting leap-flag, not changing time. It could take up to a day for leap to trigger.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			settime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		case 'i':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			iterations = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		case 't':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			tai_time = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			printf("Usage: %s [-w] [-i <iterations>]\n", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			printf("	-w: Set flag and wait for leap second each iteration");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			printf("	    (default sets time to right before leapsecond)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			printf("	-i: Number of iterations (-1 = infinite, default is 10)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			printf("	-t: Print TAI time\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			exit(-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	/* Make sure TAI support is present if -t was used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if (tai_time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		struct timespec ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		if (clock_gettime(CLOCK_TAI, &ts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			printf("System doesn't support CLOCK_TAI\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			ksft_exit_fail();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	signal(SIGINT, handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	signal(SIGKILL, handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	/* Set up timer signal handler: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	sigfillset(&act.sa_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	act.sa_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	act.sa_handler = sigalarm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	sigaction(signum, &act, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (iterations < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		printf("This runs continuously. Press ctrl-c to stop\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		printf("Running for %i iterations. Press ctrl-c to stop\n", iterations);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		struct timespec ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		struct timex tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		time_t now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		/* Get the current time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		clock_gettime(CLOCK_REALTIME, &ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		/* Calculate the next possible leap second 23:59:60 GMT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		next_leap = ts.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		next_leap += 86400 - (next_leap % 86400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		if (settime) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			struct timeval tv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			tv.tv_sec = next_leap - 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			tv.tv_usec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			settimeofday(&tv, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			printf("Setting time to %s", ctime(&tv.tv_sec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		/* Reset NTP time state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		clear_time_state();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		/* Set the leap second insert flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		tx.modes = ADJ_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		if (insert)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			tx.status = STA_INS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			tx.status = STA_DEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		ret = adjtimex(&tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			printf("Error: Problem setting STA_INS/STA_DEL!: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 							time_state_str(ret));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 			return ksft_exit_fail();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		/* Validate STA_INS was set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		tx.modes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		ret = adjtimex(&tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		if (tx.status != STA_INS && tx.status != STA_DEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			printf("Error: STA_INS/STA_DEL not set!: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 							time_state_str(ret));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			return ksft_exit_fail();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		if (tai_time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			printf("Using TAI time,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 				" no inconsistencies should be seen!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		printf("Scheduling leap second for %s", ctime(&next_leap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		/* Set up timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		printf("Setting timer for %ld -  %s", next_leap, ctime(&next_leap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		memset(&se, 0, sizeof(se));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		se.sigev_notify = SIGEV_SIGNAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		se.sigev_signo = signum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		se.sigev_value.sival_int = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		if (timer_create(CLOCK_REALTIME, &se, &tm1) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			printf("Error: timer_create failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			return ksft_exit_fail();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		its1.it_value.tv_sec = next_leap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		its1.it_value.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		its1.it_interval.tv_sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		its1.it_interval.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		timer_settime(tm1, TIMER_ABSTIME, &its1, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		/* Wake up 3 seconds before leap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		ts.tv_sec = next_leap - 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		ts.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		while (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &ts, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			printf("Something woke us up, returning to sleep\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		/* Validate STA_INS is still set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		tx.modes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		ret = adjtimex(&tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		if (tx.status != STA_INS && tx.status != STA_DEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			printf("Something cleared STA_INS/STA_DEL, setting it again.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			tx.modes = ADJ_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			if (insert)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 				tx.status = STA_INS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 				tx.status = STA_DEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			ret = adjtimex(&tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		/* Check adjtimex output every half second */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		now = tx.time.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		while (now < next_leap + 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			char buf[26];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			struct timespec tai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			tx.modes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			ret = adjtimex(&tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			if (tai_time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 				clock_gettime(CLOCK_TAI, &tai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 				printf("%ld sec, %9ld ns\t%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 						tai.tv_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 						tai.tv_nsec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 						time_state_str(ret));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 				ctime_r(&tx.time.tv_sec, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 				buf[strlen(buf)-1] = 0; /*remove trailing\n */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 				printf("%s + %6ld us (%i)\t%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 						buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 						tx.time.tv_usec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 						tx.tai,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 						time_state_str(ret));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			now = tx.time.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			/* Sleep for another half second */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			ts.tv_sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			ts.tv_nsec = NSEC_PER_SEC / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		/* Switch to using other mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		insert = !insert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		/* Note if kernel has known hrtimer failure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		test_hrtimer_failure();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		printf("Leap complete\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		if (error_found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			printf("Errors observed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			clear_time_state();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			return ksft_exit_fail();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		if ((iterations != -1) && !(--iterations))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	clear_time_state();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	return ksft_exit_pass();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }