1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci 4f08c3bdfSopenharmony_ci Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz> 5f08c3bdfSopenharmony_ci 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci/* 8f08c3bdfSopenharmony_ci 9f08c3bdfSopenharmony_ci Basic test for timens_offsets error handling. 10f08c3bdfSopenharmony_ci 11f08c3bdfSopenharmony_ci After a call to unshare(CLONE_NEWTIME) a new timer namespace is created, the 12f08c3bdfSopenharmony_ci process that has called the unshare() can adjust offsets for CLOCK_MONOTONIC 13f08c3bdfSopenharmony_ci and CLOCK_BOOTTIME for its children by writing to the '/proc/self/timens_offsets'. 14f08c3bdfSopenharmony_ci 15f08c3bdfSopenharmony_ci */ 16f08c3bdfSopenharmony_ci 17f08c3bdfSopenharmony_ci#define _GNU_SOURCE 18f08c3bdfSopenharmony_ci#include "lapi/posix_clocks.h" 19f08c3bdfSopenharmony_ci#include "tst_test.h" 20f08c3bdfSopenharmony_ci#include "lapi/sched.h" 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_cistatic struct tcase { 23f08c3bdfSopenharmony_ci const char *desc; 24f08c3bdfSopenharmony_ci const char *offsets; 25f08c3bdfSopenharmony_ci int exp_err; 26f08c3bdfSopenharmony_ci} tcases[] = { 27f08c3bdfSopenharmony_ci {"Obvious garbage", "not an offset", EINVAL}, 28f08c3bdfSopenharmony_ci {"Missing nanoseconds", "1 10", EINVAL}, 29f08c3bdfSopenharmony_ci {"Negative nanoseconds", "1 10 -10", EINVAL}, 30f08c3bdfSopenharmony_ci {"Nanoseconds > 1s", "1 10 1000000001", EINVAL}, 31f08c3bdfSopenharmony_ci {"Unsupported CLOCK_REALTIME", "0 10 0", EINVAL}, 32f08c3bdfSopenharmony_ci {"Mess on the second line", "1 10 0\na", EINVAL}, 33f08c3bdfSopenharmony_ci {"Overflow kernel 64bit ns timer", "1 9223372036 0", ERANGE}, 34f08c3bdfSopenharmony_ci {"Overflow kernel 64bit ns timer", "1 -9223372036 0", ERANGE}, 35f08c3bdfSopenharmony_ci}; 36f08c3bdfSopenharmony_ci 37f08c3bdfSopenharmony_cistatic void verify_ns_clock(unsigned int n) 38f08c3bdfSopenharmony_ci{ 39f08c3bdfSopenharmony_ci struct tcase *tc = &tcases[n]; 40f08c3bdfSopenharmony_ci int fd, ret; 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_ci SAFE_UNSHARE(CLONE_NEWTIME); 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_ci fd = SAFE_OPEN("/proc/self/timens_offsets", O_WRONLY); 45f08c3bdfSopenharmony_ci ret = write(fd, tc->offsets, strlen(tc->offsets)); 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_ci if (ret != -1) { 48f08c3bdfSopenharmony_ci tst_res(TFAIL, "%s returned %i", tc->desc, ret); 49f08c3bdfSopenharmony_ci return; 50f08c3bdfSopenharmony_ci } 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_ci if (errno != tc->exp_err) { 53f08c3bdfSopenharmony_ci tst_res(TFAIL | TERRNO, "%s should fail with %s, got:", 54f08c3bdfSopenharmony_ci tc->desc, tst_strerrno(tc->exp_err)); 55f08c3bdfSopenharmony_ci return; 56f08c3bdfSopenharmony_ci } 57f08c3bdfSopenharmony_ci 58f08c3bdfSopenharmony_ci tst_res(TPASS | TERRNO, "%s", tc->desc); 59f08c3bdfSopenharmony_ci} 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_cistatic struct tst_test test = { 62f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 63f08c3bdfSopenharmony_ci .test = verify_ns_clock, 64f08c3bdfSopenharmony_ci .needs_root = 1, 65f08c3bdfSopenharmony_ci .needs_kconfigs = (const char *[]) { 66f08c3bdfSopenharmony_ci "CONFIG_TIME_NS=y", 67f08c3bdfSopenharmony_ci NULL 68f08c3bdfSopenharmony_ci } 69f08c3bdfSopenharmony_ci}; 70