minor fixes for safecopy & safemap tests
[minix.git] / lib / libhgfs / rpc.c
blob479e8caf86bf1c3bc6fd56c5e55597ad0eb5405f
1 /* Part of libhgfs - (c) 2009, D.C. van Moolenbroek */
3 #include "inc.h"
5 char rpc_buf[RPC_BUF_SIZE];
6 char *rpc_ptr;
8 static struct channel rpc_chan;
10 /*===========================================================================*
11 * rpc_open *
12 *===========================================================================*/
13 int rpc_open()
15 /* Open a HGFS RPC backdoor channel to the VMware host, and make sure that it
16 * is working. Return OK upon success, or a negative error code otherwise; in
17 * particular, return EAGAIN if shared folders are disabled.
19 int r;
21 if ((r = channel_open(&rpc_chan, CH_OUT)) != OK)
22 return r;
24 r = rpc_test();
26 if (r != OK)
27 channel_close(&rpc_chan);
29 return r;
32 /*===========================================================================*
33 * rpc_query *
34 *===========================================================================*/
35 int rpc_query()
37 /* Send a HGFS RPC query over the backdoor channel. Return OK upon success, or
38 * a negative error code otherwise; EAGAIN is returned if shared folders are
39 * disabled. In general, we make the assumption that the sender (= VMware)
40 * speaks the protocol correctly. Hence, the callers of this function do not
41 * check for lengths.
43 int r, len, id, err;
45 len = RPC_LEN;
47 /* A more robust version of this call could reopen the channel and
48 * retry the request upon low-level failure.
50 r = channel_send(&rpc_chan, rpc_buf, len);
51 if (r < 0) return r;
53 r = channel_recv(&rpc_chan, rpc_buf, sizeof(rpc_buf));
54 if (r < 0) return r;
55 if (r < 2 || (len > 2 && r < 10)) return EIO;
57 RPC_RESET;
59 if (RPC_NEXT8 != '1') return EAGAIN;
60 if (RPC_NEXT8 != ' ') return EAGAIN;
62 if (len <= 2) return OK;
64 id = RPC_NEXT32;
65 err = RPC_NEXT32;
67 return error_convert(err);
70 /*===========================================================================*
71 * rpc_test *
72 *===========================================================================*/
73 int rpc_test()
75 /* Test whether HGFS communication is working. Return OK on success, EAGAIN if
76 * shared folders are disabled, or another negative error code upon error.
79 RPC_RESET;
80 RPC_NEXT8 = 'f';
81 RPC_NEXT8 = ' ';
83 return rpc_query();
86 /*===========================================================================*
87 * rpc_close *
88 *===========================================================================*/
89 void rpc_close()
91 /* Close the HGFS RPC backdoor channel.
94 channel_close(&rpc_chan);