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