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 |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 22 | * fcntl06.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 24 | * DESCRIPTION |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 25 | * Error checking conditions for remote locking of regions of a file. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | * |
| 27 | * CALLS |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 28 | * open(2), write(2), fcntl(2) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | * |
| 30 | * ALGORITHM |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 31 | * Test unlocking sections around a write lock using remote Lock/Unlock |
| 32 | * call which should all fail. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 33 | * |
| 34 | * USAGE |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 35 | * fcntl06 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 36 | * |
| 37 | * HISTORY |
| 38 | * 07/2001 Ported by Wayne Boyer |
| 39 | * |
| 40 | * RESTRICTIONS |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 41 | * Currently Linux kernel doesn't implement R_GETLK/R_SETLK facility, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | * but this facility seems to be present in other standard flavours of |
| 43 | * Unix. Currently this program has all the testing done under |
| 44 | * "#ifdef LINUX_FILE_REGION_LOCK", when Linux implements the regions |
| 45 | * locking then, this testcase should be recompiled accordingly with the |
| 46 | * "ifdef" removed. |
| 47 | */ |
| 48 | |
| 49 | #include <fcntl.h> |
| 50 | #include <errno.h> |
| 51 | #include <signal.h> |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 52 | #include <sys/types.h> |
| 53 | #include <sys/stat.h> |
| 54 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
| 56 | #define F_RGETLK 10 /* kludge code */ |
| 57 | #define F_RSETLK 11 /* kludge code */ |
| 58 | |
| 59 | char *TCID = "fcntl06"; |
| 60 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
| 62 | void setup(); |
| 63 | void cleanup(); |
| 64 | |
| 65 | #define STRINGSIZE 27 |
| 66 | #define STRING "abcdefghijklmnopqrstuvwxyz\n" |
| 67 | |
| 68 | int fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | void unlock_file(); |
| 70 | int do_lock(int, short, short, int, int); |
| 71 | |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 72 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | int fail = 0; |
| 75 | |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 76 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 78 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 79 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 80 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | fail = 0; |
| 84 | |
| 85 | #ifdef LINUX_FILE_REGION_LOCK |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 86 | if (fcntl(fd, F_RGETLK, &tl) == -1) { |
| 87 | if (errno == EINVAL) |
| 88 | tst_brkm(TCONF, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 89 | "fcntl remote locking feature not implemented in " |
| 90 | "the kernel"); |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 91 | else { |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 92 | /* |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 93 | * FIXME (garrcoop): having it always pass on |
| 94 | * non-EINVAL is a bad test. |
| 95 | */ |
| 96 | tst_resm(TPASS, "fcntl on file failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Add a write lock to the middle of the file and unlock a section |
| 102 | * just before the lock |
| 103 | */ |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 104 | if (do_lock(F_RSETLK, F_WRLCK, 0, 10, 5) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | tst_resm(TFAIL, "F_RSETLK WRLCK failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 107 | if (do_lock(F_RSETLK, F_UNLCK, 0, 5, 5) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 108 | tst_resm(TFAIL | TERRNO, "F_RSETLK UNLOCK failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | |
| 110 | unlock_file(); |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 111 | #else |
| 112 | tst_resm(TCONF, "system doesn't have LINUX_LOCK_FILE_REGION support"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | #endif |
| 114 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | cleanup(); |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 116 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 119 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | { |
| 121 | char *buf = STRING; |
robbiew | 83f259b | 2003-04-16 18:58:09 +0000 | [diff] [blame] | 122 | char template[PATH_MAX]; |
| 123 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 125 | |
| 126 | umask(0); |
| 127 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | TEST_PAUSE; |
| 129 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | tst_tmpdir(); |
| 131 | |
robbiew | 83f259b | 2003-04-16 18:58:09 +0000 | [diff] [blame] | 132 | snprintf(template, PATH_MAX, "fcntl06XXXXXX"); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 133 | |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 134 | if ((fd = mkstemp(template)) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 135 | tst_resm(TBROK | TERRNO, "mkstemp failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 137 | if (write(fd, buf, STRINGSIZE) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 138 | tst_resm(TBROK | TERRNO, "write failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | } |
| 140 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | int do_lock(int cmd, short type, short whence, int start, int len) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | { |
| 143 | struct flock fl; |
| 144 | |
| 145 | fl.l_type = type; |
| 146 | fl.l_whence = whence; |
| 147 | fl.l_start = start; |
| 148 | fl.l_len = len; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 149 | return (fcntl(fd, cmd, &fl)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 152 | void unlock_file(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | { |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 154 | if (do_lock(F_RSETLK, (short)F_UNLCK, (short)0, 0, 0) == -1) { |
| 155 | /* Same as FIXME comment above. */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 156 | tst_resm(TPASS | TERRNO, "fcntl on file failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 160 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | { |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 162 | |
| 163 | if (close(fd) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 164 | tst_resm(TWARN | TERRNO, "close failed"); |
Garrett Cooper | c737f2f | 2010-12-18 19:58:34 -0800 | [diff] [blame] | 165 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | tst_rmdir(); |
| 167 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 168 | } |