xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/socketcall/socketcall01.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) Wipro Technologies Ltd, 2002.  All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2016 Cyril Hrubis <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2017-2020
6*49cdfc7eSAndroid Build Coastguard Worker  * Author: Sowmya Adiga <[email protected]>
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * This is a basic test for the socketcall(2) system call.
9*49cdfc7eSAndroid Build Coastguard Worker  */
10*49cdfc7eSAndroid Build Coastguard Worker 
11*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <linux/net.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/un.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
20*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t {
23*49cdfc7eSAndroid Build Coastguard Worker 	int call;
24*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long args[3];
25*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
26*49cdfc7eSAndroid Build Coastguard Worker } TC[] = {
27*49cdfc7eSAndroid Build Coastguard Worker 	{SYS_SOCKET, {PF_INET, SOCK_STREAM, 0}, "TCP stream"},
28*49cdfc7eSAndroid Build Coastguard Worker 	{SYS_SOCKET, {PF_UNIX, SOCK_DGRAM, 0}, "unix domain dgram"},
29*49cdfc7eSAndroid Build Coastguard Worker 	{SYS_SOCKET, {AF_INET, SOCK_RAW, 6}, "Raw socket"},
30*49cdfc7eSAndroid Build Coastguard Worker 	{SYS_SOCKET, {PF_INET, SOCK_DGRAM, 17}, "UDP dgram"}
31*49cdfc7eSAndroid Build Coastguard Worker };
32*49cdfc7eSAndroid Build Coastguard Worker 
verify_socketcall(unsigned int i)33*49cdfc7eSAndroid Build Coastguard Worker void verify_socketcall(unsigned int i)
34*49cdfc7eSAndroid Build Coastguard Worker {
35*49cdfc7eSAndroid Build Coastguard Worker 	TEST(tst_syscall(__NR_socketcall, TC[i].call, TC[i].args));
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET < 0) {
38*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "socketcall() for %s failed with %li",
39*49cdfc7eSAndroid Build Coastguard Worker 			TC[i].desc, TST_RET);
40*49cdfc7eSAndroid Build Coastguard Worker 		return;
41*49cdfc7eSAndroid Build Coastguard Worker 	}
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "socketcall() for %s", TC[i].desc);
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(TST_RET);
46*49cdfc7eSAndroid Build Coastguard Worker }
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
49*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_socketcall,
50*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(TC),
51*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
52*49cdfc7eSAndroid Build Coastguard Worker };
53