1d13d74bbf207c2d01598c6fef092a75b93b07c71vapier/* 2d13d74bbf207c2d01598c6fef092a75b93b07c71vapier * Copyright (c) Wipro Technologies Ltd, 2003. All Rights Reserved. 3b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubis * Copyright (c) 2011 Cyril Hrubis <chrubis@suse.cz> 4d13d74bbf207c2d01598c6fef092a75b93b07c71vapier * 5d13d74bbf207c2d01598c6fef092a75b93b07c71vapier * This program is free software; you can redistribute it and/or modify it 6d13d74bbf207c2d01598c6fef092a75b93b07c71vapier * under the terms of version 2 of the GNU General Public License as 7d13d74bbf207c2d01598c6fef092a75b93b07c71vapier * published by the Free Software Foundation. 8d13d74bbf207c2d01598c6fef092a75b93b07c71vapier * 9d13d74bbf207c2d01598c6fef092a75b93b07c71vapier * This program is distributed in the hope that it would be useful, but 10d13d74bbf207c2d01598c6fef092a75b93b07c71vapier * WITHOUT ANY WARRANTY; without even the implied warranty of 11d13d74bbf207c2d01598c6fef092a75b93b07c71vapier * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12d13d74bbf207c2d01598c6fef092a75b93b07c71vapier * 13d13d74bbf207c2d01598c6fef092a75b93b07c71vapier * You should have received a copy of the GNU General Public License along 14fed9641096e27f79a0f2d9adfe9839dd8d11dc0fWanlong Gao * with this program; if not, write the Free Software Foundation, Inc., 15fed9641096e27f79a0f2d9adfe9839dd8d11dc0fWanlong Gao * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 16d13d74bbf207c2d01598c6fef092a75b93b07c71vapier * 17d13d74bbf207c2d01598c6fef092a75b93b07c71vapier */ 18d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 19d13d74bbf207c2d01598c6fef092a75b93b07c71vapier#include <errno.h> 20d13d74bbf207c2d01598c6fef092a75b93b07c71vapier#include <time.h> 21d13d74bbf207c2d01598c6fef092a75b93b07c71vapier#include <pwd.h> 22bc8a4616c83630c69ec3ca2bd6e4eb7acc456eeevapier#include <unistd.h> 23d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 24bc8a4616c83630c69ec3ca2bd6e4eb7acc456eeevapier#include "test.h" 253ac6ae7a78adb25b3d4496a0a5ed1ee63652da8aCyril Hrubis#include "safe_macros.h" 26bc8a4616c83630c69ec3ca2bd6e4eb7acc456eeevapier#include "common_timers.h" 27d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 28b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubisstatic void setup(void); 29b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubisstatic void cleanup(void); 30d13d74bbf207c2d01598c6fef092a75b93b07c71vapierstatic int setup_test(int option); 31d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 32e8af978e6bdaffb4c28ea9245254444d3d66be82subrata_modakclockid_t clocks[] = { 33e8af978e6bdaffb4c28ea9245254444d3d66be82subrata_modak CLOCK_REALTIME, 34e8af978e6bdaffb4c28ea9245254444d3d66be82subrata_modak CLOCK_MONOTONIC, 35e8af978e6bdaffb4c28ea9245254444d3d66be82subrata_modak MAX_CLOCKS, 36e8af978e6bdaffb4c28ea9245254444d3d66be82subrata_modak MAX_CLOCKS + 1, 37e8af978e6bdaffb4c28ea9245254444d3d66be82subrata_modak CLOCK_REALTIME, 38e8af978e6bdaffb4c28ea9245254444d3d66be82subrata_modak CLOCK_REALTIME, 39e8af978e6bdaffb4c28ea9245254444d3d66be82subrata_modak CLOCK_REALTIME, 40e8af978e6bdaffb4c28ea9245254444d3d66be82subrata_modak CLOCK_PROCESS_CPUTIME_ID, 41e8af978e6bdaffb4c28ea9245254444d3d66be82subrata_modak CLOCK_THREAD_CPUTIME_ID 42d13d74bbf207c2d01598c6fef092a75b93b07c71vapier}; 43d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 44b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubisint testcases[] = { 45354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao EFAULT, /* tp bad */ 46354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao EINVAL, /* CLOCK_MONOTONIC */ 47354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao EINVAL, /* MAX_CLOCKS */ 48354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao EINVAL, /* MAX_CLOCKS + 1 */ 49354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao EINVAL, /* Invalid timespec */ 50354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao EINVAL, /* NSEC_PER_SEC + 1 */ 51354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao EPERM, /* non-root user */ 52a2d159595a761da1626b3f489dfe9cf2c351b774Wanlong Gao EINVAL, /* PROCESS_CPUTIME_ID */ 53a2d159595a761da1626b3f489dfe9cf2c351b774Wanlong Gao EINVAL, /* THREAD_CPUTIME_ID */ 54d13d74bbf207c2d01598c6fef092a75b93b07c71vapier}; 55d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 56b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubischar *TCID = "clock_settime03"; 57b863a0bd366f8af57b000e2f1da5da0bfd8cebf7Cyril Hrubisint TST_TOTAL = ARRAY_SIZE(testcases); 58b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubis 59b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubischar nobody_uid[] = "nobody"; 60b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubisstruct passwd *ltpuser; 61b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubisstatic struct timespec spec, *temp, saved; 62b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubis 63b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubisint main(int ac, char **av) 64d13d74bbf207c2d01598c6fef092a75b93b07c71vapier{ 65b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubis int lc, i; 66d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 67d6d11d08678aac1ed2c370ea8e42e5f45aea07beCyril Hrubis tst_parse_opts(ac, av, NULL, NULL); 68d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 69d13d74bbf207c2d01598c6fef092a75b93b07c71vapier setup(); 70d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 71d13d74bbf207c2d01598c6fef092a75b93b07c71vapier for (lc = 0; TEST_LOOPING(lc); lc++) { 72d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 73d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang tst_count = 0; 74d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 75d13d74bbf207c2d01598c6fef092a75b93b07c71vapier for (i = 0; i < TST_TOTAL; i++) { 76d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 77b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubis if (setup_test(i) < 0) 78d13d74bbf207c2d01598c6fef092a75b93b07c71vapier continue; 79d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 80359980f68b19c77c698b121b57a071dfe6e3ca31Jan Stancek TEST(ltp_syscall(__NR_clock_settime, clocks[i], temp)); 81d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 82d13d74bbf207c2d01598c6fef092a75b93b07c71vapier /* Change the UID back to root */ 83d13d74bbf207c2d01598c6fef092a75b93b07c71vapier if (i == TST_TOTAL - 1) { 843ac6ae7a78adb25b3d4496a0a5ed1ee63652da8aCyril Hrubis SAFE_SETEUID(cleanup, 0); 85d13d74bbf207c2d01598c6fef092a75b93b07c71vapier } 86d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 87d13d74bbf207c2d01598c6fef092a75b93b07c71vapier /* check return code */ 88b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubis if (TEST_RETURN == -1 && TEST_ERRNO == testcases[i]) { 89261d0fb582b51fd3148a263bf894933bfe8e0ec9yaberauneya tst_resm(TPASS | TTERRNO, 90354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao "clock_settime(2) got expected " 91354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao "failure."); 92d13d74bbf207c2d01598c6fef092a75b93b07c71vapier } else { 93261d0fb582b51fd3148a263bf894933bfe8e0ec9yaberauneya tst_resm(TFAIL | TTERRNO, 94354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao "clock_settime(2) failed to produce " 95354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao "expected error (return code = %ld)", 96354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao TEST_RETURN); 971a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya /* Restore the clock to its previous state. */ 98d13d74bbf207c2d01598c6fef092a75b93b07c71vapier if (TEST_RETURN == 0) { 99359980f68b19c77c698b121b57a071dfe6e3ca31Jan Stancek if (ltp_syscall(__NR_clock_settime, 100354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao CLOCK_REALTIME, 101354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao &saved) < 0) { 1021a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya tst_resm(TWARN | TERRNO, 103354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao "FATAL: could not set " 104354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao "the clock!"); 105d13d74bbf207c2d01598c6fef092a75b93b07c71vapier } 106d13d74bbf207c2d01598c6fef092a75b93b07c71vapier } 107b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubis } 108d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 1092c28215423293e443469a07ae7011135d058b671Garrett Cooper } 1101a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya 1112c28215423293e443469a07ae7011135d058b671Garrett Cooper } 112d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 113d13d74bbf207c2d01598c6fef092a75b93b07c71vapier cleanup(); 1141a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya tst_exit(); 115d13d74bbf207c2d01598c6fef092a75b93b07c71vapier} 116d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 117b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubisstatic int setup_test(int option) 118d13d74bbf207c2d01598c6fef092a75b93b07c71vapier{ 119e8af978e6bdaffb4c28ea9245254444d3d66be82subrata_modak /* valid timespec */ 120e8af978e6bdaffb4c28ea9245254444d3d66be82subrata_modak spec = saved; 121e8af978e6bdaffb4c28ea9245254444d3d66be82subrata_modak temp = &spec; 122d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 123e8af978e6bdaffb4c28ea9245254444d3d66be82subrata_modak /* error sceanrios */ 124d13d74bbf207c2d01598c6fef092a75b93b07c71vapier switch (option) { 1251a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya case 0: 1261a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya /* Make tp argument bad pointer */ 127354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao temp = (struct timespec *)-1; 1281a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya break; 1291a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya case 4: 1301a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya /* Make the parameter of timespec invalid */ 1311a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya spec.tv_nsec = -1; 1321a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya break; 1331a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya case 5: 1341a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya /* Make the parameter of timespec invalid */ 1351a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya spec.tv_nsec = NSEC_PER_SEC + 1; 1361a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya break; 1371a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya case 6: 1381a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya /* change the User to non-root */ 1391a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya spec.tv_nsec = 0; 1401a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya if ((ltpuser = getpwnam(nobody_uid)) == NULL) { 141a9670cd56bfee821a9b2d34b5c5d1710d7f03777Garrett Cooper tst_resm(TWARN, "user \"nobody\" not present; " 142354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao "skipping test"); 1431a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya return -1; 1441a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya } 1451a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya if (seteuid(ltpuser->pw_uid) == -1) { 1461a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya tst_resm(TWARN | TERRNO, 147354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao "seteuid failed to set the effective " 148354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao "uid to %d (nobody)", ltpuser->pw_uid); 1491a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya return -1; 1501a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya } 1511a7f54261e0ab0d8318f1833987f1a7178b546e6yaberauneya break; 152d13d74bbf207c2d01598c6fef092a75b93b07c71vapier } 153d13d74bbf207c2d01598c6fef092a75b93b07c71vapier return 0; 154d13d74bbf207c2d01598c6fef092a75b93b07c71vapier} 155d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 156b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubisstatic void setup(void) 157d13d74bbf207c2d01598c6fef092a75b93b07c71vapier{ 158d13d74bbf207c2d01598c6fef092a75b93b07c71vapier tst_sig(NOFORK, DEF_HANDLER, cleanup); 159d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 160d1e794d62b1bf619df8390535e4c2a58899b1145Cyril Hrubis tst_require_root(); 161d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 162359980f68b19c77c698b121b57a071dfe6e3ca31Jan Stancek if (ltp_syscall(__NR_clock_gettime, CLOCK_REALTIME, &saved) < 0) 16353740500924f6439623a8ac256b5be2d6c59ed1fGarrett Cooper tst_brkm(TBROK, NULL, "Clock gettime failed"); 164d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 165d13d74bbf207c2d01598c6fef092a75b93b07c71vapier spec.tv_sec = 1; 166d13d74bbf207c2d01598c6fef092a75b93b07c71vapier spec.tv_nsec = 0; 167d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 168d13d74bbf207c2d01598c6fef092a75b93b07c71vapier TEST_PAUSE; 169a9670cd56bfee821a9b2d34b5c5d1710d7f03777Garrett Cooper} 170d13d74bbf207c2d01598c6fef092a75b93b07c71vapier 171b7a95045ac942be724d9471c5c54e717ee1786e9Cyril Hrubisstatic void cleanup(void) 172d13d74bbf207c2d01598c6fef092a75b93b07c71vapier{ 173ec544efd9871416ac266262abd740fb9561e5b4eWanlong Gao} 174