1/*
2 * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 *
16 */
17/**********************************************************
18 *
19 *    TEST IDENTIFIER   : fdatasync01
20 *
21 *    EXECUTED BY       : Any user
22 *
23 *    TEST TITLE        : Basic test for fdatasync(2)
24 *
25 *    TEST CASE TOTAL   : 1
26 *
27 *    AUTHOR            : Madhu T L <madhu.tarikere@wipro.com>
28 *
29 *    SIGNALS
30 *	Uses SIGUSR1 to pause before test if option set.
31 *	(See the parse_opts(3) man page).
32 *
33 *    DESCRIPTION
34 *	This is a Phase I test for the fdatasync(2) system call.
35 *	It is intended to provide a limited exposure of the system call.
36 *
37 *	Setup:
38 *	  Setup signal handling.
39 *	  Pause for SIGUSR1 if option specified.
40 *	  Create a temp directory and cd to it
41 *	  Initialize filename and open it in write mode for each child process.
42 *
43 *	Test:
44 *	 Loop if the proper options are given.
45 *	  Execute system call
46 *	  Check return code, if system call failed (return=-1)
47 *		Issue FAIL message with errno.
48 *	  Otherwise, Issue PASS message.
49 *
50 *	Cleanup:
51 *	  Print errno log and/or timing stats if options given
52 *	  Remove temporary directory and all files in it.
53 *
54 * USAGE:  <for command-line>
55 *  fdatasync01 [-c n] [-e] [-f] [-h] [-i n] [-I x] [-p] [-P x] [-t]
56 *		where,  -c n : Run n copies concurrently.
57 *			-e   : Turn on errno logging.
58 *			-f   : Turn off functional testing
59 *			-h   : Show help screen
60 *			-i n : Execute test n times.
61 *			-I x : Execute test for x seconds.
62 *			-p   : Pause for SIGUSR1 before starting
63 *			-P x : Pause for x seconds between iterations.
64 *			-t   : Turn on syscall timing.
65 *
66 ****************************************************************/
67#include <errno.h>
68#include <sys/types.h>
69#include <sys/stat.h>
70#include <fcntl.h>
71#include <unistd.h>
72#include "test.h"
73
74static int fd;
75static char filename[30];
76static void setup(void);
77static void cleanup(void);
78
79char *TCID = "fdatasync01";
80int TST_TOTAL = 1;
81
82int main(int argc, char **argv)
83{
84	int lc;
85
86	tst_parse_opts(argc, argv, NULL, NULL);
87
88	setup();
89
90	for (lc = 0; TEST_LOOPING(lc); lc++) {
91
92		/* reset tst_count in case we are looping */
93		tst_count = 0;
94
95		/* Test the system call */
96		TEST(fdatasync(fd));
97
98		/* check return code */
99		if (TEST_RETURN == -1) {
100			tst_resm(TFAIL, "fdatasync() failed, errno=%d : %s",
101				 TEST_ERRNO, strerror(TEST_ERRNO));
102		} else {
103			/* No Functional verification yet */
104			tst_resm(TPASS, "fdatasync() successful");
105		}
106	}
107
108	/* perform global cleanup and exit */
109	cleanup();
110
111	tst_exit();
112
113}
114
115/* setup() - performs all ONE TIME setup for this test */
116void setup(void)
117{
118
119	tst_sig(NOFORK, DEF_HANDLER, cleanup);
120
121	/* Pause if that option was specified
122	 * TEST_PAUSE contains the code to fork the test with the -c option.
123	 */
124	TEST_PAUSE;
125
126	tst_tmpdir();
127
128	/* Initialize unique filename for each child process */
129	if (sprintf(filename, "fdatasync_%d", getpid()) <= 0) {
130		tst_brkm(TBROK, cleanup, "Failed to initialize filename");
131	}
132	if ((fd = open(filename, O_CREAT | O_WRONLY, 0777)) == -1) {	//mode must be specified when O_CREATE is in the flag
133		tst_brkm(TBROK, cleanup, "open() failed");
134	}
135	if ((write(fd, filename, strlen(filename) + 1)) == -1) {
136		tst_brkm(TBROK, cleanup, "write() failed");
137	}
138}
139
140/*
141 * cleanup()
142 *	performs all ONE TIME cleanup for this test at
143 *	completion or premature exit
144 */
145void cleanup(void)
146{
147	/*
148	 * print timing stats if that option was specified.
149	 * print errno log if that option was specified.
150	 */
151	close(fd);
152
153	tst_rmdir();
154
155}
156