source: rtems-libbsd/freebsd/lib/libc/rpc/clnt_simple.c @ f41a394

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f41a394 was 60b1d40, checked in by Sebastian Huber <sebastian.huber@…>, on 06/09/16 at 08:23:57

RPC(3): Import from FreeBSD

  • Property mode set to 100644
File size: 6.2 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*      $NetBSD: clnt_simple.c,v 1.21 2000/07/06 03:10:34 christos Exp $        */
4
5/*-
6 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 *   this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 *   this list of conditions and the following disclaimer in the documentation
15 *   and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 *   contributors may be used to endorse or promote products derived
18 *   from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32/*
33 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34 */
35
36#if defined(LIBC_SCCS) && !defined(lint)
37static char *sccsid2 = "from: @(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro";
38static char *sccsid = "from: @(#)clnt_simple.c  2.2 88/08/01 4.0 RPCSRC";
39#endif
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD$");
42
43/*
44 * clnt_simple.c
45 * Simplified front end to client rpc.
46 *
47 */
48
49#include "namespace.h"
50#include "reentrant.h"
51#include <rtems/bsd/sys/param.h>
52#include <stdio.h>
53#include <errno.h>
54#include <rpc/rpc.h>
55#include <string.h>
56#include <stdlib.h>
57#include <fcntl.h>
58#include <unistd.h>
59#include "un-namespace.h"
60#include "mt_misc.h"
61
62#ifndef MAXHOSTNAMELEN
63#define MAXHOSTNAMELEN 64
64#endif
65
66#ifndef NETIDLEN
67#define NETIDLEN 32
68#endif
69
70struct rpc_call_private {
71        int     valid;                  /* Is this entry valid ? */
72        CLIENT  *client;                /* Client handle */
73        pid_t   pid;                    /* process-id at moment of creation */
74        rpcprog_t prognum;              /* Program */
75        rpcvers_t versnum;              /* Version */
76        char    host[MAXHOSTNAMELEN];   /* Servers host */
77        char    nettype[NETIDLEN];      /* Network type */
78};
79static struct rpc_call_private *rpc_call_private_main;
80static thread_key_t rpc_call_key;
81static once_t rpc_call_once = ONCE_INITIALIZER;
82static int rpc_call_key_error;
83
84static void rpc_call_key_init(void);
85static void rpc_call_destroy(void *);
86
87static void
88rpc_call_destroy(void *vp)
89{
90        struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
91
92        if (rcp) {
93                if (rcp->client)
94                        CLNT_DESTROY(rcp->client);
95                free(rcp);
96        }
97}
98
99static void
100rpc_call_key_init(void)
101{
102
103        rpc_call_key_error = thr_keycreate(&rpc_call_key, rpc_call_destroy);
104}
105
106/*
107 * This is the simplified interface to the client rpc layer.
108 * The client handle is not destroyed here and is reused for
109 * the future calls to same prog, vers, host and nettype combination.
110 *
111 * The total time available is 25 seconds.
112 */
113enum clnt_stat
114rpc_call(host, prognum, versnum, procnum, inproc, in, outproc, out, nettype)
115        const char *host;                       /* host name */
116        rpcprog_t prognum;                      /* program number */
117        rpcvers_t versnum;                      /* version number */
118        rpcproc_t procnum;                      /* procedure number */
119        xdrproc_t inproc, outproc;      /* in/out XDR procedures */
120        const char *in;
121        char  *out;                     /* recv/send data */
122        const char *nettype;                    /* nettype */
123{
124        struct rpc_call_private *rcp = (struct rpc_call_private *) 0;
125        enum clnt_stat clnt_stat;
126        struct timeval timeout, tottimeout;
127        int main_thread = 1;
128
129        if ((main_thread = thr_main())) {
130                rcp = rpc_call_private_main;
131        } else {
132                if (thr_once(&rpc_call_once, rpc_call_key_init) != 0 ||
133                    rpc_call_key_error != 0) {
134                        rpc_createerr.cf_stat = RPC_SYSTEMERROR;
135                        rpc_createerr.cf_error.re_errno = rpc_call_key_error;
136                        return (rpc_createerr.cf_stat);
137                }
138                rcp = (struct rpc_call_private *)thr_getspecific(rpc_call_key);
139        }
140        if (rcp == NULL) {
141                rcp = malloc(sizeof (*rcp));
142                if (rcp == NULL) {
143                        rpc_createerr.cf_stat = RPC_SYSTEMERROR;
144                        rpc_createerr.cf_error.re_errno = errno;
145                        return (rpc_createerr.cf_stat);
146                }
147                if (main_thread)
148                        rpc_call_private_main = rcp;
149                else
150                        thr_setspecific(rpc_call_key, (void *) rcp);
151                rcp->valid = 0;
152                rcp->client = NULL;
153        }
154        if ((nettype == NULL) || (nettype[0] == 0))
155                nettype = "netpath";
156        if (!(rcp->valid && rcp->pid == getpid() &&
157                (rcp->prognum == prognum) &&
158                (rcp->versnum == versnum) &&
159                (!strcmp(rcp->host, host)) &&
160                (!strcmp(rcp->nettype, nettype)))) {
161                int fd;
162
163                rcp->valid = 0;
164                if (rcp->client)
165                        CLNT_DESTROY(rcp->client);
166                /*
167                 * Using the first successful transport for that type
168                 */
169                rcp->client = clnt_create(host, prognum, versnum, nettype);
170                rcp->pid = getpid();
171                if (rcp->client == NULL) {
172                        return (rpc_createerr.cf_stat);
173                }
174                /*
175                 * Set time outs for connectionless case.  Do it
176                 * unconditionally.  Faster than doing a t_getinfo()
177                 * and then doing the right thing.
178                 */
179                timeout.tv_usec = 0;
180                timeout.tv_sec = 5;
181                (void) CLNT_CONTROL(rcp->client,
182                                CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
183                if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd))
184                        _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
185                rcp->prognum = prognum;
186                rcp->versnum = versnum;
187                if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
188                    (strlen(nettype) < (size_t)NETIDLEN)) {
189                        (void) strcpy(rcp->host, host);
190                        (void) strcpy(rcp->nettype, nettype);
191                        rcp->valid = 1;
192                } else {
193                        rcp->valid = 0;
194                }
195        } /* else reuse old client */
196        tottimeout.tv_sec = 25;
197        tottimeout.tv_usec = 0;
198        /*LINTED const castaway*/
199        clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *) in,
200            outproc, out, tottimeout);
201        /*
202         * if call failed, empty cache
203         */
204        if (clnt_stat != RPC_SUCCESS)
205                rcp->valid = 0;
206        return (clnt_stat);
207}
Note: See TracBrowser for help on using the repository browser.