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