>From 6bf48dd6a77261af0daaa9be19cd13a9a11a008d Mon Sep 17 00:00:00 2001 From: Andrey Vagin Date: Wed, 26 Dec 2012 13:45:54 +0400 Subject: [PATCH 2/2] signalfd: add ability to get signal without removing from the queue lseek sets a sequence number of signal in a queue, then read() returns siginfo if a signal is exists, otherwise it returns 0. All signals remain in a queue. If lseek sets a positive position, signals are taken from a shared queue. If lseek sets a negative position, signals are taken from a private queue. If ppos is zero (default), signalfd dequeues signals. Signed-off-by: Andrey Vagin --- fs/signalfd.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/fs/signalfd.c b/fs/signalfd.c index ee60d5f..0ce6fb3 100644 --- a/fs/signalfd.c +++ b/fs/signalfd.c @@ -48,8 +48,21 @@ void signalfd_cleanup(struct sighand_struct *sighand) struct signalfd_ctx { sigset_t sigmask; + loff_t peek_offset; }; +static ssize_t signalfd_peek(struct signalfd_ctx *ctx, siginfo_t *info) +{ + int ret; + + spin_lock_irq(¤t->sighand->siglock); + ret = peek_signal(current, &ctx->sigmask, info, + abs(ctx->peek_offset) - 1, ctx->peek_offset > 0); + spin_unlock_irq(¤t->sighand->siglock); + + return ret; +} + static int signalfd_release(struct inode *inode, struct file *file) { kfree(file->private_data); @@ -230,7 +243,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 (ctx->peek_offset == 0) + ret = signalfd_dequeue(ctx, &info, nonblock); + else + ret = signalfd_peek(ctx, &info); + if (unlikely(ret <= 0)) break; @@ -242,6 +259,13 @@ static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count, if (ret < 0) break; + if (ctx->peek_offset) { + if (ctx->peek_offset > 0) + ctx->peek_offset++; + else + ctx->peek_offset--; + } + siginfo++; total += ret; nonblock = 1; @@ -264,6 +288,24 @@ static int signalfd_show_fdinfo(struct seq_file *m, struct file *f) } #endif +loff_t signalfd_llseek(struct file *f, loff_t offset, int whence) +{ + struct signalfd_ctx *ctx = f->private_data; + + switch (whence) { + case SEEK_SET: + ctx->peek_offset = offset; + break; + case SEEK_CUR: + ctx->peek_offset += offset; + break; + default: + return -EINVAL; + } + + return ctx->peek_offset; +} + static const struct file_operations signalfd_fops = { #ifdef CONFIG_PROC_FS .show_fdinfo = signalfd_show_fdinfo, @@ -271,7 +313,7 @@ static const struct file_operations signalfd_fops = { .release = signalfd_release, .poll = signalfd_poll, .read = signalfd_read, - .llseek = noop_llseek, + .llseek = signalfd_llseek, }; SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask, @@ -300,6 +342,7 @@ SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask, return -ENOMEM; ctx->sigmask = sigmask; + ctx->peek_offset = 0; ufd = get_unused_fd_flags(flags); if (ufd < 0) { @@ -321,6 +364,7 @@ SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask, } file->f_flags |= flags & SFD_RAW; + file->f_mode |= FMODE_LSEEK; fd_install(ufd, file); } else { -- 1.7.11.7