All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: Fei Li <fli@suse.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 1/5] Fix segmentation fault when qemu_signal_init fails
Date: Tue, 4 Sep 2018 12:26:20 +0100	[thread overview]
Message-ID: <20180904112620.GG22349@redhat.com> (raw)
In-Reply-To: <20180904110822.12863-2-fli@suse.com>

On Tue, Sep 04, 2018 at 07:08:18PM +0800, Fei Li wrote:
> Currently, when qemu_signal_init() fails it only returns a non-zero
> value but without propagating any Error. But its callers need a
> non-null err when runs error_report_err(err), or else 0->msg occurs.
> 
> To avoid such segmentation fault, add a new Error parameter to make
> the call trace to propagate the err to the final caller.
> 
> Signed-off-by: Fei Li <fli@suse.com>
> ---
>  include/qemu/osdep.h |  2 +-
>  util/compatfd.c      | 17 ++++++++++++-----
>  util/main-loop.c     | 11 +++++++----
>  3 files changed, 20 insertions(+), 10 deletions(-)
> 
> diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
> index a91068df0e..09ed85fcb8 100644
> --- a/include/qemu/osdep.h
> +++ b/include/qemu/osdep.h
> @@ -421,7 +421,7 @@ struct qemu_signalfd_siginfo {
>                               additional fields in the future) */
>  };
>  
> -int qemu_signalfd(const sigset_t *mask);
> +int qemu_signalfd(const sigset_t *mask, Error **errp);
>  void sigaction_invoke(struct sigaction *action,
>                        struct qemu_signalfd_siginfo *info);
>  #endif
> diff --git a/util/compatfd.c b/util/compatfd.c
> index 980bd33e52..65501de622 100644
> --- a/util/compatfd.c
> +++ b/util/compatfd.c
> @@ -16,6 +16,7 @@
>  #include "qemu/osdep.h"
>  #include "qemu-common.h"
>  #include "qemu/thread.h"
> +#include "qapi/error.h"
>  
>  #include <sys/syscall.h>
>  
> @@ -65,7 +66,7 @@ static void *sigwait_compat(void *opaque)
>      }
>  }
>  
> -static int qemu_signalfd_compat(const sigset_t *mask)
> +static int qemu_signalfd_compat(const sigset_t *mask, Error **errp)
>  {
>      struct sigfd_compat_info *info;
>      QemuThread thread;
> @@ -73,11 +74,13 @@ static int qemu_signalfd_compat(const sigset_t *mask)
>  
>      info = malloc(sizeof(*info));
>      if (info == NULL) {
> +        error_setg(errp, "Failed to malloc in %s", __func__);

I'd avoid using __func__ in error messages. Instead

   "Failed to allocate signalfd memory"

>          errno = ENOMEM;
>          return -1;
>      }
>  
>      if (pipe(fds) == -1) {
> +        error_setg(errp, "Failed to create a pipe in %s", __func__);

    "Failed to create signalfd pipe"

>          free(info);
>          return -1;
>      }
> @@ -94,17 +97,21 @@ static int qemu_signalfd_compat(const sigset_t *mask)
>      return fds[0];
>  }
>  
> -int qemu_signalfd(const sigset_t *mask)
> +int qemu_signalfd(const sigset_t *mask, Error **errp)
>  {
> -#if defined(CONFIG_SIGNALFD)
>      int ret;
> +    Error *local_err = NULL;
>  
> +#if defined(CONFIG_SIGNALFD)
>      ret = syscall(SYS_signalfd, -1, mask, _NSIG / 8);
>      if (ret != -1) {
>          qemu_set_cloexec(ret);
>          return ret;
>      }
>  #endif
> -
> -    return qemu_signalfd_compat(mask);
> +    ret = qemu_signalfd_compat(mask, &local_err);
> +    if (local_err) {
> +        error_propagate(errp, local_err);
> +    }

Using a local_err is not required - you can just pass errp stright
to qemu_signalfd_compat() and then check

   if (ret < 0)

> +    return ret;
>  }
> diff --git a/util/main-loop.c b/util/main-loop.c
> index affe0403c5..20f6ad3849 100644
> --- a/util/main-loop.c
> +++ b/util/main-loop.c
> @@ -71,10 +71,11 @@ static void sigfd_handler(void *opaque)
>      }
>  }
>  
> -static int qemu_signal_init(void)
> +static int qemu_signal_init(Error **errp)
>  {
>      int sigfd;
>      sigset_t set;
> +    Error *local_err = NULL;
>  
>      /*
>       * SIG_IPI must be blocked in the main thread and must not be caught
> @@ -94,9 +95,10 @@ static int qemu_signal_init(void)
>      pthread_sigmask(SIG_BLOCK, &set, NULL);
>  
>      sigdelset(&set, SIG_IPI);
> -    sigfd = qemu_signalfd(&set);
> +    sigfd = qemu_signalfd(&set, &local_err);
>      if (sigfd == -1) {
>          fprintf(stderr, "failed to create signalfd\n");
> +        error_propagate(errp, local_err);
>          return -errno;
>      }

Again, no need for local_err - just pass errp stright in

>  
> @@ -109,7 +111,7 @@ static int qemu_signal_init(void)
>  
>  #else /* _WIN32 */
>  
> -static int qemu_signal_init(void)
> +static int qemu_signal_init(Error **errp)
>  {
>      return 0;
>  }
> @@ -148,8 +150,9 @@ int qemu_init_main_loop(Error **errp)
>  
>      init_clocks(qemu_timer_notify_cb);
>  
> -    ret = qemu_signal_init();
> +    ret = qemu_signal_init(&local_error);
>      if (ret) {
> +        error_propagate(errp, local_error);
>          return ret;
>      }

Again, no need for local_error.


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

  reply	other threads:[~2018-09-04 11:26 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-04 11:08 [Qemu-devel] [PATCH 0/5] qemu_thread_create: propagate errors to callers to check Fei Li
2018-09-04 11:08 ` [Qemu-devel] [PATCH 1/5] Fix segmentation fault when qemu_signal_init fails Fei Li
2018-09-04 11:26   ` Daniel P. Berrangé [this message]
2018-09-05  4:17     ` Fei Li
2018-09-05  8:36       ` Daniel P. Berrangé
2018-09-05 11:20         ` Fei Li
2018-09-05 14:38           ` Fam Zheng
2018-09-06  6:31             ` Fei Li
2018-09-04 11:08 ` [Qemu-devel] [PATCH 2/5] ui/vnc.c: polish vnc_init_func Fei Li
2018-09-04 11:08 ` [Qemu-devel] [PATCH 3/5] qemu_init_vcpu: add a new Error paramater to propagate Fei Li
2018-09-04 11:22   ` Daniel P. Berrangé
2018-09-05  4:36     ` Fei Li
2018-09-05 17:15     ` Eric Blake
2018-09-06  6:52       ` Fei Li
2018-09-04 11:08 ` [Qemu-devel] [PATCH 4/5] qemu_thread_create: propagate the error to callers to check Fei Li
2018-09-04 11:18   ` Daniel P. Berrangé
2018-09-05  4:20     ` Fei Li
2018-09-04 11:08 ` [Qemu-devel] [PATCH 5/5] Propagate qemu_thread_create's error to all callers to handle Fei Li
2018-09-04 11:20   ` Daniel P. Berrangé

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180904112620.GG22349@redhat.com \
    --to=berrange@redhat.com \
    --cc=fli@suse.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.