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;
}
next prev parent reply other threads:[~2014-11-22 11:36 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1412295197-8100-1-git-send-email-xypron.glpk@gmx.de>
[not found] ` <1412295313-8198-1-git-send-email-xypron.glpk@gmx.de>
2014-10-28 11:37 ` [PATCH 1/3] getrandom.2: new manpage 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-16 15:55 ` Michael Kerrisk (man-pages)
2014-11-22 11:28 ` Heinrich Schuchardt [this message]
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)
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@gmx.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-man@vger.kernel.org \
--cc=mtk.manpages@gmail.com \
--cc=tytso@mit.edu \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox