All of lore.kernel.org
 help / color / mirror / Atom feed
From: Heinrich Schuchardt <xypron.glpk-Mmb7MZpHnFY@public.gmane.org>
To: Theodore Ts'o <tytso-3s7WtUTddSA@public.gmane.org>
Cc: Michael Kerrisk
	<mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
	linux-man <linux-man-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
	lkml <linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>
Subject: getrandom.2: treatment of interrupts
Date: Sat, 22 Nov 2014 12:28:18 +0100	[thread overview]
Message-ID: <54707352.8050208@gmx.de> (raw)
In-Reply-To: <CAKgNAkgVUeekKZjyTsjWgkXfv_u72T_ms7qofSJ_omqhfExfCw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>

Hello Theodore,

I created the test program below.

While running it I issued
kill -SIGUSR1 <pid>
and
kill -SIGUSR2 <pid>

What I found was rather strange.

No matter whether specifying GRND_NONBLOCK or not, signals do not 
interrupt the execution of getrandom() while reading from the 
/dev/urandom pool.

Only after getrandom has finished signals are handled.

I would have expected getrandom() to react to interrupts immediately and 
to return whatever number of random bytes have been collected before the 
interrupt.

A system call not reacting to interrupts for several seconds looks like 
a bug to me.

Tested on Linux 3.18.0-rc4 mips64.

Best regards

Heinrich Schuchardt



#define _GNU_SOURCE
#include <errno.h>
#include <linux/unistd.h>
#include <linux/random.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/types.h>

#if _MIPS_SIM == _MIPS_SIM_ABI32
#define __NR_getrandom                  (__NR_Linux + 353)
#endif /* _MIPS_SIM == _MIPS_SIM_ABI32 */

#if _MIPS_SIM == _MIPS_SIM_ABI64
#define __NR_getrandom                  (__NR_Linux + 313)
#endif /* _MIPS_SIM == _MIPS_SIM_ABI64 */

#if _MIPS_SIM == _MIPS_SIM_NABI32
#define __NR_getrandom                  (__NR_Linux + 317)
#endif /* _MIPS_SIM == _MIPS_SIM_NABI32 */

#ifdef __i386__
#define __NR_getrandom                  (355)
#endif /* __i386__ */

#ifdef __x86_64__
#define __NR_getrandom                  (318)
#endif /* __x86_64__ */

#define SYS_getrandom __NR_getrandom

#define GRND_NONBLOCK   0x0001
#define GRND_RANDOM     0x0002

#define BUFLEN 0x12345678

int getrandom(void *buf, size_t buflen, unsigned int flags)
{
         return syscall(SYS_getrandom, buf, buflen, flags);
}

/**
  * Handles signal.
  *
  * @param sig signal
  */


int do_print = 0;

static void hdl(int sig)
{
         if (sig == SIGUSR1) {
                 fprintf(stderr, "Main received SIGUSR1\n");
                 do_print = 1;
         }
}

int
main(int argc, char *argv[])
{

         char *buf;
         size_t buflen = BUFLEN;
         int ret;
         pid_t pid;
         // action to take when signal occurs
         struct sigaction act;
         // signal mask
         sigset_t blockset;


         pid = getpid();

         printf("PID = %u\n", pid);

         printf("__NR_getrandom = %u\n", __NR_getrandom);

         // Set handler for SIGUSR1
         act.sa_handler = hdl;
         sigemptyset(&act.sa_mask);
         act.sa_flags = 0;
         if (sigaction(SIGUSR1, &act, NULL)) {
                 perror("sigaction");
                 exit(EXIT_FAILURE);
         }

         buf = (char *) malloc(buflen);
         if (buf == NULL) {
                 perror("malloc");
                 exit(EXIT_FAILURE);
         }

         buf = (char *) malloc(buflen);

         for (;;) {
                 ret = getrandom(buf, buflen, GRND_NONBLOCK);
                 if (ret == -1) {
                         fprintf(stderr, "errno = %d\n", errno);
                         perror("getrandom");
                         exit(EXIT_FAILURE);
                 }
                 if (do_print) {
                         do_print = 0;
                         printf("ret = %d\n", ret);
                 }

         }
         printf("ret = %d\n", ret);

         free(buf);

         return EXIT_SUCCESS;
}

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

WARNING: multiple messages have this Message-ID (diff)
From: Heinrich Schuchardt <xypron.glpk@gmx.de>
To: "Theodore Ts'o" <tytso@mit.edu>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>,
	linux-man <linux-man@vger.kernel.org>,
	lkml <linux-kernel@vger.kernel.org>
Subject: getrandom.2: treatment of interrupts
Date: Sat, 22 Nov 2014 12:28:18 +0100	[thread overview]
Message-ID: <54707352.8050208@gmx.de> (raw)
In-Reply-To: <CAKgNAkgVUeekKZjyTsjWgkXfv_u72T_ms7qofSJ_omqhfExfCw@mail.gmail.com>

Hello Theodore,

I created the test program below.

While running it I issued
kill -SIGUSR1 <pid>
and
kill -SIGUSR2 <pid>

What I found was rather strange.

No matter whether specifying GRND_NONBLOCK or not, signals do not 
interrupt the execution of getrandom() while reading from the 
/dev/urandom pool.

Only after getrandom has finished signals are handled.

I would have expected getrandom() to react to interrupts immediately and 
to return whatever number of random bytes have been collected before the 
interrupt.

A system call not reacting to interrupts for several seconds looks like 
a bug to me.

Tested on Linux 3.18.0-rc4 mips64.

Best regards

Heinrich Schuchardt



#define _GNU_SOURCE
#include <errno.h>
#include <linux/unistd.h>
#include <linux/random.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/types.h>

#if _MIPS_SIM == _MIPS_SIM_ABI32
#define __NR_getrandom                  (__NR_Linux + 353)
#endif /* _MIPS_SIM == _MIPS_SIM_ABI32 */

#if _MIPS_SIM == _MIPS_SIM_ABI64
#define __NR_getrandom                  (__NR_Linux + 313)
#endif /* _MIPS_SIM == _MIPS_SIM_ABI64 */

#if _MIPS_SIM == _MIPS_SIM_NABI32
#define __NR_getrandom                  (__NR_Linux + 317)
#endif /* _MIPS_SIM == _MIPS_SIM_NABI32 */

#ifdef __i386__
#define __NR_getrandom                  (355)
#endif /* __i386__ */

#ifdef __x86_64__
#define __NR_getrandom                  (318)
#endif /* __x86_64__ */

#define SYS_getrandom __NR_getrandom

#define GRND_NONBLOCK   0x0001
#define GRND_RANDOM     0x0002

#define BUFLEN 0x12345678

int getrandom(void *buf, size_t buflen, unsigned int flags)
{
         return syscall(SYS_getrandom, buf, buflen, flags);
}

/**
  * Handles signal.
  *
  * @param sig signal
  */


int do_print = 0;

static void hdl(int sig)
{
         if (sig == SIGUSR1) {
                 fprintf(stderr, "Main received SIGUSR1\n");
                 do_print = 1;
         }
}

int
main(int argc, char *argv[])
{

         char *buf;
         size_t buflen = BUFLEN;
         int ret;
         pid_t pid;
         // action to take when signal occurs
         struct sigaction act;
         // signal mask
         sigset_t blockset;


         pid = getpid();

         printf("PID = %u\n", pid);

         printf("__NR_getrandom = %u\n", __NR_getrandom);

         // Set handler for SIGUSR1
         act.sa_handler = hdl;
         sigemptyset(&act.sa_mask);
         act.sa_flags = 0;
         if (sigaction(SIGUSR1, &act, NULL)) {
                 perror("sigaction");
                 exit(EXIT_FAILURE);
         }

         buf = (char *) malloc(buflen);
         if (buf == NULL) {
                 perror("malloc");
                 exit(EXIT_FAILURE);
         }

         buf = (char *) malloc(buflen);

         for (;;) {
                 ret = getrandom(buf, buflen, GRND_NONBLOCK);
                 if (ret == -1) {
                         fprintf(stderr, "errno = %d\n", errno);
                         perror("getrandom");
                         exit(EXIT_FAILURE);
                 }
                 if (do_print) {
                         do_print = 0;
                         printf("ret = %d\n", ret);
                 }

         }
         printf("ret = %d\n", ret);

         free(buf);

         return EXIT_SUCCESS;
}


  parent reply	other threads:[~2014-11-22 11:28 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-13 13:15 [PATCH 1/1] getrandom(2) : new man page Heinrich Schuchardt
     [not found] ` <1410614156-16175-1-git-send-email-xypron.glpk-Mmb7MZpHnFY@public.gmane.org>
2014-09-29 15:22   ` Michael Kerrisk (man-pages)
     [not found]     ` <5429791D.6080903-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-09-29 15:52       ` Theodore Ts'o
2014-09-30  0:38       ` Aw: " Heinrich Schuchardt
2014-09-30  9:22         ` Michael Kerrisk (man-pages)
     [not found]           ` <542A7645.8070802-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-10-03  0:13             ` [PATCH 0/3] getrandom.2: new manpage Heinrich Schuchardt
     [not found]               ` <1412295197-8100-1-git-send-email-xypron.glpk-Mmb7MZpHnFY@public.gmane.org>
2014-10-03  0:15                 ` [PATCH 1/3] " Heinrich Schuchardt
     [not found]                   ` <1412295313-8198-1-git-send-email-xypron.glpk-Mmb7MZpHnFY@public.gmane.org>
2014-10-06 18:13                     ` Michael Kerrisk (man-pages)
2014-10-28 11:37                     ` Michael Kerrisk (man-pages)
2014-10-28 11:37                       ` Michael Kerrisk (man-pages)
2014-10-28 19:51                     ` Michael Kerrisk (man-pages)
2014-11-11 11:44                   ` Michael Kerrisk (man-pages)
2014-11-11 16:19                     ` [PATCH] getrandom.2: treatment of interrupts Heinrich Schuchardt
2014-11-11 16:19                       ` Heinrich Schuchardt
     [not found]                       ` <1415722798-4894-1-git-send-email-xypron.glpk-Mmb7MZpHnFY@public.gmane.org>
2014-11-16 15:55                         ` Michael Kerrisk (man-pages)
2014-11-16 15:55                           ` Michael Kerrisk (man-pages)
     [not found]                           ` <CAKgNAkgVUeekKZjyTsjWgkXfv_u72T_ms7qofSJ_omqhfExfCw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-11-22 11:28                             ` Heinrich Schuchardt [this message]
2014-11-22 11:28                               ` Heinrich Schuchardt
2014-11-29  9:12                               ` [PATCH 1/1] urandom: handle signals immediately Heinrich Schuchardt
2014-12-19 16:57                                 ` Theodore Ts'o
2014-12-19 18:55                                   ` Heinrich Schuchardt
2015-01-10 13:23                     ` [PATCH 1/3] getrandom.2: new manpage Michael Kerrisk (man-pages)
2015-01-10 13:23                       ` Michael Kerrisk (man-pages)
     [not found]                       ` <CAKgNAkiQUXFZ82jDNqEPxpBmdkKOg03uQXg=iEuUNzg0rvgkZA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-01-22 18:30                         ` [PATCH 1/1] getrandom.2: mention bug concerning treatment of interrupts Heinrich Schuchardt
     [not found]                           ` <1421951410-6420-1-git-send-email-xypron.glpk-Mmb7MZpHnFY@public.gmane.org>
2015-01-22 19:58                             ` Michael Kerrisk (man-pages)
2015-01-22 19:30                         ` [PATCH 1/1] getrandom.2: rework paragraphs marked with FIXME Heinrich Schuchardt
     [not found]                           ` <1421955046-8296-1-git-send-email-xypron.glpk-Mmb7MZpHnFY@public.gmane.org>
2015-01-23  6:01                             ` Michael Kerrisk (man-pages)
2014-10-03  0:15                 ` [PATCH 2/3] random.3: SEE ALSO getrandom.2 Heinrich Schuchardt
     [not found]                   ` <1412295324-8241-1-git-send-email-xypron.glpk-Mmb7MZpHnFY@public.gmane.org>
2014-10-28 14:37                     ` Michael Kerrisk (man-pages)
2014-10-03  0:15                 ` [PATCH 3/3] random.4: " Heinrich Schuchardt
     [not found]                   ` <1412295335-8287-1-git-send-email-xypron.glpk-Mmb7MZpHnFY@public.gmane.org>
2014-10-28 14:38                     ` Michael Kerrisk (man-pages)

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=54707352.8050208@gmx.de \
    --to=xypron.glpk-mmb7mzphnfy@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-man-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=tytso-3s7WtUTddSA@public.gmane.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.