1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Copyright (c) International Business Machines Corp., 2001 4 * 5 * Test Description: 6 * Verify that the system call stime() successfully sets the system's idea 7 * of date and time if invoked by "root" user. 8 * 9 * Expected Result: 10 * stime() should succeed to set the system data/time to the specified time. 11 * 12 * History 13 * 07/2001 John George 14 * -Ported 15 */ 16 17#include <time.h> 18#include <sys/time.h> 19 20#include "tst_test.h" 21#include "stime_var.h" 22 23static struct timeval real_time_tv; 24 25static void run(void) 26{ 27 time_t new_time; 28 struct timeval pres_time_tv; 29 30 if (gettimeofday(&real_time_tv, NULL) < 0) 31 tst_brk(TBROK | TERRNO, "gettimeofday() failed"); 32 33 new_time = real_time_tv.tv_sec + 30; 34 35 if (do_stime(&new_time) < 0) { 36 tst_res(TFAIL | TERRNO, "stime(%ld) failed", new_time); 37 return; 38 } 39 40 if (gettimeofday(&pres_time_tv, NULL) < 0) 41 tst_brk(TBROK | TERRNO, "gettimeofday() failed"); 42 43 switch (pres_time_tv.tv_sec - new_time) { 44 case 0: 45 case 1: 46 tst_res(TINFO, "pt.tv_sec: %ld", pres_time_tv.tv_sec); 47 tst_res(TPASS, "system time was set to %ld", new_time); 48 break; 49 default: 50 tst_res(TFAIL, "system time not set to %ld (got: %ld)", 51 new_time, pres_time_tv.tv_sec); 52 } 53} 54 55static void setup(void) 56{ 57 stime_info(); 58} 59 60static struct tst_test test = { 61 .test_all = run, 62 .needs_root = 1, 63 .restore_wallclock = 1, 64 .setup = setup, 65 .test_variants = TEST_VARIANTS, 66}; 67