blob: 0549e1226204df402c55dc033ca843817e5cccea [file] [log] [blame]
vapier6eeb1512006-08-24 04:18:31 +00001/******************************************************************************
2 *
3 * Copyright (c) International Business Machines Corp., 2006
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the 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, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
vapier6eeb1512006-08-24 04:18:31 +000018 *
19 * NAME
20 * futimesat01.c
21 *
22 * DESCRIPTION
23 * This test case will verify basic function of futimesat
24 * added by kernel 2.6.16 or up.
25 *
vapier6eeb1512006-08-24 04:18:31 +000026 * Author
subrata_modak4bb656a2009-02-26 12:02:09 +000027 * Yi Yang <yyangcdl@cn.ibm.com>
vapier6eeb1512006-08-24 04:18:31 +000028 *
29 * History
30 * 08/24/2006 Created first by Yi Yang <yyangcdl@cn.ibm.com>
31 *
32 *****************************************************************************/
33
34#define _GNU_SOURCE
35
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <sys/time.h>
39#include <fcntl.h>
40#include <error.h>
41#include <stdlib.h>
42#include <errno.h>
43#include <string.h>
44#include <signal.h>
45#include "test.h"
vapier9cf96d82006-09-10 09:54:16 +000046#include "linux_syscall_numbers.h"
vapier6eeb1512006-08-24 04:18:31 +000047
48#define TEST_CASES 5
49#ifndef AT_FDCWD
50#define AT_FDCWD -100
51#endif
52void setup();
53void cleanup();
54void setup_every_copy();
55
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020056char *TCID = "futimesat01";
57int TST_TOTAL = TEST_CASES;
vapier6eeb1512006-08-24 04:18:31 +000058char pathname[256];
59char testfile[256];
60char testfile2[256];
61char testfile3[256];
62int dirfd, fd, ret;
63int fds[TEST_CASES];
64char *filenames[TEST_CASES];
65int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, 0 };
Wanlong Gao354ebb42012-12-07 10:10:04 +080066
vapier6eeb1512006-08-24 04:18:31 +000067struct timeval times[2];
68
69int myfutimesat(int dirfd, const char *filename, struct timeval *times)
70{
Jan Stancek359980f2013-02-15 10:16:05 +010071 return ltp_syscall(__NR_futimesat, dirfd, filename, times);
vapier6eeb1512006-08-24 04:18:31 +000072}
73
74int main(int ac, char **av)
75{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020076 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020077 const char *msg;
vapier6eeb1512006-08-24 04:18:31 +000078 int i;
79
Wanlong Gao354ebb42012-12-07 10:10:04 +080080 /* Disable test if the version of the kernel is less than 2.6.16 */
81 if ((tst_kvercmp(2, 6, 16)) < 0) {
82 tst_resm(TWARN, "This test can only run on kernels that are ");
83 tst_resm(TWARN, "2.6.16 and higher");
84 exit(0);
85 }
mreed10807cfe52006-09-18 19:03:19 +000086
Garrett Cooper45e285d2010-11-22 12:19:25 -080087 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080088 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapier6eeb1512006-08-24 04:18:31 +000089
vapier6eeb1512006-08-24 04:18:31 +000090 setup();
91
vapier6eeb1512006-08-24 04:18:31 +000092 for (lc = 0; TEST_LOOPING(lc); lc++) {
93 setup_every_copy();
94
Caspar Zhangd59a6592013-03-07 14:59:12 +080095 tst_count = 0;
vapier6eeb1512006-08-24 04:18:31 +000096
vapier6eeb1512006-08-24 04:18:31 +000097 for (i = 0; i < TST_TOTAL; i++) {
98 gettimeofday(&times[0], NULL);
99 gettimeofday(&times[1], NULL);
100 TEST(myfutimesat(fds[i], filenames[i], times));
101
vapier6eeb1512006-08-24 04:18:31 +0000102 if (TEST_ERRNO == expected_errno[i]) {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200103 tst_resm(TPASS,
104 "futimesat() returned the expected errno %d: %s",
105 TEST_ERRNO,
106 strerror(TEST_ERRNO));
vapier6eeb1512006-08-24 04:18:31 +0000107 } else {
vapier6eeb1512006-08-24 04:18:31 +0000108 tst_resm(TFAIL,
109 "futimesat() Failed, errno=%d : %s",
110 TEST_ERRNO, strerror(TEST_ERRNO));
111 }
112 }
113
Garrett Cooper2c282152010-12-16 00:55:50 -0800114 }
vapier6eeb1512006-08-24 04:18:31 +0000115
vapier6eeb1512006-08-24 04:18:31 +0000116 cleanup();
Cyril Hrubis4dff8542014-09-23 12:11:59 +0200117 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800118}
vapier6eeb1512006-08-24 04:18:31 +0000119
Mike Frysingerc57fba52014-04-09 18:56:30 -0400120void setup_every_copy(void)
vapier6eeb1512006-08-24 04:18:31 +0000121{
122 /* Initialize test dir and file names */
123 sprintf(pathname, "futimesattestdir%d", getpid());
124 sprintf(testfile, "futimesattestfile%d.txt", getpid());
125 sprintf(testfile2, "futimesattestdir%d/futimesattestfile%d.txt",
126 getpid(), getpid());
127 sprintf(testfile3, "/tmp/futimesattestfile%d.txt", getpid());
128
129 ret = mkdir(pathname, 0700);
130 if (ret < 0) {
131 perror("mkdir: ");
132 exit(-1);
133 }
134
135 dirfd = open(pathname, O_DIRECTORY);
136 if (dirfd < 0) {
137 perror("open: ");
138 exit(-1);
139 }
140
141 fd = open(testfile, O_CREAT | O_RDWR, 0600);
142 if (fd < 0) {
143 perror("open: ");
144 exit(-1);
145 }
146
147 fd = open(testfile2, O_CREAT | O_RDWR, 0600);
148 if (fd < 0) {
149 perror("open: ");
150 exit(-1);
151 }
152
153 fd = open(testfile3, O_CREAT | O_RDWR, 0600);
154 if (fd < 0) {
155 perror("open: ");
156 exit(-1);
157 }
158
159 fds[0] = fds[1] = dirfd;
160 fds[2] = fd;
161 fds[3] = 100;
162 fds[4] = AT_FDCWD;
163
164 filenames[0] = filenames[2] = filenames[3] = filenames[4] = testfile;
165 filenames[1] = testfile3;
166}
167
Mike Frysingerc57fba52014-04-09 18:56:30 -0400168void setup(void)
vapier6eeb1512006-08-24 04:18:31 +0000169{
vapier6eeb1512006-08-24 04:18:31 +0000170 tst_sig(NOFORK, DEF_HANDLER, cleanup);
vapier6eeb1512006-08-24 04:18:31 +0000171 TEST_PAUSE;
Garrett Cooper2c282152010-12-16 00:55:50 -0800172}
vapier6eeb1512006-08-24 04:18:31 +0000173
Mike Frysingerc57fba52014-04-09 18:56:30 -0400174void cleanup(void)
vapier6eeb1512006-08-24 04:18:31 +0000175{
vapier6eeb1512006-08-24 04:18:31 +0000176 unlink(testfile2);
177 unlink(testfile3);
178 unlink(testfile);
179 rmdir(pathname);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700180}