1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved. 4 * Author: Vatsal Avasthi 5 * 6 * Test Description: 7 * 1) flock() returns -1 and sets error number to EBADF if the file descriptor 8 * is invalid. 9 * 2) flock() returns -1 and sets error number to EINVAL if the argument 10 * operation does not include LOCK_SH,LOCK_EX,LOCK_UN. 11 * 3) flock() returns -1 and sets error number to EINVAL if an invalid 12 * combination of locking modes is used i.e LOCK_SH with LOCK_EX 13 */ 14 15#include <errno.h> 16#include <sys/file.h> 17 18#include "tst_test.h" 19 20static int badfd = -1; 21static int fd; 22 23static struct tcase { 24 int *fd; 25 int operation; 26 int exp_err; 27} tcases[] = { 28 {&badfd, LOCK_SH, EBADF}, 29 {&fd, LOCK_NB, EINVAL}, 30 {&fd, LOCK_SH | LOCK_EX, EINVAL}, 31}; 32 33static void verify_flock(unsigned n) 34{ 35 struct tcase *tc = &tcases[n]; 36 37 fd = SAFE_OPEN("testfile", O_RDWR); 38 TEST(flock(*tc->fd, tc->operation)); 39 if (TST_RET == 0) { 40 tst_res(TFAIL | TTERRNO, "flock() succeeded unexpectedly"); 41 SAFE_CLOSE(fd); 42 return; 43 } 44 45 if (tc->exp_err == TST_ERR) { 46 tst_res(TPASS | TTERRNO, "flock() failed expectedly"); 47 } else { 48 tst_res(TFAIL | TTERRNO, "flock() failed unexpectedly, " 49 "expected %s", tst_strerrno(tc->exp_err)); 50 } 51 52 SAFE_CLOSE(fd); 53} 54 55static void setup(void) 56{ 57 int fd1; 58 59 fd1 = SAFE_OPEN("testfile", O_CREAT | O_TRUNC | O_RDWR, 0666); 60 SAFE_CLOSE(fd1); 61} 62 63static struct tst_test test = { 64 .tcnt = ARRAY_SIZE(tcases), 65 .test = verify_flock, 66 .needs_tmpdir = 1, 67 .setup = setup, 68}; 69