]> git.ozlabs.org Git - ccan/blob - ccan/nfs/tools/nfsclient-raw.c
nfs: remove trailing whitespace
[ccan] / ccan / nfs / tools / nfsclient-raw.c
1 /* 
2    Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
3    
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8    
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13    
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /* Example program using the lowlevel raw interface.
19  * This allow accurate control of the exact commands that are being used.
20  */
21
22 #define SERVER "10.1.1.27"
23 #define EXPORT "/VIRTUAL"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <poll.h>
28 #include <ccan/nfs/nfs.h>
29 #include <ccan/nfs/libnfs-raw.h>
30 #include <ccan/nfs/rpc/mount.h>
31
32 struct client {
33        char *server;
34        char *export;
35        uint32_t mount_port;
36        int is_finished;
37 };
38
39 void mount_mnt_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
40 {
41         mountres3 *res;
42         struct client *client = private_data;
43
44         if (status == RPC_STATUS_ERROR) {
45                 printf("mount/mnt call failed with \"%s\"\n", (char *)data);
46                 exit(10);
47         }
48         if (status != RPC_STATUS_SUCCESS) {
49                 printf("mount/mnt call to server %s failed, status:%d\n", client->server, status);
50                 exit(10);
51         }
52
53         res = data;
54         if (res->fhs_status != MNT3_OK) {
55                 printf("RPC error: Mount failed with error %s(%d) %s(%d)", mountstat3_to_str(res->fhs_status), res->fhs_status, strerror(-mountstat3_to_errno(res->fhs_status)), -mountstat3_to_errno(res->fhs_status));
56                 exit(10);
57         }
58
59         printf("Got reply from server for MOUNT/MNT procedure.\n");
60         client->is_finished = 1;
61 }
62
63
64 void mount_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
65 {
66         struct client *client = private_data;
67
68         if (status == RPC_STATUS_ERROR) {
69                 printf("mount null call failed with \"%s\"\n", (char *)data);
70                 exit(10);
71         }
72         if (status != RPC_STATUS_SUCCESS) {
73                 printf("mount null call to server %s failed, status:%d\n", client->server, status);
74                 exit(10);
75         }
76
77         printf("Got reply from server for MOUNT/NULL procedure.\n");
78         printf("Send MOUNT/MNT command for %s\n", client->export);
79         if (rpc_mount_mnt_async(rpc, mount_mnt_cb, client->export, client) != 0) {
80                 printf("Failed to send mnt request\n");
81                 exit(10);
82         }
83
84 }
85
86 void mount_connect_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
87 {
88         struct client *client = private_data;
89
90         if (status != RPC_STATUS_SUCCESS) {
91                 printf("connection to RPC.MOUNTD on server %s failed\n", client->server);
92                 exit(10);
93         }
94
95         printf("Connected to RPC.MOUNTD on %s:%d\n", client->server, client->mount_port);
96         printf("Send NULL request to check if RPC.MOUNTD is actually running\n");
97         if (rpc_mount_null_async(rpc, mount_null_cb, client) != 0) {
98                 printf("Failed to send null request\n");
99                 exit(10);
100         }
101 }
102
103
104 void pmap_getport_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
105 {
106         struct client *client = private_data;
107         uint32_t port;
108
109         if (status == RPC_STATUS_ERROR) {
110                 printf("portmapper getport call failed with \"%s\"\n", (char *)data);
111                 exit(10);
112         }
113         if (status != RPC_STATUS_SUCCESS) {
114                 printf("portmapper getport call to server %s failed, status:%d\n", client->server, status);
115                 exit(10);
116         }
117
118         client->mount_port = *(uint32_t *)data;
119         printf("GETPORT returned Port:%d\n", client->mount_port);
120         if (client->mount_port == 0) {
121                 printf("RPC.MOUNTD is not available on server : %s\n", client->server, client->mount_port);
122                 exit(10);
123         }               
124
125         printf("Disconnect socket from portmap server\n");
126         if (rpc_disconnect(rpc, "normal disconnect") != 0) {
127                 printf("Failed to disconnect socket to portmapper\n");
128                 exit(10);
129         }
130
131         printf("Connect to RPC.MOUNTD on %s:%d\n", client->server, client->mount_port);
132         if (rpc_connect_async(rpc, client->server, client->mount_port, 1, mount_connect_cb, client) != 0) {
133                 printf("Failed to start connection\n");
134                 exit(10);
135         }
136 }
137
138
139 void pmap_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
140 {
141         struct client *client = private_data;
142
143         if (status == RPC_STATUS_ERROR) {
144                 printf("portmapper null call failed with \"%s\"\n", (char *)data);
145                 exit(10);
146         }
147         if (status != RPC_STATUS_SUCCESS) {
148                 printf("portmapper null call to server %s failed, status:%d\n", client->server, status);
149                 exit(10);
150         }
151
152         printf("Got reply from server for PORTMAP/NULL procedure.\n");
153         printf("Send getport request asking for MOUNT port\n");
154         if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, pmap_getport_cb, client) != 0) {
155                 printf("Failed to send getport request\n");
156                 exit(10);
157         }
158 }
159
160 void pmap_connect_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
161 {
162         struct client *client = private_data;
163
164         printf("pmap_connect_cb    status:%d.\n", status);
165         if (status != RPC_STATUS_SUCCESS) {
166                 printf("connection to portmapper on server %s failed\n", client->server);
167                 exit(10);
168         }
169
170         printf("Send NULL request to check if portmapper is actually running\n");
171         if (rpc_pmap_null_async(rpc, pmap_null_cb, client) != 0) {
172                 printf("Failed to send null request\n");
173                 exit(10);
174         }
175 }
176
177
178 int main(int argc, char *argv[])
179 {
180         struct rpc_context *rpc;
181         struct pollfd pfd;
182         int ret;
183         struct client client;
184
185         rpc = rpc_init_context();
186         if (rpc == NULL) {
187                 printf("failed to init context\n");
188                 exit(10);
189         }
190
191         client.server = SERVER;
192         client.export = EXPORT;
193         client.is_finished = 0;
194         if (rpc_connect_async(rpc, client.server, 111, 0, pmap_connect_cb, &client) != 0) {
195                 printf("Failed to start connection\n");
196                 exit(10);
197         }
198
199         for (;;) {
200                 pfd.fd = rpc_get_fd(rpc);
201                 pfd.events = rpc_which_events(rpc);
202
203                 if (poll(&pfd, 1, -1) < 0) {
204                         printf("Poll failed");
205                         exit(10);
206                 }
207                 if (rpc_service(rpc, pfd.revents) < 0) {
208                         printf("rpc_service failed\n");
209                         break;
210                 }
211                 if (client.is_finished) {
212                         break;
213                 }
214         }
215         
216         rpc_destroy_context(rpc);
217         rpc=NULL;
218         printf("nfsclient finished\n");
219         return 0;
220 }