blob: b89441260c9bb401bba631a28568abd746ebb8c7 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
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
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
nstrazfa31d552002-05-14 16:50:06 +000022 * fsync03.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * DESCRIPTION
25 * Testcase to check that fsync(2) sets errno correctly.
26 *
27 * ALGORITHM
28 * 1. Call fsync() with an invalid fd, and test for EBADF.
29 * 2. Call fsync() on a pipe(fd), and expect EINVAL.
30 *
31 * USAGE: <for command-line>
nstrazfa31d552002-05-14 16:50:06 +000032 * fsync03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000033 * where, -c n : Run n copies concurrently.
34 * -e : Turn on errno logging.
35 * -i n : Execute test n times.
36 * -I x : Execute test for x seconds.
37 * -P x : Pause for x seconds between iterations.
38 * -t : Turn on syscall timing.
39 *
40 * HISTORY
41 * 07/2001 Ported by Wayne Boyer
42 *
43 * RESTRICTIONS
44 * NONE
45 */
46
47#include <unistd.h>
48#include <errno.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080049#include "test.h"
plars865695b2001-08-27 22:15:12 +000050
51void setup(void);
52void cleanup(void);
53
subrata_modak56207ce2009-03-23 13:35:39 +000054int fd[2]; /* fd's for the pipe() call in setup() */
55int pfd; /* holds the value for fd[1] */
56int bfd = -1; /* an invalid fd */
plars865695b2001-08-27 22:15:12 +000057
plars865695b2001-08-27 22:15:12 +000058struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000059 int *fd;
60 int error;
plars865695b2001-08-27 22:15:12 +000061} TC[] = {
62 /* EBADF - fd is invalid (-1) */
subrata_modak56207ce2009-03-23 13:35:39 +000063 {
64 &bfd, EBADF},
65 /* EINVAL - fsync() on pipe should not succeed. */
66 {
67 &pfd, EINVAL}
plars865695b2001-08-27 22:15:12 +000068};
69
nstrazfa31d552002-05-14 16:50:06 +000070char *TCID = "fsync03";
plars865695b2001-08-27 22:15:12 +000071int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +000072
plars74948ad2002-11-14 16:16:14 +000073int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000074{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020075 int lc;
plars865695b2001-08-27 22:15:12 +000076 int i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020077 const char *msg;
plars865695b2001-08-27 22:15:12 +000078
Garrett Cooper45e285d2010-11-22 12:19:25 -080079 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080080 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000081 }
82
83 setup();
84
plars865695b2001-08-27 22:15:12 +000085 for (lc = 0; TEST_LOOPING(lc); lc++) {
86
Caspar Zhangd59a6592013-03-07 14:59:12 +080087 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000088
89 /* loop through the test cases */
90 for (i = 0; i < TST_TOTAL; i++) {
91
92 TEST(fsync(*(TC[i].fd)));
93
subrata_modak56207ce2009-03-23 13:35:39 +000094 if (TEST_RETURN != -1) {
95 tst_resm(TFAIL, "call succeeded unexpectedly");
96 continue;
97 }
plars865695b2001-08-27 22:15:12 +000098
subrata_modak56207ce2009-03-23 13:35:39 +000099 if (TEST_ERRNO == TC[i].error) {
100 tst_resm(TPASS, "expected failure - "
101 "errno = %d : %s", TEST_ERRNO,
102 strerror(TEST_ERRNO));
103 } else {
104 tst_resm(TFAIL, "unexpected error - %d : %s - "
105 "expected %d", TEST_ERRNO,
106 strerror(TEST_ERRNO), TC[i].error);
plars865695b2001-08-27 22:15:12 +0000107 }
108 }
109 }
110 cleanup();
111
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800112 tst_exit();
plars865695b2001-08-27 22:15:12 +0000113}
114
115/*
116 * setup() - performs all ONE TIME setup for this test.
117 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400118void setup(void)
plars865695b2001-08-27 22:15:12 +0000119{
Garrett Cooper2c282152010-12-16 00:55:50 -0800120
plars865695b2001-08-27 22:15:12 +0000121 tst_sig(NOFORK, DEF_HANDLER, cleanup);
122
plars865695b2001-08-27 22:15:12 +0000123 TEST_PAUSE;
124
125 /* make a temporary directory and cd to it */
126 tst_tmpdir();
127
128 if (pipe(fd) < 0) {
129 tst_brkm(TBROK, cleanup, "pipe call failed");
130 }
131
132 pfd = fd[1];
133}
134
plars865695b2001-08-27 22:15:12 +0000135/*
136 * cleanup() - performs all ONE TIME cleanup for this test at
137 * completion or premature exit.
138 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400139void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000140{
plars865695b2001-08-27 22:15:12 +0000141
142 /* delete the test directory created in setup() */
143 tst_rmdir();
144
Chris Dearmanec6edca2012-10-17 19:54:01 -0700145}