1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 6f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by 7f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 8f08c3bdfSopenharmony_ci * (at your option) any later version. 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 11f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 12f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13f08c3bdfSopenharmony_ci * the GNU General Public License for more details. 14f08c3bdfSopenharmony_ci * 15f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License 16f08c3bdfSopenharmony_ci * along with this program; if not, write to the Free Software 17f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18f08c3bdfSopenharmony_ci */ 19f08c3bdfSopenharmony_ci 20f08c3bdfSopenharmony_ci/* 21f08c3bdfSopenharmony_ci * NAME 22f08c3bdfSopenharmony_ci * fcntl06.c 23f08c3bdfSopenharmony_ci * 24f08c3bdfSopenharmony_ci * DESCRIPTION 25f08c3bdfSopenharmony_ci * Error checking conditions for remote locking of regions of a file. 26f08c3bdfSopenharmony_ci * 27f08c3bdfSopenharmony_ci * CALLS 28f08c3bdfSopenharmony_ci * open(2), write(2), fcntl(2) 29f08c3bdfSopenharmony_ci * 30f08c3bdfSopenharmony_ci * ALGORITHM 31f08c3bdfSopenharmony_ci * Test unlocking sections around a write lock using remote Lock/Unlock 32f08c3bdfSopenharmony_ci * call which should all fail. 33f08c3bdfSopenharmony_ci * 34f08c3bdfSopenharmony_ci * USAGE 35f08c3bdfSopenharmony_ci * fcntl06 36f08c3bdfSopenharmony_ci * 37f08c3bdfSopenharmony_ci * HISTORY 38f08c3bdfSopenharmony_ci * 07/2001 Ported by Wayne Boyer 39f08c3bdfSopenharmony_ci * 40f08c3bdfSopenharmony_ci * RESTRICTIONS 41f08c3bdfSopenharmony_ci * Currently Linux kernel doesn't implement R_GETLK/R_SETLK facility, 42f08c3bdfSopenharmony_ci * but this facility seems to be present in other standard flavours of 43f08c3bdfSopenharmony_ci * Unix. Currently this program has all the testing done under 44f08c3bdfSopenharmony_ci * "#ifdef LINUX_FILE_REGION_LOCK", when Linux implements the regions 45f08c3bdfSopenharmony_ci * locking then, this testcase should be recompiled accordingly with the 46f08c3bdfSopenharmony_ci * "ifdef" removed. 47f08c3bdfSopenharmony_ci */ 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_ci#include <fcntl.h> 50f08c3bdfSopenharmony_ci#include <errno.h> 51f08c3bdfSopenharmony_ci#include <signal.h> 52f08c3bdfSopenharmony_ci#include <sys/types.h> 53f08c3bdfSopenharmony_ci#include <sys/stat.h> 54f08c3bdfSopenharmony_ci#include "test.h" 55f08c3bdfSopenharmony_ci 56f08c3bdfSopenharmony_ci#define F_RGETLK 10 /* kludge code */ 57f08c3bdfSopenharmony_ci#define F_RSETLK 11 /* kludge code */ 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_cichar *TCID = "fcntl06"; 60f08c3bdfSopenharmony_ciint TST_TOTAL = 1; 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_civoid setup(); 63f08c3bdfSopenharmony_civoid cleanup(); 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_ci#define STRINGSIZE 27 66f08c3bdfSopenharmony_ci#define STRING "abcdefghijklmnopqrstuvwxyz\n" 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_ciint fd; 69f08c3bdfSopenharmony_civoid unlock_file(); 70f08c3bdfSopenharmony_ciint do_lock(int, short, short, int, int); 71f08c3bdfSopenharmony_ci 72f08c3bdfSopenharmony_ciint main(int ac, char **av) 73f08c3bdfSopenharmony_ci{ 74f08c3bdfSopenharmony_ci int fail = 0; 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_ci tst_parse_opts(ac, av, NULL, NULL); 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_ci setup(); /* global setup */ 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_ci fail = 0; 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_ci#ifdef LINUX_FILE_REGION_LOCK 83f08c3bdfSopenharmony_ci if (fcntl(fd, F_RGETLK, &tl) == -1) { 84f08c3bdfSopenharmony_ci if (errno == EINVAL) 85f08c3bdfSopenharmony_ci tst_brkm(TCONF, cleanup, 86f08c3bdfSopenharmony_ci "fcntl remote locking feature not implemented in " 87f08c3bdfSopenharmony_ci "the kernel"); 88f08c3bdfSopenharmony_ci else { 89f08c3bdfSopenharmony_ci /* 90f08c3bdfSopenharmony_ci * FIXME (garrcoop): having it always pass on 91f08c3bdfSopenharmony_ci * non-EINVAL is a bad test. 92f08c3bdfSopenharmony_ci */ 93f08c3bdfSopenharmony_ci tst_resm(TPASS, "fcntl on file failed"); 94f08c3bdfSopenharmony_ci } 95f08c3bdfSopenharmony_ci } 96f08c3bdfSopenharmony_ci 97f08c3bdfSopenharmony_ci /* 98f08c3bdfSopenharmony_ci * Add a write lock to the middle of the file and unlock a section 99f08c3bdfSopenharmony_ci * just before the lock 100f08c3bdfSopenharmony_ci */ 101f08c3bdfSopenharmony_ci if (do_lock(F_RSETLK, F_WRLCK, 0, 10, 5) == -1) 102f08c3bdfSopenharmony_ci tst_resm(TFAIL, "F_RSETLK WRLCK failed"); 103f08c3bdfSopenharmony_ci 104f08c3bdfSopenharmony_ci if (do_lock(F_RSETLK, F_UNLCK, 0, 5, 5) == -1) 105f08c3bdfSopenharmony_ci tst_resm(TFAIL | TERRNO, "F_RSETLK UNLOCK failed"); 106f08c3bdfSopenharmony_ci 107f08c3bdfSopenharmony_ci unlock_file(); 108f08c3bdfSopenharmony_ci#else 109f08c3bdfSopenharmony_ci tst_resm(TCONF, "system doesn't have LINUX_LOCK_FILE_REGION support"); 110f08c3bdfSopenharmony_ci#endif 111f08c3bdfSopenharmony_ci 112f08c3bdfSopenharmony_ci cleanup(); 113f08c3bdfSopenharmony_ci tst_exit(); 114f08c3bdfSopenharmony_ci} 115f08c3bdfSopenharmony_ci 116f08c3bdfSopenharmony_civoid setup(void) 117f08c3bdfSopenharmony_ci{ 118f08c3bdfSopenharmony_ci char *buf = STRING; 119f08c3bdfSopenharmony_ci char template[PATH_MAX]; 120f08c3bdfSopenharmony_ci 121f08c3bdfSopenharmony_ci tst_sig(FORK, DEF_HANDLER, cleanup); 122f08c3bdfSopenharmony_ci 123f08c3bdfSopenharmony_ci umask(0); 124f08c3bdfSopenharmony_ci 125f08c3bdfSopenharmony_ci TEST_PAUSE; 126f08c3bdfSopenharmony_ci 127f08c3bdfSopenharmony_ci tst_tmpdir(); 128f08c3bdfSopenharmony_ci 129f08c3bdfSopenharmony_ci snprintf(template, PATH_MAX, "fcntl06XXXXXX"); 130f08c3bdfSopenharmony_ci 131f08c3bdfSopenharmony_ci if ((fd = mkstemp(template)) == -1) 132f08c3bdfSopenharmony_ci tst_resm(TBROK | TERRNO, "mkstemp failed"); 133f08c3bdfSopenharmony_ci 134f08c3bdfSopenharmony_ci if (write(fd, buf, STRINGSIZE) == -1) 135f08c3bdfSopenharmony_ci tst_resm(TBROK | TERRNO, "write failed"); 136f08c3bdfSopenharmony_ci} 137f08c3bdfSopenharmony_ci 138f08c3bdfSopenharmony_ciint do_lock(int cmd, short type, short whence, int start, int len) 139f08c3bdfSopenharmony_ci{ 140f08c3bdfSopenharmony_ci struct flock fl; 141f08c3bdfSopenharmony_ci 142f08c3bdfSopenharmony_ci fl.l_type = type; 143f08c3bdfSopenharmony_ci fl.l_whence = whence; 144f08c3bdfSopenharmony_ci fl.l_start = start; 145f08c3bdfSopenharmony_ci fl.l_len = len; 146f08c3bdfSopenharmony_ci return (fcntl(fd, cmd, &fl)); 147f08c3bdfSopenharmony_ci} 148f08c3bdfSopenharmony_ci 149f08c3bdfSopenharmony_civoid unlock_file(void) 150f08c3bdfSopenharmony_ci{ 151f08c3bdfSopenharmony_ci if (do_lock(F_RSETLK, (short)F_UNLCK, (short)0, 0, 0) == -1) { 152f08c3bdfSopenharmony_ci /* Same as FIXME comment above. */ 153f08c3bdfSopenharmony_ci tst_resm(TPASS | TERRNO, "fcntl on file failed"); 154f08c3bdfSopenharmony_ci } 155f08c3bdfSopenharmony_ci} 156f08c3bdfSopenharmony_ci 157f08c3bdfSopenharmony_civoid cleanup(void) 158f08c3bdfSopenharmony_ci{ 159f08c3bdfSopenharmony_ci 160f08c3bdfSopenharmony_ci if (close(fd) == -1) 161f08c3bdfSopenharmony_ci tst_resm(TWARN | TERRNO, "close failed"); 162f08c3bdfSopenharmony_ci 163f08c3bdfSopenharmony_ci tst_rmdir(); 164f08c3bdfSopenharmony_ci 165f08c3bdfSopenharmony_ci} 166