1 /* 2 * Copyright (c) 2014 Fujitsu Ltd. 3 * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com> 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of version 2 of the GNU General Public License as 7 * published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it would be useful, but 10 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 * 13 * You should have received a copy of the GNU General Public License along 14 * with this program; if not, write the Free Software Foundation, Inc., 15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 16 */ 17 /* 18 * DESCRIPTION 19 * Verify that, 20 * 1. fd is not a valid file descriptor, EBADF would return. 21 * 2. old_value is not valid a pointer, EFAULT would return. 22 * 3. fd is not a valid timerfd file descriptor, EINVAL would return. 23 * 4. flags is invalid, EINVAL would return. 24 */ 25 26 #define _GNU_SOURCE 27 28 #include <errno.h> 29 #include <sys/types.h> 30 #include <sys/stat.h> 31 #include <fcntl.h> 32 33 #include "test.h" 34 #include "safe_macros.h" 35 #include "lapi/timerfd.h" 36 37 char *TCID = "timerfd_settime01"; 38 39 static int bad_clockfd = -1; 40 static int clockfd; 41 static int fd; 42 43 static struct test_case_t { 44 int *fd; 45 int flags; 46 struct itimerspec *old_value; 47 int exp_errno; 48 } test_cases[] = { 49 {&bad_clockfd, 0, NULL, EBADF}, 50 {&clockfd, 0, (struct itimerspec *)-1, EFAULT}, 51 {&fd, 0, NULL, EINVAL}, 52 {&clockfd, -1, NULL, EINVAL}, 53 }; 54 55 int TST_TOTAL = ARRAY_SIZE(test_cases); 56 static void setup(void); 57 static void timerfd_settime_verify(const struct test_case_t *); 58 static void cleanup(void); 59 static struct itimerspec new_value; 60 61 int main(int argc, char *argv[]) 62 { 63 int lc; 64 int i; 65 66 tst_parse_opts(argc, argv, NULL, NULL); 67 68 setup(); 69 70 for (lc = 0; TEST_LOOPING(lc); lc++) { 71 tst_count = 0; 72 for (i = 0; i < TST_TOTAL; i++) 73 timerfd_settime_verify(&test_cases[i]); 74 } 75 76 cleanup(); 77 tst_exit(); 78 } 79 80 static void setup(void) 81 { 82 if ((tst_kvercmp(2, 6, 25)) < 0) 83 tst_brkm(TCONF, NULL, "This test needs kernel 2.6.25 or newer"); 84 85 tst_sig(NOFORK, DEF_HANDLER, cleanup); 86 87 TEST_PAUSE; 88 89 tst_tmpdir(); 90 91 clockfd = timerfd_create(CLOCK_REALTIME, 0); 92 if (clockfd == -1) 93 tst_brkm(TBROK | TERRNO, cleanup, "timerfd_create() fail"); 94 95 fd = SAFE_OPEN(cleanup, "test_file", O_RDWR | O_CREAT, 0644); 96 } 97 98 static void timerfd_settime_verify(const struct test_case_t *test) 99 { 100 TEST(timerfd_settime(*test->fd, test->flags, &new_value, 101 test->old_value)); 102 103 if (TEST_RETURN != -1) { 104 tst_resm(TFAIL, "timerfd_settime() succeeded unexpectedly"); 105 return; 106 } 107 108 if (TEST_ERRNO == test->exp_errno) { 109 tst_resm(TPASS | TTERRNO, 110 "timerfd_settime() failed as expected"); 111 } else { 112 tst_resm(TFAIL | TTERRNO, 113 "timerfd_settime() failed unexpectedly; expected: " 114 "%d - %s", test->exp_errno, strerror(test->exp_errno)); 115 } 116 } 117 118 static void cleanup(void) 119 { 120 if (clockfd > 0) 121 close(clockfd); 122 123 if (fd > 0) 124 close(fd); 125 126 tst_rmdir(); 127 } 128