1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *   07/2001 Ported by Wayne Boyer
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci/*
7f08c3bdfSopenharmony_ci * Description:
8f08c3bdfSopenharmony_ci * 1) lseek(2) fails and sets errno to EINVAL when whence is invalid.
9f08c3bdfSopenharmony_ci * 2) lseek(2) fails ans sets errno to EBADF when fd is not an open
10f08c3bdfSopenharmony_ci * file descriptor.
11f08c3bdfSopenharmony_ci */
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ci#ifndef _GNU_SOURCE
14f08c3bdfSopenharmony_ci#define _GNU_SOURCE
15f08c3bdfSopenharmony_ci#endif
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include <stdio.h>
18f08c3bdfSopenharmony_ci#include "tst_test.h"
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#define TEMP_FILE1 "tmp_file1"
21f08c3bdfSopenharmony_ci#define TEMP_FILE2 "tmp_file2"
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
24f08c3bdfSopenharmony_ci#define SEEK_TOP 10
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic int fd1;
27f08c3bdfSopenharmony_cistatic int fd2;
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_cistatic struct tcase {
30f08c3bdfSopenharmony_ci	int *fd;
31f08c3bdfSopenharmony_ci	int whence;
32f08c3bdfSopenharmony_ci	int exp_err;
33f08c3bdfSopenharmony_ci} tcases[] = {
34f08c3bdfSopenharmony_ci	{&fd1, SEEK_TOP, EINVAL},
35f08c3bdfSopenharmony_ci	{&fd2, SEEK_SET, EBADF},
36f08c3bdfSopenharmony_ci};
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_cistatic void verify_llseek(unsigned int n)
39f08c3bdfSopenharmony_ci{
40f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	TEST(lseek(*tc->fd, (loff_t) 1, tc->whence));
43f08c3bdfSopenharmony_ci	if (TST_RET != (off_t) -1) {
44f08c3bdfSopenharmony_ci		tst_res(TFAIL, "lseek(%d, 1, %d) succeeded unexpectedly (%ld)",
45f08c3bdfSopenharmony_ci			*tc->fd, tc->whence, TST_RET);
46f08c3bdfSopenharmony_ci		return;
47f08c3bdfSopenharmony_ci	}
48f08c3bdfSopenharmony_ci	if (TST_ERR == tc->exp_err) {
49f08c3bdfSopenharmony_ci		tst_res(TPASS | TTERRNO, "lseek(%d, 1, %d) failed as expected",
50f08c3bdfSopenharmony_ci			*tc->fd, tc->whence);
51f08c3bdfSopenharmony_ci	} else {
52f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "lseek(%d, 1, %d) failed "
53f08c3bdfSopenharmony_ci		        "unexpectedly, expected %s", *tc->fd, tc->whence,
54f08c3bdfSopenharmony_ci		        tst_strerrno(tc->exp_err));
55f08c3bdfSopenharmony_ci	}
56f08c3bdfSopenharmony_ci}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic void setup(void)
59f08c3bdfSopenharmony_ci{
60f08c3bdfSopenharmony_ci	fd1 = SAFE_OPEN(TEMP_FILE1, O_RDWR | O_CREAT, FILE_MODE);
61f08c3bdfSopenharmony_ci	fd2 = SAFE_OPEN(TEMP_FILE2, O_RDWR | O_CREAT, FILE_MODE);
62f08c3bdfSopenharmony_ci	close(fd2);
63f08c3bdfSopenharmony_ci}
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_cistatic struct tst_test test = {
66f08c3bdfSopenharmony_ci	.setup = setup ,
67f08c3bdfSopenharmony_ci	.needs_tmpdir = 1 ,
68f08c3bdfSopenharmony_ci	.test = verify_llseek,
69f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
70f08c3bdfSopenharmony_ci};
71