[PATCH] Properly handle short writes in sigint handlers

Dmitry Kurochkin dmitry.kurochkin at gmail.com
Fri Dec 23 11:10:35 PST 2011


Hi Austin.

On Thu, 22 Dec 2011 15:15:48 -0500, Austin Clements <amdragon at MIT.EDU> wrote:
> Even if we don't care about errors from write(2), it's still necessary
> to handle short writes in order to use write correctly.  Some versions
> of glibc even mark write as warn_unused_result because of this, so our
> previous usage would trigger compiler warnings.

I think we should put the write loop into a separate function and reuse
it.

Also, does it make sense to add a retry counter to prevent infinite loop
if write keeps returning 0?

Regards,
  Dmitry

> ---
>  notmuch-new.c |    5 ++++-
>  notmuch-tag.c |    6 +++++-
>  2 files changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/notmuch-new.c b/notmuch-new.c
> index 3512de7..fc09bbb 100644
> --- a/notmuch-new.c
> +++ b/notmuch-new.c
> @@ -66,8 +66,11 @@ static void
>  handle_sigint (unused (int sig))
>  {
>      static char msg[] = "Stopping...         \n";
> +    const char *pos = msg, *end = msg + sizeof (msg) - 1;
> +    ssize_t c = 0;
>  
> -    (void) write(2, msg, sizeof(msg)-1);
> +    for (; pos < end && c >= 0; pos += c)
> +	c = write (2, pos, end - pos);
>      interrupted = 1;
>  }
>  
> diff --git a/notmuch-tag.c b/notmuch-tag.c
> index 292c5da..0d4873d 100644
> --- a/notmuch-tag.c
> +++ b/notmuch-tag.c
> @@ -26,7 +26,11 @@ static void
>  handle_sigint (unused (int sig))
>  {
>      static char msg[] = "Stopping...         \n";
> -    (void) write(2, msg, sizeof(msg)-1);
> +    const char *pos = msg, *end = msg + sizeof (msg) - 1;
> +    ssize_t c = 0;
> +
> +    for (; pos < end && c >= 0; pos += c)
> +	c = write (2, pos, end - pos);
>      interrupted = 1;
>  }
>  
> -- 
> 1.7.7.3
> 
> _______________________________________________
> notmuch mailing list
> notmuch at notmuchmail.org
> http://notmuchmail.org/mailman/listinfo/notmuch


More information about the notmuch mailing list