plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2002 |
| 3 | * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz> |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 4 | * |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 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. |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 9 | * |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 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. |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 14 | * |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 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 |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 20 | /* |
| 21 | * This test verifies that flock cannot unlock a file locked |
| 22 | * by another task |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 23 | * |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 24 | * Test Steps: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 25 | * |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 26 | * 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 | */ |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 32 | |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 33 | #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" |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 41 | |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 42 | #define FILE_NAME "flock03" |
| 43 | |
| 44 | static void setup(void); |
| 45 | static void cleanup(void); |
| 46 | static void childfunc(int); |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 47 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 48 | #ifdef UCLINUX |
| 49 | static int fd_uc; |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 50 | static void childfunc_uc(void) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 51 | { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 52 | childfunc(fd_uc); |
| 53 | } |
| 54 | #endif |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 55 | |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 56 | char *TCID = "flock03"; |
| 57 | int TST_TOTAL = 3; |
| 58 | |
| 59 | static struct tst_checkpoint checkpoint; |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 60 | |
| 61 | int main(int argc, char **argv) |
| 62 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 63 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 64 | const char *msg; |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 65 | pid_t pid; |
| 66 | int status; |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 67 | int fd; |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 68 | |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 69 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 70 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 71 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 72 | #ifdef UCLINUX |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 73 | maybe_run_child(&childfunc_uc, "ds", &fd_uc, FILE_NAME); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 74 | #endif |
| 75 | |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 76 | setup(); |
| 77 | |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 78 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 79 | tst_count = 0; |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 80 | |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 81 | fd = open(FILE_NAME, O_RDWR); |
| 82 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | if (fd == -1) |
| 84 | tst_brkm(TFAIL, cleanup, "parent failed to open the" |
| 85 | "file, errno %d", errno); |
robbiew | 85d1bda | 2003-04-01 17:17:02 +0000 | [diff] [blame] | 86 | |
vapier | b6dcbc4 | 2006-02-16 04:36:30 +0000 | [diff] [blame] | 87 | pid = FORK_OR_VFORK(); |
Benoit Marcot | 12a9e94 | 2013-07-10 09:56:56 +0800 | [diff] [blame] | 88 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | if (pid == -1) |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 90 | tst_brkm(TFAIL, cleanup, "fork() failed, errno %d", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | errno); |
| 92 | if (pid == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 93 | #ifdef UCLINUX |
Benoit Marcot | 12a9e94 | 2013-07-10 09:56:56 +0800 | [diff] [blame] | 94 | if (self_exec(argv[0], "ds", fd, FILE_NAME) < 0) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 95 | tst_brkm(TFAIL, cleanup, "self_exec failed, " |
| 96 | "errno &d", errno); |
| 97 | #else |
robbiew | 85d1bda | 2003-04-01 17:17:02 +0000 | [diff] [blame] | 98 | childfunc(fd); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 99 | #endif |
| 100 | } |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 101 | |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 102 | TEST(flock(fd, LOCK_EX | LOCK_NB)); |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 103 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | if (TEST_RETURN != 0) |
| 105 | tst_resm(TFAIL, |
| 106 | "Parent: Initial attempt to flock() failed, " |
| 107 | "errno %d", TEST_ERRNO); |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 108 | else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | tst_resm(TPASS, |
| 110 | "Parent: Initial attempt to flock() passed"); |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 111 | |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 112 | TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint); |
| 113 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | if ((waitpid(pid, &status, 0)) < 0) { |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 115 | 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 122 | |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 123 | close(fd); |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 124 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 125 | } |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 126 | |
| 127 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 128 | tst_exit(); |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 131 | static void childfunc(int fd) |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 132 | { |
robbiew | 85d1bda | 2003-04-01 17:17:02 +0000 | [diff] [blame] | 133 | int fd2; |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 134 | |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 135 | TST_CHECKPOINT_CHILD_WAIT(&checkpoint); |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 136 | |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 137 | 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 | } |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 149 | |
| 150 | TEST(flock(fd, LOCK_UN)); |
robbiew | 85d1bda | 2003-04-01 17:17:02 +0000 | [diff] [blame] | 151 | /* XXX: LOCK_UN does not return an error if there was nothing to |
| 152 | * unlock. |
| 153 | */ |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 154 | 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 | } |
plars | b617b0f | 2002-12-04 15:49:45 +0000 | [diff] [blame] | 161 | |
robbiew | 85d1bda | 2003-04-01 17:17:02 +0000 | [diff] [blame] | 162 | TEST(flock(fd2, LOCK_EX | LOCK_NB)); |
Benoit Marcot | 12a9e94 | 2013-07-10 09:56:56 +0800 | [diff] [blame] | 163 | |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 164 | 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 Marcot | 12a9e94 | 2013-07-10 09:56:56 +0800 | [diff] [blame] | 171 | |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 172 | close(fd); |
robbiew | 85d1bda | 2003-04-01 17:17:02 +0000 | [diff] [blame] | 173 | close(fd2); |
Benoit Marcot | 12a9e94 | 2013-07-10 09:56:56 +0800 | [diff] [blame] | 174 | |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 175 | exit(0); |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 178 | static void setup(void) |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 179 | { |
robbiew | 85d1bda | 2003-04-01 17:17:02 +0000 | [diff] [blame] | 180 | int fd; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 181 | |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 182 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 183 | |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 184 | TEST_PAUSE; |
| 185 | |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 186 | tst_tmpdir(); |
| 187 | |
Jan Stancek | 4caa631 | 2014-10-06 14:57:56 +0200 | [diff] [blame] | 188 | TST_CHECKPOINT_CREATE(&checkpoint); |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 189 | |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 190 | fd = creat(FILE_NAME, 0666); |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 191 | if (fd < 0) { |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 192 | tst_resm(TBROK, "creating a new file failed"); |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 193 | cleanup(); |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 194 | } |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 195 | close(fd); |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 198 | static void cleanup(void) |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 199 | { |
plars | 5c1cc59 | 2002-12-03 23:03:54 +0000 | [diff] [blame] | 200 | tst_rmdir(); |
Cyril Hrubis | 8e684e8 | 2012-12-11 20:48:49 +0100 | [diff] [blame] | 201 | } |