1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2014 Fujitsu Ltd.
3*49cdfc7eSAndroid Build Coastguard Worker * Author: Zeng Linggang <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify it
6*49cdfc7eSAndroid Build Coastguard Worker * under the terms of version 2 of the GNU General Public License as
7*49cdfc7eSAndroid Build Coastguard Worker * published by the Free Software Foundation.
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it would be useful, but
10*49cdfc7eSAndroid Build Coastguard Worker * WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License along
14*49cdfc7eSAndroid Build Coastguard Worker * with this program.
15*49cdfc7eSAndroid Build Coastguard Worker */
16*49cdfc7eSAndroid Build Coastguard Worker /*
17*49cdfc7eSAndroid Build Coastguard Worker * DESCRIPTION
18*49cdfc7eSAndroid Build Coastguard Worker * This test case will verify basic function of open(2) with the flags
19*49cdfc7eSAndroid Build Coastguard Worker * O_APPEND, O_NOATIME, O_CLOEXEC and O_LARGEFILE.
20*49cdfc7eSAndroid Build Coastguard Worker */
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <mntent.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
31*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
32*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
33*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/mount.h"
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker #define MNTPOINT "mntpoint"
36*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE MNTPOINT"/test_file"
37*49cdfc7eSAndroid Build Coastguard Worker #define LARGE_FILE "large_file"
38*49cdfc7eSAndroid Build Coastguard Worker
39*49cdfc7eSAndroid Build Coastguard Worker #define DIR_MODE 0755
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "open12";
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker static const char *device;
44*49cdfc7eSAndroid Build Coastguard Worker static unsigned int mount_flag, skip_noatime;
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
47*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
48*49cdfc7eSAndroid Build Coastguard Worker static void test_append(void);
49*49cdfc7eSAndroid Build Coastguard Worker static void test_noatime(void);
50*49cdfc7eSAndroid Build Coastguard Worker static void test_cloexec(void);
51*49cdfc7eSAndroid Build Coastguard Worker static void test_largefile(void);
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker static void (*test_func[])(void) = { test_append, test_noatime, test_cloexec,
54*49cdfc7eSAndroid Build Coastguard Worker test_largefile };
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = ARRAY_SIZE(test_func);
57*49cdfc7eSAndroid Build Coastguard Worker
main(int argc,char ** argv)58*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char **argv)
59*49cdfc7eSAndroid Build Coastguard Worker {
60*49cdfc7eSAndroid Build Coastguard Worker int lc;
61*49cdfc7eSAndroid Build Coastguard Worker int i;
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(argc, argv, NULL, NULL);
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker setup();
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); lc++) {
68*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
69*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < TST_TOTAL; i++)
70*49cdfc7eSAndroid Build Coastguard Worker (*test_func[i])();
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker cleanup();
74*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker
setup(void)77*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
78*49cdfc7eSAndroid Build Coastguard Worker {
79*49cdfc7eSAndroid Build Coastguard Worker const char *mount_flags[] = {"noatime", "relatime", NULL};
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker tst_sig(FORK, DEF_HANDLER, cleanup);
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker tst_tmpdir();
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(cleanup, MNTPOINT, DIR_MODE);
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker if (tst_path_has_mnt_flags(cleanup, NULL, mount_flags)) {
90*49cdfc7eSAndroid Build Coastguard Worker const char *fs_type;
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker fs_type = tst_dev_fs_type();
93*49cdfc7eSAndroid Build Coastguard Worker device = tst_acquire_device(cleanup);
94*49cdfc7eSAndroid Build Coastguard Worker
95*49cdfc7eSAndroid Build Coastguard Worker if (!device) {
96*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Failed to obtain block device");
97*49cdfc7eSAndroid Build Coastguard Worker skip_noatime = 1;
98*49cdfc7eSAndroid Build Coastguard Worker goto end;
99*49cdfc7eSAndroid Build Coastguard Worker }
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker tst_mkfs(cleanup, device, fs_type, NULL, NULL);
102*49cdfc7eSAndroid Build Coastguard Worker
103*49cdfc7eSAndroid Build Coastguard Worker SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, MS_STRICTATIME, NULL);
104*49cdfc7eSAndroid Build Coastguard Worker mount_flag = 1;
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker end:
108*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(cleanup, TEST_FILE, TEST_FILE);
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker
test_append(void)111*49cdfc7eSAndroid Build Coastguard Worker static void test_append(void)
112*49cdfc7eSAndroid Build Coastguard Worker {
113*49cdfc7eSAndroid Build Coastguard Worker off_t len1, len2;
114*49cdfc7eSAndroid Build Coastguard Worker
115*49cdfc7eSAndroid Build Coastguard Worker TEST(open(TEST_FILE, O_RDWR | O_APPEND, 0777));
116*49cdfc7eSAndroid Build Coastguard Worker
117*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN == -1) {
118*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL | TTERRNO, "open failed");
119*49cdfc7eSAndroid Build Coastguard Worker return;
120*49cdfc7eSAndroid Build Coastguard Worker }
121*49cdfc7eSAndroid Build Coastguard Worker
122*49cdfc7eSAndroid Build Coastguard Worker len1 = SAFE_LSEEK(cleanup, TEST_RETURN, 0, SEEK_CUR);
123*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(cleanup, SAFE_WRITE_ALL, TEST_RETURN, TEST_FILE,
124*49cdfc7eSAndroid Build Coastguard Worker sizeof(TEST_FILE));
125*49cdfc7eSAndroid Build Coastguard Worker len2 = SAFE_LSEEK(cleanup, TEST_RETURN, 0, SEEK_CUR);
126*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(cleanup, TEST_RETURN);
127*49cdfc7eSAndroid Build Coastguard Worker
128*49cdfc7eSAndroid Build Coastguard Worker if (len2 > len1)
129*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "test O_APPEND for open success");
130*49cdfc7eSAndroid Build Coastguard Worker else
131*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "test O_APPEND for open failed");
132*49cdfc7eSAndroid Build Coastguard Worker }
133*49cdfc7eSAndroid Build Coastguard Worker
test_noatime(void)134*49cdfc7eSAndroid Build Coastguard Worker static void test_noatime(void)
135*49cdfc7eSAndroid Build Coastguard Worker {
136*49cdfc7eSAndroid Build Coastguard Worker char read_buf;
137*49cdfc7eSAndroid Build Coastguard Worker struct stat old_stat, new_stat;
138*49cdfc7eSAndroid Build Coastguard Worker
139*49cdfc7eSAndroid Build Coastguard Worker if (skip_noatime) {
140*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TCONF,
141*49cdfc7eSAndroid Build Coastguard Worker "test O_NOATIME flag for open needs filesystems which "
142*49cdfc7eSAndroid Build Coastguard Worker "is mounted without noatime and relatime");
143*49cdfc7eSAndroid Build Coastguard Worker return;
144*49cdfc7eSAndroid Build Coastguard Worker }
145*49cdfc7eSAndroid Build Coastguard Worker
146*49cdfc7eSAndroid Build Coastguard Worker SAFE_STAT(cleanup, TEST_FILE, &old_stat);
147*49cdfc7eSAndroid Build Coastguard Worker
148*49cdfc7eSAndroid Build Coastguard Worker sleep(1);
149*49cdfc7eSAndroid Build Coastguard Worker
150*49cdfc7eSAndroid Build Coastguard Worker TEST(open(TEST_FILE, O_RDONLY | O_NOATIME, 0777));
151*49cdfc7eSAndroid Build Coastguard Worker
152*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN == -1) {
153*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL | TTERRNO, "open failed");
154*49cdfc7eSAndroid Build Coastguard Worker return;
155*49cdfc7eSAndroid Build Coastguard Worker }
156*49cdfc7eSAndroid Build Coastguard Worker SAFE_READ(cleanup, 1, TEST_RETURN, &read_buf, 1);
157*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(cleanup, TEST_RETURN);
158*49cdfc7eSAndroid Build Coastguard Worker SAFE_STAT(cleanup, TEST_FILE, &new_stat);
159*49cdfc7eSAndroid Build Coastguard Worker
160*49cdfc7eSAndroid Build Coastguard Worker if (old_stat.st_atime == new_stat.st_atime)
161*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "test O_NOATIME for open success");
162*49cdfc7eSAndroid Build Coastguard Worker else
163*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "test O_NOATIME for open failed");
164*49cdfc7eSAndroid Build Coastguard Worker }
165*49cdfc7eSAndroid Build Coastguard Worker
test_cloexec(void)166*49cdfc7eSAndroid Build Coastguard Worker static void test_cloexec(void)
167*49cdfc7eSAndroid Build Coastguard Worker {
168*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
169*49cdfc7eSAndroid Build Coastguard Worker int status;
170*49cdfc7eSAndroid Build Coastguard Worker char buf[20];
171*49cdfc7eSAndroid Build Coastguard Worker
172*49cdfc7eSAndroid Build Coastguard Worker TEST(open(TEST_FILE, O_RDWR | O_APPEND | O_CLOEXEC, 0777));
173*49cdfc7eSAndroid Build Coastguard Worker
174*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN == -1) {
175*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL | TTERRNO, "open failed");
176*49cdfc7eSAndroid Build Coastguard Worker return;
177*49cdfc7eSAndroid Build Coastguard Worker }
178*49cdfc7eSAndroid Build Coastguard Worker
179*49cdfc7eSAndroid Build Coastguard Worker sprintf(buf, "%ld", TEST_RETURN);
180*49cdfc7eSAndroid Build Coastguard Worker
181*49cdfc7eSAndroid Build Coastguard Worker pid = tst_fork();
182*49cdfc7eSAndroid Build Coastguard Worker if (pid < 0)
183*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
184*49cdfc7eSAndroid Build Coastguard Worker
185*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0) {
186*49cdfc7eSAndroid Build Coastguard Worker if (execlp("open12_child", "open12_child", buf, NULL))
187*49cdfc7eSAndroid Build Coastguard Worker exit(2);
188*49cdfc7eSAndroid Build Coastguard Worker }
189*49cdfc7eSAndroid Build Coastguard Worker
190*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(cleanup, TEST_RETURN);
191*49cdfc7eSAndroid Build Coastguard Worker
192*49cdfc7eSAndroid Build Coastguard Worker if (wait(&status) != pid)
193*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "wait() failed");
194*49cdfc7eSAndroid Build Coastguard Worker
195*49cdfc7eSAndroid Build Coastguard Worker if (WIFEXITED(status)) {
196*49cdfc7eSAndroid Build Coastguard Worker switch ((int8_t)WEXITSTATUS(status)) {
197*49cdfc7eSAndroid Build Coastguard Worker case 0:
198*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "test O_CLOEXEC for open success");
199*49cdfc7eSAndroid Build Coastguard Worker break;
200*49cdfc7eSAndroid Build Coastguard Worker case 1:
201*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "test O_CLOEXEC for open failed");
202*49cdfc7eSAndroid Build Coastguard Worker break;
203*49cdfc7eSAndroid Build Coastguard Worker default:
204*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "execlp() failed");
205*49cdfc7eSAndroid Build Coastguard Worker }
206*49cdfc7eSAndroid Build Coastguard Worker } else {
207*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup,
208*49cdfc7eSAndroid Build Coastguard Worker "open12_child exits with unexpected error");
209*49cdfc7eSAndroid Build Coastguard Worker }
210*49cdfc7eSAndroid Build Coastguard Worker }
211*49cdfc7eSAndroid Build Coastguard Worker
test_largefile(void)212*49cdfc7eSAndroid Build Coastguard Worker static void test_largefile(void)
213*49cdfc7eSAndroid Build Coastguard Worker {
214*49cdfc7eSAndroid Build Coastguard Worker int fd;
215*49cdfc7eSAndroid Build Coastguard Worker off_t offset;
216*49cdfc7eSAndroid Build Coastguard Worker
217*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(cleanup, LARGE_FILE,
218*49cdfc7eSAndroid Build Coastguard Worker O_LARGEFILE | O_RDWR | O_CREAT, 0777);
219*49cdfc7eSAndroid Build Coastguard Worker
220*49cdfc7eSAndroid Build Coastguard Worker offset = lseek(fd, 4.1*1024*1024*1024, SEEK_SET);
221*49cdfc7eSAndroid Build Coastguard Worker if (offset == -1)
222*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "lseek failed");
223*49cdfc7eSAndroid Build Coastguard Worker
224*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(cleanup, SAFE_WRITE_ALL, fd, LARGE_FILE,
225*49cdfc7eSAndroid Build Coastguard Worker sizeof(LARGE_FILE));
226*49cdfc7eSAndroid Build Coastguard Worker
227*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(cleanup, fd);
228*49cdfc7eSAndroid Build Coastguard Worker
229*49cdfc7eSAndroid Build Coastguard Worker TEST(open(LARGE_FILE, O_LARGEFILE | O_RDONLY, 0777));
230*49cdfc7eSAndroid Build Coastguard Worker
231*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN == -1) {
232*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "test O_LARGEFILE for open failed");
233*49cdfc7eSAndroid Build Coastguard Worker } else {
234*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "test O_LARGEFILE for open success");
235*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(cleanup, TEST_RETURN);
236*49cdfc7eSAndroid Build Coastguard Worker }
237*49cdfc7eSAndroid Build Coastguard Worker }
238*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)239*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
240*49cdfc7eSAndroid Build Coastguard Worker {
241*49cdfc7eSAndroid Build Coastguard Worker if (mount_flag && tst_umount(MNTPOINT) == -1)
242*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TWARN | TERRNO, NULL, "umount(2) failed");
243*49cdfc7eSAndroid Build Coastguard Worker
244*49cdfc7eSAndroid Build Coastguard Worker if (device)
245*49cdfc7eSAndroid Build Coastguard Worker tst_release_device(device);
246*49cdfc7eSAndroid Build Coastguard Worker
247*49cdfc7eSAndroid Build Coastguard Worker tst_rmdir();
248*49cdfc7eSAndroid Build Coastguard Worker }
249