1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0 2f08c3bdfSopenharmony_ci 3f08c3bdfSopenharmony_ci/* 4f08c3bdfSopenharmony_ci * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved. 5f08c3bdfSopenharmony_ci * AUTHOR : Saji Kumar.V.R <saji.kumar@wipro.com> 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci 8f08c3bdfSopenharmony_ci/*\ 9f08c3bdfSopenharmony_ci * [Description] 10f08c3bdfSopenharmony_ci * 11f08c3bdfSopenharmony_ci * Tests for adjtimex() error conditions: 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * - EPERM with SET_MODE as nobody 14f08c3bdfSopenharmony_ci * - EFAULT with SET_MODE and invalid timex pointer 15f08c3bdfSopenharmony_ci * - EINVAL with ADJ_TICK greater than max tick 16f08c3bdfSopenharmony_ci * - EINVAL with ADJ_TICK smaller than min tick 17f08c3bdfSopenharmony_ci */ 18f08c3bdfSopenharmony_ci 19f08c3bdfSopenharmony_ci#include <errno.h> 20f08c3bdfSopenharmony_ci#include <sys/timex.h> 21f08c3bdfSopenharmony_ci#include <unistd.h> 22f08c3bdfSopenharmony_ci#include <pwd.h> 23f08c3bdfSopenharmony_ci#include "tst_test.h" 24f08c3bdfSopenharmony_ci#include "lapi/syscalls.h" 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_ci#define SET_MODE (ADJ_OFFSET | ADJ_FREQUENCY | ADJ_MAXERROR | ADJ_ESTERROR | \ 27f08c3bdfSopenharmony_ci ADJ_STATUS | ADJ_TIMECONST | ADJ_TICK) 28f08c3bdfSopenharmony_ci 29f08c3bdfSopenharmony_cistatic int hz; /* HZ from sysconf */ 30f08c3bdfSopenharmony_ci 31f08c3bdfSopenharmony_cistatic struct timex *tim_save, *buf; 32f08c3bdfSopenharmony_cistatic struct passwd *ltpuser; 33f08c3bdfSopenharmony_ci 34f08c3bdfSopenharmony_cistatic int libc_adjtimex(struct timex *value) 35f08c3bdfSopenharmony_ci{ 36f08c3bdfSopenharmony_ci return adjtimex(value); 37f08c3bdfSopenharmony_ci} 38f08c3bdfSopenharmony_ci 39f08c3bdfSopenharmony_cistatic int sys_adjtimex(struct timex *value) 40f08c3bdfSopenharmony_ci{ 41f08c3bdfSopenharmony_ci return tst_syscall(__NR_adjtimex, value); 42f08c3bdfSopenharmony_ci} 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_cistatic struct test_case { 45f08c3bdfSopenharmony_ci unsigned int modes; 46f08c3bdfSopenharmony_ci long lowlimit; 47f08c3bdfSopenharmony_ci long highlimit; 48f08c3bdfSopenharmony_ci long delta; 49f08c3bdfSopenharmony_ci int exp_err; 50f08c3bdfSopenharmony_ci} tc[] = { 51f08c3bdfSopenharmony_ci {.modes = SET_MODE, .exp_err = EPERM}, 52f08c3bdfSopenharmony_ci {.modes = SET_MODE, .exp_err = EFAULT}, 53f08c3bdfSopenharmony_ci {.modes = ADJ_TICK, .lowlimit = 900000, .delta = 1, .exp_err = EINVAL}, 54f08c3bdfSopenharmony_ci {.modes = ADJ_TICK, .highlimit = 1100000, .delta = 1, .exp_err = EINVAL}, 55f08c3bdfSopenharmony_ci}; 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_cistatic struct test_variants 58f08c3bdfSopenharmony_ci{ 59f08c3bdfSopenharmony_ci int (*adjtimex)(struct timex *value); 60f08c3bdfSopenharmony_ci char *desc; 61f08c3bdfSopenharmony_ci} variants[] = { 62f08c3bdfSopenharmony_ci { .adjtimex = libc_adjtimex, .desc = "libc adjtimex()"}, 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci#if (__NR_adjtimex != __LTP__NR_INVALID_SYSCALL) 65f08c3bdfSopenharmony_ci { .adjtimex = sys_adjtimex, .desc = "__NR_adjtimex syscall"}, 66f08c3bdfSopenharmony_ci#endif 67f08c3bdfSopenharmony_ci}; 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_cistatic void verify_adjtimex(unsigned int i) 70f08c3bdfSopenharmony_ci{ 71f08c3bdfSopenharmony_ci struct timex *bufp; 72f08c3bdfSopenharmony_ci struct test_variants *tv = &variants[tst_variant]; 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_ci *buf = *tim_save; 75f08c3bdfSopenharmony_ci buf->modes = tc[i].modes; 76f08c3bdfSopenharmony_ci bufp = buf; 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_ci if (tc[i].exp_err == EPERM) 79f08c3bdfSopenharmony_ci SAFE_SETEUID(ltpuser->pw_uid); 80f08c3bdfSopenharmony_ci 81f08c3bdfSopenharmony_ci if (tc[i].exp_err == EINVAL) { 82f08c3bdfSopenharmony_ci if (tc[i].modes == ADJ_TICK) { 83f08c3bdfSopenharmony_ci if (tc[i].lowlimit) 84f08c3bdfSopenharmony_ci buf->tick = tc[i].lowlimit - tc[i].delta; 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_ci if (tc[i].highlimit) 87f08c3bdfSopenharmony_ci buf->tick = tc[i].highlimit + tc[i].delta; 88f08c3bdfSopenharmony_ci } 89f08c3bdfSopenharmony_ci } 90f08c3bdfSopenharmony_ci 91f08c3bdfSopenharmony_ci if (tc[i].exp_err == EFAULT) { 92f08c3bdfSopenharmony_ci if (tv->adjtimex != libc_adjtimex) { 93f08c3bdfSopenharmony_ci bufp = (struct timex *) -1; 94f08c3bdfSopenharmony_ci } else { 95f08c3bdfSopenharmony_ci tst_res(TCONF, "EFAULT is skipped for libc variant"); 96f08c3bdfSopenharmony_ci return; 97f08c3bdfSopenharmony_ci } 98f08c3bdfSopenharmony_ci } 99f08c3bdfSopenharmony_ci 100f08c3bdfSopenharmony_ci TST_EXP_FAIL2(tv->adjtimex(bufp), tc[i].exp_err, "adjtimex() error"); 101f08c3bdfSopenharmony_ci 102f08c3bdfSopenharmony_ci if (tc[i].exp_err == EPERM) 103f08c3bdfSopenharmony_ci SAFE_SETEUID(0); 104f08c3bdfSopenharmony_ci} 105f08c3bdfSopenharmony_ci 106f08c3bdfSopenharmony_cistatic void setup(void) 107f08c3bdfSopenharmony_ci{ 108f08c3bdfSopenharmony_ci struct test_variants *tv = &variants[tst_variant]; 109f08c3bdfSopenharmony_ci size_t i; 110f08c3bdfSopenharmony_ci 111f08c3bdfSopenharmony_ci tst_res(TINFO, "Testing variant: %s", tv->desc); 112f08c3bdfSopenharmony_ci 113f08c3bdfSopenharmony_ci tim_save->modes = 0; 114f08c3bdfSopenharmony_ci 115f08c3bdfSopenharmony_ci ltpuser = SAFE_GETPWNAM("nobody"); 116f08c3bdfSopenharmony_ci SAFE_SETEUID(ltpuser->pw_uid); 117f08c3bdfSopenharmony_ci 118f08c3bdfSopenharmony_ci /* set the HZ from sysconf */ 119f08c3bdfSopenharmony_ci hz = SAFE_SYSCONF(_SC_CLK_TCK); 120f08c3bdfSopenharmony_ci 121f08c3bdfSopenharmony_ci for (i = 0; i < ARRAY_SIZE(tc); i++) { 122f08c3bdfSopenharmony_ci if (tc[i].modes == ADJ_TICK) { 123f08c3bdfSopenharmony_ci tc[i].highlimit /= hz; 124f08c3bdfSopenharmony_ci tc[i].lowlimit /= hz; 125f08c3bdfSopenharmony_ci } 126f08c3bdfSopenharmony_ci } 127f08c3bdfSopenharmony_ci 128f08c3bdfSopenharmony_ci if ((adjtimex(tim_save)) == -1) { 129f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, 130f08c3bdfSopenharmony_ci "adjtimex(): failed to save current params"); 131f08c3bdfSopenharmony_ci } 132f08c3bdfSopenharmony_ci} 133f08c3bdfSopenharmony_ci 134f08c3bdfSopenharmony_cistatic void cleanup(void) 135f08c3bdfSopenharmony_ci{ 136f08c3bdfSopenharmony_ci 137f08c3bdfSopenharmony_ci tim_save->modes = SET_MODE; 138f08c3bdfSopenharmony_ci 139f08c3bdfSopenharmony_ci /* Restore saved parameters */ 140f08c3bdfSopenharmony_ci if ((adjtimex(tim_save)) == -1) 141f08c3bdfSopenharmony_ci tst_res(TWARN, "Failed to restore saved parameters"); 142f08c3bdfSopenharmony_ci} 143f08c3bdfSopenharmony_ci 144f08c3bdfSopenharmony_cistatic struct tst_test test = { 145f08c3bdfSopenharmony_ci .test = verify_adjtimex, 146f08c3bdfSopenharmony_ci .setup = setup, 147f08c3bdfSopenharmony_ci .cleanup = cleanup, 148f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tc), 149f08c3bdfSopenharmony_ci .test_variants = ARRAY_SIZE(variants), 150f08c3bdfSopenharmony_ci .needs_root = 1, 151f08c3bdfSopenharmony_ci .bufs = (struct tst_buffers []) { 152f08c3bdfSopenharmony_ci {&buf, .size = sizeof(*buf)}, 153f08c3bdfSopenharmony_ci {&tim_save, .size = sizeof(*tim_save)}, 154f08c3bdfSopenharmony_ci {}, 155f08c3bdfSopenharmony_ci } 156f08c3bdfSopenharmony_ci}; 157