Home | History | Annotate | Download | only in timerfd
      1 /*
      2  * Copyright (c) 2014 Fujitsu Ltd.
      3  * Author: Zeng Linggang <zenglg.jy (at) 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. curr_value is not valid a pointer, EFAULT would return.
     22  *   3. fd is not a valid timerfd file descriptor, EINVAL would return.
     23  */
     24 
     25 #define _GNU_SOURCE
     26 
     27 #include <errno.h>
     28 #include <sys/types.h>
     29 #include <sys/stat.h>
     30 #include <fcntl.h>
     31 
     32 #include "test.h"
     33 #include "safe_macros.h"
     34 #include "lapi/timerfd.h"
     35 
     36 char *TCID = "timerfd_gettime01";
     37 
     38 static int bad_clockfd = -1;
     39 static int clockfd;
     40 static int fd;
     41 
     42 static struct test_case_t {
     43 	int *fd;
     44 	struct itimerspec *curr_value;
     45 	int exp_errno;
     46 } test_cases[] = {
     47 	{&bad_clockfd, NULL, EBADF},
     48 	{&clockfd, (struct itimerspec *)-1, EFAULT},
     49 	{&fd, NULL, EINVAL},
     50 };
     51 
     52 int TST_TOTAL = ARRAY_SIZE(test_cases);
     53 static void setup(void);
     54 static void timerfd_gettime_verify(const struct test_case_t *);
     55 static void cleanup(void);
     56 
     57 int main(int argc, char *argv[])
     58 {
     59 	int lc;
     60 	int i;
     61 
     62 	tst_parse_opts(argc, argv, NULL, NULL);
     63 
     64 	setup();
     65 
     66 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     67 		tst_count = 0;
     68 		for (i = 0; i < TST_TOTAL; i++)
     69 			timerfd_gettime_verify(&test_cases[i]);
     70 	}
     71 
     72 	cleanup();
     73 	tst_exit();
     74 }
     75 
     76 static void setup(void)
     77 {
     78 	if ((tst_kvercmp(2, 6, 25)) < 0)
     79 		tst_brkm(TCONF, NULL, "This test needs kernel 2.6.25 or newer");
     80 
     81 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
     82 
     83 	TEST_PAUSE;
     84 
     85 	tst_tmpdir();
     86 
     87 	clockfd = timerfd_create(CLOCK_REALTIME, 0);
     88 	if (clockfd == -1)
     89 		tst_brkm(TBROK | TERRNO, cleanup, "timerfd_create() fail");
     90 
     91 	fd = SAFE_OPEN(cleanup, "test_file", O_RDWR | O_CREAT, 0644);
     92 }
     93 
     94 static void timerfd_gettime_verify(const struct test_case_t *test)
     95 {
     96 	TEST(timerfd_gettime(*test->fd, test->curr_value));
     97 
     98 	if (TEST_RETURN != -1) {
     99 		tst_resm(TFAIL, "timerfd_gettime() succeeded unexpectedly");
    100 		return;
    101 	}
    102 
    103 	if (TEST_ERRNO == test->exp_errno) {
    104 		tst_resm(TPASS | TTERRNO,
    105 			 "timerfd_gettime() failed as expected");
    106 	} else {
    107 		tst_resm(TFAIL | TTERRNO,
    108 			 "timerfd_gettime() failed unexpectedly; expected: "
    109 			 "%d - %s", test->exp_errno, strerror(test->exp_errno));
    110 	}
    111 }
    112 
    113 static void cleanup(void)
    114 {
    115 	if (clockfd > 0)
    116 		close(clockfd);
    117 
    118 	if (fd > 0)
    119 		close(fd);
    120 
    121 	tst_rmdir();
    122 }
    123