[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <1356690181-1796-4-git-send-email-avagin@openvz.org>
Date: Fri, 28 Dec 2012 14:23:01 +0400
From: Andrey Vagin <avagin@...nvz.org>
To: linux-kernel@...r.kernel.org
Cc: criu@...nvz.org, linux-fsdevel@...r.kernel.org,
linux-api@...r.kernel.org, Andrey Vagin <avagin@...nvz.org>,
Oleg Nesterov <oleg@...hat.com>,
Alexander Viro <viro@...iv.linux.org.uk>,
"Paul E. McKenney" <paulmck@...ux.vnet.ibm.com>,
David Howells <dhowells@...hat.com>,
Dave Jones <davej@...hat.com>,
Michael Kerrisk <mtk.manpages@...il.com>,
Pavel Emelyanov <xemul@...allels.com>,
Cyrill Gorcunov <gorcunov@...nvz.org>
Subject: [PATCH 3/3] signalfd: add ability to read siginfo-s without dequeuing signals (v3)
pread(fd, buf, size, pos) with non-zero pos returns siginfo-s
without dequeuing signals.
A sequence number and a queue are encoded in pos.
pos = seq + SFD_*_OFFSET
seq is a sequence number of a signal in a queue.
SFD_PER_THREAD_QUEUE_OFFSET - read signals from a per-thread queue.
SFD_SHARED_QUEUE_OFFSET - read signals from a shared (process wide) queue.
This functionality is required for checkpointing pending signals.
v2: llseek() can't be used here, because peek_offset/f_pos/whatever
has to be shared with all processes which have this file opened.
Suppose that the task forks after sys_signalfd(). Now if parent or child
do llseek this affects them both. This is insane because signalfd is
"strange" to say at least, fork/dup/etc inherits signalfd_ctx but not
the" source" of the data. // Oleg Nesterov
v3: minor cleanup
Cc: Oleg Nesterov <oleg@...hat.com>
Cc: Alexander Viro <viro@...iv.linux.org.uk>
Cc: "Paul E. McKenney" <paulmck@...ux.vnet.ibm.com>
Cc: David Howells <dhowells@...hat.com>
Cc: Dave Jones <davej@...hat.com>
Cc: Andrey Vagin <avagin@...nvz.org>
Cc: Michael Kerrisk <mtk.manpages@...il.com>
Cc: Pavel Emelyanov <xemul@...allels.com>
CC: Cyrill Gorcunov <gorcunov@...nvz.org>
Signed-off-by: Andrey Vagin <avagin@...nvz.org>
---
fs/signalfd.c | 44 ++++++++++++++++++++++++++++++++++++++++++-
include/uapi/linux/signalfd.h | 5 +++++
2 files changed, 48 insertions(+), 1 deletion(-)
diff --git a/fs/signalfd.c b/fs/signalfd.c
index 4439a81..86eb130 100644
--- a/fs/signalfd.c
+++ b/fs/signalfd.c
@@ -51,6 +51,43 @@ struct signalfd_ctx {
sigset_t sigmask;
};
+static ssize_t signalfd_peek(struct signalfd_ctx *ctx,
+ siginfo_t *info, loff_t *ppos)
+{
+ struct sigpending *pending;
+ struct sigqueue *q;
+ loff_t seq;
+ int ret = 0;
+
+ spin_lock_irq(¤t->sighand->siglock);
+
+ if (*ppos >= SFD_SHARED_QUEUE_OFFSET) {
+ pending = ¤t->signal->shared_pending;
+ seq = *ppos - SFD_SHARED_QUEUE_OFFSET;
+ } else {
+ pending = ¤t->pending;
+ seq = *ppos - SFD_PER_THREAD_QUEUE_OFFSET;
+ }
+
+ list_for_each_entry(q, &pending->list, list) {
+ if (sigismember(&ctx->sigmask, q->info.si_signo))
+ continue;
+
+ if (seq-- == 0) {
+ copy_siginfo(info, &q->info);
+ ret = info->si_signo;
+ break;
+ }
+ }
+
+ spin_unlock_irq(¤t->sighand->siglock);
+
+ if (ret)
+ (*ppos)++;
+
+ return ret;
+}
+
static int signalfd_release(struct inode *inode, struct file *file)
{
kfree(file->private_data);
@@ -248,7 +285,11 @@ static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
siginfo = (struct signalfd_siginfo __user *) buf;
do {
- ret = signalfd_dequeue(ctx, &info, nonblock);
+ if (*ppos == 0)
+ ret = signalfd_dequeue(ctx, &info, nonblock);
+ else
+ ret = signalfd_peek(ctx, &info, ppos);
+
if (unlikely(ret <= 0))
break;
@@ -338,6 +379,7 @@ SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
}
file->f_flags |= flags & SFD_RAW;
+ file->f_mode |= FMODE_PREAD;
fd_install(ufd, file);
} else {
diff --git a/include/uapi/linux/signalfd.h b/include/uapi/linux/signalfd.h
index bc31849..0953785 100644
--- a/include/uapi/linux/signalfd.h
+++ b/include/uapi/linux/signalfd.h
@@ -17,6 +17,11 @@
#define SFD_NONBLOCK O_NONBLOCK
#define SFD_RAW O_DIRECT
+/* Read signals from a shared (process wide) queue */
+#define SFD_SHARED_QUEUE_OFFSET (1LL << 62)
+/* Read signals from a per-thread queue */
+#define SFD_PER_THREAD_QUEUE_OFFSET 1
+
struct signalfd_siginfo {
__u32 ssi_signo;
__s32 ssi_errno;
--
1.7.11.7
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists