+/** =================================================================
+ * test number 3
+ *
+ * Bug 24040: Two conflicting flocks from same process different fds should fail
+ * two conflicting flocks from different processes but same fs
+ * should succeed.
+ */
+int t3(int argc, char *argv[])
+{
+ int fd, fd2;
+ int pid;
+ int rc = EXIT_SUCCESS;
+
+ if (argc != 3) {
+ fprintf(stderr, "Usage: ./flocks_test 3 filename\n");
+ return EXIT_FAILURE;
+ }
+
+ if ((fd = open(argv[2], O_RDWR)) < 0) {
+ fprintf(stderr, "Couldn't open file: %s\n", argv[1]);
+ return EXIT_FAILURE;
+ }
+ if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
+ perror("first flock failed");
+ rc = EXIT_FAILURE;
+ goto out;
+ }
+ if ((fd2 = open(argv[2], O_RDWR)) < 0) {
+ fprintf(stderr, "Couldn't open file: %s\n", argv[1]);
+ rc = EXIT_FAILURE;
+ goto out;
+ }
+ if (flock(fd2, LOCK_EX | LOCK_NB) >= 0) {
+ fprintf(stderr, "Second flock succeeded - FAIL\n");
+ rc = EXIT_FAILURE;
+ close(fd2);
+ goto out;
+ }
+
+ close(fd2);
+
+ pid = fork();
+ if (pid == -1) {
+ perror("fork");
+ rc = EXIT_FAILURE;
+ goto out;
+ }
+
+ if (pid == 0) {
+ if ((fd2 = open(argv[2], O_RDWR)) < 0) {
+ fprintf(stderr, "Couldn't open file: %s\n", argv[1]);
+ rc = EXIT_FAILURE;
+ exit(rc);
+ }
+ if (flock(fd2, LOCK_EX | LOCK_NB) >= 0) {
+ fprintf(stderr, "Second flock succeeded - FAIL\n");
+ rc = EXIT_FAILURE;
+ goto out_child;
+ }
+ if (flock(fd, LOCK_UN) == -1) {
+ fprintf(stderr, "Child unlock on parent fd failed\n");
+ rc = EXIT_FAILURE;
+ goto out_child;
+ }
+ if (flock(fd2, LOCK_EX | LOCK_NB) == -1) {
+ fprintf(stderr, "Relock after parent unlock failed!\n");
+ rc = EXIT_FAILURE;
+ goto out_child;
+ }
+ out_child:
+ close(fd2);
+ exit(rc);
+ }
+
+ waitpid(pid, &rc, 0);
+out:
+ close(fd);
+ return rc;
+}
+
+