• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  * 06/2017 modified by Xiao Yang <yangx.jy@cn.fujitsu.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * Further, this software is distributed without any warranty that it is
14  * free of the rightful claim of any third person regarding infringement
15  * or the like.  Any license provided herein, whether implied or
16  * otherwise, applies only to this software file.  Patent licenses, if
17  * any, provided herein do not apply to combinations of this program with
18  * other software, or any other product whatsoever.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 /*
25  * DESCRIPTION
26  * 1) lseek(2) fails and sets errno to EBADF when fd is invalid.
27  * 2) lseek(2) fails ans sets errno to EINVAL when whence is invalid.
28  * 3) lseek(2) fails and sets errno to ESPIPE when fd is associated
29  *    with a pipe or FIFO.
30  */
31 
32 #include <errno.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <sys/stat.h>
37 #include "tst_test.h"
38 
39 #define TFILE "tfile"
40 #define TFIFO1 "tfifo1"
41 #define TFIFO2 "tfifo2"
42 
43 static int bad_fd = -1;
44 static int fd, pfd1, pfd2;
45 static int pfds[2];
46 
47 static struct tcase {
48 	int *fd;
49 	int whence;
50 	int exp_err;
51 } tcases[] = {
52 	{&bad_fd, SEEK_SET, EBADF},
53 	{&bad_fd, SEEK_CUR, EBADF},
54 	{&bad_fd, SEEK_END, EBADF},
55 	{&fd, 5, EINVAL},
56 	{&fd, -1, EINVAL},
57 	{&fd, 7, EINVAL},
58 	{&pfd1, SEEK_SET, ESPIPE},
59 	{&pfd1, SEEK_CUR, ESPIPE},
60 	{&pfd1, SEEK_END, ESPIPE},
61 	{&pfds[0], SEEK_SET, ESPIPE},
62 	{&pfds[0], SEEK_CUR, ESPIPE},
63 	{&pfds[0], SEEK_END, ESPIPE},
64 	{&pfd2, SEEK_SET, ESPIPE},
65 	{&pfd2, SEEK_CUR, ESPIPE},
66 	{&pfd2, SEEK_END, ESPIPE},
67 };
68 
verify_lseek(unsigned int n)69 static void verify_lseek(unsigned int n)
70 {
71 	struct tcase *tc = &tcases[n];
72 
73 	TEST(lseek(*tc->fd, (off_t) 1, tc->whence));
74 	if (TST_RET != (off_t) -1) {
75 		tst_res(TFAIL, "lseek(%d, 1, %d) succeeded unexpectedly",
76 			*tc->fd, tc->whence);
77 			return;
78 	}
79 
80 	if (TST_ERR == tc->exp_err) {
81 		tst_res(TPASS | TTERRNO, "lseek(%d, 1, %d) failed as expected",
82 			*tc->fd, tc->whence);
83 	} else {
84 		tst_res(TFAIL | TTERRNO, "lseek(%d, 1, %d) failed "
85 			"unexpectedly, expected %s", *tc->fd, tc->whence,
86 			tst_strerrno(tc->exp_err));
87 	}
88 }
89 
setup(void)90 static void setup(void)
91 {
92 	fd = SAFE_OPEN(TFILE, O_RDWR | O_CREAT, 0777);
93 	SAFE_MKFIFO(TFIFO1, 0777);
94 	pfd1 = SAFE_OPEN(TFIFO1, O_RDWR, 0777);
95 	SAFE_PIPE(pfds);
96 	SAFE_MKNOD(TFIFO2, S_IFIFO | 0777, 0);
97 	pfd2 = SAFE_OPEN(TFIFO2, O_RDWR, 0777);
98 }
99 
cleanup(void)100 static void cleanup(void)
101 {
102 	if (fd > 0)
103 		SAFE_CLOSE(fd);
104 
105 	if (pfd1 > 0)
106 		SAFE_CLOSE(pfd1);
107 
108 	if (pfds[0] > 0)
109 		SAFE_CLOSE(pfds[0]);
110 
111 	if (pfds[1] > 0)
112 		SAFE_CLOSE(pfds[1]);
113 
114 	if (pfd2 > 0)
115 		SAFE_CLOSE(pfd2);
116 }
117 
118 static struct tst_test test = {
119 	.setup = setup,
120 	.cleanup = cleanup,
121 	.tcnt = ARRAY_SIZE(tcases),
122 	.test = verify_lseek,
123 	.needs_tmpdir = 1,
124 };
125