Version 1.11.4.
[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 <signal.h>
26 #include <inttypes.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <sys/param.h>          /* defines MIN */
30 #include <sys/select.h>
31 #include <sys/time.h>
32 #include <rpc/types.h>
33 #include <rpc/xdr.h>
34
35 #ifdef HAVE_WINDOWS_H
36 #include <windows.h>
37 #endif
38
39 #include "c-ctype.h"
40 #include "ignore-value.h"
41
42 #include "daemon.h"
43 #include "guestfs_protocol.h"
44 #include "errnostring.h"
45
46 /* The message currently being processed. */
47 int proc_nr;
48 int serial;
49
50 /* Hint for implementing progress messages for uploaded/incoming data.
51  * The caller sets this to a value > 0 if it knows or can estimate how
52  * much data will be sent (this is not always known, eg. for uploads
53  * coming from a pipe).  If this is known then we can emit progress
54  * messages as we write the data.
55  */
56 uint64_t progress_hint;
57
58 /* Optional arguments bitmask.  Caller sets this to indicate which
59  * optional arguments in the guestfs_<foo>_args structure are
60  * meaningful.  Optional arguments not covered by the bitmask are set
61  * to arbitrary values and the daemon should ignore them.  If the
62  * bitmask has bits set that the daemon doesn't understand, then the
63  * whole call is rejected early in processing.
64  */
65 uint64_t optargs_bitmask;
66
67 /* Time at which we received the current request. */
68 static struct timeval start_t;
69
70 /* Time at which the last progress notification was sent. */
71 static struct timeval last_progress_t;
72
73 /* Counts the number of progress notifications sent during this call. */
74 static int count_progress;
75
76 /* The daemon communications socket. */
77 static int sock;
78
79 void
80 main_loop (int _sock)
81 {
82   XDR xdr;
83   char *buf;
84   char lenbuf[4];
85   uint32_t len;
86   struct guestfs_message_header hdr;
87
88   sock = _sock;
89
90   for (;;) {
91     /* Most common errors are leaked memory and leaked file descriptors,
92      * so run this between each command:
93      */
94     if (verbose && 0)
95       ignore_value (system ("ls -l /proc/self/fd"));
96
97     /* Read the length word. */
98     if (xread (sock, lenbuf, 4) == -1)
99       exit (EXIT_FAILURE);
100
101     xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
102     xdr_u_int (&xdr, &len);
103     xdr_destroy (&xdr);
104
105     if (verbose)
106       fprintf (stderr,
107                "guestfsd: main_loop: new request, len 0x%" PRIx32 "\n",
108                len);
109
110     if (len > GUESTFS_MESSAGE_MAX) {
111       fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
112                len);
113       exit (EXIT_FAILURE);
114     }
115
116     buf = malloc (len);
117     if (!buf) {
118       reply_with_perror ("malloc");
119       continue;
120     }
121
122     if (xread (sock, buf, len) == -1)
123       exit (EXIT_FAILURE);
124
125 #ifdef ENABLE_PACKET_DUMP
126     if (verbose) {
127       size_t i, j;
128
129       for (i = 0; i < len; i += 16) {
130         printf ("%04zx: ", i);
131         for (j = i; j < MIN (i+16, len); ++j)
132           printf ("%02x ", (unsigned char) buf[j]);
133         for (; j < i+16; ++j)
134           printf ("   ");
135         printf ("|");
136         for (j = i; j < MIN (i+16, len); ++j)
137           if (c_isprint (buf[j]))
138             printf ("%c", buf[j]);
139           else
140             printf (".");
141         for (; j < i+16; ++j)
142           printf (" ");
143         printf ("|\n");
144       }
145     }
146 #endif
147
148     gettimeofday (&start_t, NULL);
149     last_progress_t = start_t;
150     count_progress = 0;
151
152     /* Decode the message header. */
153     xdrmem_create (&xdr, buf, len, XDR_DECODE);
154     if (!xdr_guestfs_message_header (&xdr, &hdr)) {
155       fprintf (stderr, "guestfsd: could not decode message header\n");
156       exit (EXIT_FAILURE);
157     }
158
159     /* Check the version etc. */
160     if (hdr.prog != GUESTFS_PROGRAM) {
161       reply_with_error ("wrong program (%d)", hdr.prog);
162       goto cont;
163     }
164     if (hdr.vers != GUESTFS_PROTOCOL_VERSION) {
165       reply_with_error ("wrong protocol version (%d)", hdr.vers);
166       goto cont;
167     }
168     if (hdr.direction != GUESTFS_DIRECTION_CALL) {
169       reply_with_error ("unexpected message direction (%d)", hdr.direction);
170       goto cont;
171     }
172     if (hdr.status != GUESTFS_STATUS_OK) {
173       reply_with_error ("unexpected message status (%d)", hdr.status);
174       goto cont;
175     }
176
177     proc_nr = hdr.proc;
178     serial = hdr.serial;
179     progress_hint = hdr.progress_hint;
180     optargs_bitmask = hdr.optargs_bitmask;
181
182     /* Clear errors before we call the stub functions.  This is just
183      * to ensure that we can accurately report errors in cases where
184      * error handling paths don't set errno correctly.
185      */
186     errno = 0;
187 #ifdef WIN32
188     SetLastError (0);
189     WSASetLastError (0);
190 #endif
191
192     /* Now start to process this message. */
193     dispatch_incoming_message (&xdr);
194     /* Note that dispatch_incoming_message will also send a reply. */
195
196     /* In verbose mode, display the time taken to run each command. */
197     if (verbose) {
198       struct timeval end_t;
199       gettimeofday (&end_t, NULL);
200
201       int64_t start_us, end_us, elapsed_us;
202       start_us = (int64_t) start_t.tv_sec * 1000000 + start_t.tv_usec;
203       end_us = (int64_t) end_t.tv_sec * 1000000 + end_t.tv_usec;
204       elapsed_us = end_us - start_us;
205
206       fprintf (stderr,
207                "guestfsd: main_loop: proc %d (%s) took %d.%02d seconds\n",
208                proc_nr,
209                proc_nr >= 0 && proc_nr < GUESTFS_PROC_NR_PROCS
210                ? function_names[proc_nr] : "UNKNOWN PROCEDURE",
211                (int) (elapsed_us / 1000000),
212                (int) ((elapsed_us / 10000) % 100));
213     }
214
215   cont:
216     xdr_destroy (&xdr);
217     free (buf);
218   }
219 }
220
221 static void send_error (int errnum, const char *msg);
222
223 void
224 reply_with_error (const char *fs, ...)
225 {
226   char err[GUESTFS_ERROR_LEN];
227   va_list args;
228
229   va_start (args, fs);
230   vsnprintf (err, sizeof err, fs, args);
231   va_end (args);
232
233   send_error (0, err);
234 }
235
236 void
237 reply_with_perror_errno (int err, const char *fs, ...)
238 {
239   char buf1[GUESTFS_ERROR_LEN];
240   char buf2[GUESTFS_ERROR_LEN];
241   va_list args;
242
243   va_start (args, fs);
244   vsnprintf (buf1, sizeof buf1, fs, args);
245   va_end (args);
246
247   snprintf (buf2, sizeof buf2, "%s: %s", buf1, strerror (err));
248
249   send_error (err, buf2);
250 }
251
252 static void
253 send_error (int errnum, const char *msg)
254 {
255   XDR xdr;
256   char buf[GUESTFS_ERROR_LEN + 200];
257   char lenbuf[4];
258   struct guestfs_message_header hdr;
259   struct guestfs_message_error err;
260   unsigned len;
261
262   fprintf (stderr, "guestfsd: error: %s\n", msg);
263
264   xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
265
266   hdr.prog = GUESTFS_PROGRAM;
267   hdr.vers = GUESTFS_PROTOCOL_VERSION;
268   hdr.direction = GUESTFS_DIRECTION_REPLY;
269   hdr.status = GUESTFS_STATUS_ERROR;
270   hdr.proc = proc_nr;
271   hdr.serial = serial;
272
273   if (!xdr_guestfs_message_header (&xdr, &hdr)) {
274     fprintf (stderr, "guestfsd: failed to encode error message header\n");
275     exit (EXIT_FAILURE);
276   }
277
278   /* These strings are not going to be freed.  We just cast them
279    * to (char *) because they are defined that way in the XDR structs.
280    */
281   err.errno_string =
282     (char *) (errnum > 0 ? guestfs___errno_to_string (errnum) : "");
283   err.error_message = (char *) msg;
284
285   if (!xdr_guestfs_message_error (&xdr, &err)) {
286     fprintf (stderr, "guestfsd: failed to encode error message body\n");
287     exit (EXIT_FAILURE);
288   }
289
290   len = xdr_getpos (&xdr);
291   xdr_destroy (&xdr);
292
293   xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
294   xdr_u_int (&xdr, &len);
295   xdr_destroy (&xdr);
296
297   if (xwrite (sock, lenbuf, 4) == -1) {
298     fprintf (stderr, "guestfsd: xwrite failed\n");
299     exit (EXIT_FAILURE);
300   }
301   if (xwrite (sock, buf, len) == -1) {
302     fprintf (stderr, "guestfsd: xwrite failed\n");
303     exit (EXIT_FAILURE);
304   }
305 }
306
307 void
308 reply (xdrproc_t xdrp, char *ret)
309 {
310   XDR xdr;
311   char buf[GUESTFS_MESSAGE_MAX];
312   char lenbuf[4];
313   struct guestfs_message_header hdr;
314   unsigned len;
315
316   xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
317
318   hdr.prog = GUESTFS_PROGRAM;
319   hdr.vers = GUESTFS_PROTOCOL_VERSION;
320   hdr.direction = GUESTFS_DIRECTION_REPLY;
321   hdr.status = GUESTFS_STATUS_OK;
322   hdr.proc = proc_nr;
323   hdr.serial = serial;
324
325   if (!xdr_guestfs_message_header (&xdr, &hdr)) {
326     fprintf (stderr, "guestfsd: failed to encode reply header\n");
327     exit (EXIT_FAILURE);
328   }
329
330   if (xdrp) {
331     /* This can fail if the reply body is too large, for example
332      * if it exceeds the maximum message size.  In that case
333      * we want to return an error message instead. (RHBZ#509597).
334      */
335     if (!(*xdrp) (&xdr, ret)) {
336       reply_with_error ("guestfsd: failed to encode reply body\n(maybe the reply exceeds the maximum message size in the protocol?)");
337       xdr_destroy (&xdr);
338       return;
339     }
340   }
341
342   len = xdr_getpos (&xdr);
343   xdr_destroy (&xdr);
344
345   xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
346   xdr_u_int (&xdr, &len);
347   xdr_destroy (&xdr);
348
349   if (xwrite (sock, lenbuf, 4) == -1) {
350     fprintf (stderr, "guestfsd: xwrite failed\n");
351     exit (EXIT_FAILURE);
352   }
353   if (xwrite (sock, buf, len) == -1) {
354     fprintf (stderr, "guestfsd: xwrite failed\n");
355     exit (EXIT_FAILURE);
356   }
357 }
358
359 /* Receive file chunks, repeatedly calling 'cb'. */
360 int
361 receive_file (receive_cb cb, void *opaque)
362 {
363   guestfs_chunk chunk;
364   char lenbuf[4];
365   char *buf;
366   XDR xdr;
367   int r;
368   uint32_t len;
369
370   for (;;) {
371     if (verbose)
372       fprintf (stderr, "guestfsd: receive_file: reading length word\n");
373
374     /* Read the length word. */
375     if (xread (sock, lenbuf, 4) == -1)
376       exit (EXIT_FAILURE);
377
378     xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
379     xdr_u_int (&xdr, &len);
380     xdr_destroy (&xdr);
381
382     if (len == GUESTFS_CANCEL_FLAG)
383       continue;                 /* Just ignore it. */
384
385     if (len > GUESTFS_MESSAGE_MAX) {
386       fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
387                len);
388       exit (EXIT_FAILURE);
389     }
390
391     buf = malloc (len);
392     if (!buf) {
393       perror ("malloc");
394       return -1;
395     }
396
397     if (xread (sock, buf, len) == -1)
398       exit (EXIT_FAILURE);
399
400     xdrmem_create (&xdr, buf, len, XDR_DECODE);
401     memset (&chunk, 0, sizeof chunk);
402     if (!xdr_guestfs_chunk (&xdr, &chunk)) {
403       xdr_destroy (&xdr);
404       free (buf);
405       return -1;
406     }
407     xdr_destroy (&xdr);
408     free (buf);
409
410     if (verbose)
411       fprintf (stderr,
412                "guestfsd: receive_file: got chunk: cancel = 0x%x, len = %d, buf = %p\n",
413                chunk.cancel, chunk.data.data_len, chunk.data.data_val);
414
415     if (chunk.cancel != 0 && chunk.cancel != 1) {
416       fprintf (stderr,
417                "guestfsd: receive_file: chunk.cancel != [0|1] ... "
418                "continuing even though we have probably lost synchronization with the library\n");
419       return -1;
420     }
421
422     if (chunk.cancel) {
423       if (verbose)
424         fprintf (stderr,
425           "guestfsd: receive_file: received cancellation from library\n");
426       xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
427       return -2;
428     }
429     if (chunk.data.data_len == 0) {
430       if (verbose)
431         fprintf (stderr,
432                  "guestfsd: receive_file: end of file, leaving function\n");
433       xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
434       return 0;                 /* end of file */
435     }
436
437     /* Note that the callback can generate progress messages. */
438     if (cb)
439       r = cb (opaque, chunk.data.data_val, chunk.data.data_len);
440     else
441       r = 0;
442
443     xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
444     if (r == -1) {              /* write error */
445       if (verbose)
446         fprintf (stderr, "guestfsd: receive_file: write error\n");
447       return -1;
448     }
449   }
450 }
451
452 /* Send a cancellation flag back to the library. */
453 int
454 cancel_receive (void)
455 {
456   XDR xdr;
457   char fbuf[4];
458   uint32_t flag = GUESTFS_CANCEL_FLAG;
459
460   xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
461   xdr_u_int (&xdr, &flag);
462   xdr_destroy (&xdr);
463
464   if (xwrite (sock, fbuf, sizeof fbuf) == -1) {
465     perror ("write to socket");
466     return -1;
467   }
468
469   /* Keep receiving chunks and discarding, until library sees cancel. */
470   return receive_file (NULL, NULL);
471 }
472
473 static int check_for_library_cancellation (void);
474 static int send_chunk (const guestfs_chunk *);
475
476 /* Also check if the library sends us a cancellation message. */
477 int
478 send_file_write (const void *buf, int len)
479 {
480   guestfs_chunk chunk;
481   int cancel;
482
483   if (len > GUESTFS_MAX_CHUNK_SIZE) {
484     fprintf (stderr, "guestfsd: send_file_write: len (%d) > GUESTFS_MAX_CHUNK_SIZE (%d)\n",
485              len, GUESTFS_MAX_CHUNK_SIZE);
486     return -1;
487   }
488
489   cancel = check_for_library_cancellation ();
490
491   if (cancel) {
492     chunk.cancel = 1;
493     chunk.data.data_len = 0;
494     chunk.data.data_val = NULL;
495   } else {
496     chunk.cancel = 0;
497     chunk.data.data_len = len;
498     chunk.data.data_val = (char *) buf;
499   }
500
501   if (send_chunk (&chunk) == -1)
502     return -1;
503
504   if (cancel) return -2;
505   return 0;
506 }
507
508 static int
509 check_for_library_cancellation (void)
510 {
511   fd_set rset;
512   struct timeval tv;
513   int r;
514   char buf[4];
515   uint32_t flag;
516   XDR xdr;
517
518   FD_ZERO (&rset);
519   FD_SET (sock, &rset);
520   tv.tv_sec = 0;
521   tv.tv_usec = 0;
522   r = select (sock+1, &rset, NULL, NULL, &tv);
523   if (r == -1) {
524     perror ("select");
525     return 0;
526   }
527   if (r == 0)
528     return 0;
529
530   /* Read the message from the daemon. */
531   r = xread (sock, buf, sizeof buf);
532   if (r == -1)
533     return 0;
534
535   xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
536   xdr_u_int (&xdr, &flag);
537   xdr_destroy (&xdr);
538
539   if (flag != GUESTFS_CANCEL_FLAG) {
540     fprintf (stderr, "guestfsd: check_for_library_cancellation: read 0x%x from library, expected 0x%x\n",
541              flag, GUESTFS_CANCEL_FLAG);
542     return 0;
543   }
544
545   return 1;
546 }
547
548 int
549 send_file_end (int cancel)
550 {
551   guestfs_chunk chunk;
552
553   chunk.cancel = cancel;
554   chunk.data.data_len = 0;
555   chunk.data.data_val = NULL;
556   return send_chunk (&chunk);
557 }
558
559 static int
560 send_chunk (const guestfs_chunk *chunk)
561 {
562   char buf[GUESTFS_MAX_CHUNK_SIZE + 48];
563   char lenbuf[4];
564   XDR xdr;
565   uint32_t len;
566
567   xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
568   if (!xdr_guestfs_chunk (&xdr, (guestfs_chunk *) chunk)) {
569     fprintf (stderr, "guestfsd: send_chunk: failed to encode chunk\n");
570     xdr_destroy (&xdr);
571     return -1;
572   }
573
574   len = xdr_getpos (&xdr);
575   xdr_destroy (&xdr);
576
577   xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
578   xdr_u_int (&xdr, &len);
579   xdr_destroy (&xdr);
580
581   int err = (xwrite (sock, lenbuf, 4) == 0
582              && xwrite (sock, buf, len) == 0 ? 0 : -1);
583   if (err) {
584     fprintf (stderr, "guestfsd: send_chunk: write failed\n");
585     exit (EXIT_FAILURE);
586   }
587
588   return err;
589 }
590
591 /* Initial delay before sending notification messages, and
592  * the period at which we send them thereafter.  These times
593  * are in microseconds.
594  */
595 #define NOTIFICATION_INITIAL_DELAY 2000000
596 #define NOTIFICATION_PERIOD         333333
597
598 void
599 notify_progress (uint64_t position, uint64_t total)
600 {
601   struct timeval now_t;
602   gettimeofday (&now_t, NULL);
603
604   /* Always send a notification at 100%.  This simplifies callers by
605    * allowing them to 'finish' the progress bar at 100% without
606    * needing special code.
607    */
608   if (count_progress > 0 && position == total)
609     goto send;
610
611   /* Calculate time in microseconds since the last progress message
612    * was sent out (or since the start of the call).
613    */
614   int64_t last_us, now_us, elapsed_us;
615   last_us =
616     (int64_t) last_progress_t.tv_sec * 1000000 + last_progress_t.tv_usec;
617   now_us = (int64_t) now_t.tv_sec * 1000000 + now_t.tv_usec;
618   elapsed_us = now_us - last_us;
619
620   /* Rate limit. */
621   if ((count_progress == 0 && elapsed_us < NOTIFICATION_INITIAL_DELAY) ||
622       (count_progress > 0 && elapsed_us < NOTIFICATION_PERIOD))
623     return;
624
625  send:
626   /* We're going to send a message now ... */
627   count_progress++;
628   last_progress_t = now_t;
629
630   /* Send the header word. */
631   XDR xdr;
632   char buf[128];
633   uint32_t i = GUESTFS_PROGRESS_FLAG;
634   size_t len;
635   xdrmem_create (&xdr, buf, 4, XDR_ENCODE);
636   xdr_u_int (&xdr, &i);
637   xdr_destroy (&xdr);
638
639   if (xwrite (sock, buf, 4) == -1) {
640     fprintf (stderr, "guestfsd: xwrite failed\n");
641     exit (EXIT_FAILURE);
642   }
643
644   guestfs_progress message = {
645     .proc = proc_nr,
646     .serial = serial,
647     .position = position,
648     .total = total,
649   };
650
651   xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
652   if (!xdr_guestfs_progress (&xdr, &message)) {
653     fprintf (stderr, "guestfsd: xdr_guestfs_progress: failed to encode message\n");
654     xdr_destroy (&xdr);
655     return;
656   }
657   len = xdr_getpos (&xdr);
658   xdr_destroy (&xdr);
659
660   if (xwrite (sock, buf, len) == -1) {
661     fprintf (stderr, "guestfsd: xwrite failed\n");
662     exit (EXIT_FAILURE);
663   }
664 }
665
666 /* "Pulse mode" progress messages. */
667
668 #if defined(HAVE_SETITIMER) && defined(HAVE_SIGACTION)
669
670 static void async_safe_send_pulse (int sig);
671
672 void
673 pulse_mode_start (void)
674 {
675   struct sigaction act;
676   struct itimerval it;
677
678   memset (&act, 0, sizeof act);
679   act.sa_handler = async_safe_send_pulse;
680   act.sa_flags = SA_RESTART;
681
682   if (sigaction (SIGALRM, &act, NULL) == -1) {
683     perror ("pulse_mode_start: sigaction");
684     return;
685   }
686
687   it.it_value.tv_sec = NOTIFICATION_INITIAL_DELAY / 1000000;
688   it.it_value.tv_usec = NOTIFICATION_INITIAL_DELAY % 1000000;
689   it.it_interval.tv_sec = NOTIFICATION_PERIOD / 1000000;
690   it.it_interval.tv_usec = NOTIFICATION_PERIOD % 1000000;
691
692   if (setitimer (ITIMER_REAL, &it, NULL) == -1)
693     perror ("pulse_mode_start: setitimer");
694 }
695
696 void
697 pulse_mode_end (void)
698 {
699   pulse_mode_cancel ();         /* Cancel the itimer. */
700
701   notify_progress (1, 1);
702 }
703
704 void
705 pulse_mode_cancel (void)
706 {
707   int err = errno;              /* Function must preserve errno. */
708   struct itimerval it;
709   struct sigaction act;
710
711   /* Setting it_value to zero cancels the itimer. */
712   it.it_value.tv_sec = 0;
713   it.it_value.tv_usec = 0;
714   it.it_interval.tv_sec = 0;
715   it.it_interval.tv_usec = 0;
716
717   if (setitimer (ITIMER_REAL, &it, NULL) == -1)
718     perror ("pulse_mode_cancel: setitimer");
719
720   memset (&act, 0, sizeof act);
721   act.sa_handler = SIG_DFL;
722
723   if (sigaction (SIGALRM, &act, NULL) == -1)
724     perror ("pulse_mode_cancel: sigaction");
725
726   errno = err;
727 }
728
729 /* Send a position = 0, total = 1 (pulse mode) message.  The tricky
730  * part is we have to do it without invoking any non-async-safe
731  * functions (see signal(7) for a list).  Therefore, KISS.
732  */
733 static void
734 async_safe_send_pulse (int sig)
735 {
736   /* XDR is a RFC ... */
737   unsigned char msg[] = {
738     (GUESTFS_PROGRESS_FLAG & 0xff000000) >> 24,
739     (GUESTFS_PROGRESS_FLAG & 0x00ff0000) >> 16,
740     (GUESTFS_PROGRESS_FLAG & 0x0000ff00) >> 8,
741     GUESTFS_PROGRESS_FLAG & 0x000000ff,
742     (proc_nr & 0xff000000) >> 24,
743     (proc_nr & 0x00ff0000) >> 16,
744     (proc_nr & 0x0000ff00) >> 8,
745     proc_nr & 0x000000ff,
746     (serial & 0xff000000) >> 24,
747     (serial & 0x00ff0000) >> 16,
748     (serial & 0x0000ff00) >> 8,
749     serial & 0x000000ff,
750     /* 64 bit position = 0 */ 0, 0, 0, 0, 0, 0, 0, 0,
751     /* 64 bit total = 1 */    0, 0, 0, 0, 0, 0, 0, 1
752   };
753
754   if (xwrite (sock, msg, sizeof msg) == -1)
755     exit (EXIT_FAILURE);
756 }
757
758 #else /* !HAVE_SETITIMER || !HAVE_SIGACTION */
759
760 void
761 pulse_mode_start (void)
762 {
763   /* empty */
764 }
765
766 void
767 pulse_mode_end (void)
768 {
769   /* empty */
770 }
771
772 void
773 pulse_mode_cancel (void)
774 {
775   /* empty */
776 }
777
778 #endif /* !HAVE_SETITIMER || !HAVE_SIGACTION */