blob: aa38d8825315104f997c2b33fd96b1c484ab1e72 [file] [log] [blame]
plars5c1cc592002-12-03 23:03:54 +00001/*
Cyril Hrubis8e684e82012-12-11 20:48:49 +01002 * Copyright (c) International Business Machines Corp., 2002
3 * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz>
plars5c1cc592002-12-03 23:03:54 +00004 *
Cyril Hrubis8e684e82012-12-11 20:48:49 +01005 * 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.
plars5c1cc592002-12-03 23:03:54 +00009 *
Cyril Hrubis8e684e82012-12-11 20:48:49 +010010 * 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.
plars5c1cc592002-12-03 23:03:54 +000014 *
Cyril Hrubis8e684e82012-12-11 20:48:49 +010015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars5c1cc592002-12-03 23:03:54 +000018 */
19
Cyril Hrubis8e684e82012-12-11 20:48:49 +010020/*
21 * This test verifies that flock cannot unlock a file locked
22 * by another task
subrata_modak4bb656a2009-02-26 12:02:09 +000023 *
Cyril Hrubis8e684e82012-12-11 20:48:49 +010024 * Test Steps:
subrata_modak4bb656a2009-02-26 12:02:09 +000025 *
Cyril Hrubis8e684e82012-12-11 20:48:49 +010026 * Fork a child processes The parent flocks a file with LOCK_EX Child waits
27 * for that to happen, then checks to make sure it is locked. Child then
28 * tries to unlock the file. If the unlock succeeds, the child attempts to
29 * lock the file with LOCK_EX. The test passes if the child is able to lock
30 * the file.
31 */
plars5c1cc592002-12-03 23:03:54 +000032
plars5c1cc592002-12-03 23:03:54 +000033#include <stdio.h>
34#include <errno.h>
35#include <fcntl.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <sys/file.h>
39#include <sys/wait.h>
40#include "test.h"
plars5c1cc592002-12-03 23:03:54 +000041
Cyril Hrubis8e684e82012-12-11 20:48:49 +010042#define FILE_NAME "flock03"
43
44static void setup(void);
45static void cleanup(void);
46static void childfunc(int);
plars5c1cc592002-12-03 23:03:54 +000047
robbiewd34d5812005-07-11 22:28:09 +000048#ifdef UCLINUX
49static int fd_uc;
Cyril Hrubis8e684e82012-12-11 20:48:49 +010050static void childfunc_uc(void)
subrata_modak56207ce2009-03-23 13:35:39 +000051{
robbiewd34d5812005-07-11 22:28:09 +000052 childfunc(fd_uc);
53}
54#endif
plars5c1cc592002-12-03 23:03:54 +000055
Cyril Hrubis8e684e82012-12-11 20:48:49 +010056char *TCID = "flock03";
57int TST_TOTAL = 3;
58
59static struct tst_checkpoint checkpoint;
plars5c1cc592002-12-03 23:03:54 +000060
61int main(int argc, char **argv)
62{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020063 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020064 const char *msg;
plars5c1cc592002-12-03 23:03:54 +000065 pid_t pid;
66 int status;
Cyril Hrubis8e684e82012-12-11 20:48:49 +010067 int fd;
plars5c1cc592002-12-03 23:03:54 +000068
Cyril Hrubis8e684e82012-12-11 20:48:49 +010069 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080070 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Cyril Hrubis8e684e82012-12-11 20:48:49 +010071
robbiewd34d5812005-07-11 22:28:09 +000072#ifdef UCLINUX
Cyril Hrubis8e684e82012-12-11 20:48:49 +010073 maybe_run_child(&childfunc_uc, "ds", &fd_uc, FILE_NAME);
robbiewd34d5812005-07-11 22:28:09 +000074#endif
75
plars5c1cc592002-12-03 23:03:54 +000076 setup();
77
plars5c1cc592002-12-03 23:03:54 +000078 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080079 tst_count = 0;
plars5c1cc592002-12-03 23:03:54 +000080
Cyril Hrubis8e684e82012-12-11 20:48:49 +010081 fd = open(FILE_NAME, O_RDWR);
82
subrata_modak56207ce2009-03-23 13:35:39 +000083 if (fd == -1)
84 tst_brkm(TFAIL, cleanup, "parent failed to open the"
85 "file, errno %d", errno);
robbiew85d1bda2003-04-01 17:17:02 +000086
vapierb6dcbc42006-02-16 04:36:30 +000087 pid = FORK_OR_VFORK();
Benoit Marcot12a9e942013-07-10 09:56:56 +080088
subrata_modak56207ce2009-03-23 13:35:39 +000089 if (pid == -1)
plars5c1cc592002-12-03 23:03:54 +000090 tst_brkm(TFAIL, cleanup, "fork() failed, errno %d",
subrata_modak56207ce2009-03-23 13:35:39 +000091 errno);
92 if (pid == 0) {
robbiewd34d5812005-07-11 22:28:09 +000093#ifdef UCLINUX
Benoit Marcot12a9e942013-07-10 09:56:56 +080094 if (self_exec(argv[0], "ds", fd, FILE_NAME) < 0)
robbiewd34d5812005-07-11 22:28:09 +000095 tst_brkm(TFAIL, cleanup, "self_exec failed, "
96 "errno &d", errno);
97#else
robbiew85d1bda2003-04-01 17:17:02 +000098 childfunc(fd);
robbiewd34d5812005-07-11 22:28:09 +000099#endif
100 }
plars5c1cc592002-12-03 23:03:54 +0000101
plars5c1cc592002-12-03 23:03:54 +0000102 TEST(flock(fd, LOCK_EX | LOCK_NB));
Cyril Hrubis8e684e82012-12-11 20:48:49 +0100103
subrata_modak56207ce2009-03-23 13:35:39 +0000104 if (TEST_RETURN != 0)
105 tst_resm(TFAIL,
106 "Parent: Initial attempt to flock() failed, "
107 "errno %d", TEST_ERRNO);
plars5c1cc592002-12-03 23:03:54 +0000108 else
subrata_modak56207ce2009-03-23 13:35:39 +0000109 tst_resm(TPASS,
110 "Parent: Initial attempt to flock() passed");
plars5c1cc592002-12-03 23:03:54 +0000111
Cyril Hrubis8e684e82012-12-11 20:48:49 +0100112 TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint);
113
subrata_modak56207ce2009-03-23 13:35:39 +0000114 if ((waitpid(pid, &status, 0)) < 0) {
plars5c1cc592002-12-03 23:03:54 +0000115 tst_resm(TFAIL, "wait() failed");
116 continue;
117 }
118 if ((WIFEXITED(status)) && (WEXITSTATUS(status) == 0))
119 tst_resm(TPASS, "flock03 Passed");
120 else
121 tst_resm(TFAIL, "flock03 Failed");
subrata_modakbdbaec52009-02-26 12:14:51 +0000122
subrata_modakdad6e1a2007-10-30 10:46:58 +0000123 close(fd);
plars5c1cc592002-12-03 23:03:54 +0000124
Garrett Cooper2c282152010-12-16 00:55:50 -0800125 }
plars5c1cc592002-12-03 23:03:54 +0000126
127 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800128 tst_exit();
plars5c1cc592002-12-03 23:03:54 +0000129}
130
Cyril Hrubis8e684e82012-12-11 20:48:49 +0100131static void childfunc(int fd)
plars5c1cc592002-12-03 23:03:54 +0000132{
robbiew85d1bda2003-04-01 17:17:02 +0000133 int fd2;
plars5c1cc592002-12-03 23:03:54 +0000134
Cyril Hrubis8e684e82012-12-11 20:48:49 +0100135 TST_CHECKPOINT_CHILD_WAIT(&checkpoint);
plars5c1cc592002-12-03 23:03:54 +0000136
Cyril Hrubis8e684e82012-12-11 20:48:49 +0100137 fd2 = open(FILE_NAME, O_RDWR);
138
139 if (fd2 == -1) {
140 fprintf(stderr, "CHILD: failed to open the file: %s\n",
141 strerror(errno));
142 exit(1);
143 }
144
145 if (flock(fd2, LOCK_EX | LOCK_NB) != -1) {
146 fprintf(stderr, "CHILD: The file was not already locked\n");
147 exit(1);
148 }
plars5c1cc592002-12-03 23:03:54 +0000149
150 TEST(flock(fd, LOCK_UN));
robbiew85d1bda2003-04-01 17:17:02 +0000151 /* XXX: LOCK_UN does not return an error if there was nothing to
152 * unlock.
153 */
Cyril Hrubis8e684e82012-12-11 20:48:49 +0100154 if (TEST_RETURN == -1) {
155 fprintf(stderr, "CHILD: Unable to unlock file locked by "
156 "parent: %s\n", strerror(TEST_ERRNO));
157 exit(1);
158 } else {
159 fprintf(stderr, "CHILD: File locked by parent unlocked\n");
160 }
plarsb617b0f2002-12-04 15:49:45 +0000161
robbiew85d1bda2003-04-01 17:17:02 +0000162 TEST(flock(fd2, LOCK_EX | LOCK_NB));
Benoit Marcot12a9e942013-07-10 09:56:56 +0800163
Cyril Hrubis8e684e82012-12-11 20:48:49 +0100164 if (TEST_RETURN == -1) {
165 fprintf(stderr, "CHILD: Unable to lock file after "
166 "unlocking: %s\n", strerror(TEST_ERRNO));
167 exit(1);
168 } else {
169 fprintf(stderr, "CHILD: Locking after unlock passed\n");
170 }
Benoit Marcot12a9e942013-07-10 09:56:56 +0800171
subrata_modakdad6e1a2007-10-30 10:46:58 +0000172 close(fd);
robbiew85d1bda2003-04-01 17:17:02 +0000173 close(fd2);
Benoit Marcot12a9e942013-07-10 09:56:56 +0800174
Cyril Hrubis8e684e82012-12-11 20:48:49 +0100175 exit(0);
plars5c1cc592002-12-03 23:03:54 +0000176}
177
Cyril Hrubis8e684e82012-12-11 20:48:49 +0100178static void setup(void)
plars5c1cc592002-12-03 23:03:54 +0000179{
robbiew85d1bda2003-04-01 17:17:02 +0000180 int fd;
Garrett Cooper2c282152010-12-16 00:55:50 -0800181
plars5c1cc592002-12-03 23:03:54 +0000182 tst_sig(FORK, DEF_HANDLER, cleanup);
183
plars5c1cc592002-12-03 23:03:54 +0000184 TEST_PAUSE;
185
plars5c1cc592002-12-03 23:03:54 +0000186 tst_tmpdir();
187
Jan Stancek4caa6312014-10-06 14:57:56 +0200188 TST_CHECKPOINT_CREATE(&checkpoint);
plars5c1cc592002-12-03 23:03:54 +0000189
Cyril Hrubis8e684e82012-12-11 20:48:49 +0100190 fd = creat(FILE_NAME, 0666);
plars5c1cc592002-12-03 23:03:54 +0000191 if (fd < 0) {
Cyril Hrubis8e684e82012-12-11 20:48:49 +0100192 tst_resm(TBROK, "creating a new file failed");
Cyril Hrubis8e684e82012-12-11 20:48:49 +0100193 cleanup();
plars5c1cc592002-12-03 23:03:54 +0000194 }
subrata_modakdad6e1a2007-10-30 10:46:58 +0000195 close(fd);
plars5c1cc592002-12-03 23:03:54 +0000196}
197
Cyril Hrubis8e684e82012-12-11 20:48:49 +0100198static void cleanup(void)
plars5c1cc592002-12-03 23:03:54 +0000199{
plars5c1cc592002-12-03 23:03:54 +0000200 tst_rmdir();
Cyril Hrubis8e684e82012-12-11 20:48:49 +0100201}