Skip to content

Commit 0253495

Browse files
Radoslav Kolev via ltpmetan-ucw
authored andcommitted
Convert dup03 to new API and clean up
Signed-off-by: Radoslav Kolev <[email protected]> Reviewed-by: Cyril Hrubis <[email protected]>
1 parent b8a5222 commit 0253495

File tree

1 file changed

+46
-183
lines changed
  • testcases/kernel/syscalls/dup

1 file changed

+46
-183
lines changed

testcases/kernel/syscalls/dup/dup03.c

Lines changed: 46 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -1,212 +1,75 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
12
/*
23
* Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3-
*
4-
* This program is free software; you can redistribute it and/or modify it
5-
* under the terms of version 2 of the GNU General Public License as
6-
* published by the Free Software Foundation.
7-
*
8-
* This program is distributed in the hope that it would be useful, but
9-
* WITHOUT ANY WARRANTY; without even the implied warranty of
10-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11-
*
12-
* Further, this software is distributed without any warranty that it is
13-
* free of the rightful claim of any third person regarding infringement
14-
* or the like. Any license provided herein, whether implied or
15-
* otherwise, applies only to this software file. Patent licenses, if
16-
* any, provided herein do not apply to combinations of this program with
17-
* other software, or any other product whatsoever.
18-
*
19-
* You should have received a copy of the GNU General Public License along
20-
* with this program; if not, write the Free Software Foundation, Inc.,
21-
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22-
*
23-
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24-
* Mountain View, CA 94043, or:
25-
*
26-
* http://www.sgi.com
27-
*
28-
* For further information regarding this notice, see:
29-
*
30-
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/
4+
* Copyright (c) 2020 SUSE LLC
315
*
326
*/
33-
/* $Id: dup03.c,v 1.5 2009/10/13 14:00:46 subrata_modak Exp $ */
34-
/**********************************************************
35-
*
36-
* OS Test - Silicon Graphics, Inc.
37-
*
38-
* TEST IDENTIFIER : dup03
39-
*
40-
* EXECUTED BY : anyone
41-
*
42-
* TEST TITLE : Negative test for dup(2) (too many fds)
43-
*
44-
* PARENT DOCUMENT : usctpl01
45-
*
46-
* TEST CASE TOTAL : 1
47-
*
48-
* WALL CLOCK TIME : 1
49-
*
50-
* CPU TYPES : ALL
51-
*
52-
*
53-
* DATE STARTED : 06/94
54-
*
55-
* INITIAL RELEASE : UNICOS 7.0
56-
*
57-
* TEST CASES
58-
*
59-
* 1.) dup(2) returns...(See Description)
60-
*
61-
* INPUT SPECIFICATIONS
62-
* The standard options for system call tests are accepted.
63-
* (See the parse_opts(3) man page).
64-
*
65-
* OUTPUT SPECIFICATIONS
66-
*$
67-
* DURATION
68-
* Terminates - with frequency and infinite modes.
69-
*
70-
* SIGNALS
71-
* Uses SIGUSR1 to pause before test if option set.
72-
* (See the parse_opts(3) man page).
7+
/*\
8+
* [DESCRIPTION]
9+
* Negative test for dup(2) (too many fds).
7310
*
74-
* RESOURCES
75-
* None
76-
*
77-
* ENVIRONMENTAL NEEDS
78-
* No run-time environmental needs.
79-
*
80-
* SPECIAL PROCEDURAL REQUIREMENTS
81-
* None
82-
*
83-
* INTERCASE DEPENDENCIES
84-
* None
85-
*
86-
* DETAILED DESCRIPTION
87-
*
88-
* Setup:
89-
* Setup signal handling.
90-
* Pause for SIGUSR1 if option specified.
91-
*
92-
* Test:
93-
* Loop if the proper options are given.
94-
* Execute system call
95-
* Check return code, if system call failed (return=-1)
96-
* Log the errno and Issue a FAIL message.
97-
* Otherwise, Issue a PASS message.
98-
*
99-
* Cleanup:
100-
* Print errno log and/or timing stats if options given
101-
*
102-
*
103-
*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
11+
* [ALGORITHM]
12+
* Open the maximum allowed number of file descriptors and then try to call
13+
* dup() once more and verify it fails with EMFILE.
14+
\*/
10415

105-
#ifndef _GNU_SOURCE
106-
#define _GNU_SOURCE
107-
#endif
108-
#include <sys/types.h>
109-
#include <fcntl.h>
110-
#include <errno.h>
111-
#include <string.h>
112-
#include <signal.h>
11316
#include <stdlib.h>
114-
#include "test.h"
115-
#include "safe_macros.h"
17+
#include "tst_test.h"
11618

117-
void setup();
118-
void cleanup();
19+
int *fd;
20+
int nfds;
11921

120-
char *TCID = "dup03";
121-
int TST_TOTAL = 1;
122-
123-
char filename[255];
124-
int *fd = NULL;
125-
int nfds = 0;
126-
127-
int main(int ac, char **av)
22+
static void run(void)
12823
{
129-
int lc;
130-
131-
tst_parse_opts(ac, av, NULL, NULL);
24+
TEST(dup(fd[0]));
13225

133-
setup();
134-
135-
for (lc = 0; TEST_LOOPING(lc); lc++) {
136-
137-
tst_count = 0;
138-
139-
TEST(dup(fd[0]));
140-
141-
if (TEST_RETURN == -1) {
142-
if (TEST_ERRNO == EMFILE)
143-
tst_resm(TPASS,
144-
"dup failed as expected with "
145-
"EMFILE");
146-
else
147-
tst_resm(TFAIL | TTERRNO,
148-
"dup failed unexpectedly");
149-
} else {
150-
tst_resm(TFAIL, "dup succeeded unexpectedly");
151-
152-
SAFE_CLOSE(cleanup, TEST_RETURN);
153-
}
26+
if (TST_RET < -1) {
27+
tst_res(TFAIL, "Invalid dup() return value %ld", TST_RET);
28+
return;
29+
}
15430

31+
if (TST_RET == -1) {
32+
if (TST_ERR == EMFILE)
33+
tst_res(TPASS | TERRNO, "dup() failed as expected");
34+
else
35+
tst_res(TFAIL | TERRNO, "dup() failed unexpectedly");
36+
return;
15537
}
15638

157-
cleanup();
158-
tst_exit();
39+
tst_res(TFAIL, "dup() succeeded unexpectedly");
40+
SAFE_CLOSE(TST_RET);
15941
}
16042

161-
void setup(void)
43+
static void setup(void)
16244
{
16345
long maxfds;
16446

16547
maxfds = sysconf(_SC_OPEN_MAX);
166-
/*
167-
* Read the errors section if you're so inclined to determine
168-
* why == -1 matters for errno.
169-
*/
170-
if (maxfds < 1)
171-
tst_brkm((maxfds == -1 ? TBROK | TERRNO : TBROK), NULL,
172-
"sysconf(_SC_OPEN_MAX) failed");
48+
if (maxfds == -1)
49+
tst_brk(TBROK, "sysconf(_SC_OPEN_MAX) failed");
17350

174-
fd = malloc(maxfds * sizeof(int));
175-
if (fd == NULL)
176-
tst_brkm(TBROK | TERRNO, NULL, "malloc failed");
177-
fd[0] = -1;
51+
fd = SAFE_MALLOC(maxfds * sizeof(int));
17852

179-
tst_sig(FORK, DEF_HANDLER, cleanup);
53+
fd[0] = SAFE_OPEN("dupfile", O_RDWR | O_CREAT, 0700);
18054

181-
TEST_PAUSE;
182-
183-
tst_tmpdir();
184-
185-
sprintf(filename, "dupfile");
186-
for (nfds = 1; nfds <= maxfds; nfds++)
187-
if ((fd[nfds - 1] =
188-
open(filename, O_RDWR | O_CREAT, 0700)) == -1) {
189-
if (errno == EMFILE)
190-
break;
191-
else
192-
tst_brkm(TBROK | TBROK, cleanup, "open failed");
193-
nfds--;
194-
}
195-
196-
if (nfds == 0)
197-
tst_brkm(TBROK, cleanup, "unable to open at least one file");
198-
if (nfds > maxfds)
199-
tst_brkm(TBROK, cleanup,
200-
"unable to open enough files to use all file descriptors, "
201-
"tried %ld", maxfds);
55+
for (nfds = 1; nfds < maxfds; nfds++) {
56+
fd[nfds] = SAFE_DUP(fd[0]);
57+
if (fd[nfds] >= maxfds - 1)
58+
break;
59+
}
20260
}
20361

204-
void cleanup(void)
62+
static void cleanup(void)
20563
{
20664
int i;
20765

208-
for (i = 0; i <= nfds; i++)
209-
close(fd[i]);
210-
211-
tst_rmdir();
66+
for (i = 0; i < nfds; i++)
67+
SAFE_CLOSE(fd[i]);
21268
}
69+
70+
static struct tst_test test = {
71+
.test_all = run,
72+
.setup = setup,
73+
.cleanup = cleanup,
74+
.needs_tmpdir = 1,
75+
};

0 commit comments

Comments
 (0)