X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=daemon%2Fproto.c;h=88e2d28c793739a19cd6b46d990586ecdd087b85;hp=32137bbf3d32a54dc1b47d533a66315cd15e5f1d;hb=5ce759bfdc70c97121ef34ab4d5c918d568f2474;hpb=7fb25f53f836172376c25b405b5f88f4ec37eed8 diff --git a/daemon/proto.c b/daemon/proto.c index 32137bb..88e2d28 100644 --- a/daemon/proto.c +++ b/daemon/proto.c @@ -1,5 +1,5 @@ /* libguestfs - the guestfsd daemon - * Copyright (C) 2009 Red Hat Inc. + * Copyright (C) 2009-2011 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -87,12 +88,6 @@ main_loop (int _sock) sock = _sock; for (;;) { - /* Most common errors are leaked memory and leaked file descriptors, - * so run this between each command: - */ - if (verbose && 0) - ignore_value (system ("ls -l /proc/self/fd")); - /* Read the length word. */ if (xread (sock, lenbuf, 4) == -1) exit (EXIT_FAILURE); @@ -661,3 +656,117 @@ notify_progress (uint64_t position, uint64_t total) exit (EXIT_FAILURE); } } + +/* "Pulse mode" progress messages. */ + +#if defined(HAVE_SETITIMER) && defined(HAVE_SIGACTION) + +static void async_safe_send_pulse (int sig); + +void +pulse_mode_start (void) +{ + struct sigaction act; + struct itimerval it; + + memset (&act, 0, sizeof act); + act.sa_handler = async_safe_send_pulse; + act.sa_flags = SA_RESTART; + + if (sigaction (SIGALRM, &act, NULL) == -1) { + perror ("pulse_mode_start: sigaction"); + return; + } + + it.it_value.tv_sec = NOTIFICATION_INITIAL_DELAY / 1000000; + it.it_value.tv_usec = NOTIFICATION_INITIAL_DELAY % 1000000; + it.it_interval.tv_sec = NOTIFICATION_PERIOD / 1000000; + it.it_interval.tv_usec = NOTIFICATION_PERIOD % 1000000; + + if (setitimer (ITIMER_REAL, &it, NULL) == -1) + perror ("pulse_mode_start: setitimer"); +} + +void +pulse_mode_end (void) +{ + pulse_mode_cancel (); /* Cancel the itimer. */ + + notify_progress (1, 1); +} + +void +pulse_mode_cancel (void) +{ + int err = errno; /* Function must preserve errno. */ + struct itimerval it; + struct sigaction act; + + /* Setting it_value to zero cancels the itimer. */ + it.it_value.tv_sec = 0; + it.it_value.tv_usec = 0; + it.it_interval.tv_sec = 0; + it.it_interval.tv_usec = 0; + + if (setitimer (ITIMER_REAL, &it, NULL) == -1) + perror ("pulse_mode_cancel: setitimer"); + + memset (&act, 0, sizeof act); + act.sa_handler = SIG_DFL; + + if (sigaction (SIGALRM, &act, NULL) == -1) + perror ("pulse_mode_cancel: sigaction"); + + errno = err; +} + +/* Send a position = 0, total = 1 (pulse mode) message. The tricky + * part is we have to do it without invoking any non-async-safe + * functions (see signal(7) for a list). Therefore, KISS. + */ +static void +async_safe_send_pulse (int sig) +{ + /* XDR is a RFC ... */ + unsigned char msg[] = { + (GUESTFS_PROGRESS_FLAG & 0xff000000) >> 24, + (GUESTFS_PROGRESS_FLAG & 0x00ff0000) >> 16, + (GUESTFS_PROGRESS_FLAG & 0x0000ff00) >> 8, + GUESTFS_PROGRESS_FLAG & 0x000000ff, + (proc_nr & 0xff000000) >> 24, + (proc_nr & 0x00ff0000) >> 16, + (proc_nr & 0x0000ff00) >> 8, + proc_nr & 0x000000ff, + (serial & 0xff000000) >> 24, + (serial & 0x00ff0000) >> 16, + (serial & 0x0000ff00) >> 8, + serial & 0x000000ff, + /* 64 bit position = 0 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 64 bit total = 1 */ 0, 0, 0, 0, 0, 0, 0, 1 + }; + + if (xwrite (sock, msg, sizeof msg) == -1) + exit (EXIT_FAILURE); +} + +#else /* !HAVE_SETITIMER || !HAVE_SIGACTION */ + +void +pulse_mode_start (void) +{ + /* empty */ +} + +void +pulse_mode_end (void) +{ + /* empty */ +} + +void +pulse_mode_cancel (void) +{ + /* empty */ +} + +#endif /* !HAVE_SETITIMER || !HAVE_SIGACTION */