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 <error.h> 32 #include <stdlib.h> 33 #include <errno.h> 34 #include <string.h> 35 #include <signal.h> 36 #include "test.h" 37 #include "safe_macros.h" 38 #include "linux_syscall_numbers.h" 39 40 #define TEST_CASES 5 41 #ifndef AT_FDCWD 42 #define AT_FDCWD -100 43 #endif 44 45 void setup(); 46 void cleanup(); 47 48 char *TCID = "futimesat01"; 49 int TST_TOTAL = TEST_CASES; 50 51 static const char pathname[] = "futimesattestdir", 52 testfile[] = "futimesattestfile.txt", 53 testfile2[] = "futimesattestdir/futimesattestfile.txt"; 54 static char *testfile3; 55 56 static int fds[TEST_CASES]; 57 static const char *filenames[TEST_CASES]; 58 static const int expected_errno[] = { 0, 0, ENOTDIR, EBADF, 0 }; 59 60 int myfutimesat(int dirfd, const char *filename, struct timeval *times) 61 { 62 return ltp_syscall(__NR_futimesat, dirfd, filename, times); 63 } 64 65 int main(int ac, char **av) 66 { 67 int lc, i; 68 struct timeval times[2]; 69 70 if (tst_kvercmp(2, 6, 16) < 0) 71 tst_brkm(TCONF, NULL, "Test must be run with kernel 2.6.16+"); 72 73 tst_parse_opts(ac, av, NULL, NULL); 74 75 setup(); 76 77 for (lc = 0; TEST_LOOPING(lc); lc++) { 78 tst_count = 0; 79 80 for (i = 0; i < TST_TOTAL; i++) { 81 gettimeofday(×[0], NULL); 82 gettimeofday(×[1], NULL); 83 TEST(myfutimesat(fds[i], filenames[i], times)); 84 85 if (TEST_ERRNO == expected_errno[i]) { 86 tst_resm(TPASS | TTERRNO, 87 "futimesat() returned expected errno"); 88 } else { 89 tst_resm(TFAIL | TTERRNO, "futimesat() failed"); 90 } 91 } 92 93 } 94 95 cleanup(); 96 tst_exit(); 97 } 98 99 void setup(void) 100 { 101 tst_sig(NOFORK, DEF_HANDLER, cleanup); 102 103 tst_tmpdir(); 104 105 char *abs_path = tst_get_tmpdir(); 106 107 SAFE_ASPRINTF(cleanup, &testfile3, "%s/futimesatfile3.txt", abs_path); 108 free(abs_path); 109 110 SAFE_MKDIR(cleanup, pathname, 0700); 111 112 fds[0] = SAFE_OPEN(cleanup, pathname, O_DIRECTORY); 113 fds[1] = fds[0]; 114 115 SAFE_FILE_PRINTF(cleanup, testfile, testfile); 116 SAFE_FILE_PRINTF(cleanup, testfile2, testfile2); 117 118 fds[2] = SAFE_OPEN(cleanup, testfile3, O_CREAT | O_RDWR, 0600); 119 120 fds[3] = 100; 121 fds[4] = AT_FDCWD; 122 123 filenames[0] = filenames[2] = filenames[3] = filenames[4] = testfile; 124 filenames[1] = testfile3; 125 126 TEST_PAUSE; 127 } 128 129 void cleanup(void) 130 { 131 if (fds[0] > 0) 132 close(fds[0]); 133 if (fds[2] > 0) 134 close(fds[2]); 135 136 free(testfile3); 137 tst_rmdir(); 138 } 139