xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getsockopt/getsockopt02.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) 2017 Red Hat, Inc.
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Test getsockopt(2) for retrieving peer credentials (SO_PEERCRED).
10*49cdfc7eSAndroid Build Coastguard Worker  */
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker static int socket_fd, accepted;
19*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_un sun;
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #define SOCKNAME	"testsocket"
22*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)23*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
24*49cdfc7eSAndroid Build Coastguard Worker {
25*49cdfc7eSAndroid Build Coastguard Worker 	sun.sun_family = AF_UNIX;
26*49cdfc7eSAndroid Build Coastguard Worker 	(void)strcpy(sun.sun_path, SOCKNAME);
27*49cdfc7eSAndroid Build Coastguard Worker 	socket_fd = SAFE_SOCKET(sun.sun_family, SOCK_STREAM, 0);
28*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_BIND(socket_fd, (struct sockaddr *)&sun, sizeof(sun));
29*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LISTEN(socket_fd, SOMAXCONN);
30*49cdfc7eSAndroid Build Coastguard Worker }
31*49cdfc7eSAndroid Build Coastguard Worker 
fork_func(void)32*49cdfc7eSAndroid Build Coastguard Worker static void fork_func(void)
33*49cdfc7eSAndroid Build Coastguard Worker {
34*49cdfc7eSAndroid Build Coastguard Worker 	int fork_socket_fd = SAFE_SOCKET(sun.sun_family, SOCK_STREAM, 0);
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CONNECT(fork_socket_fd, (struct sockaddr *)&sun, sizeof(sun));
37*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAIT(0);
38*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fork_socket_fd);
39*49cdfc7eSAndroid Build Coastguard Worker 	exit(0);
40*49cdfc7eSAndroid Build Coastguard Worker }
41*49cdfc7eSAndroid Build Coastguard Worker 
test_function(void)42*49cdfc7eSAndroid Build Coastguard Worker static void test_function(void)
43*49cdfc7eSAndroid Build Coastguard Worker {
44*49cdfc7eSAndroid Build Coastguard Worker 	pid_t fork_id;
45*49cdfc7eSAndroid Build Coastguard Worker 	struct ucred cred;
46*49cdfc7eSAndroid Build Coastguard Worker 	socklen_t cred_len = sizeof(cred);
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	fork_id = SAFE_FORK();
49*49cdfc7eSAndroid Build Coastguard Worker 	if (!fork_id)
50*49cdfc7eSAndroid Build Coastguard Worker 		fork_func();
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	accepted = accept(socket_fd, NULL, NULL);
53*49cdfc7eSAndroid Build Coastguard Worker 	if (accepted < 0) {
54*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "Error with accepting connection");
55*49cdfc7eSAndroid Build Coastguard Worker 		goto clean;
56*49cdfc7eSAndroid Build Coastguard Worker 	}
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	if (getsockopt(accepted, SOL_SOCKET,
59*49cdfc7eSAndroid Build Coastguard Worker 				SO_PEERCRED, &cred, &cred_len) < 0) {
60*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "Error while getting socket option");
61*49cdfc7eSAndroid Build Coastguard Worker 		goto clean;
62*49cdfc7eSAndroid Build Coastguard Worker 	}
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker 	if (fork_id != cred.pid)
65*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Received wrong PID %d, expected %d",
66*49cdfc7eSAndroid Build Coastguard Worker 				cred.pid, getpid());
67*49cdfc7eSAndroid Build Coastguard Worker 	else
68*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Test passed");
69*49cdfc7eSAndroid Build Coastguard Worker clean:
70*49cdfc7eSAndroid Build Coastguard Worker 	if (accepted >= 0)
71*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(accepted);
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAKE(0);
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)76*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
77*49cdfc7eSAndroid Build Coastguard Worker {
78*49cdfc7eSAndroid Build Coastguard Worker 	if (accepted >= 0)
79*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(accepted);
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	if (socket_fd >= 0)
82*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(socket_fd);
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
86*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = test_function,
87*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
88*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
89*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
90*49cdfc7eSAndroid Build Coastguard Worker 	.needs_checkpoints = 1,
91*49cdfc7eSAndroid Build Coastguard Worker };
92