62871b5d3285780ce6d0dfcd2d8b91feb074635a
[libguestfs.git] / daemon / proto.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc.
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 2 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, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <sys/param.h>          /* defines MIN */
29 #include <rpc/types.h>
30 #include <rpc/xdr.h>
31
32 #include "daemon.h"
33 #include "../src/guestfs_protocol.h"
34
35 /* The message currently being processed. */
36 int proc_nr;
37 int serial;
38
39 /* The daemon communications socket. */
40 static int sock;
41
42 void
43 main_loop (int _sock)
44 {
45   XDR xdr;
46   char *buf;
47   char lenbuf[4];
48   unsigned len;
49   struct guestfs_message_header hdr;
50   struct timeval start_t, end_t;
51   int64_t start_us, end_us, elapsed_us;
52
53   sock = _sock;
54
55   for (;;) {
56 #if 0
57     /* Most common errors are leaked memory and leaked file descriptors,
58      * so run this between each command:
59      */
60     if (verbose)
61       system ("ls -l /proc/self/fd");
62 #endif
63
64     /* Read the length word. */
65     xread (sock, lenbuf, 4);
66     xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
67     xdr_uint32_t (&xdr, &len);
68     xdr_destroy (&xdr);
69
70     if (len > GUESTFS_MESSAGE_MAX) {
71       fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
72                len);
73       exit (1);
74     }
75
76     buf = malloc (len);
77     if (!buf) {
78       reply_with_perror ("malloc");
79       continue;
80     }
81
82     xread (sock, buf, len);
83
84 #if 0
85     if (verbose) {
86       int i, j;
87
88       for (i = 0; i < len; i += 16) {
89         printf ("%04x: ", i);
90         for (j = i; j < MIN (i+16, len); ++j)
91           printf ("%02x ", (unsigned char) buf[j]);
92         for (; j < i+16; ++j)
93           printf ("   ");
94         printf ("|");
95         for (j = i; j < MIN (i+16, len); ++j)
96           if (isprint (buf[j]))
97             printf ("%c", buf[j]);
98           else
99             printf (".");
100         for (; j < i+16; ++j)
101           printf (" ");
102         printf ("|\n");
103       }
104     }
105 #endif
106
107     /* In verbose mode, display the time taken to run each command. */
108     if (verbose)
109       gettimeofday (&start_t, NULL);
110
111     /* Decode the message header. */
112     xdrmem_create (&xdr, buf, len, XDR_DECODE);
113     if (!xdr_guestfs_message_header (&xdr, &hdr)) {
114       fprintf (stderr, "guestfsd: could not decode message header\n");
115       exit (1);
116     }
117
118     /* Check the version etc. */
119     if (hdr.prog != GUESTFS_PROGRAM) {
120       reply_with_error ("wrong program (%d)", hdr.prog);
121       goto cont;
122     }
123     if (hdr.vers != GUESTFS_PROTOCOL_VERSION) {
124       reply_with_error ("wrong protocol version (%d)", hdr.vers);
125       goto cont;
126     }
127     if (hdr.direction != GUESTFS_DIRECTION_CALL) {
128       reply_with_error ("unexpected message direction (%d)", hdr.direction);
129       goto cont;
130     }
131     if (hdr.status != GUESTFS_STATUS_OK) {
132       reply_with_error ("unexpected message status (%d)", hdr.status);
133       goto cont;
134     }
135
136     /* Now start to process this message. */
137     proc_nr = hdr.proc;
138     serial = hdr.serial;
139     dispatch_incoming_message (&xdr);
140     /* Note that dispatch_incoming_message will also send a reply. */
141
142     /* In verbose mode, display the time taken to run each command. */
143     if (verbose) {
144       gettimeofday (&end_t, NULL);
145
146       start_us = (int64_t) start_t.tv_sec * 1000000 + start_t.tv_usec;
147       end_us = (int64_t) end_t.tv_sec * 1000000 + end_t.tv_usec;
148       elapsed_us = end_us - start_us;
149       fprintf (stderr, "proc %d (%s) took %d.%02d seconds\n",
150                proc_nr,
151                proc_nr >= 0 && proc_nr < GUESTFS_PROC_NR_PROCS
152                ? function_names[proc_nr] : "UNKNOWN PROCEDURE",
153                (int) (elapsed_us / 1000000),
154                (int) ((elapsed_us / 10000) % 100));
155     }
156
157   cont:
158     xdr_destroy (&xdr);
159     free (buf);
160   }
161 }
162
163 static void send_error (const char *msg);
164
165 void
166 reply_with_error (const char *fs, ...)
167 {
168   char err[GUESTFS_ERROR_LEN];
169   va_list args;
170
171   va_start (args, fs);
172   vsnprintf (err, sizeof err, fs, args);
173   va_end (args);
174
175   send_error (err);
176 }
177
178 void
179 reply_with_perror (const char *fs, ...)
180 {
181   char buf1[GUESTFS_ERROR_LEN];
182   char buf2[GUESTFS_ERROR_LEN];
183   va_list args;
184   int err = errno;
185
186   va_start (args, fs);
187   vsnprintf (buf1, sizeof buf1, fs, args);
188   va_end (args);
189
190   snprintf (buf2, sizeof buf2, "%s: %s", buf1, strerror (err));
191
192   send_error (buf2);
193 }
194
195 static void
196 send_error (const char *msg)
197 {
198   XDR xdr;
199   char buf[GUESTFS_ERROR_LEN + 200];
200   char lenbuf[4];
201   struct guestfs_message_header hdr;
202   struct guestfs_message_error err;
203   unsigned len;
204
205   fprintf (stderr, "guestfsd: error: %s\n", msg);
206
207   xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
208
209   hdr.prog = GUESTFS_PROGRAM;
210   hdr.vers = GUESTFS_PROTOCOL_VERSION;
211   hdr.direction = GUESTFS_DIRECTION_REPLY;
212   hdr.status = GUESTFS_STATUS_ERROR;
213   hdr.proc = proc_nr;
214   hdr.serial = serial;
215
216   if (!xdr_guestfs_message_header (&xdr, &hdr)) {
217     fprintf (stderr, "guestfsd: failed to encode error message header\n");
218     exit (1);
219   }
220
221   err.error_message = (char *) msg;
222
223   if (!xdr_guestfs_message_error (&xdr, &err)) {
224     fprintf (stderr, "guestfsd: failed to encode error message body\n");
225     exit (1);
226   }
227
228   len = xdr_getpos (&xdr);
229   xdr_destroy (&xdr);
230
231   xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
232   xdr_uint32_t (&xdr, &len);
233   xdr_destroy (&xdr);
234
235   if (xwrite (sock, lenbuf, 4) == -1) {
236     fprintf (stderr, "xwrite failed\n");
237     exit (1);
238   }
239   if (xwrite (sock, buf, len) == -1) {
240     fprintf (stderr, "xwrite failed\n");
241     exit (1);
242   }
243 }
244
245 void
246 reply (xdrproc_t xdrp, char *ret)
247 {
248   XDR xdr;
249   char buf[GUESTFS_MESSAGE_MAX];
250   char lenbuf[4];
251   struct guestfs_message_header hdr;
252   unsigned len;
253
254   xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
255
256   hdr.prog = GUESTFS_PROGRAM;
257   hdr.vers = GUESTFS_PROTOCOL_VERSION;
258   hdr.direction = GUESTFS_DIRECTION_REPLY;
259   hdr.status = GUESTFS_STATUS_OK;
260   hdr.proc = proc_nr;
261   hdr.serial = serial;
262
263   if (!xdr_guestfs_message_header (&xdr, &hdr)) {
264     fprintf (stderr, "guestfsd: failed to encode reply header\n");
265     exit (1);
266   }
267
268   if (xdrp) {
269     if (!(*xdrp) (&xdr, ret)) {
270       fprintf (stderr, "guestfsd: failed to encode reply body\n");
271       exit (1);
272     }
273   }
274
275   len = xdr_getpos (&xdr);
276   xdr_destroy (&xdr);
277
278   xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
279   xdr_uint32_t (&xdr, &len);
280   xdr_destroy (&xdr);
281
282   if (xwrite (sock, lenbuf, 4) == -1) {
283     fprintf (stderr, "xwrite failed\n");
284     exit (1);
285   }
286   if (xwrite (sock, buf, len) == len) {
287     fprintf (stderr, "xwrite failed\n");
288     exit (1);
289   }
290 }
291
292 /* Receive file chunks, repeatedly calling 'cb'. */
293 int
294 receive_file (receive_cb cb, void *opaque)
295 {
296   guestfs_chunk chunk;
297   char lenbuf[4];
298   char *buf;
299   XDR xdr;
300   int r;
301   uint32_t len;
302
303   for (;;) {
304     /* Read the length word. */
305     xread (sock, lenbuf, 4);
306     xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
307     xdr_uint32_t (&xdr, &len);
308     xdr_destroy (&xdr);
309
310     if (len == GUESTFS_CANCEL_FLAG)
311       continue;                 /* Just ignore it. */
312
313     if (len > GUESTFS_MESSAGE_MAX) {
314       fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
315                len);
316       exit (1);
317     }
318
319     buf = malloc (len);
320     if (!buf) {
321       perror ("malloc");
322       return -1;
323     }
324
325     xread (sock, buf, len);
326
327     xdrmem_create (&xdr, buf, len, XDR_DECODE);
328     memset (&chunk, 0, sizeof chunk);
329     if (!xdr_guestfs_chunk (&xdr, &chunk)) {
330       xdr_destroy (&xdr);
331       free (buf);
332       return -1;
333     }
334     xdr_destroy (&xdr);
335     free (buf);
336
337     if (verbose)
338       printf ("receive_file: got chunk: cancel = %d, len = %d, buf = %p\n",
339               chunk.cancel, chunk.data.data_len, chunk.data.data_val);
340
341     if (chunk.cancel) {
342       fprintf (stderr, "receive_file: received cancellation from library\n");
343       xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
344       return -2;
345     }
346     if (chunk.data.data_len == 0) {
347       xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
348       return 0;                 /* end of file */
349     }
350
351     if (cb)
352       r = cb (opaque, chunk.data.data_val, chunk.data.data_len);
353     else
354       r = 0;
355
356     xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
357     if (r == -1)                /* write error */
358       return -1;
359   }
360 }
361
362 /* Send a cancellation flag back to the library. */
363 void
364 cancel_receive (void)
365 {
366   XDR xdr;
367   char fbuf[4];
368   uint32_t flag = GUESTFS_CANCEL_FLAG;
369
370   xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
371   xdr_uint32_t (&xdr, &flag);
372   xdr_destroy (&xdr);
373
374   if (xwrite (sock, fbuf, sizeof fbuf) == -1) {
375     perror ("write to socket");
376     return;
377   }
378
379   /* Keep receiving chunks and discarding, until library sees cancel. */
380   (void) receive_file (NULL, NULL);
381 }
382
383 static int check_for_library_cancellation (void);
384 static int send_chunk (const guestfs_chunk *);
385
386 /* Also check if the library sends us a cancellation message. */
387 int
388 send_file_write (const void *buf, int len)
389 {
390   guestfs_chunk chunk;
391   int cancel;
392
393   if (len > GUESTFS_MAX_CHUNK_SIZE) {
394     fprintf (stderr, "send_file_write: len (%d) > GUESTFS_MAX_CHUNK_SIZE (%d)\n",
395              len, GUESTFS_MAX_CHUNK_SIZE);
396     return -1;
397   }
398
399   cancel = check_for_library_cancellation ();
400
401   if (cancel) {
402     chunk.cancel = 1;
403     chunk.data.data_len = 0;
404     chunk.data.data_val = NULL;
405   } else {
406     chunk.cancel = 0;
407     chunk.data.data_len = len;
408     chunk.data.data_val = (char *) buf;
409   }
410
411   if (send_chunk (&chunk) == -1)
412     return -1;
413
414   if (cancel) return -2;
415   return 0;
416 }
417
418 static int
419 check_for_library_cancellation (void)
420 {
421   fd_set rset;
422   struct timeval tv;
423   int r;
424   char buf[4];
425   uint32_t flag;
426   XDR xdr;
427
428   FD_ZERO (&rset);
429   FD_SET (sock, &rset);
430   tv.tv_sec = 0;
431   tv.tv_usec = 0;
432   r = select (sock+1, &rset, NULL, NULL, &tv);
433   if (r == -1) {
434     perror ("select");
435     return 0;
436   }
437   if (r == 0)
438     return 0;
439
440   /* Read the message from the daemon. */
441   r = xread (sock, buf, sizeof buf);
442   if (r == -1) {
443     perror ("read");
444     return 0;
445   }
446
447   xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
448   xdr_uint32_t (&xdr, &flag);
449   xdr_destroy (&xdr);
450
451   if (flag != GUESTFS_CANCEL_FLAG) {
452     fprintf (stderr, "check_for_library_cancellation: read 0x%x from library, expected 0x%x\n",
453              flag, GUESTFS_CANCEL_FLAG);
454     return 0;
455   }
456
457   return 1;
458 }
459
460 void
461 send_file_end (int cancel)
462 {
463   guestfs_chunk chunk;
464
465   chunk.cancel = cancel;
466   chunk.data.data_len = 0;
467   chunk.data.data_val = NULL;
468   send_chunk (&chunk);
469 }
470
471 static int
472 send_chunk (const guestfs_chunk *chunk)
473 {
474   char buf[GUESTFS_MAX_CHUNK_SIZE + 48];
475   char lenbuf[4];
476   XDR xdr;
477   uint32_t len;
478
479   xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
480   if (!xdr_guestfs_chunk (&xdr, (guestfs_chunk *) chunk)) {
481     fprintf (stderr, "send_chunk: failed to encode chunk\n");
482     xdr_destroy (&xdr);
483     return -1;
484   }
485
486   len = xdr_getpos (&xdr);
487   xdr_destroy (&xdr);
488
489   xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
490   xdr_uint32_t (&xdr, &len);
491   xdr_destroy (&xdr);
492
493   (void) xwrite (sock, lenbuf, 4);
494   (void) xwrite (sock, buf, len);
495
496   return 0;
497 }