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 /*\
8 * [Description]
9 *
10 * Test that closing a file/pipe/socket works correctly.
11 */
12
13 #include <stdio.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <sys/stat.h>
17 #include "tst_test.h"
18
19 #define FILENAME "close01_testfile"
20
get_fd_file(void)21 static int get_fd_file(void)
22 {
23 return SAFE_OPEN(FILENAME, O_RDWR | O_CREAT, 0700);
24 }
25
get_fd_pipe(void)26 static int get_fd_pipe(void)
27 {
28 int pipefildes[2];
29 SAFE_PIPE(pipefildes);
30 SAFE_CLOSE(pipefildes[1]);
31 return pipefildes[0];
32 }
33
get_fd_socket(void)34 static int get_fd_socket(void)
35 {
36 return SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
37 }
38
39 struct test_case_t {
40 int (*get_fd) ();
41 char *type;
42 } tc[] = {
43 {get_fd_file, "file"},
44 {get_fd_pipe, "pipe"},
45 {get_fd_socket, "socket"}
46 };
47
run(unsigned int i)48 static void run(unsigned int i)
49 {
50 TST_EXP_PASS(close(tc[i].get_fd()), "close a %s fd", tc[i].type);
51 }
52
53 static struct tst_test test = {
54 .tcnt = ARRAY_SIZE(tc),
55 .needs_tmpdir = 1,
56 .test = run,
57 };
58