1f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci#!/bin/sh
3f08c3bdfSopenharmony_ci# Copyright (c) 2014 Oracle and/or its affiliates. All Rights Reserved.
4f08c3bdfSopenharmony_ci# Copyright (c) 2017-2020 Petr Vorel <pvorel@suse.cz>
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ciSERVER=
7f08c3bdfSopenharmony_ciCLIENT=
8f08c3bdfSopenharmony_ciCLIENT_EXTRA_OPTS=
9f08c3bdfSopenharmony_ciCLEANER=
10f08c3bdfSopenharmony_ci# Program number to register the services to rpcbind
11f08c3bdfSopenharmony_ciPROGNUMNOSVC=536875000
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ciTST_TESTFUNC=do_test
14f08c3bdfSopenharmony_ciTST_USAGE=usage
15f08c3bdfSopenharmony_ciTST_OPTS="c:e:s:"
16f08c3bdfSopenharmony_ciTST_SETUP=setup
17f08c3bdfSopenharmony_ciTST_CLEANUP=cleanup
18f08c3bdfSopenharmony_ciTST_PARSE_ARGS=rpc_parse_args
19f08c3bdfSopenharmony_ciTST_NEEDS_CMDS="pkill rpcinfo"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ciusage()
22f08c3bdfSopenharmony_ci{
23f08c3bdfSopenharmony_ci	cat << EOF
24f08c3bdfSopenharmony_ciUSAGE: $0 [-s sprog] -c clprog [ -e extra ]
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ciConnect to the remote host and start sprog.
27f08c3bdfSopenharmony_ciThen execute clprog and passing it the remote host value.
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci-c clprog client program binary
30f08c3bdfSopenharmony_ci-s sprog  server program binary
31f08c3bdfSopenharmony_ci-e extra  extra client options
32f08c3bdfSopenharmony_ciEOF
33f08c3bdfSopenharmony_ci}
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cirpc_parse_args()
36f08c3bdfSopenharmony_ci{
37f08c3bdfSopenharmony_ci	case "$1" in
38f08c3bdfSopenharmony_ci		c) CLIENT="$OPTARG" ;;
39f08c3bdfSopenharmony_ci		e) tst_check_cmds sed
40f08c3bdfSopenharmony_ci		   CLIENT_EXTRA_OPTS="$(echo $OPTARG | sed 's/,/ /')" ;;
41f08c3bdfSopenharmony_ci		s) SERVER="$OPTARG" ;;
42f08c3bdfSopenharmony_ci	esac
43f08c3bdfSopenharmony_ci}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_cisetup()
46f08c3bdfSopenharmony_ci{
47f08c3bdfSopenharmony_ci	check_rpc
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	if [ -n "$SERVER" ]; then
50f08c3bdfSopenharmony_ci		CLEANER="rpc_cleaner"
51f08c3bdfSopenharmony_ci		if echo "$SERVER" | grep -q '^tirpc'; then
52f08c3bdfSopenharmony_ci			CLEANER="tirpc_cleaner"
53f08c3bdfSopenharmony_ci		fi
54f08c3bdfSopenharmony_ci	fi
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	[ -n "$CLIENT" ] || tst_brk TBROK "client program not set"
57f08c3bdfSopenharmony_ci	tst_check_cmds $CLIENT $SERVER || tst_brk TCONF "LTP compiled without TI-RPC support?"
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	tst_cmd_available ldd which || return
60f08c3bdfSopenharmony_ci	if ldd $(which $CLIENT) |grep -q /libtirpc\.so; then
61f08c3bdfSopenharmony_ci		tst_res TINFO "using libtirpc: yes"
62f08c3bdfSopenharmony_ci	else
63f08c3bdfSopenharmony_ci		tst_res TINFO "using libtirpc: no (probably using glibc)"
64f08c3bdfSopenharmony_ci	fi
65f08c3bdfSopenharmony_ci}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_cicleanup()
68f08c3bdfSopenharmony_ci{
69f08c3bdfSopenharmony_ci	if [ "$SERVER_STARTED" ]; then
70f08c3bdfSopenharmony_ci		pkill -13 -x $SERVER
71f08c3bdfSopenharmony_ci		$CLEANER $PROGNUMNOSVC
72f08c3bdfSopenharmony_ci	fi
73f08c3bdfSopenharmony_ci}
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_cido_test()
76f08c3bdfSopenharmony_ci{
77f08c3bdfSopenharmony_ci	local i
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci	if [ -n "$SERVER" ]; then
80f08c3bdfSopenharmony_ci		$SERVER $PROGNUMNOSVC &
81f08c3bdfSopenharmony_ci		SERVER_STARTED=1
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci		for i in $(seq 1 10); do
84f08c3bdfSopenharmony_ci			rpcinfo -p localhost | grep -q $PROGNUMNOSVC && break
85f08c3bdfSopenharmony_ci			[ "$i" -eq 10 ] && tst_brk TBROK "server not registered"
86f08c3bdfSopenharmony_ci			tst_sleep 100ms
87f08c3bdfSopenharmony_ci		done
88f08c3bdfSopenharmony_ci	fi
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	EXPECT_RHOST_PASS $CLIENT $(tst_ipaddr) $PROGNUMNOSVC $CLIENT_EXTRA_OPTS
91f08c3bdfSopenharmony_ci}
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci. rpc_lib.sh
94f08c3bdfSopenharmony_citst_run
95