xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/futimesat/futimesat01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 <[email protected]>
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 
myfutimesat(int dirfd,const char * filename,struct timeval * times)59 int myfutimesat(int dirfd, const char *filename, struct timeval *times)
60 {
61 	return tst_syscall(__NR_futimesat, dirfd, filename, times);
62 }
63 
main(int ac,char ** av)64 int main(int ac, char **av)
65 {
66 	int lc, i;
67 	struct timeval times[2];
68 
69 	tst_parse_opts(ac, av, NULL, NULL);
70 
71 	setup();
72 
73 	for (lc = 0; TEST_LOOPING(lc); lc++) {
74 		tst_count = 0;
75 
76 		for (i = 0; i < TST_TOTAL; i++) {
77 			gettimeofday(&times[0], NULL);
78 			gettimeofday(&times[1], NULL);
79 			TEST(myfutimesat(fds[i], filenames[i], times));
80 
81 			if (TEST_ERRNO == expected_errno[i]) {
82 				tst_resm(TPASS | TTERRNO,
83 					 "futimesat() returned expected errno");
84 			} else {
85 				tst_resm(TFAIL | TTERRNO, "futimesat() failed");
86 			}
87 		}
88 
89 	}
90 
91 	cleanup();
92 	tst_exit();
93 }
94 
setup(void)95 void setup(void)
96 {
97 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
98 
99 	tst_tmpdir();
100 
101 	char *abs_path = tst_get_tmpdir();
102 
103 	SAFE_ASPRINTF(cleanup, &testfile3, "%s/futimesatfile3.txt", abs_path);
104 	free(abs_path);
105 
106 	SAFE_MKDIR(cleanup, pathname, 0700);
107 
108 	fds[0] = SAFE_OPEN(cleanup, pathname, O_DIRECTORY);
109 	fds[1] = fds[0];
110 
111 	SAFE_FILE_PRINTF(cleanup, testfile, testfile);
112 	SAFE_FILE_PRINTF(cleanup, testfile2, testfile2);
113 
114 	fds[2] = SAFE_OPEN(cleanup, testfile3, O_CREAT | O_RDWR, 0600);
115 
116 	fds[3] = 100;
117 	fds[4] = AT_FDCWD;
118 
119 	filenames[0] = filenames[2] = filenames[3] = filenames[4] = testfile;
120 	filenames[1] = testfile3;
121 
122 	TEST_PAUSE;
123 }
124 
cleanup(void)125 void cleanup(void)
126 {
127 	if (fds[0] > 0)
128 		close(fds[0]);
129 	if (fds[2] > 0)
130 		close(fds[2]);
131 
132 	free(testfile3);
133 	tst_rmdir();
134 }
135