1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4f08c3bdfSopenharmony_ci * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
5f08c3bdfSopenharmony_ci * Mountain View, CA  94043, or:
6f08c3bdfSopenharmony_ci *
7f08c3bdfSopenharmony_ci * http://www.sgi.com
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * For further information regarding this notice, see:
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * AUTHOR            : William Roske
14f08c3bdfSopenharmony_ci * CO-PILOT          : Dave Fenner
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci/*\
18f08c3bdfSopenharmony_ci * [Description]
19f08c3bdfSopenharmony_ci *
20f08c3bdfSopenharmony_ci * Basic test for fcntl(2) using F_GETLK argument.
21f08c3bdfSopenharmony_ci *
22f08c3bdfSopenharmony_ci * If the lock could be placed, fcntl() does not actually place it, but
23f08c3bdfSopenharmony_ci * returns F_UNLCK in the l_type field of lock and leaves the other field
24f08c3bdfSopenharmony_ci * of the structure unchanged.
25f08c3bdfSopenharmony_ci */
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#include <stdio.h>
28f08c3bdfSopenharmony_ci#include <sys/types.h>
29f08c3bdfSopenharmony_ci#include <fcntl.h>
30f08c3bdfSopenharmony_ci#include <unistd.h>
31f08c3bdfSopenharmony_ci#include <sys/stat.h>
32f08c3bdfSopenharmony_ci#include "tst_test.h"
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_cistatic int fd = -1, pid;
35f08c3bdfSopenharmony_cistatic struct flock flocks;
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cistatic void verify_fcntl(void)
38f08c3bdfSopenharmony_ci{
39f08c3bdfSopenharmony_ci	/* F_GETLK will change flock.l_type to F_UNLCK, so need to reset */
40f08c3bdfSopenharmony_ci	flocks.l_type = F_RDLCK;
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	TST_EXP_PASS(fcntl(fd, F_GETLK, &flocks), "fcntl(%d, F_GETLK, &flocks)", fd);
43f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(flocks.l_type, F_UNLCK);
44f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(flocks.l_whence, SEEK_CUR);
45f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(flocks.l_start, 0);
46f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(flocks.l_len, 0);
47f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(flocks.l_pid, pid);
48f08c3bdfSopenharmony_ci}
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_cistatic void setup(void)
51f08c3bdfSopenharmony_ci{
52f08c3bdfSopenharmony_ci	pid = getpid();
53f08c3bdfSopenharmony_ci	fd = SAFE_OPEN("filename", O_RDWR | O_CREAT, 0700);
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	flocks.l_whence = SEEK_CUR;
56f08c3bdfSopenharmony_ci	flocks.l_start = 0;
57f08c3bdfSopenharmony_ci	flocks.l_len = 0;
58f08c3bdfSopenharmony_ci	flocks.l_pid = pid;
59f08c3bdfSopenharmony_ci}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_cistatic void cleanup(void)
62f08c3bdfSopenharmony_ci{
63f08c3bdfSopenharmony_ci	if (fd > -1)
64f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
65f08c3bdfSopenharmony_ci}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_cistatic struct tst_test test = {
68f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
69f08c3bdfSopenharmony_ci	.test_all = verify_fcntl,
70f08c3bdfSopenharmony_ci	.setup = setup,
71f08c3bdfSopenharmony_ci	.cleanup = cleanup,
72f08c3bdfSopenharmony_ci};
73