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>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <netinet/ip.h>
30 /* TCP port number to use for NFS protocol. (Should be 2049.) */
31 #define NFS_SERVICE_PORT 2051
33 /* Maximum size of a single RPC message that we will accept (8 MB). */
34 #define MAX_RPC_MSGSIZE (8 << 20)
37 mount_program_3(struct svc_req *rqstp, register SVCXPRT *transp)
40 dirpath mountproc3_mnt_3_arg;
41 dirpath mountproc3_umnt_3_arg;
44 xdrproc_t _xdr_argument, _xdr_result;
45 char *(*local)(char *, struct svc_req *);
47 switch (rqstp->rq_proc) {
49 _xdr_argument = (xdrproc_t) xdr_void;
50 _xdr_result = (xdrproc_t) xdr_void;
51 local = (char *(*)(char *, struct svc_req *)) mountproc3_null_3_svc;
55 _xdr_argument = (xdrproc_t) xdr_dirpath;
56 _xdr_result = (xdrproc_t) xdr_mountres3;
57 local = (char *(*)(char *, struct svc_req *)) mountproc3_mnt_3_svc;
61 _xdr_argument = (xdrproc_t) xdr_void;
62 _xdr_result = (xdrproc_t) xdr_mountlist;
63 local = (char *(*)(char *, struct svc_req *)) mountproc3_dump_3_svc;
67 _xdr_argument = (xdrproc_t) xdr_dirpath;
68 _xdr_result = (xdrproc_t) xdr_void;
69 local = (char *(*)(char *, struct svc_req *)) mountproc3_umnt_3_svc;
72 case MOUNTPROC3_UMNTALL:
73 _xdr_argument = (xdrproc_t) xdr_void;
74 _xdr_result = (xdrproc_t) xdr_void;
75 local = (char *(*)(char *, struct svc_req *)) mountproc3_umntall_3_svc;
78 case MOUNTPROC3_EXPORT:
79 _xdr_argument = (xdrproc_t) xdr_void;
80 _xdr_result = (xdrproc_t) xdr_exports;
81 local = (char *(*)(char *, struct svc_req *)) mountproc3_export_3_svc;
85 svcerr_noproc (transp);
88 memset ((char *)&argument, 0, sizeof (argument));
89 if (!svc_getargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
90 svcerr_decode (transp);
93 result = (*local)((char *)&argument, rqstp);
94 if (result != NULL && !svc_sendreply(transp, (xdrproc_t) _xdr_result, result)) {
95 svcerr_systemerr (transp);
97 if (!svc_freeargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
98 fprintf (stderr, "%s", "unable to free arguments");
108 uint32_t verf_flavor;
110 uint32_t accept_stat;
113 static void async_rpc_write(RPCConnection *rpc,
114 const char *buf, gsize len);
116 struct rpc_fail_reply {
120 uint32_t verf_flavor;
122 uint32_t accept_stat;
125 /* Routines for XDR-encoding to a growable string. */
126 static bool_t xdr_string_putlong(XDR *xdrs, const long *lp)
128 GString *str = (GString *)xdrs->x_private;
129 uint32_t data = htonl(*lp);
130 g_string_set_size(str, str->len + 4);
131 memcpy(str->str + str->len - 4, &data, 4);
135 static bool_t xdr_string_putbytes(XDR *xdrs, const char *addr, u_int len)
137 GString *str = (GString *)xdrs->x_private;
138 g_string_set_size(str, str->len + len);
139 memcpy(str->str + str->len - len, addr, len);
143 static u_int xdr_string_getpos(const XDR *xdrs)
145 GString *str = (GString *)xdrs->x_private;
149 static bool_t xdr_string_putint32(XDR *xdrs, const int32_t *ip)
151 GString *str = (GString *)xdrs->x_private;
152 uint32_t data = htonl(*ip);
153 g_string_set_size(str, str->len + 4);
154 memcpy(str->str + str->len - 4, &data, 4);
158 static int32_t *xdr_string_inline(XDR *xdrs, u_int len)
160 GString *str = (GString *)xdrs->x_private;
161 g_string_set_size(str, str->len + len);
162 return (int32_t *)(str->str + str->len - len);
165 static void xdr_string_destroy(XDR *xdrs)
169 static struct xdr_ops xdr_string_ops = {
170 .x_putlong = xdr_string_putlong,
171 .x_putbytes = xdr_string_putbytes,
172 .x_getpostn = xdr_string_getpos,
173 .x_putint32 = xdr_string_putint32,
174 .x_inline = xdr_string_inline,
175 .x_destroy = xdr_string_destroy,
178 static void xdr_string_create(XDR *xdrs, GString *string, enum xdr_op op)
181 xdrs->x_ops = &xdr_string_ops;
182 xdrs->x_private = (char *)string;
188 async_rpc_send_failure(RPCRequest *req, enum accept_stat stat)
190 struct rpc_fail_reply header;
192 header.xid = htonl(req->xid);
193 header.type = htonl(1); /* REPLY */
194 header.stat = htonl(MSG_ACCEPTED);
195 header.verf_flavor = 0;
197 header.accept_stat = htonl(stat);
199 g_mutex_lock(req->connection->send_lock);
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);
204 g_mutex_unlock(req->connection->send_lock);
206 if (req->args != NULL) {
209 xdrmem_create(&xdr, buf, sizeof(buf), XDR_FREE);
210 if (!req->xdr_args_free(&xdr, req->args)) {
211 fprintf(stderr, "unable to free arguments");
216 if (req->raw_args != NULL)
217 g_string_free(req->raw_args, TRUE);
219 while (req->cleanup != NULL) {
220 struct cleanup_list *c = req->cleanup;
221 req->cleanup = c->next;
230 async_rpc_send_reply(RPCRequest *req, void *result)
232 bluesky_time_hires time_end;
234 GString *str = g_string_new("");
236 xdr_string_create(&xdr_out, str, XDR_ENCODE);
237 if (!req->xdr_result(&xdr_out, result)) {
238 async_rpc_send_failure(req, SYSTEM_ERR);
239 g_string_free(str, TRUE);
243 struct rpc_reply header;
244 header.xid = htonl(req->xid);
245 header.type = htonl(1); /* REPLY */
246 header.stat = htonl(MSG_ACCEPTED);
247 header.verf_flavor = 0;
249 header.accept_stat = 0;
251 g_mutex_lock(req->connection->send_lock);
252 gsize msg_size = str->len;
253 uint32_t fragment = htonl((msg_size + sizeof(header)) | 0x80000000);
254 async_rpc_write(req->connection, (const char *)&fragment, sizeof(fragment));
255 async_rpc_write(req->connection, (const char *)&header, sizeof(header));
256 async_rpc_write(req->connection, str->str, str->len);
257 g_io_channel_flush(req->connection->channel, NULL);
258 g_mutex_unlock(req->connection->send_lock);
260 time_end = bluesky_now_hires();
262 printf("RPC[%"PRIx32"]: time = %"PRId64" ns\n",
263 req->xid, time_end - req->time_start);
266 g_string_free(str, TRUE);
268 if (req->args != NULL) {
271 xdrmem_create(&xdr, buf, sizeof(buf), XDR_FREE);
272 if (!req->xdr_args_free(&xdr, req->args)) {
273 fprintf(stderr, "unable to free arguments");
278 if (req->raw_args != NULL)
279 g_string_free(req->raw_args, TRUE);
281 while (req->cleanup != NULL) {
282 struct cleanup_list *c = req->cleanup;
283 req->cleanup = c->next;
291 static const char *nfs_proc_names[] = {
292 [NFSPROC3_NULL] = "NULL",
293 [NFSPROC3_GETATTR] = "GETATTR",
294 [NFSPROC3_SETATTR] = "SETATTR",
295 [NFSPROC3_LOOKUP] = "LOOKUP",
296 [NFSPROC3_ACCESS] = "ACCESS",
297 [NFSPROC3_READLINK] = "READLINK",
298 [NFSPROC3_READ] = "READ",
299 [NFSPROC3_WRITE] = "WRITE",
300 [NFSPROC3_CREATE] = "CREATE",
301 [NFSPROC3_MKDIR] = "MKDIR",
302 [NFSPROC3_SYMLINK] = "SYMLINK",
303 [NFSPROC3_MKNOD] = "MKNOD",
304 [NFSPROC3_REMOVE] = "REMOVE",
305 [NFSPROC3_RMDIR] = "RMDIR",
306 [NFSPROC3_RENAME] = "RENAME",
307 [NFSPROC3_LINK] = "LINK",
308 [NFSPROC3_READDIR] = "READDIR",
309 [NFSPROC3_READDIRPLUS] = "READDIRPLUS",
310 [NFSPROC3_FSSTAT] = "FSSTAT",
311 [NFSPROC3_FSINFO] = "FSINFO",
312 [NFSPROC3_PATHCONF] = "PATHCONF",
313 [NFSPROC3_COMMIT] = "COMMIT",
317 nfs_program_3(RPCRequest *req)
319 RPCConnection *connection = req->connection;
320 uint32_t xid = req->xid;
321 const char *msg_buf = req->raw_args->str + req->raw_args_header_bytes;
322 size_t msg_len = req->raw_args->len - req->raw_args_header_bytes;
325 nfs_fh3 nfsproc3_getattr_3_arg;
326 setattr3args nfsproc3_setattr_3_arg;
327 diropargs3 nfsproc3_lookup_3_arg;
328 access3args nfsproc3_access_3_arg;
329 nfs_fh3 nfsproc3_readlink_3_arg;
330 read3args nfsproc3_read_3_arg;
331 write3args nfsproc3_write_3_arg;
332 create3args nfsproc3_create_3_arg;
333 mkdir3args nfsproc3_mkdir_3_arg;
334 symlink3args nfsproc3_symlink_3_arg;
335 mknod3args nfsproc3_mknod_3_arg;
336 diropargs3 nfsproc3_remove_3_arg;
337 diropargs3 nfsproc3_rmdir_3_arg;
338 rename3args nfsproc3_rename_3_arg;
339 link3args nfsproc3_link_3_arg;
340 readdir3args nfsproc3_readdir_3_arg;
341 readdirplus3args nfsproc3_readdirplus_3_arg;
342 nfs_fh3 nfsproc3_fsstat_3_arg;
343 nfs_fh3 nfsproc3_fsinfo_3_arg;
344 nfs_fh3 nfsproc3_pathconf_3_arg;
345 commit3args nfsproc3_commit_3_arg;
348 xdrproc_t _xdr_argument, _xdr_result;
349 char *(*local)(char *, RPCRequest *);
351 if (req->req_proc < sizeof(nfs_proc_names) / sizeof(const char *)) {
352 printf("Dispatched NFS RPC message type %s\n",
353 nfs_proc_names[req->req_proc]);
355 printf("Dispatched unknown NFS RPC message type %d\n", req->req_proc);
358 switch (req->req_proc) {
360 _xdr_argument = (xdrproc_t) xdr_void;
361 _xdr_result = (xdrproc_t) xdr_void;
362 local = (char *(*)(char *, RPCRequest *)) nfsproc3_null_3_svc;
365 case NFSPROC3_GETATTR:
366 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
367 _xdr_result = (xdrproc_t) xdr_getattr3res;
368 local = (char *(*)(char *, RPCRequest *)) nfsproc3_getattr_3_svc;
371 case NFSPROC3_SETATTR:
372 _xdr_argument = (xdrproc_t) xdr_setattr3args;
373 _xdr_result = (xdrproc_t) xdr_wccstat3;
374 local = (char *(*)(char *, RPCRequest *)) nfsproc3_setattr_3_svc;
377 case NFSPROC3_LOOKUP:
378 _xdr_argument = (xdrproc_t) xdr_diropargs3;
379 _xdr_result = (xdrproc_t) xdr_lookup3res;
380 local = (char *(*)(char *, RPCRequest *)) nfsproc3_lookup_3_svc;
383 case NFSPROC3_ACCESS:
384 _xdr_argument = (xdrproc_t) xdr_access3args;
385 _xdr_result = (xdrproc_t) xdr_access3res;
386 local = (char *(*)(char *, RPCRequest *)) nfsproc3_access_3_svc;
389 case NFSPROC3_READLINK:
390 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
391 _xdr_result = (xdrproc_t) xdr_readlink3res;
392 local = (char *(*)(char *, RPCRequest *)) nfsproc3_readlink_3_svc;
396 _xdr_argument = (xdrproc_t) xdr_read3args;
397 _xdr_result = (xdrproc_t) xdr_read3res;
398 local = (char *(*)(char *, RPCRequest *)) nfsproc3_read_3_svc;
402 _xdr_argument = (xdrproc_t) xdr_write3args;
403 _xdr_result = (xdrproc_t) xdr_write3res;
404 local = (char *(*)(char *, RPCRequest *)) nfsproc3_write_3_svc;
407 case NFSPROC3_CREATE:
408 _xdr_argument = (xdrproc_t) xdr_create3args;
409 _xdr_result = (xdrproc_t) xdr_diropres3;
410 local = (char *(*)(char *, RPCRequest *)) nfsproc3_create_3_svc;
414 _xdr_argument = (xdrproc_t) xdr_mkdir3args;
415 _xdr_result = (xdrproc_t) xdr_diropres3;
416 local = (char *(*)(char *, RPCRequest *)) nfsproc3_mkdir_3_svc;
419 case NFSPROC3_SYMLINK:
420 _xdr_argument = (xdrproc_t) xdr_symlink3args;
421 _xdr_result = (xdrproc_t) xdr_diropres3;
422 local = (char *(*)(char *, RPCRequest *)) nfsproc3_symlink_3_svc;
426 _xdr_argument = (xdrproc_t) xdr_mknod3args;
427 _xdr_result = (xdrproc_t) xdr_diropres3;
428 local = (char *(*)(char *, RPCRequest *)) nfsproc3_mknod_3_svc;
431 case NFSPROC3_REMOVE:
432 _xdr_argument = (xdrproc_t) xdr_diropargs3;
433 _xdr_result = (xdrproc_t) xdr_wccstat3;
434 local = (char *(*)(char *, RPCRequest *)) nfsproc3_remove_3_svc;
438 _xdr_argument = (xdrproc_t) xdr_diropargs3;
439 _xdr_result = (xdrproc_t) xdr_wccstat3;
440 local = (char *(*)(char *, RPCRequest *)) nfsproc3_rmdir_3_svc;
443 case NFSPROC3_RENAME:
444 _xdr_argument = (xdrproc_t) xdr_rename3args;
445 _xdr_result = (xdrproc_t) xdr_rename3res;
446 local = (char *(*)(char *, RPCRequest *)) nfsproc3_rename_3_svc;
450 _xdr_argument = (xdrproc_t) xdr_link3args;
451 _xdr_result = (xdrproc_t) xdr_link3res;
452 local = (char *(*)(char *, RPCRequest *)) nfsproc3_link_3_svc;
455 case NFSPROC3_READDIR:
456 _xdr_argument = (xdrproc_t) xdr_readdir3args;
457 _xdr_result = (xdrproc_t) xdr_readdir3res;
458 local = (char *(*)(char *, RPCRequest *)) nfsproc3_readdir_3_svc;
461 case NFSPROC3_READDIRPLUS:
462 _xdr_argument = (xdrproc_t) xdr_readdirplus3args;
463 _xdr_result = (xdrproc_t) xdr_readdirplus3res;
464 local = (char *(*)(char *, RPCRequest *)) nfsproc3_readdirplus_3_svc;
467 case NFSPROC3_FSSTAT:
468 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
469 _xdr_result = (xdrproc_t) xdr_fsstat3res;
470 local = (char *(*)(char *, RPCRequest *)) nfsproc3_fsstat_3_svc;
473 case NFSPROC3_FSINFO:
474 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
475 _xdr_result = (xdrproc_t) xdr_fsinfo3res;
476 local = (char *(*)(char *, RPCRequest *)) nfsproc3_fsinfo_3_svc;
479 case NFSPROC3_PATHCONF:
480 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
481 _xdr_result = (xdrproc_t) xdr_pathconf3res;
482 local = (char *(*)(char *, RPCRequest *)) nfsproc3_pathconf_3_svc;
485 case NFSPROC3_COMMIT:
486 _xdr_argument = (xdrproc_t) xdr_commit3args;
487 _xdr_result = (xdrproc_t) xdr_commit3res;
488 local = (char *(*)(char *, RPCRequest *)) nfsproc3_commit_3_svc;
492 async_rpc_send_failure(req, PROC_UNAVAIL);
496 /* Decode incoming message */
497 req->xdr_args_free = _xdr_argument;
498 req->args = g_new0(union argtype, 1);
500 xdrmem_create(&xdr_in, (char *)msg_buf, msg_len, XDR_DECODE);
501 if (!_xdr_argument(&xdr_in, req->args)) {
502 async_rpc_send_failure(req, GARBAGE_ARGS);
503 fprintf(stderr, "RPC decode error!\n");
507 /* Perform the call. */
508 req->xdr_result = _xdr_result;
509 result = (*local)((char *)req->args, req);
514 /* Enhanced, asynchronous-friendly RPC layer. This is a replacement for the
515 * built-in sunrpc parsing and dispatch that will allow for processing multiple
516 * requests at the same time. */
517 static GMainContext *main_context;
518 static GMainLoop *main_loop;
520 static GThreadPool *rpc_thread_pool;
522 static volatile int fs_dump_requested = 0;
524 static void sig_handler(int sig)
526 if (sig == SIGUSR1) {
527 fs_dump_requested = 1;
531 static gboolean async_flushd(gpointer data)
533 if (fs_dump_requested) {
534 bluesky_debug_dump(fs);
535 fs_dump_requested = 0;
538 bluesky_flushd_invoke(fs);
542 static void async_rpc_task(gpointer data, gpointer user_data)
544 nfs_program_3((RPCRequest *)data);
547 static async_rpc_init()
549 main_context = g_main_context_new();
550 main_loop = g_main_loop_new(main_context, FALSE);
552 rpc_thread_pool = g_thread_pool_new(async_rpc_task, NULL, -1, FALSE, NULL);
554 /* Arrange to have the cache writeback code run every five seconds. */
555 GSource *source = g_timeout_source_new_seconds(5);
556 g_source_set_callback(source, async_flushd, NULL, NULL);
557 g_source_attach(source, main_context);
558 g_source_unref(source);
560 /* Signal USR1 is used to request a debugging dump of filesyste info */
562 sa.sa_handler = sig_handler;
563 sigemptyset(&sa.sa_mask);
564 sa.sa_flags = SA_RESTART;
565 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
570 struct rpc_call_header {
584 /* Decode an RPC message and process it. Returns a boolean indicating whether
585 * the message could be processed; if false, an unrecoverable error occurred
586 * and the transport should be closed. */
587 static gboolean async_rpc_dispatch(RPCConnection *rpc)
589 bluesky_time_hires time_start = bluesky_now_hires();
591 GString *msg = rpc->msgbuf;
592 const char *buf = msg->str;
594 if (msg->len < sizeof(struct rpc_call_header)) {
595 fprintf(stderr, "Short RPC message: only %zd bytes!\n", msg->len);
599 struct rpc_call_header *header = (struct rpc_call_header *)(msg->str);
600 uint32_t xid = ntohl(header->xid);
602 if (ntohl(header->mtype) != 0) {
603 /* Not an RPC call */
607 if (ntohl(header->rpcvers) != 2) {
611 RPCRequest *req = g_new0(RPCRequest, 1);
612 req->connection = rpc;
613 req->time_start = time_start;
616 if (ntohl(header->prog) != NFS_PROGRAM) {
617 async_rpc_send_failure(req, PROG_UNAVAIL);
619 } else if (ntohl(header->vers) != NFS_V3) {
620 /* FIXME: Should be PROG_MISMATCH */
621 async_rpc_send_failure(req, PROG_UNAVAIL);
625 uint32_t proc = ntohl(header->proc);
627 /* Next, skip over authentication headers. */
628 buf += sizeof(struct rpc_call_header);
629 for (i = 0; i < 2; i++) {
630 struct rpc_auth *auth = (struct rpc_auth *)buf;
631 if (buf - msg->str + sizeof(struct rpc_auth) > msg->len)
634 gsize authsize = ntohl(auth->len) + sizeof(struct rpc_auth);
635 if (authsize > MAX_RPC_MSGSIZE)
641 if (buf - msg->str > msg->len)
645 req->raw_args_header_bytes = buf - msg->str;
646 req->req_proc = ntohl(header->proc);
647 rpc->msgbuf = g_string_new("");
649 g_thread_pool_push(rpc_thread_pool, req, NULL);
654 /* Write the given data to the RPC socket. */
655 static void async_rpc_write(RPCConnection *rpc,
656 const char *buf, gsize len)
660 switch (g_io_channel_write_chars(rpc->channel, buf, len,
662 case G_IO_STATUS_ERROR:
663 case G_IO_STATUS_EOF:
664 case G_IO_STATUS_AGAIN:
665 fprintf(stderr, "Error writing to socket!\n");
667 case G_IO_STATUS_NORMAL:
674 // g_io_channel_flush(rpc->channel, NULL);
677 static gboolean async_rpc_do_read(GIOChannel *channel,
678 GIOCondition condition,
681 RPCConnection *rpc = (RPCConnection *)data;
683 gsize bytes_to_read = 0; /* Number of bytes to attempt to read. */
685 /* If we have not yet read in the fragment header, do that first. This is
686 * 4 bytes that indicates the number of bytes in the message to follow
687 * (with the high bit set if this is the last fragment making up the
689 if (rpc->frag_len == 0) {
690 bytes_to_read = 4 - rpc->frag_hdr_bytes;
692 bytes_to_read = rpc->frag_len & 0x7fffffff;
695 if (bytes_to_read > MAX_RPC_MSGSIZE
696 || rpc->msgbuf->len + bytes_to_read > MAX_RPC_MSGSIZE)
698 fprintf(stderr, "Excessive fragment size for RPC: %zd bytes\n",
700 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
704 gsize bytes_read = 0;
705 g_string_set_size(rpc->msgbuf, rpc->msgbuf->len + bytes_to_read);
706 char *buf = &rpc->msgbuf->str[rpc->msgbuf->len - bytes_to_read];
707 switch (g_io_channel_read_chars(rpc->channel, buf,
708 bytes_to_read, &bytes_read, NULL)) {
709 case G_IO_STATUS_NORMAL:
711 case G_IO_STATUS_AGAIN:
713 case G_IO_STATUS_EOF:
714 if (bytes_read == bytes_to_read)
716 /* else fall through */
717 case G_IO_STATUS_ERROR:
718 fprintf(stderr, "Unexpected error or end of file on RPC stream %d!\n",
719 g_io_channel_unix_get_fd(rpc->channel));
720 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
721 /* TODO: Clean up connection object. */
725 g_assert(bytes_read >= 0 && bytes_read <= bytes_to_read);
727 g_string_set_size(rpc->msgbuf,
728 rpc->msgbuf->len - (bytes_to_read - bytes_read));
730 if (rpc->frag_len == 0) {
731 /* Handle reading in the fragment header. If we've read the complete
732 * header, store the fragment size. */
733 rpc->frag_hdr_bytes += bytes_read;
734 if (rpc->frag_hdr_bytes == 4) {
735 memcpy((char *)&rpc->frag_len,
736 &rpc->msgbuf->str[rpc->msgbuf->len - 4], 4);
737 rpc->frag_len = ntohl(rpc->frag_len);
738 g_string_set_size(rpc->msgbuf, rpc->msgbuf->len - 4);
739 rpc->frag_hdr_bytes = 0;
742 /* We were reading in the fragment body. */
743 rpc->frag_len -= bytes_read;
745 if (rpc->frag_len = 0x80000000) {
746 /* We have a complete message since this was the last fragment and
747 * there are no more bytes in it. Dispatch the message. */
748 if (!async_rpc_dispatch(rpc)) {
749 fprintf(stderr, "Invalid RPC message, closing channel\n");
750 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
754 g_string_set_size(rpc->msgbuf, 0);
761 static gboolean async_rpc_do_accept(GIOChannel *channel,
762 GIOCondition condition,
765 int fd = g_io_channel_unix_get_fd(channel);
766 struct sockaddr_in addr;
767 socklen_t addrlen = sizeof(addr);
769 g_print("Received new connection on fd %d!\n", fd);
770 int nfd = accept(fd, (struct sockaddr *)&addr, &addrlen);
772 fprintf(stderr, "Error accepting connection: %m\n");
776 RPCConnection *rpc = g_new0(RPCConnection, 1);
777 rpc->channel = g_io_channel_unix_new(nfd);
778 rpc->msgbuf = g_string_new("");
779 g_io_channel_set_encoding(rpc->channel, NULL, NULL);
780 rpc->send_lock = g_mutex_new();
781 GSource *source = g_io_create_watch(rpc->channel, G_IO_IN);
782 g_source_set_callback(source, (GSourceFunc)async_rpc_do_read,
784 g_source_attach(source, main_context);
785 g_source_unref(source);
790 static async_rpc_register_listening(int fd)
792 GIOChannel *channel = g_io_channel_unix_new(fd);
793 g_io_channel_set_encoding(channel, NULL, NULL);
794 GSource *source = g_io_create_watch(channel, G_IO_IN);
795 g_source_set_callback(source, (GSourceFunc)async_rpc_do_accept,
797 g_source_attach(source, main_context);
798 g_source_unref(source);
801 static gpointer async_rpc_run(gpointer data)
803 g_print("Starting NFS main loop...\n");
804 g_main_loop_run(main_loop);
814 pmap_unset (MOUNT_PROGRAM, MOUNT_V3);
816 transp = svcudp_create(RPC_ANYSOCK);
817 if (transp == NULL) {
818 fprintf(stderr, "%s", "cannot create udp service.");
821 if (!svc_register(transp, MOUNT_PROGRAM, MOUNT_V3, mount_program_3, IPPROTO_UDP)) {
822 fprintf(stderr, "%s", "unable to register (MOUNT_PROGRAM, MOUNT_V3, udp).");
826 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
827 if (transp == NULL) {
828 fprintf(stderr, "%s", "cannot create tcp service.");
831 if (!svc_register(transp, MOUNT_PROGRAM, MOUNT_V3, mount_program_3, IPPROTO_TCP)) {
832 fprintf(stderr, "%s", "unable to register (MOUNT_PROGRAM, MOUNT_V3, tcp).");
836 /* NFS protocol (version 3) */
837 pmap_unset (NFS_PROGRAM, NFS_V3);
839 int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
841 fprintf(stderr, "Unable to create NFS TCP socket: %m\n");
846 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof(n));
848 struct sockaddr_in addr;
849 addr.sin_family = AF_INET;
850 addr.sin_port = htons(NFS_SERVICE_PORT);
851 addr.sin_addr.s_addr = INADDR_ANY;
852 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
853 fprintf(stderr, "Unable to bind to NFS TCP address: %m\n");
857 if (listen(fd, SOMAXCONN) < 0) {
858 fprintf(stderr, "Unable to listen on NFS TCP socket: %m\n");
862 if (!pmap_set(NFS_PROGRAM, NFS_V3, IPPROTO_TCP, NFS_SERVICE_PORT)) {
863 fprintf(stderr, "Could not register NFS RPC service!\n");
867 async_rpc_register_listening(fd);
869 g_thread_create(async_rpc_run, NULL, TRUE, NULL);