Re: [PATCH v2 5/5] Extend events demo to show close callbacks in use

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jul 24, 2012 at 14:17:05 +0100, Daniel P. Berrange wrote:
> From: "Daniel P. Berrange" <berrange@xxxxxxxxxx>
> 
> Use a driver close callback to trigger shutdown of the
> events demo program
> 
> Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx>
> ---
>  examples/domain-events/events-c/event-test.c |   33 ++++++++++++++++++++++++--
>  1 file changed, 31 insertions(+), 2 deletions(-)
> 
> diff --git a/examples/domain-events/events-c/event-test.c b/examples/domain-events/events-c/event-test.c
> index ef6e77a..f2427ad 100644
> --- a/examples/domain-events/events-c/event-test.c
> +++ b/examples/domain-events/events-c/event-test.c
> @@ -16,6 +16,8 @@
>  # define ATTRIBUTE_UNUSED __attribute__((__unused__))
>  #endif
>  
> +int run = 1;
> +
>  /* Prototypes */
>  const char *eventToString(int event);
>  int myEventAddHandleFunc  (int fd, int event,
> @@ -39,6 +41,31 @@ void usage(const char *pname);
>  
>  /* Callback functions */
>  
> +
> +static void connectClose(virConnectPtr conn ATTRIBUTE_UNUSED,
> +                         int reason,
> +                         void *opaque ATTRIBUTE_UNUSED)
> +{
> +    switch (reason) {
> +    case VIR_CONNECT_CLOSE_REASON_ERROR:
> +        fprintf(stderr, "Connection closed due to I/O error\n");
> +        break;
> +    case VIR_CONNECT_CLOSE_REASON_EOF:
> +        fprintf(stderr, "Connection closed due to end of file\n");
> +        break;
> +    case VIR_CONNECT_CLOSE_REASON_KEEPALIVE:
> +        fprintf(stderr, "Connection closed due to keepalive timeout\n");
> +        break;
> +    case VIR_CONNECT_CLOSE_REASON_CLIENT:
> +        fprintf(stderr, "Connection closed due to client request\n");
> +        break;
> +    default:
> +        fprintf(stderr, "Connection closed due to unknown error\n");

I'd rather print "...due to unknown reason" since new reasons do not have to
be errors.

> +        break;
> +    };
> +    run = 0;
> +}
> +
>  const char *eventToString(int event) {
>      const char *ret = "";
>      switch ((virDomainEventType) event) {
> @@ -380,7 +407,6 @@ void usage(const char *pname)
>      printf("%s uri\n", pname);
>  }
>  
> -int run = 1;
>  
>  static void stop(int sig)
>  {
> @@ -426,6 +452,9 @@ int main(int argc, char **argv)
>          return -1;
>      }
>  
> +    virConnectSetCloseCallback(dconn,
> +                               connectClose, NULL, NULL);
> +
>      sigaction(SIGTERM, &action_stop, NULL);
>      sigaction(SIGINT, &action_stop, NULL);
>  
> @@ -513,7 +542,7 @@ int main(int argc, char **argv)
>              run = 0;
>          }
>  
> -        while (run && virConnectIsAlive(dconn) == 1) {
> +        while (run) {
>              if (virEventRunDefaultImpl() < 0) {
>                  virErrorPtr err = virGetLastError();
>                  fprintf(stderr, "Failed to run event loop: %s\n",

ACK

Jirka

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]