4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
25 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
26 /* All Rights Reserved */
28 * Portions of this source code were derived from Berkeley
29 * 4.3 BSD under license from the Regents of the University of
35 * Simplified front end to rpc.
39 * This interface creates a virtual listener for all the services
40 * started thru rpc_reg(). It listens on the same endpoint for
41 * all the services and then executes the corresponding service
42 * for the given prognum and procnum.
52 #include <sys/types.h>
54 #include <rpc/nettype.h>
56 static struct proglst
{
57 char *(*p_progname
)();
65 xdrproc_t p_inproc
, p_outproc
;
66 struct proglst
*p_nxt
;
69 static void universal();
71 static const char rpc_reg_err
[] = "%s: %s";
72 static const char rpc_reg_msg
[] = "rpc_reg: ";
73 static const char __reg_err1
[] = "can't find appropriate transport";
74 static const char __reg_err3
[] = "unsupported transport size";
75 static const char __no_mem_str
[] = "out of memory";
77 * For simplified, easy to use kind of rpc interfaces.
78 * nettype indicates the type of transport on which the service will be
79 * listening. Used for conservation of the system resource. Only one
80 * handle is created for all the services (actually one of each netid)
81 * and same xdrbuf is used for same netid. The size of the arguments
82 * is also limited by the recvsize for that transport, even if it is
83 * a COTS transport. This may be wrong, but for cases like these, they
84 * should not use the simplified interfaces like this.
88 rpc_reg(const rpcprog_t prognum
, const rpcvers_t versnum
,
89 const rpcproc_t procnum
, char *(*progname
)(), const xdrproc_t inproc
,
90 const xdrproc_t outproc
, const char *nettype
)
92 struct netconfig
*nconf
;
95 extern mutex_t proglst_lock
;
97 if (procnum
== NULLPROC
) {
98 (void) syslog(LOG_ERR
, (const char *) "%s: %s %d",
100 (const char *) "can't reassign procedure number %d",
106 nettype
= "netpath"; /* The default behavior */
107 if ((handle
= __rpc_setconf((char *)nettype
)) == NULL
) {
108 (void) syslog(LOG_ERR
, rpc_reg_err
, rpc_reg_msg
, __reg_err1
);
111 /* VARIABLES PROTECTED BY proglst_lock: proglst */
112 (void) mutex_lock(&proglst_lock
);
113 while (nconf
= __rpc_getconf(handle
)) {
123 for (pl
= proglst
; pl
; pl
= pl
->p_nxt
)
124 if (strcmp(pl
->p_netid
, nconf
->nc_netid
) == 0) {
125 svcxprt
= pl
->p_transp
;
126 xdrbuf
= pl
->p_xdrbuf
;
127 recvsz
= pl
->p_recvsz
;
132 if (svcxprt
== NULL
) {
135 svcxprt
= svc_tli_create(RPC_ANYFD
, nconf
, NULL
, 0, 0);
138 if (t_getinfo(svcxprt
->xp_fd
, &tinfo
) == -1) {
141 __tli_sys_strerror(errorstr
, sizeof (errorstr
),
143 (void) syslog(LOG_ERR
, "%s : %s : %s",
144 rpc_reg_msg
, "t_getinfo failed",
146 SVC_DESTROY(svcxprt
);
149 if ((recvsz
= __rpc_get_t_size(0, tinfo
.tsdu
)) == 0) {
150 (void) syslog(LOG_ERR
, rpc_reg_err
, rpc_reg_msg
,
152 SVC_DESTROY(svcxprt
);
155 if (((xdrbuf
= malloc((unsigned)recvsz
)) == NULL
) ||
156 ((netid
= strdup(nconf
->nc_netid
)) == NULL
)) {
157 (void) syslog(LOG_ERR
, rpc_reg_err
, rpc_reg_msg
,
159 SVC_DESTROY(svcxprt
);
165 * Check if this (program, version, netid) had already been
166 * registered. The check may save a few RPC calls to rpcbind
168 for (pl
= proglst
; pl
; pl
= pl
->p_nxt
)
169 if ((pl
->p_prognum
== prognum
) &&
170 (pl
->p_versnum
== versnum
) &&
171 (strcmp(pl
->p_netid
, netid
) == 0))
173 if (pl
== NULL
) { /* Not yet */
174 (void) rpcb_unset(prognum
, versnum
, nconf
);
176 /* so that svc_reg does not call rpcb_set() */
180 if (!svc_reg(svcxprt
, prognum
, versnum
, universal
, nconf
)) {
181 (void) syslog(LOG_ERR
,
182 "%s couldn't register prog %d vers %d for %s",
183 rpc_reg_msg
, prognum
, versnum
, netid
);
185 SVC_DESTROY(svcxprt
);
192 pl
= malloc(sizeof (struct proglst
));
194 (void) syslog(LOG_ERR
, rpc_reg_err
, rpc_reg_msg
,
197 SVC_DESTROY(svcxprt
);
203 pl
->p_progname
= progname
;
204 pl
->p_prognum
= prognum
;
205 pl
->p_versnum
= versnum
;
206 pl
->p_procnum
= procnum
;
207 pl
->p_inproc
= inproc
;
208 pl
->p_outproc
= outproc
;
209 pl
->p_transp
= svcxprt
;
210 pl
->p_xdrbuf
= xdrbuf
;
211 pl
->p_recvsz
= recvsz
;
217 __rpc_endconf(handle
);
218 (void) mutex_unlock(&proglst_lock
);
221 (void) syslog(LOG_ERR
,
222 (const char *) "%s cant find suitable transport for %s",
223 rpc_reg_msg
, nettype
);
230 * The universal handler for the services registered using registerrpc.
231 * It handles both the connectionless and the connection oriented cases.
235 universal(struct svc_req
*rqstp
, SVCXPRT
*transp
)
243 extern mutex_t proglst_lock
;
246 * enforce "procnum 0 is echo" convention
248 if (rqstp
->rq_proc
== NULLPROC
) {
249 if (svc_sendreply(transp
, (xdrproc_t
)xdr_void
, NULL
) == FALSE
) {
250 (void) syslog(LOG_ERR
,
251 (const char *) "svc_sendreply failed");
255 prog
= rqstp
->rq_prog
;
256 vers
= rqstp
->rq_vers
;
257 proc
= rqstp
->rq_proc
;
258 (void) mutex_lock(&proglst_lock
);
259 for (pl
= proglst
; pl
; pl
= pl
->p_nxt
) {
260 if (pl
->p_prognum
== prog
&& pl
->p_procnum
== proc
&&
261 pl
->p_versnum
== vers
&&
262 (strcmp(pl
->p_netid
, transp
->xp_netid
) == 0)) {
263 /* decode arguments into a CLEAN buffer */
264 xdrbuf
= pl
->p_xdrbuf
;
265 /* Zero the arguments: reqd ! */
266 (void) memset(xdrbuf
, 0, pl
->p_recvsz
);
268 * Assuming that sizeof (xdrbuf) would be enough
269 * for the arguments; if not then the program
272 if (!svc_getargs(transp
, pl
->p_inproc
, xdrbuf
)) {
273 svcerr_decode(transp
);
274 (void) mutex_unlock(&proglst_lock
);
277 outdata
= (*(pl
->p_progname
))(xdrbuf
);
278 if (outdata
== NULL
&&
279 pl
->p_outproc
!= (xdrproc_t
)xdr_void
) {
280 /* there was an error */
281 (void) mutex_unlock(&proglst_lock
);
284 if (!svc_sendreply(transp
, pl
->p_outproc
, outdata
)) {
285 (void) syslog(LOG_ERR
, (const char *)
286 "rpc: rpc_reg trouble replying to prog %d vers %d",
288 (void) mutex_unlock(&proglst_lock
);
291 /* free the decoded arguments */
292 (void) svc_freeargs(transp
, pl
->p_inproc
, xdrbuf
);
293 (void) mutex_unlock(&proglst_lock
);
297 (void) mutex_unlock(&proglst_lock
);
298 /* This should never happen */
299 (void) syslog(LOG_ERR
, (const char *)
300 "rpc: rpc_reg: never registered prog %d vers %d",