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 header.xid = htonl(req->xid);
192 header.type = htonl(1); /* REPLY */
193 header.stat = htonl(MSG_ACCEPTED);
194 header.verf_flavor = 0;
196 header.accept_stat = htonl(stat);
198 g_mutex_lock(req->connection->send_lock);
199 uint32_t fragment = htonl(sizeof(header) | 0x80000000);
200 async_rpc_write(req->connection, (const char *)&fragment, sizeof(fragment));
201 async_rpc_write(req->connection, (const char *)&header, sizeof(header));
202 g_io_channel_flush(req->connection->channel, NULL);
203 g_mutex_unlock(req->connection->send_lock);
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);
217 while (req->cleanup != NULL) {
218 struct cleanup_list *c = req->cleanup;
219 req->cleanup = c->next;
228 async_rpc_send_reply(RPCRequest *req, void *result)
230 bluesky_time_hires time_end;
232 GString *str = g_string_new("");
234 xdr_string_create(&xdr_out, str, XDR_ENCODE);
235 if (!req->xdr_result(&xdr_out, result)) {
236 async_rpc_send_failure(req, SYSTEM_ERR);
237 g_string_free(str, TRUE);
241 struct rpc_reply header;
242 header.xid = htonl(req->xid);
243 header.type = htonl(1); /* REPLY */
244 header.stat = htonl(MSG_ACCEPTED);
245 header.verf_flavor = 0;
247 header.accept_stat = 0;
249 g_mutex_lock(req->connection->send_lock);
250 gsize msg_size = str->len;
251 uint32_t fragment = htonl((msg_size + sizeof(header)) | 0x80000000);
252 async_rpc_write(req->connection, (const char *)&fragment, sizeof(fragment));
253 async_rpc_write(req->connection, (const char *)&header, sizeof(header));
254 async_rpc_write(req->connection, str->str, str->len);
255 g_io_channel_flush(req->connection->channel, NULL);
256 g_mutex_unlock(req->connection->send_lock);
258 time_end = bluesky_now_hires();
260 printf("RPC[%"PRIx32"]: time = %"PRId64" ns\n",
261 req->xid, time_end - req->time_start);
264 g_string_free(str, TRUE);
266 if (req->args != NULL) {
269 xdrmem_create(&xdr, buf, sizeof(buf), XDR_FREE);
270 if (!req->xdr_args_free(&xdr, req->args)) {
271 fprintf(stderr, "unable to free arguments");
275 if (req->raw_args != NULL)
276 g_string_free(req->raw_args, TRUE);
278 while (req->cleanup != NULL) {
279 struct cleanup_list *c = req->cleanup;
280 req->cleanup = c->next;
288 static const char *nfs_proc_names[] = {
289 [NFSPROC3_NULL] = "NULL",
290 [NFSPROC3_GETATTR] = "GETATTR",
291 [NFSPROC3_SETATTR] = "SETATTR",
292 [NFSPROC3_LOOKUP] = "LOOKUP",
293 [NFSPROC3_ACCESS] = "ACCESS",
294 [NFSPROC3_READLINK] = "READLINK",
295 [NFSPROC3_READ] = "READ",
296 [NFSPROC3_WRITE] = "WRITE",
297 [NFSPROC3_CREATE] = "CREATE",
298 [NFSPROC3_MKDIR] = "MKDIR",
299 [NFSPROC3_SYMLINK] = "SYMLINK",
300 [NFSPROC3_MKNOD] = "MKNOD",
301 [NFSPROC3_REMOVE] = "REMOVE",
302 [NFSPROC3_RMDIR] = "RMDIR",
303 [NFSPROC3_RENAME] = "RENAME",
304 [NFSPROC3_LINK] = "LINK",
305 [NFSPROC3_READDIR] = "READDIR",
306 [NFSPROC3_READDIRPLUS] = "READDIRPLUS",
307 [NFSPROC3_FSSTAT] = "FSSTAT",
308 [NFSPROC3_FSINFO] = "FSINFO",
309 [NFSPROC3_PATHCONF] = "PATHCONF",
310 [NFSPROC3_COMMIT] = "COMMIT",
314 nfs_program_3(RPCRequest *req)
316 RPCConnection *connection = req->connection;
317 uint32_t xid = req->xid;
318 const char *msg_buf = req->raw_args->str + req->raw_args_header_bytes;
319 size_t msg_len = req->raw_args->len - req->raw_args_header_bytes;
322 nfs_fh3 nfsproc3_getattr_3_arg;
323 setattr3args nfsproc3_setattr_3_arg;
324 diropargs3 nfsproc3_lookup_3_arg;
325 access3args nfsproc3_access_3_arg;
326 nfs_fh3 nfsproc3_readlink_3_arg;
327 read3args nfsproc3_read_3_arg;
328 write3args nfsproc3_write_3_arg;
329 create3args nfsproc3_create_3_arg;
330 mkdir3args nfsproc3_mkdir_3_arg;
331 symlink3args nfsproc3_symlink_3_arg;
332 mknod3args nfsproc3_mknod_3_arg;
333 diropargs3 nfsproc3_remove_3_arg;
334 diropargs3 nfsproc3_rmdir_3_arg;
335 rename3args nfsproc3_rename_3_arg;
336 link3args nfsproc3_link_3_arg;
337 readdir3args nfsproc3_readdir_3_arg;
338 readdirplus3args nfsproc3_readdirplus_3_arg;
339 nfs_fh3 nfsproc3_fsstat_3_arg;
340 nfs_fh3 nfsproc3_fsinfo_3_arg;
341 nfs_fh3 nfsproc3_pathconf_3_arg;
342 commit3args nfsproc3_commit_3_arg;
345 xdrproc_t _xdr_argument, _xdr_result;
346 char *(*local)(char *, RPCRequest *);
348 if (req->req_proc < sizeof(nfs_proc_names) / sizeof(const char *)) {
349 printf("Dispatched NFS RPC message type %s\n",
350 nfs_proc_names[req->req_proc]);
352 printf("Dispatched unknown NFS RPC message type %d\n", req->req_proc);
355 switch (req->req_proc) {
357 _xdr_argument = (xdrproc_t) xdr_void;
358 _xdr_result = (xdrproc_t) xdr_void;
359 local = (char *(*)(char *, RPCRequest *)) nfsproc3_null_3_svc;
362 case NFSPROC3_GETATTR:
363 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
364 _xdr_result = (xdrproc_t) xdr_getattr3res;
365 local = (char *(*)(char *, RPCRequest *)) nfsproc3_getattr_3_svc;
368 case NFSPROC3_SETATTR:
369 _xdr_argument = (xdrproc_t) xdr_setattr3args;
370 _xdr_result = (xdrproc_t) xdr_wccstat3;
371 local = (char *(*)(char *, RPCRequest *)) nfsproc3_setattr_3_svc;
374 case NFSPROC3_LOOKUP:
375 _xdr_argument = (xdrproc_t) xdr_diropargs3;
376 _xdr_result = (xdrproc_t) xdr_lookup3res;
377 local = (char *(*)(char *, RPCRequest *)) nfsproc3_lookup_3_svc;
380 case NFSPROC3_ACCESS:
381 _xdr_argument = (xdrproc_t) xdr_access3args;
382 _xdr_result = (xdrproc_t) xdr_access3res;
383 local = (char *(*)(char *, RPCRequest *)) nfsproc3_access_3_svc;
386 case NFSPROC3_READLINK:
387 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
388 _xdr_result = (xdrproc_t) xdr_readlink3res;
389 local = (char *(*)(char *, RPCRequest *)) nfsproc3_readlink_3_svc;
393 _xdr_argument = (xdrproc_t) xdr_read3args;
394 _xdr_result = (xdrproc_t) xdr_read3res;
395 local = (char *(*)(char *, RPCRequest *)) nfsproc3_read_3_svc;
399 _xdr_argument = (xdrproc_t) xdr_write3args;
400 _xdr_result = (xdrproc_t) xdr_write3res;
401 local = (char *(*)(char *, RPCRequest *)) nfsproc3_write_3_svc;
404 case NFSPROC3_CREATE:
405 _xdr_argument = (xdrproc_t) xdr_create3args;
406 _xdr_result = (xdrproc_t) xdr_diropres3;
407 local = (char *(*)(char *, RPCRequest *)) nfsproc3_create_3_svc;
411 _xdr_argument = (xdrproc_t) xdr_mkdir3args;
412 _xdr_result = (xdrproc_t) xdr_diropres3;
413 local = (char *(*)(char *, RPCRequest *)) nfsproc3_mkdir_3_svc;
416 case NFSPROC3_SYMLINK:
417 _xdr_argument = (xdrproc_t) xdr_symlink3args;
418 _xdr_result = (xdrproc_t) xdr_diropres3;
419 local = (char *(*)(char *, RPCRequest *)) nfsproc3_symlink_3_svc;
423 _xdr_argument = (xdrproc_t) xdr_mknod3args;
424 _xdr_result = (xdrproc_t) xdr_diropres3;
425 local = (char *(*)(char *, RPCRequest *)) nfsproc3_mknod_3_svc;
428 case NFSPROC3_REMOVE:
429 _xdr_argument = (xdrproc_t) xdr_diropargs3;
430 _xdr_result = (xdrproc_t) xdr_wccstat3;
431 local = (char *(*)(char *, RPCRequest *)) nfsproc3_remove_3_svc;
435 _xdr_argument = (xdrproc_t) xdr_diropargs3;
436 _xdr_result = (xdrproc_t) xdr_wccstat3;
437 local = (char *(*)(char *, RPCRequest *)) nfsproc3_rmdir_3_svc;
440 case NFSPROC3_RENAME:
441 _xdr_argument = (xdrproc_t) xdr_rename3args;
442 _xdr_result = (xdrproc_t) xdr_rename3res;
443 local = (char *(*)(char *, RPCRequest *)) nfsproc3_rename_3_svc;
447 _xdr_argument = (xdrproc_t) xdr_link3args;
448 _xdr_result = (xdrproc_t) xdr_link3res;
449 local = (char *(*)(char *, RPCRequest *)) nfsproc3_link_3_svc;
452 case NFSPROC3_READDIR:
453 _xdr_argument = (xdrproc_t) xdr_readdir3args;
454 _xdr_result = (xdrproc_t) xdr_readdir3res;
455 local = (char *(*)(char *, RPCRequest *)) nfsproc3_readdir_3_svc;
458 case NFSPROC3_READDIRPLUS:
459 _xdr_argument = (xdrproc_t) xdr_readdirplus3args;
460 _xdr_result = (xdrproc_t) xdr_readdirplus3res;
461 local = (char *(*)(char *, RPCRequest *)) nfsproc3_readdirplus_3_svc;
464 case NFSPROC3_FSSTAT:
465 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
466 _xdr_result = (xdrproc_t) xdr_fsstat3res;
467 local = (char *(*)(char *, RPCRequest *)) nfsproc3_fsstat_3_svc;
470 case NFSPROC3_FSINFO:
471 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
472 _xdr_result = (xdrproc_t) xdr_fsinfo3res;
473 local = (char *(*)(char *, RPCRequest *)) nfsproc3_fsinfo_3_svc;
476 case NFSPROC3_PATHCONF:
477 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
478 _xdr_result = (xdrproc_t) xdr_pathconf3res;
479 local = (char *(*)(char *, RPCRequest *)) nfsproc3_pathconf_3_svc;
482 case NFSPROC3_COMMIT:
483 _xdr_argument = (xdrproc_t) xdr_commit3args;
484 _xdr_result = (xdrproc_t) xdr_commit3res;
485 local = (char *(*)(char *, RPCRequest *)) nfsproc3_commit_3_svc;
489 async_rpc_send_failure(req, PROC_UNAVAIL);
493 /* Decode incoming message */
494 req->xdr_args_free = _xdr_argument;
495 req->args = g_new0(union argtype, 1);
497 xdrmem_create(&xdr_in, (char *)msg_buf, msg_len, XDR_DECODE);
498 if (!_xdr_argument(&xdr_in, req->args)) {
499 async_rpc_send_failure(req, GARBAGE_ARGS);
500 fprintf(stderr, "RPC decode error!\n");
504 /* Perform the call. */
505 req->xdr_result = _xdr_result;
506 result = (*local)((char *)req->args, req);
511 /* Enhanced, asynchronous-friendly RPC layer. This is a replacement for the
512 * built-in sunrpc parsing and dispatch that will allow for processing multiple
513 * requests at the same time. */
514 static GMainContext *main_context;
515 static GMainLoop *main_loop;
517 static GThreadPool *rpc_thread_pool;
519 static gboolean async_flushd(gpointer data)
521 bluesky_flushd_invoke(fs);
525 static void async_rpc_task(gpointer data, gpointer user_data)
527 nfs_program_3((RPCRequest *)data);
530 static async_rpc_init()
532 main_context = g_main_context_new();
533 main_loop = g_main_loop_new(main_context, FALSE);
535 rpc_thread_pool = g_thread_pool_new(async_rpc_task, NULL, -1, FALSE, NULL);
537 /* Arrange to have the cache writeback code run every five seconds. */
538 GSource *source = g_timeout_source_new_seconds(5);
539 g_source_set_callback(source, async_flushd, NULL, NULL);
540 g_source_attach(source, main_context);
541 g_source_unref(source);
544 struct rpc_call_header {
558 /* Decode an RPC message and process it. Returns a boolean indicating whether
559 * the message could be processed; if false, an unrecoverable error occurred
560 * and the transport should be closed. */
561 static gboolean async_rpc_dispatch(RPCConnection *rpc)
563 bluesky_time_hires time_start = bluesky_now_hires();
565 GString *msg = rpc->msgbuf;
566 const char *buf = msg->str;
568 if (msg->len < sizeof(struct rpc_call_header)) {
569 fprintf(stderr, "Short RPC message: only %zd bytes!\n", msg->len);
573 struct rpc_call_header *header = (struct rpc_call_header *)(msg->str);
574 uint32_t xid = ntohl(header->xid);
576 if (ntohl(header->mtype) != 0) {
577 /* Not an RPC call */
581 if (ntohl(header->rpcvers) != 2) {
585 RPCRequest *req = g_new0(RPCRequest, 1);
586 req->connection = rpc;
587 req->time_start = time_start;
590 if (ntohl(header->prog) != NFS_PROGRAM) {
591 async_rpc_send_failure(req, PROG_UNAVAIL);
593 } else if (ntohl(header->vers) != NFS_V3) {
594 /* FIXME: Should be PROG_MISMATCH */
595 async_rpc_send_failure(req, PROG_UNAVAIL);
599 uint32_t proc = ntohl(header->proc);
601 /* Next, skip over authentication headers. */
602 buf += sizeof(struct rpc_call_header);
603 for (i = 0; i < 2; i++) {
604 struct rpc_auth *auth = (struct rpc_auth *)buf;
605 if (buf - msg->str + sizeof(struct rpc_auth) > msg->len)
608 gsize authsize = ntohl(auth->len) + sizeof(struct rpc_auth);
609 if (authsize > MAX_RPC_MSGSIZE)
615 if (buf - msg->str > msg->len)
619 req->raw_args_header_bytes = buf - msg->str;
620 req->req_proc = ntohl(header->proc);
621 rpc->msgbuf = g_string_new("");
623 g_thread_pool_push(rpc_thread_pool, req, NULL);
628 /* Write the given data to the RPC socket. */
629 static void async_rpc_write(RPCConnection *rpc,
630 const char *buf, gsize len)
634 switch (g_io_channel_write_chars(rpc->channel, buf, len,
636 case G_IO_STATUS_ERROR:
637 case G_IO_STATUS_EOF:
638 case G_IO_STATUS_AGAIN:
639 fprintf(stderr, "Error writing to socket!\n");
641 case G_IO_STATUS_NORMAL:
648 // g_io_channel_flush(rpc->channel, NULL);
651 static gboolean async_rpc_do_read(GIOChannel *channel,
652 GIOCondition condition,
655 RPCConnection *rpc = (RPCConnection *)data;
657 gsize bytes_to_read = 0; /* Number of bytes to attempt to read. */
659 /* If we have not yet read in the fragment header, do that first. This is
660 * 4 bytes that indicates the number of bytes in the message to follow
661 * (with the high bit set if this is the last fragment making up the
663 if (rpc->frag_len == 0) {
664 bytes_to_read = 4 - rpc->frag_hdr_bytes;
666 bytes_to_read = rpc->frag_len & 0x7fffffff;
669 if (bytes_to_read > MAX_RPC_MSGSIZE
670 || rpc->msgbuf->len + bytes_to_read > MAX_RPC_MSGSIZE)
672 fprintf(stderr, "Excessive fragment size for RPC: %zd bytes\n",
674 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
678 gsize bytes_read = 0;
679 g_string_set_size(rpc->msgbuf, rpc->msgbuf->len + bytes_to_read);
680 char *buf = &rpc->msgbuf->str[rpc->msgbuf->len - bytes_to_read];
681 switch (g_io_channel_read_chars(rpc->channel, buf,
682 bytes_to_read, &bytes_read, NULL)) {
683 case G_IO_STATUS_NORMAL:
685 case G_IO_STATUS_AGAIN:
687 case G_IO_STATUS_EOF:
688 if (bytes_read == bytes_to_read)
690 /* else fall through */
691 case G_IO_STATUS_ERROR:
692 fprintf(stderr, "Unexpected error or end of file on RPC stream %d!\n",
693 g_io_channel_unix_get_fd(rpc->channel));
694 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
695 /* TODO: Clean up connection object. */
699 g_assert(bytes_read >= 0 && bytes_read <= bytes_to_read);
701 g_string_set_size(rpc->msgbuf,
702 rpc->msgbuf->len - (bytes_to_read - bytes_read));
704 if (rpc->frag_len == 0) {
705 /* Handle reading in the fragment header. If we've read the complete
706 * header, store the fragment size. */
707 rpc->frag_hdr_bytes += bytes_read;
708 if (rpc->frag_hdr_bytes == 4) {
709 memcpy((char *)&rpc->frag_len,
710 &rpc->msgbuf->str[rpc->msgbuf->len - 4], 4);
711 rpc->frag_len = ntohl(rpc->frag_len);
712 g_string_set_size(rpc->msgbuf, rpc->msgbuf->len - 4);
713 rpc->frag_hdr_bytes = 0;
716 /* We were reading in the fragment body. */
717 rpc->frag_len -= bytes_read;
719 if (rpc->frag_len = 0x80000000) {
720 /* We have a complete message since this was the last fragment and
721 * there are no more bytes in it. Dispatch the message. */
722 if (!async_rpc_dispatch(rpc)) {
723 fprintf(stderr, "Invalid RPC message, closing channel\n");
724 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
728 g_string_set_size(rpc->msgbuf, 0);
735 static gboolean async_rpc_do_accept(GIOChannel *channel,
736 GIOCondition condition,
739 int fd = g_io_channel_unix_get_fd(channel);
740 struct sockaddr_in addr;
741 socklen_t addrlen = sizeof(addr);
743 g_print("Received new connection on fd %d!\n", fd);
744 int nfd = accept(fd, (struct sockaddr *)&addr, &addrlen);
746 fprintf(stderr, "Error accepting connection: %m\n");
750 RPCConnection *rpc = g_new0(RPCConnection, 1);
751 rpc->channel = g_io_channel_unix_new(nfd);
752 rpc->msgbuf = g_string_new("");
753 g_io_channel_set_encoding(rpc->channel, NULL, NULL);
754 rpc->send_lock = g_mutex_new();
755 GSource *source = g_io_create_watch(rpc->channel, G_IO_IN);
756 g_source_set_callback(source, (GSourceFunc)async_rpc_do_read,
758 g_source_attach(source, main_context);
759 g_source_unref(source);
764 static async_rpc_register_listening(int fd)
766 GIOChannel *channel = g_io_channel_unix_new(fd);
767 g_io_channel_set_encoding(channel, NULL, NULL);
768 GSource *source = g_io_create_watch(channel, G_IO_IN);
769 g_source_set_callback(source, (GSourceFunc)async_rpc_do_accept,
771 g_source_attach(source, main_context);
772 g_source_unref(source);
775 static gpointer async_rpc_run(gpointer data)
777 g_print("Starting NFS main loop...\n");
778 g_main_loop_run(main_loop);
788 pmap_unset (MOUNT_PROGRAM, MOUNT_V3);
790 transp = svcudp_create(RPC_ANYSOCK);
791 if (transp == NULL) {
792 fprintf(stderr, "%s", "cannot create udp service.");
795 if (!svc_register(transp, MOUNT_PROGRAM, MOUNT_V3, mount_program_3, IPPROTO_UDP)) {
796 fprintf(stderr, "%s", "unable to register (MOUNT_PROGRAM, MOUNT_V3, udp).");
800 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
801 if (transp == NULL) {
802 fprintf(stderr, "%s", "cannot create tcp service.");
805 if (!svc_register(transp, MOUNT_PROGRAM, MOUNT_V3, mount_program_3, IPPROTO_TCP)) {
806 fprintf(stderr, "%s", "unable to register (MOUNT_PROGRAM, MOUNT_V3, tcp).");
810 /* NFS protocol (version 3) */
811 pmap_unset (NFS_PROGRAM, NFS_V3);
813 int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
815 fprintf(stderr, "Unable to create NFS TCP socket: %m\n");
820 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof(n));
822 struct sockaddr_in addr;
823 addr.sin_family = AF_INET;
824 addr.sin_port = htons(NFS_SERVICE_PORT);
825 addr.sin_addr.s_addr = INADDR_ANY;
826 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
827 fprintf(stderr, "Unable to bind to NFS TCP address: %m\n");
831 if (listen(fd, SOMAXCONN) < 0) {
832 fprintf(stderr, "Unable to listen on NFS TCP socket: %m\n");
836 if (!pmap_set(NFS_PROGRAM, NFS_V3, IPPROTO_TCP, NFS_SERVICE_PORT)) {
837 fprintf(stderr, "Could not register NFS RPC service!\n");
841 async_rpc_register_listening(fd);
843 g_thread_create(async_rpc_run, NULL, TRUE, NULL);