blob: 2313879db8a42aeb823b693d910b272f66026bf0 [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
subrata_modak56207ce2009-03-23 13:35:39 +000022 * fcntl06.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * DESCRIPTION
subrata_modak56207ce2009-03-23 13:35:39 +000025 * Error checking conditions for remote locking of regions of a file.
plars865695b2001-08-27 22:15:12 +000026 *
27 * CALLS
subrata_modak56207ce2009-03-23 13:35:39 +000028 * open(2), write(2), fcntl(2)
plars865695b2001-08-27 22:15:12 +000029 *
30 * ALGORITHM
subrata_modak56207ce2009-03-23 13:35:39 +000031 * Test unlocking sections around a write lock using remote Lock/Unlock
32 * call which should all fail.
plars865695b2001-08-27 22:15:12 +000033 *
34 * USAGE
subrata_modak56207ce2009-03-23 13:35:39 +000035 * fcntl06
plars865695b2001-08-27 22:15:12 +000036 *
37 * HISTORY
38 * 07/2001 Ported by Wayne Boyer
39 *
40 * RESTRICTIONS
subrata_modak56207ce2009-03-23 13:35:39 +000041 * Currently Linux kernel doesn't implement R_GETLK/R_SETLK facility,
plars865695b2001-08-27 22:15:12 +000042 * 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>
robbiew4cf80962003-03-25 21:53:41 +000052#include <sys/types.h>
53#include <sys/stat.h>
54#include "test.h"
plars865695b2001-08-27 22:15:12 +000055
56#define F_RGETLK 10 /* kludge code */
57#define F_RSETLK 11 /* kludge code */
58
59char *TCID = "fcntl06";
60int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000061
62void setup();
63void cleanup();
64
65#define STRINGSIZE 27
66#define STRING "abcdefghijklmnopqrstuvwxyz\n"
67
68int fd;
plars865695b2001-08-27 22:15:12 +000069void unlock_file();
70int do_lock(int, short, short, int, int);
71
robbiew4cf80962003-03-25 21:53:41 +000072int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000073{
plars865695b2001-08-27 22:15:12 +000074 int fail = 0;
75
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020076 const char *msg;
plars865695b2001-08-27 22:15:12 +000077
Garrett Cooperc737f2f2010-12-18 19:58:34 -080078 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080079 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modakbdbaec52009-02-26 12:14:51 +000080
subrata_modak56207ce2009-03-23 13:35:39 +000081 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000082
plars865695b2001-08-27 22:15:12 +000083 fail = 0;
84
85#ifdef LINUX_FILE_REGION_LOCK
Garrett Cooperc737f2f2010-12-18 19:58:34 -080086 if (fcntl(fd, F_RGETLK, &tl) == -1) {
87 if (errno == EINVAL)
88 tst_brkm(TCONF, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +080089 "fcntl remote locking feature not implemented in "
90 "the kernel");
Garrett Cooperc737f2f2010-12-18 19:58:34 -080091 else {
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080092 /*
Garrett Cooperc737f2f2010-12-18 19:58:34 -080093 * FIXME (garrcoop): having it always pass on
94 * non-EINVAL is a bad test.
95 */
96 tst_resm(TPASS, "fcntl on file failed");
plars865695b2001-08-27 22:15:12 +000097 }
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 Cooperc737f2f2010-12-18 19:58:34 -0800104 if (do_lock(F_RSETLK, F_WRLCK, 0, 10, 5) == -1)
plars865695b2001-08-27 22:15:12 +0000105 tst_resm(TFAIL, "F_RSETLK WRLCK failed");
plars865695b2001-08-27 22:15:12 +0000106
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800107 if (do_lock(F_RSETLK, F_UNLCK, 0, 5, 5) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800108 tst_resm(TFAIL | TERRNO, "F_RSETLK UNLOCK failed");
plars865695b2001-08-27 22:15:12 +0000109
110 unlock_file();
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800111#else
112 tst_resm(TCONF, "system doesn't have LINUX_LOCK_FILE_REGION support");
plars865695b2001-08-27 22:15:12 +0000113#endif
114
plars865695b2001-08-27 22:15:12 +0000115 cleanup();
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800116 tst_exit();
plars865695b2001-08-27 22:15:12 +0000117}
118
Mike Frysingerc57fba52014-04-09 18:56:30 -0400119void setup(void)
plars865695b2001-08-27 22:15:12 +0000120{
121 char *buf = STRING;
robbiew83f259b2003-04-16 18:58:09 +0000122 char template[PATH_MAX];
123
plars865695b2001-08-27 22:15:12 +0000124 tst_sig(FORK, DEF_HANDLER, cleanup);
125
126 umask(0);
127
plars865695b2001-08-27 22:15:12 +0000128 TEST_PAUSE;
129
plars865695b2001-08-27 22:15:12 +0000130 tst_tmpdir();
131
robbiew83f259b2003-04-16 18:58:09 +0000132 snprintf(template, PATH_MAX, "fcntl06XXXXXX");
subrata_modakbdbaec52009-02-26 12:14:51 +0000133
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800134 if ((fd = mkstemp(template)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800135 tst_resm(TBROK | TERRNO, "mkstemp failed");
plars865695b2001-08-27 22:15:12 +0000136
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800137 if (write(fd, buf, STRINGSIZE) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800138 tst_resm(TBROK | TERRNO, "write failed");
plars865695b2001-08-27 22:15:12 +0000139}
140
subrata_modak56207ce2009-03-23 13:35:39 +0000141int do_lock(int cmd, short type, short whence, int start, int len)
plars865695b2001-08-27 22:15:12 +0000142{
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_modak56207ce2009-03-23 13:35:39 +0000149 return (fcntl(fd, cmd, &fl));
plars865695b2001-08-27 22:15:12 +0000150}
151
Mike Frysingerc57fba52014-04-09 18:56:30 -0400152void unlock_file(void)
plars865695b2001-08-27 22:15:12 +0000153{
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800154 if (do_lock(F_RSETLK, (short)F_UNLCK, (short)0, 0, 0) == -1) {
155 /* Same as FIXME comment above. */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800156 tst_resm(TPASS | TERRNO, "fcntl on file failed");
plars865695b2001-08-27 22:15:12 +0000157 }
158}
159
Mike Frysingerc57fba52014-04-09 18:56:30 -0400160void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000161{
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800162
163 if (close(fd) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800164 tst_resm(TWARN | TERRNO, "close failed");
Garrett Cooperc737f2f2010-12-18 19:58:34 -0800165
plars865695b2001-08-27 22:15:12 +0000166 tst_rmdir();
167
Chris Dearmanec6edca2012-10-17 19:54:01 -0700168}