Incorrect x86 CPU family and model check.
[glibc.git] / sunrpc / clnt_unix.c
blobbca1273e2237ea1bba843001ef258301e52549a6
1 /*
2 * clnt_unix.c, Implements a TCP/IP based, client side RPC.
4 * Copyright (C) 1984, Sun Microsystems, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * 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.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * TCP based RPC supports 'batched calls'.
34 * A sequence of calls may be batched-up in a send buffer. The rpc call
35 * return immediately to the client even though the call was not necessarily
36 * sent. The batching occurs if the results' xdr routine is NULL (0) AND
37 * the rpc timeout value is zero (see clnt.h, rpc).
39 * Clients should NOT casually batch calls that in fact return results; that is,
40 * the server side should be aware that a call is batched and not produce any
41 * return message. Batched calls that produce many result messages can
42 * deadlock (netlock) the client and the server....
44 * Now go hang yourself.
47 #include <netdb.h>
48 #include <errno.h>
49 #include <stdio.h>
50 #include <unistd.h>
51 #include <libintl.h>
52 #include <rpc/rpc.h>
53 #include <sys/uio.h>
54 #include <sys/poll.h>
55 #include <sys/socket.h>
56 #include <rpc/pmap_clnt.h>
57 #ifdef USE_IN_LIBIO
58 # include <wchar.h>
59 #endif
61 extern u_long _create_xid (void);
63 #define MCALL_MSG_SIZE 24
65 struct ct_data
67 int ct_sock;
68 bool_t ct_closeit;
69 struct timeval ct_wait;
70 bool_t ct_waitset; /* wait set by clnt_control? */
71 struct sockaddr_un ct_addr;
72 struct rpc_err ct_error;
73 char ct_mcall[MCALL_MSG_SIZE]; /* marshalled callmsg */
74 u_int ct_mpos; /* pos after marshal */
75 XDR ct_xdrs;
78 static int readunix (char *, char *, int);
79 static int writeunix (char *, char *, int);
81 static enum clnt_stat clntunix_call (CLIENT *, u_long, xdrproc_t, caddr_t,
82 xdrproc_t, caddr_t, struct timeval);
83 static void clntunix_abort (void);
84 static void clntunix_geterr (CLIENT *, struct rpc_err *);
85 static bool_t clntunix_freeres (CLIENT *, xdrproc_t, caddr_t);
86 static bool_t clntunix_control (CLIENT *, int, char *);
87 static void clntunix_destroy (CLIENT *);
89 static const struct clnt_ops unix_ops =
91 clntunix_call,
92 clntunix_abort,
93 clntunix_geterr,
94 clntunix_freeres,
95 clntunix_destroy,
96 clntunix_control
100 * Create a client handle for a tcp/ip connection.
101 * If *sockp<0, *sockp is set to a newly created TCP socket and it is
102 * connected to raddr. If *sockp non-negative then
103 * raddr is ignored. The rpc/tcp package does buffering
104 * similar to stdio, so the client must pick send and receive buffer sizes,];
105 * 0 => use the default.
106 * If raddr->sin_port is 0, then a binder on the remote machine is
107 * consulted for the right port number.
108 * NB: *sockp is copied into a private area.
109 * NB: It is the clients responsibility to close *sockp.
110 * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
111 * something more useful.
113 CLIENT *
114 clntunix_create (struct sockaddr_un *raddr, u_long prog, u_long vers,
115 int *sockp, u_int sendsz, u_int recvsz)
117 CLIENT *h;
118 struct ct_data *ct = (struct ct_data *) mem_alloc (sizeof (*ct));
119 struct rpc_msg call_msg;
120 int len;
122 h = (CLIENT *) mem_alloc (sizeof (*h));
123 if (h == NULL || ct == NULL)
125 struct rpc_createerr *ce = &get_rpc_createerr ();
126 (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
127 ce->cf_stat = RPC_SYSTEMERROR;
128 ce->cf_error.re_errno = ENOMEM;
129 goto fooy;
133 * If no socket given, open one
135 if (*sockp < 0)
137 *sockp = __socket (AF_UNIX, SOCK_STREAM, 0);
138 len = strlen (raddr->sun_path) + sizeof (raddr->sun_family) + 1;
139 if (*sockp < 0
140 || __connect (*sockp, (struct sockaddr *) raddr, len) < 0)
142 struct rpc_createerr *ce = &get_rpc_createerr ();
143 ce->cf_stat = RPC_SYSTEMERROR;
144 ce->cf_error.re_errno = errno;
145 if (*sockp != -1)
146 __close (*sockp);
147 goto fooy;
149 ct->ct_closeit = TRUE;
151 else
153 ct->ct_closeit = FALSE;
157 * Set up private data struct
159 ct->ct_sock = *sockp;
160 ct->ct_wait.tv_usec = 0;
161 ct->ct_waitset = FALSE;
162 ct->ct_addr = *raddr;
165 * Initialize call message
167 call_msg.rm_xid = _create_xid ();
168 call_msg.rm_direction = CALL;
169 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
170 call_msg.rm_call.cb_prog = prog;
171 call_msg.rm_call.cb_vers = vers;
174 * pre-serialize the static part of the call msg and stash it away
176 INTUSE(xdrmem_create) (&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE,
177 XDR_ENCODE);
178 if (!INTUSE(xdr_callhdr) (&(ct->ct_xdrs), &call_msg))
180 if (ct->ct_closeit)
181 __close (*sockp);
182 goto fooy;
184 ct->ct_mpos = XDR_GETPOS (&(ct->ct_xdrs));
185 XDR_DESTROY (&(ct->ct_xdrs));
188 * Create a client handle which uses xdrrec for serialization
189 * and authnone for authentication.
191 INTUSE(xdrrec_create) (&(ct->ct_xdrs), sendsz, recvsz,
192 (caddr_t) ct, readunix, writeunix);
193 h->cl_ops = (struct clnt_ops *) &unix_ops;
194 h->cl_private = (caddr_t) ct;
195 h->cl_auth = INTUSE(authnone_create) ();
196 return h;
198 fooy:
200 * Something goofed, free stuff and barf
202 mem_free ((caddr_t) ct, sizeof (struct ct_data));
203 mem_free ((caddr_t) h, sizeof (CLIENT));
204 return (CLIENT *) NULL;
206 INTDEF (clntunix_create)
208 static enum clnt_stat
209 clntunix_call (h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
210 CLIENT *h;
211 u_long proc;
212 xdrproc_t xdr_args;
213 caddr_t args_ptr;
214 xdrproc_t xdr_results;
215 caddr_t results_ptr;
216 struct timeval timeout;
218 struct ct_data *ct = (struct ct_data *) h->cl_private;
219 XDR *xdrs = &(ct->ct_xdrs);
220 struct rpc_msg reply_msg;
221 u_long x_id;
222 u_int32_t *msg_x_id = (u_int32_t *) (ct->ct_mcall); /* yuk */
223 bool_t shipnow;
224 int refreshes = 2;
226 if (!ct->ct_waitset)
228 ct->ct_wait = timeout;
231 shipnow =
232 (xdr_results == (xdrproc_t) 0 && ct->ct_wait.tv_sec == 0
233 && ct->ct_wait.tv_usec == 0) ? FALSE : TRUE;
235 call_again:
236 xdrs->x_op = XDR_ENCODE;
237 ct->ct_error.re_status = RPC_SUCCESS;
238 x_id = ntohl (--(*msg_x_id));
239 if ((!XDR_PUTBYTES (xdrs, ct->ct_mcall, ct->ct_mpos)) ||
240 (!XDR_PUTLONG (xdrs, (long *) &proc)) ||
241 (!AUTH_MARSHALL (h->cl_auth, xdrs)) ||
242 (!(*xdr_args) (xdrs, args_ptr)))
244 if (ct->ct_error.re_status == RPC_SUCCESS)
245 ct->ct_error.re_status = RPC_CANTENCODEARGS;
246 (void) INTUSE(xdrrec_endofrecord) (xdrs, TRUE);
247 return ct->ct_error.re_status;
249 if (!INTUSE(xdrrec_endofrecord) (xdrs, shipnow))
250 return ct->ct_error.re_status = RPC_CANTSEND;
251 if (!shipnow)
252 return RPC_SUCCESS;
254 * Hack to provide rpc-based message passing
256 if (ct->ct_wait.tv_sec == 0 && ct->ct_wait.tv_usec == 0)
257 return ct->ct_error.re_status = RPC_TIMEDOUT;
261 * Keep receiving until we get a valid transaction id
263 xdrs->x_op = XDR_DECODE;
264 while (TRUE)
266 reply_msg.acpted_rply.ar_verf = _null_auth;
267 reply_msg.acpted_rply.ar_results.where = NULL;
268 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)INTUSE(xdr_void);
269 if (!INTUSE(xdrrec_skiprecord) (xdrs))
270 return ct->ct_error.re_status;
271 /* now decode and validate the response header */
272 if (!INTUSE(xdr_replymsg) (xdrs, &reply_msg))
274 if (ct->ct_error.re_status == RPC_SUCCESS)
275 continue;
276 return ct->ct_error.re_status;
278 if (reply_msg.rm_xid == x_id)
279 break;
283 * process header
285 _seterr_reply (&reply_msg, &(ct->ct_error));
286 if (ct->ct_error.re_status == RPC_SUCCESS)
288 if (!AUTH_VALIDATE (h->cl_auth, &reply_msg.acpted_rply.ar_verf))
290 ct->ct_error.re_status = RPC_AUTHERROR;
291 ct->ct_error.re_why = AUTH_INVALIDRESP;
293 else if (!(*xdr_results) (xdrs, results_ptr))
295 if (ct->ct_error.re_status == RPC_SUCCESS)
296 ct->ct_error.re_status = RPC_CANTDECODERES;
298 /* free verifier ... */
299 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
301 xdrs->x_op = XDR_FREE;
302 (void) INTUSE(xdr_opaque_auth) (xdrs,
303 &(reply_msg.acpted_rply.ar_verf));
305 } /* end successful completion */
306 else
308 /* maybe our credentials need to be refreshed ... */
309 if (refreshes-- && AUTH_REFRESH (h->cl_auth))
310 goto call_again;
311 } /* end of unsuccessful completion */
312 return ct->ct_error.re_status;
315 static void
316 clntunix_geterr (CLIENT *h, struct rpc_err *errp)
318 struct ct_data *ct = (struct ct_data *) h->cl_private;
320 *errp = ct->ct_error;
323 static bool_t
324 clntunix_freeres (cl, xdr_res, res_ptr)
325 CLIENT *cl;
326 xdrproc_t xdr_res;
327 caddr_t res_ptr;
329 struct ct_data *ct = (struct ct_data *) cl->cl_private;
330 XDR *xdrs = &(ct->ct_xdrs);
332 xdrs->x_op = XDR_FREE;
333 return (*xdr_res) (xdrs, res_ptr);
336 static void
337 clntunix_abort ()
341 static bool_t
342 clntunix_control (CLIENT *cl, int request, char *info)
344 struct ct_data *ct = (struct ct_data *) cl->cl_private;
347 switch (request)
349 case CLSET_FD_CLOSE:
350 ct->ct_closeit = TRUE;
351 break;
352 case CLSET_FD_NCLOSE:
353 ct->ct_closeit = FALSE;
354 break;
355 case CLSET_TIMEOUT:
356 ct->ct_wait = *(struct timeval *) info;
357 break;
358 case CLGET_TIMEOUT:
359 *(struct timeval *) info = ct->ct_wait;
360 break;
361 case CLGET_SERVER_ADDR:
362 *(struct sockaddr_un *) info = ct->ct_addr;
363 break;
364 case CLGET_FD:
365 *(int *)info = ct->ct_sock;
366 break;
367 case CLGET_XID:
369 * use the knowledge that xid is the
370 * first element in the call structure *.
371 * This will get the xid of the PREVIOUS call
373 *(u_long *) info = ntohl (*(u_long *)ct->ct_mcall);
374 break;
375 case CLSET_XID:
376 /* This will set the xid of the NEXT call */
377 *(u_long *) ct->ct_mcall = htonl (*(u_long *)info - 1);
378 /* decrement by 1 as clntunix_call() increments once */
379 break;
380 case CLGET_VERS:
382 * This RELIES on the information that, in the call body,
383 * the version number field is the fifth field from the
384 * begining of the RPC header. MUST be changed if the
385 * call_struct is changed
387 *(u_long *) info = ntohl (*(u_long *) (ct->ct_mcall
388 + 4 * BYTES_PER_XDR_UNIT));
389 break;
390 case CLSET_VERS:
391 *(u_long *) (ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT)
392 = htonl (*(u_long *) info);
393 break;
394 case CLGET_PROG:
396 * This RELIES on the information that, in the call body,
397 * the program number field is the field from the
398 * begining of the RPC header. MUST be changed if the
399 * call_struct is changed
401 *(u_long *) info = ntohl (*(u_long *) (ct->ct_mcall
402 + 3 * BYTES_PER_XDR_UNIT));
403 break;
404 case CLSET_PROG:
405 *(u_long *) (ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT)
406 = htonl(*(u_long *) info);
407 break;
408 /* The following are only possible with TI-RPC */
409 case CLGET_RETRY_TIMEOUT:
410 case CLSET_RETRY_TIMEOUT:
411 case CLGET_SVC_ADDR:
412 case CLSET_SVC_ADDR:
413 case CLSET_PUSH_TIMOD:
414 case CLSET_POP_TIMOD:
415 default:
416 return FALSE;
418 return TRUE;
422 static void
423 clntunix_destroy (CLIENT *h)
425 struct ct_data *ct =
426 (struct ct_data *) h->cl_private;
428 if (ct->ct_closeit)
430 (void) __close (ct->ct_sock);
432 XDR_DESTROY (&(ct->ct_xdrs));
433 mem_free ((caddr_t) ct, sizeof (struct ct_data));
434 mem_free ((caddr_t) h, sizeof (CLIENT));
437 static int
438 __msgread (int sock, void *data, size_t cnt)
440 struct iovec iov;
441 struct msghdr msg;
442 #ifdef SCM_CREDENTIALS
443 static char cm[CMSG_SPACE(sizeof (struct ucred))];
444 #endif
445 int len;
447 iov.iov_base = data;
448 iov.iov_len = cnt;
450 msg.msg_iov = &iov;
451 msg.msg_iovlen = 1;
452 msg.msg_name = NULL;
453 msg.msg_namelen = 0;
454 #ifdef SCM_CREDENTIALS
455 msg.msg_control = (caddr_t) &cm;
456 msg.msg_controllen = CMSG_SPACE(sizeof (struct ucred));
457 #endif
458 msg.msg_flags = 0;
460 #ifdef SO_PASSCRED
462 int on = 1;
463 if (__setsockopt (sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)))
464 return -1;
466 #endif
468 restart:
469 len = __recvmsg (sock, &msg, 0);
470 if (len >= 0)
472 if (msg.msg_flags & MSG_CTRUNC || len == 0)
473 return 0;
474 else
475 return len;
477 if (errno == EINTR)
478 goto restart;
479 return -1;
482 static int
483 __msgwrite (int sock, void *data, size_t cnt)
485 #ifndef SCM_CREDENTIALS
486 /* We cannot implement this reliably. */
487 __set_errno (ENOSYS);
488 return -1;
489 #else
490 struct iovec iov;
491 struct msghdr msg;
492 struct cmsghdr *cmsg = alloca (CMSG_SPACE(sizeof (struct ucred)));
493 struct ucred cred;
494 int len;
496 /* XXX I'm not sure, if gete?id() is always correct, or if we should use
497 get?id(). But since keyserv needs geteuid(), we have no other chance.
498 It would be much better, if the kernel could pass both to the server. */
499 cred.pid = __getpid ();
500 cred.uid = __geteuid ();
501 cred.gid = __getegid ();
503 memcpy (CMSG_DATA(cmsg), &cred, sizeof (struct ucred));
504 cmsg->cmsg_level = SOL_SOCKET;
505 cmsg->cmsg_type = SCM_CREDENTIALS;
506 cmsg->cmsg_len = sizeof(*cmsg) + sizeof(struct ucred);
508 iov.iov_base = data;
509 iov.iov_len = cnt;
511 msg.msg_iov = &iov;
512 msg.msg_iovlen = 1;
513 msg.msg_name = NULL;
514 msg.msg_namelen = 0;
515 msg.msg_control = cmsg;
516 msg.msg_controllen = CMSG_ALIGN(cmsg->cmsg_len);
517 msg.msg_flags = 0;
519 restart:
520 len = __sendmsg (sock, &msg, 0);
521 if (len >= 0)
522 return len;
523 if (errno == EINTR)
524 goto restart;
525 return -1;
527 #endif
532 * Interface between xdr serializer and unix connection.
533 * Behaves like the system calls, read & write, but keeps some error state
534 * around for the rpc level.
536 static int
537 readunix (char *ctptr, char *buf, int len)
539 struct ct_data *ct = (struct ct_data *) ctptr;
540 struct pollfd fd;
541 int milliseconds = ((ct->ct_wait.tv_sec * 1000)
542 + (ct->ct_wait.tv_usec / 1000));
544 if (len == 0)
545 return 0;
547 fd.fd = ct->ct_sock;
548 fd.events = POLLIN;
549 while (TRUE)
551 switch (__poll (&fd, 1, milliseconds))
553 case 0:
554 ct->ct_error.re_status = RPC_TIMEDOUT;
555 return -1;
557 case -1:
558 if (errno == EINTR)
559 continue;
560 ct->ct_error.re_status = RPC_CANTRECV;
561 ct->ct_error.re_errno = errno;
562 return -1;
564 break;
566 switch (len = __msgread (ct->ct_sock, buf, len))
569 case 0:
570 /* premature eof */
571 ct->ct_error.re_errno = ECONNRESET;
572 ct->ct_error.re_status = RPC_CANTRECV;
573 len = -1; /* it's really an error */
574 break;
576 case -1:
577 ct->ct_error.re_errno = errno;
578 ct->ct_error.re_status = RPC_CANTRECV;
579 break;
581 return len;
584 static int
585 writeunix (char *ctptr, char *buf, int len)
587 int i, cnt;
588 struct ct_data *ct = (struct ct_data *) ctptr;
590 for (cnt = len; cnt > 0; cnt -= i, buf += i)
592 if ((i = __msgwrite (ct->ct_sock, buf, cnt)) == -1)
594 ct->ct_error.re_errno = errno;
595 ct->ct_error.re_status = RPC_CANTSEND;
596 return -1;
599 return len;