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