xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/socketcall/socketcall03.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) 2020 Yang Xu <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  * Author: Sowmya Adiga <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  *
7*49cdfc7eSAndroid Build Coastguard Worker  * This is a basic test for the socketcall(2) for bind(2) and listen(2).
8*49cdfc7eSAndroid Build Coastguard Worker  */
9*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
11*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <linux/net.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <sys/un.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_in si;
21*49cdfc7eSAndroid Build Coastguard Worker 
verify_socketcall(void)22*49cdfc7eSAndroid Build Coastguard Worker static void verify_socketcall(void)
23*49cdfc7eSAndroid Build Coastguard Worker {
24*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long args[3];
25*49cdfc7eSAndroid Build Coastguard Worker 	int s = -1;
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker 	s = SAFE_SOCKET(AF_INET, SOCK_STREAM, 6);
28*49cdfc7eSAndroid Build Coastguard Worker 	args[0] = s;
29*49cdfc7eSAndroid Build Coastguard Worker 	args[1] = (unsigned long)&si;
30*49cdfc7eSAndroid Build Coastguard Worker 	args[2] = sizeof(si);
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	TEST(tst_syscall(__NR_socketcall, SYS_BIND, args));
33*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET < 0)
34*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "socketcall() for bind call failed with %ld", TST_RET);
35*49cdfc7eSAndroid Build Coastguard Worker 	else
36*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "socketcall() for bind call passed, returned %ld", TST_RET);
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker 	args[1] = 1;
39*49cdfc7eSAndroid Build Coastguard Worker 	args[2] = 0;
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	TEST(tst_syscall(__NR_socketcall, SYS_LISTEN, args));
42*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET < 0)
43*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "socketcall() for listen call failed with %ld", TST_RET);
44*49cdfc7eSAndroid Build Coastguard Worker 	else
45*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "socketcall() for listen call passed, returned %ld", TST_RET);
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(s);
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)50*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker 	si.sin_family = AF_INET;
53*49cdfc7eSAndroid Build Coastguard Worker 	si.sin_addr.s_addr = htons(INADDR_ANY);
54*49cdfc7eSAndroid Build Coastguard Worker 	si.sin_port = 0;
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
58*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
59*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_socketcall,
60*49cdfc7eSAndroid Build Coastguard Worker };
61