tools/llvm: Do not build with symbols
[minix3.git] / tests / lib / libc / rpc / t_rpc.c
blobcc1ec09fb216134501a65117e807e8f313fae9bf
1 /* $NetBSD: t_rpc.c,v 1.3 2013/02/28 15:56:53 christos Exp $ */
3 #include <sys/cdefs.h>
4 __RCSID("$NetBSD: t_rpc.c,v 1.3 2013/02/28 15:56:53 christos Exp $");
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <rpc/rpc.h>
9 #include <stdlib.h>
10 #include <err.h>
11 #include <netdb.h>
12 #include <stdio.h>
13 #include <unistd.h>
16 #ifndef TEST
17 #include <atf-c.h>
19 #define ERRX(ev, msg, ...) ATF_REQUIRE_MSG(0, msg, __VA_ARGS__)
21 #define SKIPX(ev, msg, ...) do { \
22 atf_tc_skip(msg, __VA_ARGS__); \
23 return; \
24 } while(/*CONSTCOND*/0)
26 #else
27 #define ERRX(ev, msg, ...) errx(ev, msg, __VA_ARGS__)
28 #define SKIPX(ev, msg, ...) errx(ev, msg, __VA_ARGS__)
29 #endif
32 #define RPCBPROC_NULL 0
34 static int
35 reply(caddr_t replyp, struct netbuf * raddrp, struct netconfig * nconf)
37 char host[NI_MAXHOST];
38 struct sockaddr *sock = raddrp->buf;
39 int error;
42 error = getnameinfo(sock, sock->sa_len, host, sizeof(host), NULL, 0, 0);
43 if (error)
44 warnx("Cannot resolve address (%s)", gai_strerror(error));
45 else
46 printf("response from: %s\n", host);
47 return 0;
50 extern bool __rpc_control(int, void *);
52 static void
53 onehost(const char *host, const char *transp)
55 CLIENT *clnt;
56 struct netbuf addr;
57 struct timeval tv;
60 * Magic!
62 tv.tv_sec = 0;
63 tv.tv_usec = 500000;
64 #define CLCR_SET_RPCB_TIMEOUT 2
65 __rpc_control(CLCR_SET_RPCB_TIMEOUT, &tv);
67 if ((clnt = clnt_create(host, RPCBPROG, RPCBVERS, transp)) == NULL)
68 SKIPX(EXIT_FAILURE, "clnt_create (%s)", clnt_spcreateerror(""));
70 tv.tv_sec = 1;
71 tv.tv_usec = 0;
72 if (clnt_call(clnt, RPCBPROC_NULL, xdr_void, NULL, xdr_void, NULL, tv)
73 != RPC_SUCCESS)
74 ERRX(EXIT_FAILURE, "clnt_call (%s)", clnt_sperror(clnt, ""));
75 clnt_control(clnt, CLGET_SVC_ADDR, (char *) &addr);
76 reply(NULL, &addr, NULL);
79 #ifdef TEST
80 static void
81 allhosts(void)
83 enum clnt_stat clnt_stat;
85 clnt_stat = rpc_broadcast(RPCBPROG, RPCBVERS, RPCBPROC_NULL,
86 (xdrproc_t)xdr_void, NULL, (xdrproc_t)xdr_void,
87 NULL, (resultproc_t)reply, transp);
88 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
89 ERRX(EXIT_FAILURE, "%s", clnt_sperrno(clnt_stat));
92 int
93 main(int argc, char *argv[])
95 int ch;
96 const char *transp = "udp";
99 while ((ch = getopt(argc, argv, "ut")) != -1)
100 switch (ch) {
101 case 't':
102 transp = "tcp";
103 break;
104 case 'u':
105 transp = "udp";
106 break;
107 default:
108 fprintf(stderr, "Usage: %s -[t|u] [<hostname>...]\n",
109 getprogname());
110 return EXIT_FAILURE;
113 if (argc == optind)
114 allhosts();
115 else
116 for (; optind < argc; optind++)
117 onehost(argv[optind], transp);
119 return EXIT_SUCCESS;
122 #else
124 ATF_TC(get_svc_addr_tcp);
125 ATF_TC_HEAD(get_svc_addr_tcp, tc)
127 atf_tc_set_md_var(tc, "descr", "Checks CLGET_SVC_ADDR for tcp");
131 ATF_TC_BODY(get_svc_addr_tcp, tc)
133 onehost("localhost", "tcp");
137 ATF_TC(get_svc_addr_udp);
138 ATF_TC_HEAD(get_svc_addr_udp, tc)
140 atf_tc_set_md_var(tc, "descr", "Checks CLGET_SVC_ADDR for udp");
143 ATF_TC_BODY(get_svc_addr_udp, tc)
145 onehost("localhost", "udp");
149 ATF_TP_ADD_TCS(tp)
151 ATF_TP_ADD_TC(tp, get_svc_addr_udp);
152 ATF_TP_ADD_TC(tp, get_svc_addr_tcp);
154 return atf_no_error();
157 #endif