1 /* Blue Sky: File Systems in the Cloud
3 * Copyright (C) 2009 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
9 /* RPC handling: registration, marshalling and unmarshalling of messages. For
10 * now this uses the standard Sun RPC mechanisms in the standard C library.
11 * Later, it might be changed to use something better. Much of this code was
12 * generated with rpcgen from the XDR specifications, but has been hand-edited
15 #include "mount_prot.h"
16 #include "nfs3_prot.h"
19 #include <rpc/pmap_clnt.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <netinet/ip.h>
29 /* TCP port number to use for NFS protocol. (Should be 2049.) */
30 #define NFS_SERVICE_PORT 2051
32 /* Maximum size of a single RPC message that we will accept (8 MB). */
33 #define MAX_RPC_MSGSIZE (8 << 20)
36 mount_program_3(struct svc_req *rqstp, register SVCXPRT *transp)
39 dirpath mountproc3_mnt_3_arg;
40 dirpath mountproc3_umnt_3_arg;
43 xdrproc_t _xdr_argument, _xdr_result;
44 char *(*local)(char *, struct svc_req *);
46 switch (rqstp->rq_proc) {
48 _xdr_argument = (xdrproc_t) xdr_void;
49 _xdr_result = (xdrproc_t) xdr_void;
50 local = (char *(*)(char *, struct svc_req *)) mountproc3_null_3_svc;
54 _xdr_argument = (xdrproc_t) xdr_dirpath;
55 _xdr_result = (xdrproc_t) xdr_mountres3;
56 local = (char *(*)(char *, struct svc_req *)) mountproc3_mnt_3_svc;
60 _xdr_argument = (xdrproc_t) xdr_void;
61 _xdr_result = (xdrproc_t) xdr_mountlist;
62 local = (char *(*)(char *, struct svc_req *)) mountproc3_dump_3_svc;
66 _xdr_argument = (xdrproc_t) xdr_dirpath;
67 _xdr_result = (xdrproc_t) xdr_void;
68 local = (char *(*)(char *, struct svc_req *)) mountproc3_umnt_3_svc;
71 case MOUNTPROC3_UMNTALL:
72 _xdr_argument = (xdrproc_t) xdr_void;
73 _xdr_result = (xdrproc_t) xdr_void;
74 local = (char *(*)(char *, struct svc_req *)) mountproc3_umntall_3_svc;
77 case MOUNTPROC3_EXPORT:
78 _xdr_argument = (xdrproc_t) xdr_void;
79 _xdr_result = (xdrproc_t) xdr_exports;
80 local = (char *(*)(char *, struct svc_req *)) mountproc3_export_3_svc;
84 svcerr_noproc (transp);
87 memset ((char *)&argument, 0, sizeof (argument));
88 if (!svc_getargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
89 svcerr_decode (transp);
92 result = (*local)((char *)&argument, rqstp);
93 if (result != NULL && !svc_sendreply(transp, (xdrproc_t) _xdr_result, result)) {
94 svcerr_systemerr (transp);
96 if (!svc_freeargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
97 fprintf (stderr, "%s", "unable to free arguments");
107 uint32_t verf_flavor;
109 uint32_t accept_stat;
112 static void async_rpc_write(RPCConnection *rpc,
113 const char *buf, gsize len);
115 struct rpc_fail_reply {
119 uint32_t verf_flavor;
121 uint32_t accept_stat;
124 /* Routines for XDR-encoding to a growable string. */
125 static bool_t xdr_string_putlong(XDR *xdrs, const long *lp)
127 GString *str = (GString *)xdrs->x_private;
128 uint32_t data = htonl(*lp);
129 g_string_set_size(str, str->len + 4);
130 memcpy(str->str + str->len - 4, &data, 4);
134 static bool_t xdr_string_putbytes(XDR *xdrs, const char *addr, u_int len)
136 GString *str = (GString *)xdrs->x_private;
137 g_string_set_size(str, str->len + len);
138 memcpy(str->str + str->len - len, addr, len);
142 static u_int xdr_string_getpos(const XDR *xdrs)
144 GString *str = (GString *)xdrs->x_private;
148 static bool_t xdr_string_putint32(XDR *xdrs, const int32_t *ip)
150 GString *str = (GString *)xdrs->x_private;
151 uint32_t data = htonl(*ip);
152 g_string_set_size(str, str->len + 4);
153 memcpy(str->str + str->len - 4, &data, 4);
157 static int32_t *xdr_string_inline(XDR *xdrs, u_int len)
159 GString *str = (GString *)xdrs->x_private;
160 g_string_set_size(str, str->len + len);
161 return (int32_t *)(str->str + str->len - len);
164 static void xdr_string_destroy(XDR *xdrs)
168 static struct xdr_ops xdr_string_ops = {
169 .x_putlong = xdr_string_putlong,
170 .x_putbytes = xdr_string_putbytes,
171 .x_getpostn = xdr_string_getpos,
172 .x_putint32 = xdr_string_putint32,
173 .x_inline = xdr_string_inline,
174 .x_destroy = xdr_string_destroy,
177 static void xdr_string_create(XDR *xdrs, GString *string, enum xdr_op op)
180 xdrs->x_ops = &xdr_string_ops;
181 xdrs->x_private = (char *)string;
187 async_rpc_send_failure(RPCRequest *req, enum accept_stat stat)
189 struct rpc_fail_reply header;
191 fprintf(stderr, "Sending RPC failure status %d\n", stat);
193 header.xid = htonl(req->xid);
194 header.type = htonl(1); /* REPLY */
195 header.stat = htonl(MSG_ACCEPTED);
196 header.verf_flavor = 0;
198 header.accept_stat = htonl(stat);
200 uint32_t fragment = htonl(sizeof(header) | 0x80000000);
201 async_rpc_write(req->connection, (const char *)&fragment, sizeof(fragment));
202 async_rpc_write(req->connection, (const char *)&header, sizeof(header));
203 g_io_channel_flush(req->connection->channel, NULL);
205 if (req->args != NULL) {
208 xdrmem_create(&xdr, buf, sizeof(buf), XDR_FREE);
209 if (!req->xdr_args_free(&xdr, req->args)) {
210 fprintf(stderr, "unable to free arguments");
214 if (req->raw_args != NULL)
215 g_string_free(req->raw_args, TRUE);
221 async_rpc_send_reply(RPCRequest *req, void *result)
223 GString *str = g_string_new("");
225 xdr_string_create(&xdr_out, str, XDR_ENCODE);
226 if (!req->xdr_result(&xdr_out, result)) {
227 async_rpc_send_failure(req, SYSTEM_ERR);
228 g_string_free(str, TRUE);
232 struct rpc_reply header;
233 header.xid = htonl(req->xid);
234 header.type = htonl(1); /* REPLY */
235 header.stat = htonl(MSG_ACCEPTED);
236 header.verf_flavor = 0;
238 header.accept_stat = 0;
240 gsize msg_size = str->len;
241 printf("Have an RPC reply of size %zd bytes\n", msg_size);
242 uint32_t fragment = htonl((msg_size + sizeof(header)) | 0x80000000);
243 async_rpc_write(req->connection, (const char *)&fragment, sizeof(fragment));
244 async_rpc_write(req->connection, (const char *)&header, sizeof(header));
245 async_rpc_write(req->connection, str->str, str->len);
246 g_io_channel_flush(req->connection->channel, NULL);
249 g_string_free(str, TRUE);
251 if (req->args != NULL) {
254 xdrmem_create(&xdr, buf, sizeof(buf), XDR_FREE);
255 if (!req->xdr_args_free(&xdr, req->args)) {
256 fprintf(stderr, "unable to free arguments");
260 if (req->raw_args != NULL)
261 g_string_free(req->raw_args, TRUE);
267 nfs_program_3(RPCRequest *req)
269 RPCConnection *connection = req->connection;
270 uint32_t xid = req->xid;
271 const char *msg_buf = req->raw_args->str + req->raw_args_header_bytes;
272 size_t msg_len = req->raw_args->len - req->raw_args_header_bytes;
275 nfs_fh3 nfsproc3_getattr_3_arg;
276 setattr3args nfsproc3_setattr_3_arg;
277 diropargs3 nfsproc3_lookup_3_arg;
278 access3args nfsproc3_access_3_arg;
279 nfs_fh3 nfsproc3_readlink_3_arg;
280 read3args nfsproc3_read_3_arg;
281 write3args nfsproc3_write_3_arg;
282 create3args nfsproc3_create_3_arg;
283 mkdir3args nfsproc3_mkdir_3_arg;
284 symlink3args nfsproc3_symlink_3_arg;
285 mknod3args nfsproc3_mknod_3_arg;
286 diropargs3 nfsproc3_remove_3_arg;
287 diropargs3 nfsproc3_rmdir_3_arg;
288 rename3args nfsproc3_rename_3_arg;
289 link3args nfsproc3_link_3_arg;
290 readdir3args nfsproc3_readdir_3_arg;
291 readdirplus3args nfsproc3_readdirplus_3_arg;
292 nfs_fh3 nfsproc3_fsstat_3_arg;
293 nfs_fh3 nfsproc3_fsinfo_3_arg;
294 nfs_fh3 nfsproc3_pathconf_3_arg;
295 commit3args nfsproc3_commit_3_arg;
298 xdrproc_t _xdr_argument, _xdr_result;
299 char *(*local)(char *, RPCRequest *);
301 switch (req->req_proc) {
303 _xdr_argument = (xdrproc_t) xdr_void;
304 _xdr_result = (xdrproc_t) xdr_void;
305 local = (char *(*)(char *, RPCRequest *)) nfsproc3_null_3_svc;
308 case NFSPROC3_GETATTR:
309 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
310 _xdr_result = (xdrproc_t) xdr_getattr3res;
311 local = (char *(*)(char *, RPCRequest *)) nfsproc3_getattr_3_svc;
314 case NFSPROC3_SETATTR:
315 _xdr_argument = (xdrproc_t) xdr_setattr3args;
316 _xdr_result = (xdrproc_t) xdr_wccstat3;
317 local = (char *(*)(char *, RPCRequest *)) nfsproc3_setattr_3_svc;
320 case NFSPROC3_LOOKUP:
321 _xdr_argument = (xdrproc_t) xdr_diropargs3;
322 _xdr_result = (xdrproc_t) xdr_lookup3res;
323 local = (char *(*)(char *, RPCRequest *)) nfsproc3_lookup_3_svc;
326 case NFSPROC3_ACCESS:
327 _xdr_argument = (xdrproc_t) xdr_access3args;
328 _xdr_result = (xdrproc_t) xdr_access3res;
329 local = (char *(*)(char *, RPCRequest *)) nfsproc3_access_3_svc;
332 case NFSPROC3_READLINK:
333 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
334 _xdr_result = (xdrproc_t) xdr_readlink3res;
335 local = (char *(*)(char *, RPCRequest *)) nfsproc3_readlink_3_svc;
339 _xdr_argument = (xdrproc_t) xdr_read3args;
340 _xdr_result = (xdrproc_t) xdr_read3res;
341 local = (char *(*)(char *, RPCRequest *)) nfsproc3_read_3_svc;
345 _xdr_argument = (xdrproc_t) xdr_write3args;
346 _xdr_result = (xdrproc_t) xdr_write3res;
347 local = (char *(*)(char *, RPCRequest *)) nfsproc3_write_3_svc;
350 case NFSPROC3_CREATE:
351 _xdr_argument = (xdrproc_t) xdr_create3args;
352 _xdr_result = (xdrproc_t) xdr_diropres3;
353 local = (char *(*)(char *, RPCRequest *)) nfsproc3_create_3_svc;
357 _xdr_argument = (xdrproc_t) xdr_mkdir3args;
358 _xdr_result = (xdrproc_t) xdr_diropres3;
359 local = (char *(*)(char *, RPCRequest *)) nfsproc3_mkdir_3_svc;
362 case NFSPROC3_SYMLINK:
363 _xdr_argument = (xdrproc_t) xdr_symlink3args;
364 _xdr_result = (xdrproc_t) xdr_diropres3;
365 local = (char *(*)(char *, RPCRequest *)) nfsproc3_symlink_3_svc;
369 _xdr_argument = (xdrproc_t) xdr_mknod3args;
370 _xdr_result = (xdrproc_t) xdr_diropres3;
371 local = (char *(*)(char *, RPCRequest *)) nfsproc3_mknod_3_svc;
374 case NFSPROC3_REMOVE:
375 _xdr_argument = (xdrproc_t) xdr_diropargs3;
376 _xdr_result = (xdrproc_t) xdr_wccstat3;
377 local = (char *(*)(char *, RPCRequest *)) nfsproc3_remove_3_svc;
381 _xdr_argument = (xdrproc_t) xdr_diropargs3;
382 _xdr_result = (xdrproc_t) xdr_wccstat3;
383 local = (char *(*)(char *, RPCRequest *)) nfsproc3_rmdir_3_svc;
386 case NFSPROC3_RENAME:
387 _xdr_argument = (xdrproc_t) xdr_rename3args;
388 _xdr_result = (xdrproc_t) xdr_rename3res;
389 local = (char *(*)(char *, RPCRequest *)) nfsproc3_rename_3_svc;
393 _xdr_argument = (xdrproc_t) xdr_link3args;
394 _xdr_result = (xdrproc_t) xdr_link3res;
395 local = (char *(*)(char *, RPCRequest *)) nfsproc3_link_3_svc;
398 case NFSPROC3_READDIR:
399 _xdr_argument = (xdrproc_t) xdr_readdir3args;
400 _xdr_result = (xdrproc_t) xdr_readdir3res;
401 local = (char *(*)(char *, RPCRequest *)) nfsproc3_readdir_3_svc;
404 case NFSPROC3_READDIRPLUS:
405 _xdr_argument = (xdrproc_t) xdr_readdirplus3args;
406 _xdr_result = (xdrproc_t) xdr_readdirplus3res;
407 local = (char *(*)(char *, RPCRequest *)) nfsproc3_readdirplus_3_svc;
410 case NFSPROC3_FSSTAT:
411 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
412 _xdr_result = (xdrproc_t) xdr_fsstat3res;
413 local = (char *(*)(char *, RPCRequest *)) nfsproc3_fsstat_3_svc;
416 case NFSPROC3_FSINFO:
417 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
418 _xdr_result = (xdrproc_t) xdr_fsinfo3res;
419 local = (char *(*)(char *, RPCRequest *)) nfsproc3_fsinfo_3_svc;
422 case NFSPROC3_PATHCONF:
423 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
424 _xdr_result = (xdrproc_t) xdr_pathconf3res;
425 local = (char *(*)(char *, RPCRequest *)) nfsproc3_pathconf_3_svc;
428 case NFSPROC3_COMMIT:
429 _xdr_argument = (xdrproc_t) xdr_commit3args;
430 _xdr_result = (xdrproc_t) xdr_commit3res;
431 local = (char *(*)(char *, RPCRequest *)) nfsproc3_commit_3_svc;
435 async_rpc_send_failure(req, PROC_UNAVAIL);
439 /* Decode incoming message */
440 req->xdr_args_free = _xdr_argument;
441 req->args = g_new0(union argtype, 1);
443 xdrmem_create(&xdr_in, (char *)msg_buf, msg_len, XDR_DECODE);
444 if (!_xdr_argument(&xdr_in, req->args)) {
445 async_rpc_send_failure(req, GARBAGE_ARGS);
446 fprintf(stderr, "RPC decode error!\n");
450 /* Perform the call. */
451 req->xdr_result = _xdr_result;
452 result = (*local)((char *)req->args, req);
454 bluesky_flushd_invoke(fs);
459 /* Enhanced, asynchronous-friendly RPC layer. This is a replacement for the
460 * built-in sunrpc parsing and dispatch that will allow for processing multiple
461 * requests at the same time. */
462 static GMainContext *main_context;
463 static GMainLoop *main_loop;
465 static async_rpc_init()
467 main_context = g_main_context_new();
468 main_loop = g_main_loop_new(main_context, FALSE);
471 struct rpc_call_header {
485 /* Decode an RPC message and process it. Returns a boolean indicating whether
486 * the message could be processed; if false, an unrecoverable error occurred
487 * and the transport should be closed. */
488 static gboolean async_rpc_dispatch(RPCConnection *rpc)
491 GString *msg = rpc->msgbuf;
492 const char *buf = msg->str;
494 if (msg->len < sizeof(struct rpc_call_header)) {
495 fprintf(stderr, "Short RPC message: only %zd bytes!\n", msg->len);
499 struct rpc_call_header *header = (struct rpc_call_header *)(msg->str);
500 uint32_t xid = ntohl(header->xid);
502 if (ntohl(header->mtype) != 0) {
503 /* Not an RPC call */
507 if (ntohl(header->rpcvers) != 2) {
511 RPCRequest *req = g_new0(RPCRequest, 1);
512 req->connection = rpc;
515 if (ntohl(header->prog) != NFS_PROGRAM) {
516 async_rpc_send_failure(req, PROG_UNAVAIL);
518 } else if (ntohl(header->vers) != NFS_V3) {
519 /* FIXME: Should be PROG_MISMATCH */
520 async_rpc_send_failure(req, PROG_UNAVAIL);
524 uint32_t proc = ntohl(header->proc);
526 /* Next, skip over authentication headers. */
527 buf += sizeof(struct rpc_call_header);
528 for (i = 0; i < 2; i++) {
529 struct rpc_auth *auth = (struct rpc_auth *)buf;
530 if (buf - msg->str + sizeof(struct rpc_auth) > msg->len)
533 gsize authsize = ntohl(auth->len) + sizeof(struct rpc_auth);
534 if (authsize > MAX_RPC_MSGSIZE)
540 if (buf - msg->str > msg->len)
543 printf("Dispatching RPC procedure %d...\n", proc);
546 req->raw_args_header_bytes = buf - msg->str;
547 req->req_proc = ntohl(header->proc);
548 rpc->msgbuf = g_string_new("");
555 /* Write the given data to the RPC socket. */
556 static void async_rpc_write(RPCConnection *rpc,
557 const char *buf, gsize len)
561 switch (g_io_channel_write_chars(rpc->channel, buf, len,
563 case G_IO_STATUS_ERROR:
564 case G_IO_STATUS_EOF:
565 case G_IO_STATUS_AGAIN:
566 fprintf(stderr, "Error writing to socket!\n");
568 case G_IO_STATUS_NORMAL:
575 // g_io_channel_flush(rpc->channel, NULL);
578 static gboolean async_rpc_do_read(GIOChannel *channel,
579 GIOCondition condition,
582 RPCConnection *rpc = (RPCConnection *)data;
584 gsize bytes_to_read = 0; /* Number of bytes to attempt to read. */
586 /* If we have not yet read in the fragment header, do that first. This is
587 * 4 bytes that indicates the number of bytes in the message to follow
588 * (with the high bit set if this is the last fragment making up the
590 if (rpc->frag_len == 0) {
591 bytes_to_read = 4 - rpc->frag_hdr_bytes;
593 bytes_to_read = rpc->frag_len & 0x7fffffff;
596 if (bytes_to_read > MAX_RPC_MSGSIZE
597 || rpc->msgbuf->len + bytes_to_read > MAX_RPC_MSGSIZE)
599 fprintf(stderr, "Excessive fragment size for RPC: %zd bytes\n",
601 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
605 gsize bytes_read = 0;
606 g_string_set_size(rpc->msgbuf, rpc->msgbuf->len + bytes_to_read);
607 char *buf = &rpc->msgbuf->str[rpc->msgbuf->len - bytes_to_read];
608 switch (g_io_channel_read_chars(rpc->channel, buf,
609 bytes_to_read, &bytes_read, NULL)) {
610 case G_IO_STATUS_NORMAL:
612 case G_IO_STATUS_AGAIN:
614 case G_IO_STATUS_EOF:
615 if (bytes_read == bytes_to_read)
617 /* else fall through */
618 case G_IO_STATUS_ERROR:
619 fprintf(stderr, "Unexpected error or end of file on RPC stream %d!\n",
620 g_io_channel_unix_get_fd(rpc->channel));
621 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
625 g_assert(bytes_read >= 0 && bytes_read <= bytes_to_read);
627 g_string_set_size(rpc->msgbuf,
628 rpc->msgbuf->len - (bytes_to_read - bytes_read));
630 if (rpc->frag_len == 0) {
631 /* Handle reading in the fragment header. If we've read the complete
632 * header, store the fragment size. */
633 rpc->frag_hdr_bytes += bytes_read;
634 if (rpc->frag_hdr_bytes == 4) {
635 memcpy((char *)&rpc->frag_len,
636 &rpc->msgbuf->str[rpc->msgbuf->len - 4], 4);
637 rpc->frag_len = ntohl(rpc->frag_len);
638 g_string_set_size(rpc->msgbuf, rpc->msgbuf->len - 4);
639 rpc->frag_hdr_bytes = 0;
640 g_print("RPC fragment header: %08x\n", rpc->frag_len);
643 /* We were reading in the fragment body. */
644 rpc->frag_len -= bytes_read;
646 if (rpc->frag_len = 0x80000000) {
647 /* We have a complete message since this was the last fragment and
648 * there are no more bytes in it. Dispatch the message. */
649 g_print("Complete RPC message: %zd bytes\n", rpc->msgbuf->len);
650 if (!async_rpc_dispatch(rpc)) {
651 fprintf(stderr, "Invalid RPC message, closing channel\n");
652 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
656 g_string_set_size(rpc->msgbuf, 0);
663 static gboolean async_rpc_do_accept(GIOChannel *channel,
664 GIOCondition condition,
667 int fd = g_io_channel_unix_get_fd(channel);
668 struct sockaddr_in addr;
669 socklen_t addrlen = sizeof(addr);
671 g_print("Received new connection on fd %d!\n", fd);
672 int nfd = accept(fd, (struct sockaddr *)&addr, &addrlen);
674 fprintf(stderr, "Error accepting connection: %m\n");
678 RPCConnection *rpc = g_new0(RPCConnection, 1);
679 rpc->channel = g_io_channel_unix_new(nfd);
680 rpc->msgbuf = g_string_new("");
681 g_io_channel_set_encoding(rpc->channel, NULL, NULL);
682 GSource *source = g_io_create_watch(rpc->channel, G_IO_IN);
683 g_source_set_callback(source, (GSourceFunc)async_rpc_do_read,
685 g_source_attach(source, main_context);
686 g_source_unref(source);
691 static async_rpc_register_listening(int fd)
693 GIOChannel *channel = g_io_channel_unix_new(fd);
694 g_io_channel_set_encoding(channel, NULL, NULL);
695 GSource *source = g_io_create_watch(channel, G_IO_IN);
696 g_source_set_callback(source, (GSourceFunc)async_rpc_do_accept,
698 g_source_attach(source, main_context);
699 g_source_unref(source);
702 static gpointer async_rpc_run(gpointer data)
704 g_print("Starting NFS main loop...\n");
705 g_main_loop_run(main_loop);
715 pmap_unset (MOUNT_PROGRAM, MOUNT_V3);
717 transp = svcudp_create(RPC_ANYSOCK);
718 if (transp == NULL) {
719 fprintf(stderr, "%s", "cannot create udp service.");
722 if (!svc_register(transp, MOUNT_PROGRAM, MOUNT_V3, mount_program_3, IPPROTO_UDP)) {
723 fprintf(stderr, "%s", "unable to register (MOUNT_PROGRAM, MOUNT_V3, udp).");
727 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
728 if (transp == NULL) {
729 fprintf(stderr, "%s", "cannot create tcp service.");
732 if (!svc_register(transp, MOUNT_PROGRAM, MOUNT_V3, mount_program_3, IPPROTO_TCP)) {
733 fprintf(stderr, "%s", "unable to register (MOUNT_PROGRAM, MOUNT_V3, tcp).");
737 /* NFS protocol (version 3) */
738 pmap_unset (NFS_PROGRAM, NFS_V3);
740 int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
742 fprintf(stderr, "Unable to create NFS TCP socket: %m\n");
747 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof(n));
749 struct sockaddr_in addr;
750 addr.sin_family = AF_INET;
751 addr.sin_port = htons(NFS_SERVICE_PORT);
752 addr.sin_addr.s_addr = INADDR_ANY;
753 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
754 fprintf(stderr, "Unable to bind to NFS TCP address: %m\n");
758 if (listen(fd, SOMAXCONN) < 0) {
759 fprintf(stderr, "Unable to listen on NFS TCP socket: %m\n");
763 if (!pmap_set(NFS_PROGRAM, NFS_V3, IPPROTO_TCP, NFS_SERVICE_PORT)) {
764 fprintf(stderr, "Could not register NFS RPC service!\n");
768 async_rpc_register_listening(fd);
770 g_thread_create(async_rpc_run, NULL, TRUE, NULL);