1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker * 06/2017 modified by Xiao Yang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker * DESCRIPTION
9*49cdfc7eSAndroid Build Coastguard Worker * 1) lseek(2) fails and sets errno to EBADF when fd is invalid.
10*49cdfc7eSAndroid Build Coastguard Worker * 2) lseek(2) fails ans sets errno to EINVAL when whence is invalid.
11*49cdfc7eSAndroid Build Coastguard Worker * 3) lseek(2) fails and sets errno to ESPIPE when fd is associated
12*49cdfc7eSAndroid Build Coastguard Worker * with a pipe or FIFO.
13*49cdfc7eSAndroid Build Coastguard Worker */
14*49cdfc7eSAndroid Build Coastguard Worker
15*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker #define TFILE "tfile"
23*49cdfc7eSAndroid Build Coastguard Worker #define TFIFO1 "tfifo1"
24*49cdfc7eSAndroid Build Coastguard Worker #define TFIFO2 "tfifo2"
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker static int bad_fd = -1;
27*49cdfc7eSAndroid Build Coastguard Worker static int fd, pfd1, pfd2;
28*49cdfc7eSAndroid Build Coastguard Worker static int pfds[2];
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
31*49cdfc7eSAndroid Build Coastguard Worker int *fd;
32*49cdfc7eSAndroid Build Coastguard Worker int whence;
33*49cdfc7eSAndroid Build Coastguard Worker int exp_err;
34*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
35*49cdfc7eSAndroid Build Coastguard Worker {&bad_fd, SEEK_SET, EBADF},
36*49cdfc7eSAndroid Build Coastguard Worker {&bad_fd, SEEK_CUR, EBADF},
37*49cdfc7eSAndroid Build Coastguard Worker {&bad_fd, SEEK_END, EBADF},
38*49cdfc7eSAndroid Build Coastguard Worker {&fd, 5, EINVAL},
39*49cdfc7eSAndroid Build Coastguard Worker {&fd, -1, EINVAL},
40*49cdfc7eSAndroid Build Coastguard Worker {&fd, 7, EINVAL},
41*49cdfc7eSAndroid Build Coastguard Worker {&pfd1, SEEK_SET, ESPIPE},
42*49cdfc7eSAndroid Build Coastguard Worker {&pfd1, SEEK_CUR, ESPIPE},
43*49cdfc7eSAndroid Build Coastguard Worker {&pfd1, SEEK_END, ESPIPE},
44*49cdfc7eSAndroid Build Coastguard Worker {&pfds[0], SEEK_SET, ESPIPE},
45*49cdfc7eSAndroid Build Coastguard Worker {&pfds[0], SEEK_CUR, ESPIPE},
46*49cdfc7eSAndroid Build Coastguard Worker {&pfds[0], SEEK_END, ESPIPE},
47*49cdfc7eSAndroid Build Coastguard Worker {&pfd2, SEEK_SET, ESPIPE},
48*49cdfc7eSAndroid Build Coastguard Worker {&pfd2, SEEK_CUR, ESPIPE},
49*49cdfc7eSAndroid Build Coastguard Worker {&pfd2, SEEK_END, ESPIPE},
50*49cdfc7eSAndroid Build Coastguard Worker };
51*49cdfc7eSAndroid Build Coastguard Worker
verify_lseek(unsigned int n)52*49cdfc7eSAndroid Build Coastguard Worker static void verify_lseek(unsigned int n)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker TEST(lseek(*tc->fd, (off_t) 1, tc->whence));
57*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != (off_t) -1) {
58*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "lseek(%d, 1, %d) succeeded unexpectedly",
59*49cdfc7eSAndroid Build Coastguard Worker *tc->fd, tc->whence);
60*49cdfc7eSAndroid Build Coastguard Worker return;
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == tc->exp_err) {
64*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "lseek(%d, 1, %d) failed as expected",
65*49cdfc7eSAndroid Build Coastguard Worker *tc->fd, tc->whence);
66*49cdfc7eSAndroid Build Coastguard Worker } else {
67*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "lseek(%d, 1, %d) failed "
68*49cdfc7eSAndroid Build Coastguard Worker "unexpectedly, expected %s", *tc->fd, tc->whence,
69*49cdfc7eSAndroid Build Coastguard Worker tst_strerrno(tc->exp_err));
70*49cdfc7eSAndroid Build Coastguard Worker }
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker
setup(void)73*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
74*49cdfc7eSAndroid Build Coastguard Worker {
75*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(TFILE, O_RDWR | O_CREAT, 0777);
76*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKFIFO(TFIFO1, 0777);
77*49cdfc7eSAndroid Build Coastguard Worker pfd1 = SAFE_OPEN(TFIFO1, O_RDWR, 0777);
78*49cdfc7eSAndroid Build Coastguard Worker SAFE_PIPE(pfds);
79*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKNOD(TFIFO2, S_IFIFO | 0777, 0);
80*49cdfc7eSAndroid Build Coastguard Worker pfd2 = SAFE_OPEN(TFIFO2, O_RDWR, 0777);
81*49cdfc7eSAndroid Build Coastguard Worker }
82*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)83*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
84*49cdfc7eSAndroid Build Coastguard Worker {
85*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
86*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
87*49cdfc7eSAndroid Build Coastguard Worker
88*49cdfc7eSAndroid Build Coastguard Worker if (pfd1 > 0)
89*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(pfd1);
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker if (pfds[0] > 0)
92*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(pfds[0]);
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker if (pfds[1] > 0)
95*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(pfds[1]);
96*49cdfc7eSAndroid Build Coastguard Worker
97*49cdfc7eSAndroid Build Coastguard Worker if (pfd2 > 0)
98*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(pfd2);
99*49cdfc7eSAndroid Build Coastguard Worker }
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
102*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
103*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
104*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
105*49cdfc7eSAndroid Build Coastguard Worker .test = verify_lseek,
106*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
107*49cdfc7eSAndroid Build Coastguard Worker };
108