1 | /* |
---|
2 | * Sun RPC is a product of Sun Microsystems, Inc. and is provided for |
---|
3 | * unrestricted use provided that this legend is included on all tape |
---|
4 | * media and as a part of the software program in whole or part. Users |
---|
5 | * may copy or modify Sun RPC without charge, but are not authorized |
---|
6 | * to license or distribute it to anyone else except as part of a product or |
---|
7 | * program developed by the user. |
---|
8 | * |
---|
9 | * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE |
---|
10 | * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR |
---|
11 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. |
---|
12 | * |
---|
13 | * Sun RPC is provided with no support and without any obligation on the |
---|
14 | * part of Sun Microsystems, Inc. to assist in its use, correction, |
---|
15 | * modification or enhancement. |
---|
16 | * |
---|
17 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE |
---|
18 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC |
---|
19 | * OR ANY PART THEREOF. |
---|
20 | * |
---|
21 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue |
---|
22 | * or profits or other special, indirect and consequential damages, even if |
---|
23 | * Sun has been advised of the possibility of such damages. |
---|
24 | * |
---|
25 | * Sun Microsystems, Inc. |
---|
26 | * 2550 Garcia Avenue |
---|
27 | * Mountain View, California 94043 |
---|
28 | */ |
---|
29 | |
---|
30 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
31 | /*static char *sccsid = "from: @(#)clnt_unix.c 1.37 87/10/05 Copyr 1984 Sun Micro";*/ |
---|
32 | /*static char *sccsid = "from: @(#)clnt_unix.c 2.2 88/08/01 4.0 RPCSRC";*/ |
---|
33 | static char *rcsid = "$FreeBSD: src/lib/libc/rpc/clnt_unix.c,v 1.5 2000/01/27 23:06:37 jasone Exp $"; |
---|
34 | #endif |
---|
35 | |
---|
36 | /* |
---|
37 | * clnt_unix.c, Implements a AF_UNIX based, client side RPC. |
---|
38 | * |
---|
39 | * Copyright (C) 1984, Sun Microsystems, Inc. |
---|
40 | * |
---|
41 | * AF_UNIX based RPC supports 'batched calls'. |
---|
42 | * A sequence of calls may be batched-up in a send buffer. The rpc call |
---|
43 | * return immediately to the client even though the call was not necessarily |
---|
44 | * sent. The batching occurs if the results' xdr routine is NULL (0) AND |
---|
45 | * the rpc timeout value is zero (see clnt.h, rpc). |
---|
46 | * |
---|
47 | * Clients should NOT casually batch calls that in fact return results; that is, |
---|
48 | * the server side should be aware that a call is batched and not produce any |
---|
49 | * return message. Batched calls that produce many result messages can |
---|
50 | * deadlock (netlock) the client and the server.... |
---|
51 | * |
---|
52 | * Now go hang yourself. |
---|
53 | */ |
---|
54 | |
---|
55 | #include <stdio.h> |
---|
56 | #include <stdlib.h> |
---|
57 | #include <unistd.h> |
---|
58 | #include <string.h> |
---|
59 | #include <rpc/rpc.h> |
---|
60 | #include <sys/uio.h> |
---|
61 | #include <sys/socket.h> |
---|
62 | #include <sys/un.h> |
---|
63 | #include <netdb.h> |
---|
64 | #include <errno.h> |
---|
65 | #include <rpc/pmap_clnt.h> |
---|
66 | |
---|
67 | #define MCALL_MSG_SIZE 24 |
---|
68 | |
---|
69 | static int readunix(); |
---|
70 | static int writeunix(); |
---|
71 | |
---|
72 | static enum clnt_stat clntunix_call(); |
---|
73 | static void clntunix_abort(); |
---|
74 | static void clntunix_geterr(); |
---|
75 | static bool_t clntunix_freeres(); |
---|
76 | static bool_t clntunix_control(); |
---|
77 | static void clntunix_destroy(); |
---|
78 | |
---|
79 | static struct clnt_ops unix_ops = { |
---|
80 | clntunix_call, |
---|
81 | clntunix_abort, |
---|
82 | clntunix_geterr, |
---|
83 | clntunix_freeres, |
---|
84 | clntunix_destroy, |
---|
85 | clntunix_control |
---|
86 | }; |
---|
87 | |
---|
88 | struct ct_data { |
---|
89 | int ct_sock; |
---|
90 | bool_t ct_closeit; |
---|
91 | struct timeval ct_wait; |
---|
92 | bool_t ct_waitset; /* wait set by clnt_control? */ |
---|
93 | struct sockaddr_un ct_addr; |
---|
94 | struct rpc_err ct_error; |
---|
95 | char ct_mcall[MCALL_MSG_SIZE]; /* marshalled callmsg */ |
---|
96 | u_int ct_mpos; /* pos after marshal */ |
---|
97 | XDR ct_xdrs; |
---|
98 | }; |
---|
99 | |
---|
100 | /* |
---|
101 | * Create a client handle for a unix/ip connection. |
---|
102 | * If *sockp<0, *sockp is set to a newly created TCP socket and it is |
---|
103 | * connected to raddr. If *sockp non-negative then |
---|
104 | * raddr is ignored. The rpc/unix package does buffering |
---|
105 | * similar to stdio, so the client must pick send and receive buffer sizes,]; |
---|
106 | * 0 => use the default. |
---|
107 | * If raddr->sin_port is 0, then a binder on the remote machine is |
---|
108 | * consulted for the right port number. |
---|
109 | * NB: *sockp is copied into a private area. |
---|
110 | * NB: It is the clients responsibility to close *sockp. |
---|
111 | * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this |
---|
112 | * something more useful. |
---|
113 | */ |
---|
114 | CLIENT * |
---|
115 | clntunix_create(raddr, prog, vers, sockp, sendsz, recvsz) |
---|
116 | struct sockaddr_un *raddr; |
---|
117 | u_long prog; |
---|
118 | u_long vers; |
---|
119 | register int *sockp; |
---|
120 | u_int sendsz; |
---|
121 | u_int recvsz; |
---|
122 | { |
---|
123 | CLIENT *h; |
---|
124 | register struct ct_data *ct = NULL; |
---|
125 | struct timeval now; |
---|
126 | struct rpc_msg call_msg; |
---|
127 | static u_int32_t disrupt; |
---|
128 | int len; |
---|
129 | |
---|
130 | if (disrupt == 0) |
---|
131 | disrupt = (u_int32_t)(long)raddr; |
---|
132 | |
---|
133 | h = (CLIENT *)mem_alloc(sizeof(*h)); |
---|
134 | if (h == NULL) { |
---|
135 | (void)fprintf(stderr, "clntunix_create: out of memory\n"); |
---|
136 | rpc_createerr.cf_stat = RPC_SYSTEMERROR; |
---|
137 | rpc_createerr.cf_error.re_errno = errno; |
---|
138 | goto fooy; |
---|
139 | } |
---|
140 | ct = (struct ct_data *)mem_alloc(sizeof(*ct)); |
---|
141 | if (ct == NULL) { |
---|
142 | (void)fprintf(stderr, "clntunix_create: out of memory\n"); |
---|
143 | rpc_createerr.cf_stat = RPC_SYSTEMERROR; |
---|
144 | rpc_createerr.cf_error.re_errno = errno; |
---|
145 | goto fooy; |
---|
146 | } |
---|
147 | |
---|
148 | /* |
---|
149 | * If no socket given, open one |
---|
150 | */ |
---|
151 | if (*sockp < 0) { |
---|
152 | *sockp = socket(AF_UNIX, SOCK_STREAM, 0); |
---|
153 | len = strlen(raddr->sun_path) + sizeof(raddr->sun_family) + |
---|
154 | sizeof(raddr->sun_len) + 1; |
---|
155 | raddr->sun_len = len; |
---|
156 | if ((*sockp < 0) |
---|
157 | || (connect(*sockp, (struct sockaddr *)raddr, len) < 0)) { |
---|
158 | rpc_createerr.cf_stat = RPC_SYSTEMERROR; |
---|
159 | rpc_createerr.cf_error.re_errno = errno; |
---|
160 | if (*sockp != -1) |
---|
161 | (void)_RPC_close(*sockp); |
---|
162 | goto fooy; |
---|
163 | } |
---|
164 | ct->ct_closeit = TRUE; |
---|
165 | } else { |
---|
166 | ct->ct_closeit = FALSE; |
---|
167 | } |
---|
168 | |
---|
169 | /* |
---|
170 | * Set up private data struct |
---|
171 | */ |
---|
172 | ct->ct_sock = *sockp; |
---|
173 | ct->ct_wait.tv_usec = 0; |
---|
174 | ct->ct_waitset = FALSE; |
---|
175 | ct->ct_addr = *raddr; |
---|
176 | |
---|
177 | /* |
---|
178 | * Initialize call message |
---|
179 | */ |
---|
180 | (void)gettimeofday(&now, (struct timezone *)0); |
---|
181 | call_msg.rm_xid = (++disrupt) ^ getpid() ^ now.tv_sec ^ now.tv_usec; |
---|
182 | call_msg.rm_direction = CALL; |
---|
183 | call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; |
---|
184 | call_msg.rm_call.cb_prog = prog; |
---|
185 | call_msg.rm_call.cb_vers = vers; |
---|
186 | |
---|
187 | /* |
---|
188 | * pre-serialize the static part of the call msg and stash it away |
---|
189 | */ |
---|
190 | xdrmem_create(&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE, |
---|
191 | XDR_ENCODE); |
---|
192 | if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) { |
---|
193 | if (ct->ct_closeit) { |
---|
194 | (void)_RPC_close(*sockp); |
---|
195 | } |
---|
196 | goto fooy; |
---|
197 | } |
---|
198 | ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs)); |
---|
199 | XDR_DESTROY(&(ct->ct_xdrs)); |
---|
200 | |
---|
201 | /* |
---|
202 | * Create a client handle which uses xdrrec for serialization |
---|
203 | * and authnone for authentication. |
---|
204 | */ |
---|
205 | xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz, |
---|
206 | (caddr_t)ct, readunix, writeunix); |
---|
207 | h->cl_ops = &unix_ops; |
---|
208 | h->cl_private = (caddr_t) ct; |
---|
209 | h->cl_auth = authnone_create(); |
---|
210 | return (h); |
---|
211 | |
---|
212 | fooy: |
---|
213 | /* |
---|
214 | * Something goofed, free stuff and barf |
---|
215 | */ |
---|
216 | if (ct) |
---|
217 | mem_free((caddr_t)ct, sizeof(struct ct_data)); |
---|
218 | if (h) |
---|
219 | mem_free((caddr_t)h, sizeof(CLIENT)); |
---|
220 | return ((CLIENT *)NULL); |
---|
221 | } |
---|
222 | |
---|
223 | static enum clnt_stat |
---|
224 | clntunix_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout) |
---|
225 | register CLIENT *h; |
---|
226 | u_long proc; |
---|
227 | xdrproc_t xdr_args; |
---|
228 | caddr_t args_ptr; |
---|
229 | xdrproc_t xdr_results; |
---|
230 | caddr_t results_ptr; |
---|
231 | struct timeval timeout; |
---|
232 | { |
---|
233 | register struct ct_data *ct = (struct ct_data *) h->cl_private; |
---|
234 | register XDR *xdrs = &(ct->ct_xdrs); |
---|
235 | struct rpc_msg reply_msg; |
---|
236 | u_long x_id; |
---|
237 | u_int32_t *msg_x_id = (u_int32_t *)(ct->ct_mcall); /* yuk */ |
---|
238 | register bool_t shipnow; |
---|
239 | int refreshes = 2; |
---|
240 | |
---|
241 | if (!ct->ct_waitset) { |
---|
242 | ct->ct_wait = timeout; |
---|
243 | } |
---|
244 | |
---|
245 | shipnow = |
---|
246 | (xdr_results == (xdrproc_t)0 && timeout.tv_sec == 0 |
---|
247 | && timeout.tv_usec == 0) ? FALSE : TRUE; |
---|
248 | |
---|
249 | call_again: |
---|
250 | xdrs->x_op = XDR_ENCODE; |
---|
251 | ct->ct_error.re_status = RPC_SUCCESS; |
---|
252 | x_id = ntohl(--(*msg_x_id)); |
---|
253 | if ((! XDR_PUTBYTES(xdrs, ct->ct_mcall, ct->ct_mpos)) || |
---|
254 | (! XDR_PUTLONG(xdrs, (long *)&proc)) || |
---|
255 | (! AUTH_MARSHALL(h->cl_auth, xdrs)) || |
---|
256 | (! (*xdr_args)(xdrs, args_ptr))) { |
---|
257 | if (ct->ct_error.re_status == RPC_SUCCESS) |
---|
258 | ct->ct_error.re_status = RPC_CANTENCODEARGS; |
---|
259 | (void)xdrrec_endofrecord(xdrs, TRUE); |
---|
260 | return (ct->ct_error.re_status); |
---|
261 | } |
---|
262 | if (! xdrrec_endofrecord(xdrs, shipnow)) |
---|
263 | return (ct->ct_error.re_status = RPC_CANTSEND); |
---|
264 | if (! shipnow) |
---|
265 | return (RPC_SUCCESS); |
---|
266 | /* |
---|
267 | * Hack to provide rpc-based message passing |
---|
268 | */ |
---|
269 | if (timeout.tv_sec == 0 && timeout.tv_usec == 0) { |
---|
270 | return(ct->ct_error.re_status = RPC_TIMEDOUT); |
---|
271 | } |
---|
272 | |
---|
273 | |
---|
274 | /* |
---|
275 | * Keep receiving until we get a valid transaction id |
---|
276 | */ |
---|
277 | xdrs->x_op = XDR_DECODE; |
---|
278 | while (TRUE) { |
---|
279 | reply_msg.acpted_rply.ar_verf = _null_auth; |
---|
280 | reply_msg.acpted_rply.ar_results.where = NULL; |
---|
281 | reply_msg.acpted_rply.ar_results.proc = xdr_void; |
---|
282 | if (! xdrrec_skiprecord(xdrs)) |
---|
283 | return (ct->ct_error.re_status); |
---|
284 | /* now decode and validate the response header */ |
---|
285 | if (! xdr_replymsg(xdrs, &reply_msg)) { |
---|
286 | if (ct->ct_error.re_status == RPC_SUCCESS) |
---|
287 | continue; |
---|
288 | return (ct->ct_error.re_status); |
---|
289 | } |
---|
290 | if (reply_msg.rm_xid == x_id) |
---|
291 | break; |
---|
292 | } |
---|
293 | |
---|
294 | /* |
---|
295 | * process header |
---|
296 | */ |
---|
297 | _seterr_reply(&reply_msg, &(ct->ct_error)); |
---|
298 | if (ct->ct_error.re_status == RPC_SUCCESS) { |
---|
299 | if (! AUTH_VALIDATE(h->cl_auth, &reply_msg.acpted_rply.ar_verf)) { |
---|
300 | ct->ct_error.re_status = RPC_AUTHERROR; |
---|
301 | ct->ct_error.re_why = AUTH_INVALIDRESP; |
---|
302 | } else if (! (*xdr_results)(xdrs, results_ptr)) { |
---|
303 | if (ct->ct_error.re_status == RPC_SUCCESS) |
---|
304 | ct->ct_error.re_status = RPC_CANTDECODERES; |
---|
305 | } |
---|
306 | /* free verifier ... */ |
---|
307 | if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) { |
---|
308 | xdrs->x_op = XDR_FREE; |
---|
309 | (void)xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf)); |
---|
310 | } |
---|
311 | } /* end successful completion */ |
---|
312 | else { |
---|
313 | /* maybe our credentials need to be refreshed ... */ |
---|
314 | if (refreshes-- && AUTH_REFRESH(h->cl_auth)) |
---|
315 | goto call_again; |
---|
316 | } /* end of unsuccessful completion */ |
---|
317 | return (ct->ct_error.re_status); |
---|
318 | } |
---|
319 | |
---|
320 | static void |
---|
321 | clntunix_geterr(h, errp) |
---|
322 | CLIENT *h; |
---|
323 | struct rpc_err *errp; |
---|
324 | { |
---|
325 | register struct ct_data *ct = |
---|
326 | (struct ct_data *) h->cl_private; |
---|
327 | |
---|
328 | *errp = ct->ct_error; |
---|
329 | } |
---|
330 | |
---|
331 | static bool_t |
---|
332 | clntunix_freeres(cl, xdr_res, res_ptr) |
---|
333 | CLIENT *cl; |
---|
334 | xdrproc_t xdr_res; |
---|
335 | caddr_t res_ptr; |
---|
336 | { |
---|
337 | register struct ct_data *ct = (struct ct_data *)cl->cl_private; |
---|
338 | register XDR *xdrs = &(ct->ct_xdrs); |
---|
339 | |
---|
340 | xdrs->x_op = XDR_FREE; |
---|
341 | return ((*xdr_res)(xdrs, res_ptr)); |
---|
342 | } |
---|
343 | |
---|
344 | static void |
---|
345 | clntunix_abort() |
---|
346 | { |
---|
347 | } |
---|
348 | |
---|
349 | |
---|
350 | static bool_t |
---|
351 | clntunix_control(cl, request, info) |
---|
352 | CLIENT *cl; |
---|
353 | int request; |
---|
354 | char *info; |
---|
355 | { |
---|
356 | register struct ct_data *ct = (struct ct_data *)cl->cl_private; |
---|
357 | register struct timeval *tv; |
---|
358 | int len; |
---|
359 | |
---|
360 | switch (request) { |
---|
361 | case CLSET_FD_CLOSE: |
---|
362 | ct->ct_closeit = TRUE; |
---|
363 | break; |
---|
364 | case CLSET_FD_NCLOSE: |
---|
365 | ct->ct_closeit = FALSE; |
---|
366 | break; |
---|
367 | case CLSET_TIMEOUT: |
---|
368 | if (info == NULL) |
---|
369 | return(FALSE); |
---|
370 | tv = (struct timeval *)info; |
---|
371 | ct->ct_wait.tv_sec = tv->tv_sec; |
---|
372 | ct->ct_wait.tv_usec = tv->tv_usec; |
---|
373 | ct->ct_waitset = TRUE; |
---|
374 | break; |
---|
375 | case CLGET_TIMEOUT: |
---|
376 | if (info == NULL) |
---|
377 | return(FALSE); |
---|
378 | *(struct timeval *)info = ct->ct_wait; |
---|
379 | break; |
---|
380 | case CLGET_SERVER_ADDR: |
---|
381 | if (info == NULL) |
---|
382 | return(FALSE); |
---|
383 | *(struct sockaddr_un *)info = ct->ct_addr; |
---|
384 | break; |
---|
385 | case CLGET_FD: |
---|
386 | if (info == NULL) |
---|
387 | return(FALSE); |
---|
388 | *(int *)info = ct->ct_sock; |
---|
389 | break; |
---|
390 | case CLGET_XID: |
---|
391 | /* |
---|
392 | * use the knowledge that xid is the |
---|
393 | * first element in the call structure *. |
---|
394 | * This will get the xid of the PREVIOUS call |
---|
395 | */ |
---|
396 | if (info == NULL) |
---|
397 | return(FALSE); |
---|
398 | *(u_long *)info = ntohl(*(u_long *)ct->ct_mcall); |
---|
399 | break; |
---|
400 | case CLSET_XID: |
---|
401 | /* This will set the xid of the NEXT call */ |
---|
402 | if (info == NULL) |
---|
403 | return(FALSE); |
---|
404 | *(u_long *)ct->ct_mcall = htonl(*(u_long *)info - 1); |
---|
405 | /* decrement by 1 as clntunix_call() increments once */ |
---|
406 | case CLGET_VERS: |
---|
407 | /* |
---|
408 | * This RELIES on the information that, in the call body, |
---|
409 | * the version number field is the fifth field from the |
---|
410 | * begining of the RPC header. MUST be changed if the |
---|
411 | * call_struct is changed |
---|
412 | */ |
---|
413 | if (info == NULL) |
---|
414 | return(FALSE); |
---|
415 | *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall + |
---|
416 | 4 * BYTES_PER_XDR_UNIT)); |
---|
417 | break; |
---|
418 | case CLSET_VERS: |
---|
419 | if (info == NULL) |
---|
420 | return(FALSE); |
---|
421 | *(u_long *)(ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT) |
---|
422 | = htonl(*(u_long *)info); |
---|
423 | break; |
---|
424 | case CLGET_PROG: |
---|
425 | /* |
---|
426 | * This RELIES on the information that, in the call body, |
---|
427 | * the program number field is the field from the |
---|
428 | * begining of the RPC header. MUST be changed if the |
---|
429 | * call_struct is changed |
---|
430 | */ |
---|
431 | if (info == NULL) |
---|
432 | return(FALSE); |
---|
433 | *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall + |
---|
434 | 3 * BYTES_PER_XDR_UNIT)); |
---|
435 | break; |
---|
436 | case CLSET_PROG: |
---|
437 | if (info == NULL) |
---|
438 | return(FALSE); |
---|
439 | *(u_long *)(ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT) |
---|
440 | = htonl(*(u_long *)info); |
---|
441 | break; |
---|
442 | case CLGET_LOCAL_ADDR: |
---|
443 | len = sizeof(struct sockaddr); |
---|
444 | if (getsockname(ct->ct_sock, (struct sockaddr *)info, &len) <0) |
---|
445 | return(FALSE); |
---|
446 | break; |
---|
447 | case CLGET_RETRY_TIMEOUT: |
---|
448 | case CLSET_RETRY_TIMEOUT: |
---|
449 | case CLGET_SVC_ADDR: |
---|
450 | case CLSET_SVC_ADDR: |
---|
451 | case CLSET_PUSH_TIMOD: |
---|
452 | case CLSET_POP_TIMOD: |
---|
453 | default: |
---|
454 | return (FALSE); |
---|
455 | } |
---|
456 | return (TRUE); |
---|
457 | } |
---|
458 | |
---|
459 | |
---|
460 | static void |
---|
461 | clntunix_destroy(h) |
---|
462 | CLIENT *h; |
---|
463 | { |
---|
464 | register struct ct_data *ct = |
---|
465 | (struct ct_data *) h->cl_private; |
---|
466 | |
---|
467 | if (ct->ct_closeit) { |
---|
468 | (void)_RPC_close(ct->ct_sock); |
---|
469 | } |
---|
470 | XDR_DESTROY(&(ct->ct_xdrs)); |
---|
471 | mem_free((caddr_t)ct, sizeof(struct ct_data)); |
---|
472 | mem_free((caddr_t)h, sizeof(CLIENT)); |
---|
473 | } |
---|
474 | |
---|
475 | /* |
---|
476 | * read() and write() are replaced with recvmsg()/sendmsg() so that |
---|
477 | * we can pass ancillary control data. In this case, the data constists |
---|
478 | * of credential information which the kernel will fill in for us. |
---|
479 | * XXX: This code is specific to FreeBSD and will not work on other |
---|
480 | * platforms without the requisite kernel modifications. |
---|
481 | */ |
---|
482 | struct cmessage { |
---|
483 | struct cmsghdr cmsg; |
---|
484 | struct cmsgcred cmcred; |
---|
485 | }; |
---|
486 | |
---|
487 | static int __msgread(sock, buf, cnt) |
---|
488 | int sock; |
---|
489 | void *buf; |
---|
490 | size_t cnt; |
---|
491 | { |
---|
492 | struct iovec iov[1]; |
---|
493 | struct msghdr msg; |
---|
494 | struct cmessage cm; |
---|
495 | |
---|
496 | bzero((char *)&cm, sizeof(cm)); |
---|
497 | iov[0].iov_base = buf; |
---|
498 | iov[0].iov_len = cnt; |
---|
499 | |
---|
500 | msg.msg_iov = iov; |
---|
501 | msg.msg_iovlen = 1; |
---|
502 | msg.msg_name = NULL; |
---|
503 | msg.msg_namelen = 0; |
---|
504 | msg.msg_control = (caddr_t)&cm; |
---|
505 | msg.msg_controllen = sizeof(struct cmessage); |
---|
506 | msg.msg_flags = 0; |
---|
507 | |
---|
508 | return(recvmsg(sock, &msg, 0)); |
---|
509 | } |
---|
510 | |
---|
511 | static int __msgwrite(sock, buf, cnt) |
---|
512 | int sock; |
---|
513 | void *buf; |
---|
514 | size_t cnt; |
---|
515 | { |
---|
516 | struct iovec iov[1]; |
---|
517 | struct msghdr msg; |
---|
518 | struct cmessage cm; |
---|
519 | |
---|
520 | bzero((char *)&cm, sizeof(cm)); |
---|
521 | iov[0].iov_base = buf; |
---|
522 | iov[0].iov_len = cnt; |
---|
523 | |
---|
524 | cm.cmsg.cmsg_type = SCM_CREDS; |
---|
525 | cm.cmsg.cmsg_level = SOL_SOCKET; |
---|
526 | cm.cmsg.cmsg_len = sizeof(struct cmessage); |
---|
527 | |
---|
528 | msg.msg_iov = iov; |
---|
529 | msg.msg_iovlen = 1; |
---|
530 | msg.msg_name = NULL; |
---|
531 | msg.msg_namelen = 0; |
---|
532 | msg.msg_control = (caddr_t)&cm; |
---|
533 | msg.msg_controllen = sizeof(struct cmessage); |
---|
534 | msg.msg_flags = 0; |
---|
535 | |
---|
536 | return(sendmsg(sock, &msg, 0)); |
---|
537 | } |
---|
538 | |
---|
539 | /* |
---|
540 | * Interface between xdr serializer and unix connection. |
---|
541 | * Behaves like the system calls, read & write, but keeps some error state |
---|
542 | * around for the rpc level. |
---|
543 | */ |
---|
544 | static int |
---|
545 | readunix(ct, buf, len) |
---|
546 | register struct ct_data *ct; |
---|
547 | caddr_t buf; |
---|
548 | register int len; |
---|
549 | { |
---|
550 | fd_set *fds, readfds; |
---|
551 | struct timeval start, after, duration, delta, tmp, tv; |
---|
552 | int r, save_errno; |
---|
553 | |
---|
554 | if (len == 0) |
---|
555 | return (0); |
---|
556 | |
---|
557 | if (ct->ct_sock + 1 > FD_SETSIZE) { |
---|
558 | int bytes = howmany(ct->ct_sock + 1, NFDBITS) * sizeof(fd_mask); |
---|
559 | fds = (fd_set *)malloc(bytes); |
---|
560 | if (fds == NULL) |
---|
561 | return (-1); |
---|
562 | memset(fds, 0, bytes); |
---|
563 | } else { |
---|
564 | fds = &readfds; |
---|
565 | FD_ZERO(fds); |
---|
566 | } |
---|
567 | |
---|
568 | gettimeofday(&start, NULL); |
---|
569 | delta = ct->ct_wait; |
---|
570 | while (TRUE) { |
---|
571 | /* XXX we know the other bits are still clear */ |
---|
572 | FD_SET(ct->ct_sock, fds); |
---|
573 | tv = delta; /* in case select writes back */ |
---|
574 | r = select(ct->ct_sock+1, fds, NULL, NULL, &tv); |
---|
575 | save_errno = errno; |
---|
576 | |
---|
577 | gettimeofday(&after, NULL); |
---|
578 | timersub(&start, &after, &duration); |
---|
579 | timersub(&delta, &duration, &tmp); |
---|
580 | delta = tmp; |
---|
581 | if (delta.tv_sec < 0 || !timerisset(&delta)) |
---|
582 | r = 0; |
---|
583 | |
---|
584 | switch (r) { |
---|
585 | case 0: |
---|
586 | if (fds != &readfds) |
---|
587 | free(fds); |
---|
588 | ct->ct_error.re_status = RPC_TIMEDOUT; |
---|
589 | return (-1); |
---|
590 | |
---|
591 | case -1: |
---|
592 | if (errno == EINTR) |
---|
593 | continue; |
---|
594 | if (fds != &readfds) |
---|
595 | free(fds); |
---|
596 | ct->ct_error.re_status = RPC_CANTRECV; |
---|
597 | ct->ct_error.re_errno = save_errno; |
---|
598 | return (-1); |
---|
599 | } |
---|
600 | break; |
---|
601 | } |
---|
602 | switch (len = __msgread(ct->ct_sock, buf, len)) { |
---|
603 | |
---|
604 | case 0: |
---|
605 | /* premature eof */ |
---|
606 | ct->ct_error.re_errno = ECONNRESET; |
---|
607 | ct->ct_error.re_status = RPC_CANTRECV; |
---|
608 | len = -1; /* it's really an error */ |
---|
609 | break; |
---|
610 | |
---|
611 | case -1: |
---|
612 | ct->ct_error.re_errno = errno; |
---|
613 | ct->ct_error.re_status = RPC_CANTRECV; |
---|
614 | break; |
---|
615 | } |
---|
616 | return (len); |
---|
617 | } |
---|
618 | |
---|
619 | static int |
---|
620 | writeunix(ct, buf, len) |
---|
621 | struct ct_data *ct; |
---|
622 | caddr_t buf; |
---|
623 | int len; |
---|
624 | { |
---|
625 | register int i, cnt; |
---|
626 | |
---|
627 | for (cnt = len; cnt > 0; cnt -= i, buf += i) { |
---|
628 | if ((i = __msgwrite(ct->ct_sock, buf, cnt)) == -1) { |
---|
629 | ct->ct_error.re_errno = errno; |
---|
630 | ct->ct_error.re_status = RPC_CANTSEND; |
---|
631 | return (-1); |
---|
632 | } |
---|
633 | } |
---|
634 | return (len); |
---|
635 | } |
---|