blob: 6e44e2cf1838519a588d897212ae44aa779f27ad [file] [log] [blame]
vapierd13d74b2006-02-11 07:24:00 +00001/*
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
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
vapierd13d74b2006-02-11 07:24:00 +000015 *
16 */
17/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
vapierd13d74b2006-02-11 07:24:00 +000019 * TEST IDENTIFIER : fdatasync02
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
vapierd13d74b2006-02-11 07:24:00 +000021 * EXECUTED BY : Any user
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
vapierd13d74b2006-02-11 07:24:00 +000023 * TEST TITLE : Checking error conditions for fdatasync(2)
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
vapierd13d74b2006-02-11 07:24:00 +000025 * TEST CASE TOTAL : 2
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
vapierd13d74b2006-02-11 07:24:00 +000027 * AUTHOR : Madhu T L <madhu.tarikere@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
vapierd13d74b2006-02-11 07:24:00 +000029 * SIGNALS
30 * Uses SIGUSR1 to pause before test if option set.
31 * (See the parse_opts(3) man page).
32 *
33 * DESCRIPTION
34 * Verify that,
35 * 1. fdatasync(2) returns -1 and sets errno to EBADF for invalid
36 * file descriptor.
37 * 2. fdatasync(2) returns -1 and sets errno to EINVAL for file
38 * descriptor to a special file.
subrata_modak4bb656a2009-02-26 12:02:09 +000039 *
vapierd13d74b2006-02-11 07:24:00 +000040 * Setup:
41 * Setup signal handling.
42 * Set expected errnos for logging
43 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000044 *
vapierd13d74b2006-02-11 07:24:00 +000045 * Test:
46 * Loop if the proper options are given.
47 * Perform testcase specific setup (if needed)
48 * Execute system call
49 * Check return code and error number, if matching,
50 * Issue PASS message
51 * Otherwise,
52 * Issue FAIL message
53 * Perform testcase specific cleanup (if needed)
subrata_modak4bb656a2009-02-26 12:02:09 +000054 *
vapierd13d74b2006-02-11 07:24:00 +000055 * Cleanup:
56 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000057 *
vapierd13d74b2006-02-11 07:24:00 +000058 * USAGE: <for command-line>
59 * fdatasync02 [-c n] [-e] [-f] [-h] [-i n] [-I x] [-p] [-P x] [-t]
60 * where, -c n : Run n copies concurrently.
61 * -e : Turn on errno logging.
62 * -f : Turn off functional testing
63 * -h : Show help screen
64 * -i n : Execute test n times.
65 * -I x : Execute test for x seconds.
66 * -p : Pause for SIGUSR1 before starting
67 * -P x : Pause for x seconds between iterations.
68 * -t : Turn on syscall timing.
subrata_modak4bb656a2009-02-26 12:02:09 +000069 *
vapierd13d74b2006-02-11 07:24:00 +000070 ****************************************************************/
71
72#include <errno.h>
73#include <pwd.h>
74#include <sys/types.h>
75#include <sys/stat.h>
76#include <fcntl.h>
77#include <unistd.h>
78#include "test.h"
vapierd13d74b2006-02-11 07:24:00 +000079
vapierd13d74b2006-02-11 07:24:00 +000080#define EXP_RET_VAL -1
81#define SPL_FILE "/dev/null"
82
subrata_modak56207ce2009-03-23 13:35:39 +000083struct test_case_t { /* test case structure */
84 int experrno; /* expected errno */
85 char *desc;
86 int (*setup) (void); /* Individual setup routine */
87 void (*cleanup) (void); /* Individual cleanup routine */
vapierd13d74b2006-02-11 07:24:00 +000088};
89
90char *TCID = "fdatasync02";
Wanlong Gao354ebb42012-12-07 10:10:04 +080091
vapierd13d74b2006-02-11 07:24:00 +000092static int testno;
93static int fd;
94
95static void setup(void);
96static void cleanup(void);
97static int setup1(void);
98static int setup2(void);
99static void cleanup2(void);
100
subrata_modak56207ce2009-03-23 13:35:39 +0000101static struct test_case_t tdat[] = {
102 {EBADF, "invalid file descriptor", setup1, NULL},
103 {EINVAL, "file descriptor to a special file", setup2, cleanup2},
vapierd13d74b2006-02-11 07:24:00 +0000104};
105
106int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
107
subrata_modak56207ce2009-03-23 13:35:39 +0000108int main(int argc, char **argv)
vapierd13d74b2006-02-11 07:24:00 +0000109{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200110 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200111 const char *msg;
vapierd13d74b2006-02-11 07:24:00 +0000112
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800114 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapierd13d74b2006-02-11 07:24:00 +0000115 }
116
117 setup();
118
vapierd13d74b2006-02-11 07:24:00 +0000119 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800120 /* reset tst_count in case we are looping */
121 tst_count = 0;
vapierd13d74b2006-02-11 07:24:00 +0000122
123 for (testno = 0; testno < TST_TOTAL; ++testno) {
subrata_modak56207ce2009-03-23 13:35:39 +0000124 if ((tdat[testno].setup) && (tdat[testno].setup())) {
vapierd13d74b2006-02-11 07:24:00 +0000125 /* setup() failed, skip this test */
126 continue;
127 }
128
129 /* Test the system call */
130 TEST(fdatasync(fd));
subrata_modak56207ce2009-03-23 13:35:39 +0000131 if ((TEST_RETURN == EXP_RET_VAL) &&
132 (TEST_ERRNO == tdat[testno].experrno)) {
vapierd13d74b2006-02-11 07:24:00 +0000133 tst_resm(TPASS, "Expected failure for %s, "
subrata_modak56207ce2009-03-23 13:35:39 +0000134 "errno: %d", tdat[testno].desc,
135 TEST_ERRNO);
vapierd13d74b2006-02-11 07:24:00 +0000136 } else {
137 tst_resm(TFAIL, "Unexpected results for %s ; "
subrata_modak358c3ee2009-10-26 14:55:46 +0000138 "returned %ld (expected %d), errno %d "
subrata_modak56207ce2009-03-23 13:35:39 +0000139 "(expected %d)", tdat[testno].desc,
140 TEST_RETURN, EXP_RET_VAL,
141 TEST_ERRNO, tdat[testno].experrno);
vapierd13d74b2006-02-11 07:24:00 +0000142 }
subrata_modak56207ce2009-03-23 13:35:39 +0000143 if (tdat[testno].cleanup) {
vapierd13d74b2006-02-11 07:24:00 +0000144 tdat[testno].cleanup();
145 }
146 }
147 }
148 cleanup();
149
Garrett Cooper53740502010-12-16 00:04:01 -0800150 tst_exit();
vapierd13d74b2006-02-11 07:24:00 +0000151}
152
subrata_modak56207ce2009-03-23 13:35:39 +0000153int setup1(void)
vapierd13d74b2006-02-11 07:24:00 +0000154{
155 fd = -1;
156 return 0;
157}
158
subrata_modak56207ce2009-03-23 13:35:39 +0000159int setup2(void)
vapierd13d74b2006-02-11 07:24:00 +0000160{
161 /* Open special file */
162 if ((fd = open(SPL_FILE, O_RDONLY)) == -1) {
163 tst_resm(TBROK, "Failed to open %s", SPL_FILE);
164 return 1;
subrata_modak4bb656a2009-02-26 12:02:09 +0000165 }
vapierd13d74b2006-02-11 07:24:00 +0000166 return 0;
167}
168
subrata_modak56207ce2009-03-23 13:35:39 +0000169void cleanup2(void)
vapierd13d74b2006-02-11 07:24:00 +0000170{
171 /* close special file */
172 if (close(fd) == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800173 tst_brkm(TBROK, NULL, "Failed to close fd of %s", SPL_FILE);
vapierd13d74b2006-02-11 07:24:00 +0000174 }
175}
176
vapierd13d74b2006-02-11 07:24:00 +0000177/*
178 * setup()
179 * performs all ONE TIME setup for this test
180 */
subrata_modak56207ce2009-03-23 13:35:39 +0000181void setup(void)
vapierd13d74b2006-02-11 07:24:00 +0000182{
Garrett Cooper2c282152010-12-16 00:55:50 -0800183
vapierd13d74b2006-02-11 07:24:00 +0000184 tst_sig(NOFORK, DEF_HANDLER, cleanup);
185
vapierd13d74b2006-02-11 07:24:00 +0000186 /* Pause if that option was specified
187 * TEST_PAUSE contains the code to fork the test with the -c option.
188 */
189 TEST_PAUSE;
190
191}
192
193/*
194 * cleanup()
195 * performs all ONE TIME cleanup for this test at
196 * completion or premature exit
197 */
subrata_modak56207ce2009-03-23 13:35:39 +0000198void cleanup(void)
vapierd13d74b2006-02-11 07:24:00 +0000199{
vapierd13d74b2006-02-11 07:24:00 +0000200
Wanlong Gao354ebb42012-12-07 10:10:04 +0800201}