More cleanups to move RPC layer towards being asynchronous.
[bluesky.git] / nfs3 / rpc.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
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
13  * slightly. */
14
15 #include "mount_prot.h"
16 #include "nfs3_prot.h"
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <rpc/pmap_clnt.h>
20 #include <string.h>
21 #include <memory.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <netinet/ip.h>
25
26 #include "bluesky.h"
27 extern BlueSkyFS *fs;
28
29 /* TCP port number to use for NFS protocol.  (Should be 2049.) */
30 #define NFS_SERVICE_PORT 2051
31
32 /* Maximum size of a single RPC message that we will accept (8 MB). */
33 #define MAX_RPC_MSGSIZE (8 << 20)
34
35 static void
36 mount_program_3(struct svc_req *rqstp, register SVCXPRT *transp)
37 {
38     union {
39         dirpath mountproc3_mnt_3_arg;
40         dirpath mountproc3_umnt_3_arg;
41     } argument;
42     char *result;
43     xdrproc_t _xdr_argument, _xdr_result;
44     char *(*local)(char *, struct svc_req *);
45
46     switch (rqstp->rq_proc) {
47     case MOUNTPROC3_NULL:
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;
51         break;
52
53     case MOUNTPROC3_MNT:
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;
57         break;
58
59     case MOUNTPROC3_DUMP:
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;
63         break;
64
65     case MOUNTPROC3_UMNT:
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;
69         break;
70
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;
75         break;
76
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;
81         break;
82
83     default:
84         svcerr_noproc (transp);
85         return;
86     }
87     memset ((char *)&argument, 0, sizeof (argument));
88     if (!svc_getargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
89         svcerr_decode (transp);
90         return;
91     }
92     result = (*local)((char *)&argument, rqstp);
93     if (result != NULL && !svc_sendreply(transp, (xdrproc_t) _xdr_result, result)) {
94         svcerr_systemerr (transp);
95     }
96     if (!svc_freeargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
97         fprintf (stderr, "%s", "unable to free arguments");
98         exit (1);
99     }
100     return;
101 }
102
103 struct rpc_reply {
104     uint32_t xid;
105     uint32_t type;
106     uint32_t stat;
107     uint32_t verf_flavor;
108     uint32_t verf_len;
109     uint32_t accept_stat;
110 };
111
112 static void async_rpc_write(RPCConnection *rpc,
113                             const char *buf, gsize len);
114
115 struct rpc_fail_reply {
116     uint32_t xid;
117     uint32_t type;
118     uint32_t stat;
119     uint32_t verf_flavor;
120     uint32_t verf_len;
121     uint32_t accept_stat;
122 };
123
124 static void
125 async_rpc_send_failure(RPCRequest *req, enum accept_stat stat)
126 {
127     struct rpc_fail_reply header;
128
129     fprintf(stderr, "Sending RPC failure status %d\n", stat);
130
131     header.xid = htonl(req->xid);
132     header.type = htonl(1);     /* REPLY */
133     header.stat = htonl(MSG_ACCEPTED);
134     header.verf_flavor = 0;
135     header.verf_len = 0;
136     header.accept_stat = htonl(stat);
137
138     uint32_t fragment = htonl(sizeof(header) | 0x80000000);
139     async_rpc_write(req->connection, (const char *)&fragment, sizeof(fragment));
140     async_rpc_write(req->connection, (const char *)&header, sizeof(header));
141     g_io_channel_flush(req->connection->channel, NULL);
142
143     if (req->raw_args != NULL)
144         g_string_free(req->raw_args, TRUE);
145
146     if (req->args != NULL) {
147         char buf[4];
148         XDR xdr;
149         xdrmem_create(&xdr, buf, sizeof(buf), XDR_FREE);
150         if (!req->xdr_args_free(&xdr, req->args)) {
151             fprintf(stderr, "unable to free arguments");
152         }
153     }
154
155     g_free(req);
156 }
157
158 static void
159 nfs_program_3(RPCRequest *req)
160 {
161     RPCConnection *connection = req->connection;
162     uint32_t xid = req->xid;
163     const char *msg_buf = req->raw_args->str + req->raw_args_header_bytes;
164     size_t msg_len = req->raw_args->len - req->raw_args_header_bytes;
165
166     union argtype {
167         nfs_fh3 nfsproc3_getattr_3_arg;
168         setattr3args nfsproc3_setattr_3_arg;
169         diropargs3 nfsproc3_lookup_3_arg;
170         access3args nfsproc3_access_3_arg;
171         nfs_fh3 nfsproc3_readlink_3_arg;
172         read3args nfsproc3_read_3_arg;
173         write3args nfsproc3_write_3_arg;
174         create3args nfsproc3_create_3_arg;
175         mkdir3args nfsproc3_mkdir_3_arg;
176         symlink3args nfsproc3_symlink_3_arg;
177         mknod3args nfsproc3_mknod_3_arg;
178         diropargs3 nfsproc3_remove_3_arg;
179         diropargs3 nfsproc3_rmdir_3_arg;
180         rename3args nfsproc3_rename_3_arg;
181         link3args nfsproc3_link_3_arg;
182         readdir3args nfsproc3_readdir_3_arg;
183         readdirplus3args nfsproc3_readdirplus_3_arg;
184         nfs_fh3 nfsproc3_fsstat_3_arg;
185         nfs_fh3 nfsproc3_fsinfo_3_arg;
186         nfs_fh3 nfsproc3_pathconf_3_arg;
187         commit3args nfsproc3_commit_3_arg;
188     };
189     char *result;
190     xdrproc_t _xdr_argument, _xdr_result;
191     char *(*local)(char *, struct svc_req *);
192
193     switch (req->req_proc) {
194     case NFSPROC3_NULL:
195         _xdr_argument = (xdrproc_t) xdr_void;
196         _xdr_result = (xdrproc_t) xdr_void;
197         local = (char *(*)(char *, struct svc_req *)) nfsproc3_null_3_svc;
198         break;
199
200     case NFSPROC3_GETATTR:
201         _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
202         _xdr_result = (xdrproc_t) xdr_getattr3res;
203         local = (char *(*)(char *, struct svc_req *)) nfsproc3_getattr_3_svc;
204         break;
205
206     case NFSPROC3_SETATTR:
207         _xdr_argument = (xdrproc_t) xdr_setattr3args;
208         _xdr_result = (xdrproc_t) xdr_wccstat3;
209         local = (char *(*)(char *, struct svc_req *)) nfsproc3_setattr_3_svc;
210         break;
211
212     case NFSPROC3_LOOKUP:
213         _xdr_argument = (xdrproc_t) xdr_diropargs3;
214         _xdr_result = (xdrproc_t) xdr_lookup3res;
215         local = (char *(*)(char *, struct svc_req *)) nfsproc3_lookup_3_svc;
216         break;
217
218     case NFSPROC3_ACCESS:
219         _xdr_argument = (xdrproc_t) xdr_access3args;
220         _xdr_result = (xdrproc_t) xdr_access3res;
221         local = (char *(*)(char *, struct svc_req *)) nfsproc3_access_3_svc;
222         break;
223
224     case NFSPROC3_READLINK:
225         _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
226         _xdr_result = (xdrproc_t) xdr_readlink3res;
227         local = (char *(*)(char *, struct svc_req *)) nfsproc3_readlink_3_svc;
228         break;
229
230     case NFSPROC3_READ:
231         _xdr_argument = (xdrproc_t) xdr_read3args;
232         _xdr_result = (xdrproc_t) xdr_read3res;
233         local = (char *(*)(char *, struct svc_req *)) nfsproc3_read_3_svc;
234         break;
235
236     case NFSPROC3_WRITE:
237         _xdr_argument = (xdrproc_t) xdr_write3args;
238         _xdr_result = (xdrproc_t) xdr_write3res;
239         local = (char *(*)(char *, struct svc_req *)) nfsproc3_write_3_svc;
240         break;
241
242     case NFSPROC3_CREATE:
243         _xdr_argument = (xdrproc_t) xdr_create3args;
244         _xdr_result = (xdrproc_t) xdr_diropres3;
245         local = (char *(*)(char *, struct svc_req *)) nfsproc3_create_3_svc;
246         break;
247
248     case NFSPROC3_MKDIR:
249         _xdr_argument = (xdrproc_t) xdr_mkdir3args;
250         _xdr_result = (xdrproc_t) xdr_diropres3;
251         local = (char *(*)(char *, struct svc_req *)) nfsproc3_mkdir_3_svc;
252         break;
253
254     case NFSPROC3_SYMLINK:
255         _xdr_argument = (xdrproc_t) xdr_symlink3args;
256         _xdr_result = (xdrproc_t) xdr_diropres3;
257         local = (char *(*)(char *, struct svc_req *)) nfsproc3_symlink_3_svc;
258         break;
259
260     case NFSPROC3_MKNOD:
261         _xdr_argument = (xdrproc_t) xdr_mknod3args;
262         _xdr_result = (xdrproc_t) xdr_diropres3;
263         local = (char *(*)(char *, struct svc_req *)) nfsproc3_mknod_3_svc;
264         break;
265
266     case NFSPROC3_REMOVE:
267         _xdr_argument = (xdrproc_t) xdr_diropargs3;
268         _xdr_result = (xdrproc_t) xdr_wccstat3;
269         local = (char *(*)(char *, struct svc_req *)) nfsproc3_remove_3_svc;
270         break;
271
272     case NFSPROC3_RMDIR:
273         _xdr_argument = (xdrproc_t) xdr_diropargs3;
274         _xdr_result = (xdrproc_t) xdr_wccstat3;
275         local = (char *(*)(char *, struct svc_req *)) nfsproc3_rmdir_3_svc;
276         break;
277
278     case NFSPROC3_RENAME:
279         _xdr_argument = (xdrproc_t) xdr_rename3args;
280         _xdr_result = (xdrproc_t) xdr_rename3res;
281         local = (char *(*)(char *, struct svc_req *)) nfsproc3_rename_3_svc;
282         break;
283
284     case NFSPROC3_LINK:
285         _xdr_argument = (xdrproc_t) xdr_link3args;
286         _xdr_result = (xdrproc_t) xdr_link3res;
287         local = (char *(*)(char *, struct svc_req *)) nfsproc3_link_3_svc;
288         break;
289
290     case NFSPROC3_READDIR:
291         _xdr_argument = (xdrproc_t) xdr_readdir3args;
292         _xdr_result = (xdrproc_t) xdr_readdir3res;
293         local = (char *(*)(char *, struct svc_req *)) nfsproc3_readdir_3_svc;
294         break;
295
296     case NFSPROC3_READDIRPLUS:
297         _xdr_argument = (xdrproc_t) xdr_readdirplus3args;
298         _xdr_result = (xdrproc_t) xdr_readdirplus3res;
299         local = (char *(*)(char *, struct svc_req *)) nfsproc3_readdirplus_3_svc;
300         break;
301
302     case NFSPROC3_FSSTAT:
303         _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
304         _xdr_result = (xdrproc_t) xdr_fsstat3res;
305         local = (char *(*)(char *, struct svc_req *)) nfsproc3_fsstat_3_svc;
306         break;
307
308     case NFSPROC3_FSINFO:
309         _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
310         _xdr_result = (xdrproc_t) xdr_fsinfo3res;
311         local = (char *(*)(char *, struct svc_req *)) nfsproc3_fsinfo_3_svc;
312         break;
313
314     case NFSPROC3_PATHCONF:
315         _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
316         _xdr_result = (xdrproc_t) xdr_pathconf3res;
317         local = (char *(*)(char *, struct svc_req *)) nfsproc3_pathconf_3_svc;
318         break;
319
320     case NFSPROC3_COMMIT:
321         _xdr_argument = (xdrproc_t) xdr_commit3args;
322         _xdr_result = (xdrproc_t) xdr_commit3res;
323         local = (char *(*)(char *, struct svc_req *)) nfsproc3_commit_3_svc;
324         break;
325
326     default:
327         async_rpc_send_failure(req, PROC_UNAVAIL);
328         return;
329     }
330
331     /* Decode incoming message */
332     req->xdr_args_free = _xdr_argument;
333     req->args = g_new0(union argtype, 1);
334     XDR xdr_in;
335     xdrmem_create(&xdr_in, (char *)msg_buf, msg_len, XDR_DECODE);
336     if (!_xdr_argument(&xdr_in, req->args)) {
337         async_rpc_send_failure(req, GARBAGE_ARGS);
338         fprintf(stderr, "RPC decode error!\n");
339         return;
340     }
341
342     /* Perform the call. */
343     result = (*local)((char *)req->args, NULL);
344
345     /* Encode result and send reply. */
346     static char reply_buf[MAX_RPC_MSGSIZE];
347     XDR xdr_out;
348     xdrmem_create(&xdr_out, reply_buf, MAX_RPC_MSGSIZE, XDR_ENCODE);
349     if (result != NULL && !_xdr_result(&xdr_out, result)) {
350         async_rpc_send_failure(req, SYSTEM_ERR);
351     }
352
353     struct rpc_reply header;
354     header.xid = htonl(xid);
355     header.type = htonl(1);     /* REPLY */
356     header.stat = htonl(MSG_ACCEPTED);
357     header.verf_flavor = 0;
358     header.verf_len = 0;
359     header.accept_stat = 0;
360
361     gsize msg_size = xdr_out.x_ops->x_getpostn(&xdr_out);
362     printf("Have an RPC reply of size %zd bytes\n", msg_size);
363     uint32_t fragment = htonl((msg_size + sizeof(header)) | 0x80000000);
364     async_rpc_write(connection, (const char *)&fragment, sizeof(fragment));
365     async_rpc_write(connection, (const char *)&header, sizeof(header));
366     async_rpc_write(connection, reply_buf, msg_size);
367     g_io_channel_flush(connection->channel, NULL);
368
369     /* Clean up. */
370     xdr_in.x_op = XDR_FREE;
371     if (!_xdr_argument(&xdr_in, (caddr_t)req->args)) {
372         fprintf (stderr, "%s", "unable to free arguments");
373         exit (1);
374     }
375     g_free(req->args);
376
377     bluesky_flushd_invoke(fs);
378
379     return;
380 }
381
382 /* Enhanced, asynchronous-friendly RPC layer.  This is a replacement for the
383  * built-in sunrpc parsing and dispatch that will allow for processing multiple
384  * requests at the same time. */
385 static GMainContext *main_context;
386 static GMainLoop *main_loop;
387
388 static async_rpc_init()
389 {
390     main_context = g_main_context_new();
391     main_loop = g_main_loop_new(main_context, FALSE);
392 }
393
394 struct rpc_call_header {
395     uint32_t xid;
396     uint32_t mtype;
397     uint32_t rpcvers;
398     uint32_t prog;
399     uint32_t vers;
400     uint32_t proc;
401 };
402
403 struct rpc_auth {
404     uint32_t flavor;
405     uint32_t len;
406 };
407
408 /* Decode an RPC message and process it.  Returns a boolean indicating whether
409  * the message could be processed; if false, an unrecoverable error occurred
410  * and the transport should be closed. */
411 static gboolean async_rpc_dispatch(RPCConnection *rpc)
412 {
413     int i;
414     GString *msg = rpc->msgbuf;
415     const char *buf = msg->str;
416
417     if (msg->len < sizeof(struct rpc_call_header)) {
418         fprintf(stderr, "Short RPC message: only %zd bytes!\n", msg->len);
419         return FALSE;
420     }
421
422     struct rpc_call_header *header = (struct rpc_call_header *)(msg->str);
423     uint32_t xid = ntohl(header->xid);
424
425     if (ntohl(header->mtype) != 0) {
426         /* Not an RPC call */
427         return FALSE;
428     }
429
430     if (ntohl(header->rpcvers) != 2) {
431         return FALSE;
432     }
433
434     RPCRequest *req = g_new0(RPCRequest, 1);
435     req->connection = rpc;
436     req->xid = xid;
437
438     if (ntohl(header->prog) != NFS_PROGRAM) {
439         async_rpc_send_failure(req, PROG_UNAVAIL);
440         return TRUE;
441     } else if (ntohl(header->vers) != NFS_V3) {
442         /* FIXME: Should be PROG_MISMATCH */
443         async_rpc_send_failure(req, PROG_UNAVAIL);
444         return TRUE;
445     }
446
447     uint32_t proc = ntohl(header->proc);
448
449     /* Next, skip over authentication headers. */
450     buf += sizeof(struct rpc_call_header);
451     for (i = 0; i < 2; i++) {
452         struct rpc_auth *auth = (struct rpc_auth *)buf;
453         if (buf - msg->str + sizeof(struct rpc_auth) > msg->len)
454             return FALSE;
455
456         gsize authsize = ntohl(auth->len) + sizeof(struct rpc_auth);
457         if (authsize > MAX_RPC_MSGSIZE)
458             return FALSE;
459
460         buf += authsize;
461     }
462
463     if (buf - msg->str > msg->len)
464         return FALSE;
465
466     printf("Dispatching RPC procedure %d...\n", proc);
467
468     req->raw_args = msg;
469     req->raw_args_header_bytes = buf - msg->str;
470     req->req_proc = ntohl(header->proc);
471     rpc->msgbuf = g_string_new("");
472
473     nfs_program_3(req);
474
475     return TRUE;
476 }
477
478 /* Write the given data to the RPC socket. */
479 static void async_rpc_write(RPCConnection *rpc,
480                             const char *buf, gsize len)
481 {
482     while (len > 0) {
483         gsize written = 0;
484         switch (g_io_channel_write_chars(rpc->channel, buf, len,
485                                          &written, NULL)) {
486         case G_IO_STATUS_ERROR:
487         case G_IO_STATUS_EOF:
488         case G_IO_STATUS_AGAIN:
489             fprintf(stderr, "Error writing to socket!\n");
490             return;
491         case G_IO_STATUS_NORMAL:
492             len -= written;
493             buf += written;
494             break;
495         }
496     }
497
498     // g_io_channel_flush(rpc->channel, NULL);
499 }
500
501 static gboolean async_rpc_do_read(GIOChannel *channel,
502                                   GIOCondition condition,
503                                   gpointer data)
504 {
505     RPCConnection *rpc = (RPCConnection *)data;
506
507     gsize bytes_to_read = 0;    /* Number of bytes to attempt to read. */
508
509     /* If we have not yet read in the fragment header, do that first.  This is
510      * 4 bytes that indicates the number of bytes in the message to follow
511      * (with the high bit set if this is the last fragment making up the
512      * message). */
513     if (rpc->frag_len == 0) {
514         bytes_to_read = 4 - rpc->frag_hdr_bytes;
515     } else {
516         bytes_to_read = rpc->frag_len & 0x7fffffff;
517     }
518
519     if (bytes_to_read > MAX_RPC_MSGSIZE
520         || rpc->msgbuf->len + bytes_to_read > MAX_RPC_MSGSIZE)
521     {
522         fprintf(stderr, "Excessive fragment size for RPC: %zd bytes\n",
523                 bytes_to_read);
524         g_io_channel_shutdown(rpc->channel, TRUE, NULL);
525         return FALSE;
526     }
527
528     gsize bytes_read = 0;
529     g_string_set_size(rpc->msgbuf, rpc->msgbuf->len + bytes_to_read);
530     char *buf = &rpc->msgbuf->str[rpc->msgbuf->len - bytes_to_read];
531     switch (g_io_channel_read_chars(rpc->channel, buf,
532                                     bytes_to_read, &bytes_read, NULL)) {
533     case G_IO_STATUS_NORMAL:
534         break;
535     case G_IO_STATUS_AGAIN:
536         return TRUE;
537     case G_IO_STATUS_EOF:
538         if (bytes_read == bytes_to_read)
539             break;
540         /* else fall through */
541     case G_IO_STATUS_ERROR:
542         fprintf(stderr, "Unexpected error or end of file on RPC stream %d!\n",
543                 g_io_channel_unix_get_fd(rpc->channel));
544         g_io_channel_shutdown(rpc->channel, TRUE, NULL);
545         return FALSE;
546     }
547
548     g_assert(bytes_read >= 0 && bytes_read <= bytes_to_read);
549
550     g_string_set_size(rpc->msgbuf,
551                       rpc->msgbuf->len - (bytes_to_read - bytes_read));
552
553     if (rpc->frag_len == 0) {
554         /* Handle reading in the fragment header.  If we've read the complete
555          * header, store the fragment size. */
556         rpc->frag_hdr_bytes += bytes_read;
557         if (rpc->frag_hdr_bytes == 4) {
558             memcpy((char *)&rpc->frag_len,
559                    &rpc->msgbuf->str[rpc->msgbuf->len - 4], 4);
560             rpc->frag_len = ntohl(rpc->frag_len);
561             g_string_set_size(rpc->msgbuf, rpc->msgbuf->len - 4);
562             rpc->frag_hdr_bytes = 0;
563             g_print("RPC fragment header: %08x\n", rpc->frag_len);
564         }
565     } else {
566         /* We were reading in the fragment body. */
567         rpc->frag_len -= bytes_read;
568
569         if (rpc->frag_len = 0x80000000) {
570             /* We have a complete message since this was the last fragment and
571              * there are no more bytes in it.  Dispatch the message. */
572             g_print("Complete RPC message: %zd bytes\n", rpc->msgbuf->len);
573             if (!async_rpc_dispatch(rpc)) {
574                 fprintf(stderr, "Invalid RPC message, closing channel\n");
575                 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
576                 return FALSE;
577             }
578             rpc->frag_len = 0;
579             g_string_set_size(rpc->msgbuf, 0);
580         }
581     }
582
583     return TRUE;
584 }
585
586 static gboolean async_rpc_do_accept(GIOChannel *channel,
587                                     GIOCondition condition,
588                                     gpointer data)
589 {
590     int fd = g_io_channel_unix_get_fd(channel);
591     struct sockaddr_in addr;
592     socklen_t addrlen = sizeof(addr);
593
594     g_print("Received new connection on fd %d!\n", fd);
595     int nfd = accept(fd, (struct sockaddr *)&addr, &addrlen);
596     if (nfd < 0) {
597         fprintf(stderr, "Error accepting connection: %m\n");
598         return TRUE;
599     }
600
601     RPCConnection *rpc = g_new0(RPCConnection, 1);
602     rpc->channel = g_io_channel_unix_new(nfd);
603     rpc->msgbuf = g_string_new("");
604     g_io_channel_set_encoding(rpc->channel, NULL, NULL);
605     GSource *source = g_io_create_watch(rpc->channel, G_IO_IN);
606     g_source_set_callback(source, (GSourceFunc)async_rpc_do_read,
607                           rpc, NULL);
608     g_source_attach(source, main_context);
609     g_source_unref(source);
610
611     return TRUE;
612 }
613
614 static async_rpc_register_listening(int fd)
615 {
616     GIOChannel *channel = g_io_channel_unix_new(fd);
617     g_io_channel_set_encoding(channel, NULL, NULL);
618     GSource *source = g_io_create_watch(channel, G_IO_IN);
619     g_source_set_callback(source, (GSourceFunc)async_rpc_do_accept,
620                           NULL, NULL);
621     g_source_attach(source, main_context);
622     g_source_unref(source);
623 }
624
625 static gpointer async_rpc_run(gpointer data)
626 {
627     g_print("Starting NFS main loop...\n");
628     g_main_loop_run(main_loop);
629 }
630
631 void register_rpc()
632 {
633     SVCXPRT *transp;
634
635     async_rpc_init();
636
637     /* MOUNT protocol */
638     pmap_unset (MOUNT_PROGRAM, MOUNT_V3);
639
640     transp = svcudp_create(RPC_ANYSOCK);
641     if (transp == NULL) {
642         fprintf(stderr, "%s", "cannot create udp service.");
643         exit(1);
644     }
645     if (!svc_register(transp, MOUNT_PROGRAM, MOUNT_V3, mount_program_3, IPPROTO_UDP)) {
646         fprintf(stderr, "%s", "unable to register (MOUNT_PROGRAM, MOUNT_V3, udp).");
647         exit(1);
648     }
649
650     transp = svctcp_create(RPC_ANYSOCK, 0, 0);
651     if (transp == NULL) {
652         fprintf(stderr, "%s", "cannot create tcp service.");
653         exit(1);
654     }
655     if (!svc_register(transp, MOUNT_PROGRAM, MOUNT_V3, mount_program_3, IPPROTO_TCP)) {
656         fprintf(stderr, "%s", "unable to register (MOUNT_PROGRAM, MOUNT_V3, tcp).");
657         exit(1);
658     }
659
660     /* NFS protocol (version 3) */
661     pmap_unset (NFS_PROGRAM, NFS_V3);
662
663     int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
664     if (fd < 0) {
665         fprintf(stderr, "Unable to create NFS TCP socket: %m\n");
666         exit(1);
667     }
668
669     int n = 1;
670     setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof(n));
671
672     struct sockaddr_in addr;
673     addr.sin_family = AF_INET;
674     addr.sin_port = htons(NFS_SERVICE_PORT);
675     addr.sin_addr.s_addr = INADDR_ANY;
676     if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
677         fprintf(stderr, "Unable to bind to NFS TCP address: %m\n");
678         exit(1);
679     }
680
681     if (listen(fd, SOMAXCONN) < 0) {
682         fprintf(stderr, "Unable to listen on NFS TCP socket: %m\n");
683         exit(1);
684     }
685
686     if (!pmap_set(NFS_PROGRAM, NFS_V3, IPPROTO_TCP, NFS_SERVICE_PORT)) {
687         fprintf(stderr, "Could not register NFS RPC service!\n");
688         exit(1);
689     }
690
691     async_rpc_register_listening(fd);
692
693     g_thread_create(async_rpc_run, NULL, TRUE, NULL);
694 }