plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 22 | * fsync03.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 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> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 32 | * fsync03 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 33 | * 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 Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 49 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | |
| 51 | void setup(void); |
| 52 | void cleanup(void); |
| 53 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 54 | int fd[2]; /* fd's for the pipe() call in setup() */ |
| 55 | int pfd; /* holds the value for fd[1] */ |
| 56 | int bfd = -1; /* an invalid fd */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 59 | int *fd; |
| 60 | int error; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | } TC[] = { |
| 62 | /* EBADF - fd is invalid (-1) */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 63 | { |
| 64 | &bfd, EBADF}, |
| 65 | /* EINVAL - fsync() on pipe should not succeed. */ |
| 66 | { |
| 67 | &pfd, EINVAL} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 70 | char *TCID = "fsync03"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | int TST_TOTAL = 2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 73 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 75 | int lc; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | int i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 77 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 79 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 80 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | setup(); |
| 84 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 86 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 87 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
| 89 | /* loop through the test cases */ |
| 90 | for (i = 0; i < TST_TOTAL; i++) { |
| 91 | |
| 92 | TEST(fsync(*(TC[i].fd))); |
| 93 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | if (TEST_RETURN != -1) { |
| 95 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 96 | continue; |
| 97 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | 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); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | cleanup(); |
| 111 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 112 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* |
| 116 | * setup() - performs all ONE TIME setup for this test. |
| 117 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 118 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 120 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 122 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | 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 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | /* |
| 136 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 137 | * completion or premature exit. |
| 138 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 139 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
| 142 | /* delete the test directory created in setup() */ |
| 143 | tst_rmdir(); |
| 144 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 145 | } |