Home | History | Annotate | Download | only in futimesat
      1 /*
      2  * Copyright (c) 2016 Oracle and/or its affiliates. All Rights Reserved.
      3  * Copyright (c) International Business Machines  Corp., 2006
      4  *
      5  * This program is free software; you can redistribute it and/or
      6  * modify it under the terms of the GNU General Public License as
      7  * published by the Free Software Foundation; either version 2 of
      8  * the License, or (at your option) any later version.
      9  *
     10  * This program is distributed in the hope that it would be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  * GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License
     16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
     17  *
     18  * DESCRIPTION
     19  *	This test case will verify basic function of futimesat
     20  *	added by kernel 2.6.16 or up.
     21  *
     22  * Author
     23  *	Yi Yang <yyangcdl (at) cn.ibm.com>
     24  */
     25 
     26 #define _GNU_SOURCE
     27 #include <sys/types.h>
     28 #include <sys/stat.h>
     29 #include <sys/time.h>
     30 #include <fcntl.h>
     31 #include <stdlib.h>
     32 #include <errno.h>
     33 #include <string.h>
     34 #include <signal.h>
     35 #include "test.h"
     36 #include "safe_macros.h"
     37 #include "lapi/syscalls.h"
     38 
     39 #define TEST_CASES 5
     40 #ifndef AT_FDCWD
     41 #define AT_FDCWD -100
     42 #endif
     43 
     44 void setup();
     45 void cleanup();
     46 
     47 char *TCID = "futimesat01";
     48 int TST_TOTAL = TEST_CASES;
     49 
     50 static const char pathname[] = "futimesattestdir",
     51 		  testfile[] = "futimesattestfile.txt",
     52 		  testfile2[] = "futimesattestdir/futimesattestfile.txt";
     53 static char *testfile3;
     54 
     55 static int fds[TEST_CASES];
     56 static const char *filenames[TEST_CASES];
     57 static const int expected_errno[] = { 0, 0, ENOTDIR, EBADF, 0 };
     58 
     59 int myfutimesat(int dirfd, const char *filename, struct timeval *times)
     60 {
     61 	return ltp_syscall(__NR_futimesat, dirfd, filename, times);
     62 }
     63 
     64 int main(int ac, char **av)
     65 {
     66 	int lc, i;
     67 	struct timeval times[2];
     68 
     69 	if (tst_kvercmp(2, 6, 16) < 0)
     70 		tst_brkm(TCONF, NULL, "Test must be run with kernel 2.6.16+");
     71 
     72 	tst_parse_opts(ac, av, NULL, NULL);
     73 
     74 	setup();
     75 
     76 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     77 		tst_count = 0;
     78 
     79 		for (i = 0; i < TST_TOTAL; i++) {
     80 			gettimeofday(&times[0], NULL);
     81 			gettimeofday(&times[1], NULL);
     82 			TEST(myfutimesat(fds[i], filenames[i], times));
     83 
     84 			if (TEST_ERRNO == expected_errno[i]) {
     85 				tst_resm(TPASS | TTERRNO,
     86 					 "futimesat() returned expected errno");
     87 			} else {
     88 				tst_resm(TFAIL | TTERRNO, "futimesat() failed");
     89 			}
     90 		}
     91 
     92 	}
     93 
     94 	cleanup();
     95 	tst_exit();
     96 }
     97 
     98 void setup(void)
     99 {
    100 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    101 
    102 	tst_tmpdir();
    103 
    104 	char *abs_path = tst_get_tmpdir();
    105 
    106 	SAFE_ASPRINTF(cleanup, &testfile3, "%s/futimesatfile3.txt", abs_path);
    107 	free(abs_path);
    108 
    109 	SAFE_MKDIR(cleanup, pathname, 0700);
    110 
    111 	fds[0] = SAFE_OPEN(cleanup, pathname, O_DIRECTORY);
    112 	fds[1] = fds[0];
    113 
    114 	SAFE_FILE_PRINTF(cleanup, testfile, testfile);
    115 	SAFE_FILE_PRINTF(cleanup, testfile2, testfile2);
    116 
    117 	fds[2] = SAFE_OPEN(cleanup, testfile3, O_CREAT | O_RDWR, 0600);
    118 
    119 	fds[3] = 100;
    120 	fds[4] = AT_FDCWD;
    121 
    122 	filenames[0] = filenames[2] = filenames[3] = filenames[4] = testfile;
    123 	filenames[1] = testfile3;
    124 
    125 	TEST_PAUSE;
    126 }
    127 
    128 void cleanup(void)
    129 {
    130 	if (fds[0] > 0)
    131 		close(fds[0]);
    132 	if (fds[2] > 0)
    133 		close(fds[2]);
    134 
    135 	free(testfile3);
    136 	tst_rmdir();
    137 }
    138