1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) Wipro Technologies Ltd, 2003. All Rights Reserved. 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * Author: Aniruddha Marathe <aniruddha.marathe@wipro.com> 6f08c3bdfSopenharmony_ci * 7f08c3bdfSopenharmony_ci * Ported to new library: 8f08c3bdfSopenharmony_ci * 07/2019 Christian Amann <camann@suse.com> 9f08c3bdfSopenharmony_ci */ 10f08c3bdfSopenharmony_ci/* 11f08c3bdfSopenharmony_ci * This tests the timer_settime(2) syscall under various conditions: 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * 1) General initialization: No old_value, no flags 14f08c3bdfSopenharmony_ci * 2) Setting a pointer to a itimerspec struct as old_set parameter 15f08c3bdfSopenharmony_ci * 3) Using a periodic timer 16f08c3bdfSopenharmony_ci * 4) Using absolute time 17f08c3bdfSopenharmony_ci * 18f08c3bdfSopenharmony_ci * All of these tests are supposed to be successful. 19f08c3bdfSopenharmony_ci * 20f08c3bdfSopenharmony_ci * This is also regression test for commit: 21f08c3bdfSopenharmony_ci * f18ddc13af98 ("alarmtimer: Use EOPNOTSUPP instead of ENOTSUPP") 22f08c3bdfSopenharmony_ci * e86fea764991 ("alarmtimer: Return relative times in timer_gettime") 23f08c3bdfSopenharmony_ci */ 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_ci#include <stdlib.h> 26f08c3bdfSopenharmony_ci#include <errno.h> 27f08c3bdfSopenharmony_ci#include <time.h> 28f08c3bdfSopenharmony_ci#include <signal.h> 29f08c3bdfSopenharmony_ci#include "time64_variants.h" 30f08c3bdfSopenharmony_ci#include "tst_timer.h" 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_cistatic struct tst_ts timenow; 33f08c3bdfSopenharmony_cistatic struct tst_its new_set, old_set; 34f08c3bdfSopenharmony_cistatic kernel_timer_t timer; 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_cistatic struct testcase { 37f08c3bdfSopenharmony_ci struct tst_its *old_ptr; 38f08c3bdfSopenharmony_ci int it_value_tv_usec; 39f08c3bdfSopenharmony_ci int it_interval_tv_usec; 40f08c3bdfSopenharmony_ci int flag; 41f08c3bdfSopenharmony_ci char *description; 42f08c3bdfSopenharmony_ci} tcases[] = { 43f08c3bdfSopenharmony_ci {NULL, 50000, 0, 0, "general initialization"}, 44f08c3bdfSopenharmony_ci {&old_set, 50000, 0, 0, "setting old_value"}, 45f08c3bdfSopenharmony_ci {&old_set, 50000, 50000, 0, "using periodic timer"}, 46f08c3bdfSopenharmony_ci {&old_set, 50000, 0, TIMER_ABSTIME, "using absolute time"}, 47f08c3bdfSopenharmony_ci}; 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_cistatic struct time64_variants variants[] = { 50f08c3bdfSopenharmony_ci#if (__NR_timer_settime != __LTP__NR_INVALID_SYSCALL) 51f08c3bdfSopenharmony_ci { .clock_gettime = sys_clock_gettime, .timer_gettime = sys_timer_gettime, .timer_settime = sys_timer_settime, .ts_type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"}, 52f08c3bdfSopenharmony_ci#endif 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_ci#if (__NR_timer_settime64 != __LTP__NR_INVALID_SYSCALL) 55f08c3bdfSopenharmony_ci { .clock_gettime = sys_clock_gettime64, .timer_gettime = sys_timer_gettime64, .timer_settime = sys_timer_settime64, .ts_type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"}, 56f08c3bdfSopenharmony_ci#endif 57f08c3bdfSopenharmony_ci}; 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_cistatic volatile int caught_signal; 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_cistatic void clear_signal(void) 62f08c3bdfSopenharmony_ci{ 63f08c3bdfSopenharmony_ci /* 64f08c3bdfSopenharmony_ci * The busy loop is intentional. The signal is sent after X 65f08c3bdfSopenharmony_ci * seconds of CPU time has been accumulated for the process and 66f08c3bdfSopenharmony_ci * thread specific clocks. 67f08c3bdfSopenharmony_ci */ 68f08c3bdfSopenharmony_ci while (!caught_signal); 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_ci if (caught_signal != SIGALRM) { 71f08c3bdfSopenharmony_ci tst_res(TFAIL, "Received incorrect signal: %s", 72f08c3bdfSopenharmony_ci tst_strsig(caught_signal)); 73f08c3bdfSopenharmony_ci } 74f08c3bdfSopenharmony_ci 75f08c3bdfSopenharmony_ci caught_signal = 0; 76f08c3bdfSopenharmony_ci} 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_cistatic void sighandler(int sig) 79f08c3bdfSopenharmony_ci{ 80f08c3bdfSopenharmony_ci caught_signal = sig; 81f08c3bdfSopenharmony_ci} 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_cistatic void setup(void) 84f08c3bdfSopenharmony_ci{ 85f08c3bdfSopenharmony_ci tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc); 86f08c3bdfSopenharmony_ci SAFE_SIGNAL(SIGALRM, sighandler); 87f08c3bdfSopenharmony_ci} 88f08c3bdfSopenharmony_ci 89f08c3bdfSopenharmony_cistatic void run(unsigned int n) 90f08c3bdfSopenharmony_ci{ 91f08c3bdfSopenharmony_ci struct time64_variants *tv = &variants[tst_variant]; 92f08c3bdfSopenharmony_ci struct testcase *tc = &tcases[n]; 93f08c3bdfSopenharmony_ci long long val; 94f08c3bdfSopenharmony_ci unsigned int i; 95f08c3bdfSopenharmony_ci 96f08c3bdfSopenharmony_ci tst_res(TINFO, "Testing for %s:", tc->description); 97f08c3bdfSopenharmony_ci 98f08c3bdfSopenharmony_ci for (i = 0; i < CLOCKS_DEFINED; ++i) { 99f08c3bdfSopenharmony_ci clock_t clock = clock_list[i]; 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_ci TEST(tst_syscall(__NR_timer_create, clock, NULL, &timer)); 102f08c3bdfSopenharmony_ci if (TST_RET != 0) { 103f08c3bdfSopenharmony_ci if (possibly_unsupported(clock) && 104f08c3bdfSopenharmony_ci (TST_ERR == EINVAL || TST_ERR == ENOTSUP)) { 105f08c3bdfSopenharmony_ci tst_res(TCONF | TTERRNO, "%s unsupported", 106f08c3bdfSopenharmony_ci get_clock_str(clock)); 107f08c3bdfSopenharmony_ci } else { 108f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, 109f08c3bdfSopenharmony_ci "timer_create(%s) failed", 110f08c3bdfSopenharmony_ci get_clock_str(clock)); 111f08c3bdfSopenharmony_ci } 112f08c3bdfSopenharmony_ci continue; 113f08c3bdfSopenharmony_ci } 114f08c3bdfSopenharmony_ci 115f08c3bdfSopenharmony_ci memset(&new_set, 0, sizeof(new_set)); 116f08c3bdfSopenharmony_ci memset(&old_set, 0, sizeof(old_set)); 117f08c3bdfSopenharmony_ci 118f08c3bdfSopenharmony_ci new_set.type = old_set.type = tv->ts_type; 119f08c3bdfSopenharmony_ci val = tc->it_value_tv_usec; 120f08c3bdfSopenharmony_ci 121f08c3bdfSopenharmony_ci if (tc->flag & TIMER_ABSTIME) { 122f08c3bdfSopenharmony_ci timenow.type = tv->ts_type; 123f08c3bdfSopenharmony_ci if (tv->clock_gettime(clock, tst_ts_get(&timenow)) < 0) { 124f08c3bdfSopenharmony_ci tst_res(TFAIL, 125f08c3bdfSopenharmony_ci "clock_gettime(%s) failed - skipping the test", 126f08c3bdfSopenharmony_ci get_clock_str(clock)); 127f08c3bdfSopenharmony_ci continue; 128f08c3bdfSopenharmony_ci } 129f08c3bdfSopenharmony_ci tst_ts_add_us(timenow, val); 130f08c3bdfSopenharmony_ci tst_its_set_value_from_ts(&new_set, timenow); 131f08c3bdfSopenharmony_ci } else { 132f08c3bdfSopenharmony_ci tst_its_set_value_from_us(&new_set, val); 133f08c3bdfSopenharmony_ci } 134f08c3bdfSopenharmony_ci 135f08c3bdfSopenharmony_ci tst_its_set_interval_from_us(&new_set, tc->it_interval_tv_usec); 136f08c3bdfSopenharmony_ci 137f08c3bdfSopenharmony_ci TEST(tv->timer_settime(timer, tc->flag, tst_its_get(&new_set), tst_its_get(tc->old_ptr))); 138f08c3bdfSopenharmony_ci 139f08c3bdfSopenharmony_ci if (TST_RET != 0) { 140f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "timer_settime(%s) failed", 141f08c3bdfSopenharmony_ci get_clock_str(clock)); 142f08c3bdfSopenharmony_ci } 143f08c3bdfSopenharmony_ci 144f08c3bdfSopenharmony_ci TEST(tv->timer_gettime(timer, tst_its_get(&new_set))); 145f08c3bdfSopenharmony_ci if (TST_RET != 0) { 146f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "timer_gettime(%s) failed", 147f08c3bdfSopenharmony_ci get_clock_str(clock)); 148f08c3bdfSopenharmony_ci } else if ((tst_its_get_interval_nsec(new_set) != 149f08c3bdfSopenharmony_ci tc->it_interval_tv_usec * 1000) || 150f08c3bdfSopenharmony_ci (tst_its_get_value_nsec(new_set) > 151f08c3bdfSopenharmony_ci MAX(tc->it_value_tv_usec * 1000, tc->it_interval_tv_usec * 1000))) { 152f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, 153f08c3bdfSopenharmony_ci "timer_gettime(%s) reported bad values (%llu: %llu)", 154f08c3bdfSopenharmony_ci get_clock_str(clock), 155f08c3bdfSopenharmony_ci tst_its_get_interval_nsec(new_set), 156f08c3bdfSopenharmony_ci tst_its_get_value_nsec(new_set)); 157f08c3bdfSopenharmony_ci } 158f08c3bdfSopenharmony_ci 159f08c3bdfSopenharmony_ci clear_signal(); 160f08c3bdfSopenharmony_ci 161f08c3bdfSopenharmony_ci /* Wait for another event when interval was set */ 162f08c3bdfSopenharmony_ci if (tc->it_interval_tv_usec) 163f08c3bdfSopenharmony_ci clear_signal(); 164f08c3bdfSopenharmony_ci 165f08c3bdfSopenharmony_ci tst_res(TPASS, "timer_settime(%s) passed", 166f08c3bdfSopenharmony_ci get_clock_str(clock)); 167f08c3bdfSopenharmony_ci 168f08c3bdfSopenharmony_ci TEST(tst_syscall(__NR_timer_delete, timer)); 169f08c3bdfSopenharmony_ci if (TST_RET != 0) 170f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "timer_delete() failed!"); 171f08c3bdfSopenharmony_ci } 172f08c3bdfSopenharmony_ci} 173f08c3bdfSopenharmony_ci 174f08c3bdfSopenharmony_cistatic struct tst_test test = { 175f08c3bdfSopenharmony_ci .test = run, 176f08c3bdfSopenharmony_ci .needs_root = 1, 177f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 178f08c3bdfSopenharmony_ci .test_variants = ARRAY_SIZE(variants), 179f08c3bdfSopenharmony_ci .setup = setup, 180f08c3bdfSopenharmony_ci .tags = (const struct tst_tag[]) { 181f08c3bdfSopenharmony_ci {"linux-git", "f18ddc13af98"}, 182f08c3bdfSopenharmony_ci {"linux-git", "e86fea764991"}, 183f08c3bdfSopenharmony_ci {} 184f08c3bdfSopenharmony_ci } 185f08c3bdfSopenharmony_ci}; 186