robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Bull S.A. 2001 |
| 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 |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Test Name: fcntl22 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, fcntl() fails with -1 and sets errno to EAGAIN when |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 25 | * Operation is prohibited by locks held by other processes. |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 26 | * |
| 27 | * Expected Result: |
| 28 | * fcntl() should fail with return value -1 and sets expected errno. |
| 29 | * |
| 30 | * Algorithm: |
| 31 | * Setup: |
| 32 | * Setup signal handling. |
| 33 | * Pause for SIGUSR1 if option specified. |
| 34 | * Create temporary directory. |
| 35 | * Create and open temporary file. |
| 36 | * Lock temporary file. |
| 37 | * |
| 38 | * Test: |
| 39 | * Loop if the proper options are given. |
| 40 | * Duplicate process |
| 41 | * Parent waits for child termination |
| 42 | * Child execute system call |
| 43 | * Check return code, if system call failed (return=-1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 44 | * if errno set == expected errno |
| 45 | * Issue sys call fails with expected return value and errno. |
| 46 | * Otherwise, |
| 47 | * Issue sys call fails with unexpected errno. |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 48 | * Otherwise, |
| 49 | * Issue sys call returns unexpected value. |
| 50 | * |
| 51 | * Cleanup: |
| 52 | * Print errno log and/or timing stats if options given |
| 53 | * |
| 54 | * Usage: <for command-line> |
| 55 | * fcntl22 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t] |
| 56 | * where, -c n : Run n copies concurrently. |
| 57 | * -e : Turn on errno logging. |
| 58 | * -f : Turn off functionality Testing. |
| 59 | * -i n : Execute test n times. |
| 60 | * -I x : Execute test for x seconds. |
| 61 | * -P x : Pause for x seconds between iterations. |
| 62 | * -t : Turn on syscall timing. |
| 63 | * |
| 64 | * HISTORY |
| 65 | * 06/2002 Ported by Jacky Malcles |
| 66 | * |
| 67 | * RESTRICTIONS: |
| 68 | * none. |
| 69 | * |
| 70 | */ |
| 71 | |
| 72 | #include <fcntl.h> |
| 73 | #include <errno.h> |
| 74 | #include <signal.h> |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 75 | #include <stdlib.h> |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 76 | #include <sys/types.h> |
| 77 | #include <sys/wait.h> |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 78 | #include "test.h" |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 79 | |
| 80 | int child_pid; |
| 81 | int file; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 82 | struct flock fl; /* struct flock for fcntl */ |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 83 | |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 84 | char *TCID = "fcntl22"; |
| 85 | int TST_TOTAL = 1; |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 86 | |
| 87 | void setup(void); |
| 88 | void cleanup(void); |
| 89 | |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 90 | int main(int ac, char **av) |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 91 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 92 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 93 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | char *test_desc; /* test specific error message */ |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 95 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 96 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 97 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* setup */ |
| 101 | setup(); |
| 102 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | /* Check for looping state if -i option is given */ |
| 104 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 105 | #ifdef __hpux |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | test_desc = "EACCES"; |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 107 | #else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 108 | test_desc = "EAGAIN"; |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 109 | #endif |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 110 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 111 | /* reset tst_count in case we are looping */ |
| 112 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | |
| 114 | /* duplicate process */ |
| 115 | if ((child_pid = FORK_OR_VFORK()) == 0) { |
| 116 | /* child */ |
| 117 | /* |
| 118 | * Call fcntl(2) with F_SETLK argument on file |
| 119 | */ |
| 120 | TEST(fcntl(file, F_SETLK, &fl)); |
| 121 | |
| 122 | /* Check return code from fcntl(2) */ |
| 123 | if (TEST_RETURN != -1) { |
subrata_modak | 358c3ee | 2009-10-26 14:55:46 +0000 | [diff] [blame] | 124 | tst_resm(TFAIL, "fcntl() returned %ld," |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 125 | "expected -1, errno=%d", TEST_RETURN, |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 126 | EAGAIN); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | } else { |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 128 | if (TEST_ERRNO == EAGAIN) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | tst_resm(TPASS, |
| 130 | "fcntl() fails with expected " |
| 131 | "error %s errno:%d", test_desc, |
| 132 | TEST_ERRNO); |
| 133 | } else { |
| 134 | tst_resm(TFAIL, "fcntl() fails, %s, " |
| 135 | "errno=%d, expected errno=%d", |
| 136 | test_desc, TEST_ERRNO, |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 137 | EAGAIN); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | /* end child */ |
| 141 | } else { |
| 142 | if (child_pid < 0) { |
| 143 | /* quit if fork fail */ |
| 144 | tst_resm(TFAIL, "Fork failed"); |
| 145 | cleanup(); |
| 146 | } else { |
| 147 | /* Parent waits for child termination */ |
| 148 | wait(0); |
| 149 | cleanup(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | } /* end for */ |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 154 | tst_exit(); |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* |
| 158 | * setup |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | * performs all ONE TIME setup for this test |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 160 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 161 | void setup(void) |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 162 | { |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 163 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 164 | tst_sig(FORK, DEF_HANDLER, cleanup); |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 165 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 166 | TEST_PAUSE; |
| 167 | |
| 168 | /* Make a temp dir and cd to it */ |
| 169 | tst_tmpdir(); |
| 170 | |
| 171 | /* create regular file */ |
| 172 | if ((file = creat("regfile", 0777)) == -1) { |
| 173 | tst_brkm(TBROK, cleanup, |
| 174 | "creat(regfile, 0777) failed, errno:%d %s", errno, |
| 175 | strerror(errno)); |
| 176 | } |
| 177 | /* struct lock */ |
| 178 | fl.l_type = F_WRLCK; |
| 179 | fl.l_whence = 0; |
| 180 | fl.l_start = 0; |
| 181 | fl.l_len = 0; |
| 182 | |
| 183 | /* lock file */ |
| 184 | if (fcntl(file, F_SETLK, &fl) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 185 | tst_resm(TFAIL | TERRNO, "fcntl on file %d failed", file); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 186 | } |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 187 | |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * cleanup() |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 192 | * performs all ONE TIME cleanup for this test at completion or |
| 193 | * premature exit |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 194 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 195 | void cleanup(void) |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 196 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 197 | /* |
| 198 | * print timing stats if that option was specified |
| 199 | * print errno log if that option was specified |
| 200 | */ |
| 201 | close(file); |
mridge | 22c9044 | 2004-08-25 15:26:18 +0000 | [diff] [blame] | 202 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 203 | tst_rmdir(); |
robbiew | 7f01a51 | 2002-07-08 17:06:30 +0000 | [diff] [blame] | 204 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 205 | } |