Skip to content

Commit 9deb27b

Browse files
Ulrich Dreppertorvalds
authored andcommitted
flag parameters: signalfd
This patch adds the new signalfd4 syscall. It extends the old signalfd syscall by one parameter which is meant to hold a flag value. In this patch the only flag support is SFD_CLOEXEC which causes the close-on-exec flag for the returned file descriptor to be set. A new name SFD_CLOEXEC is introduced which in this implementation must have the same value as O_CLOEXEC. The following test must be adjusted for architectures other than x86 and x86-64 and in case the syscall numbers changed. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #include <fcntl.h> #include <signal.h> #include <stdio.h> #include <unistd.h> #include <sys/syscall.h> #ifndef __NR_signalfd4 # ifdef __x86_64__ # define __NR_signalfd4 289 # elif defined __i386__ # define __NR_signalfd4 327 # else # error "need __NR_signalfd4" # endif #endif #define SFD_CLOEXEC O_CLOEXEC int main (void) { sigset_t ss; sigemptyset (&ss); sigaddset (&ss, SIGUSR1); int fd = syscall (__NR_signalfd4, -1, &ss, 8, 0); if (fd == -1) { puts ("signalfd4(0) failed"); return 1; } int coe = fcntl (fd, F_GETFD); if (coe == -1) { puts ("fcntl failed"); return 1; } if (coe & FD_CLOEXEC) { puts ("signalfd4(0) set close-on-exec flag"); return 1; } close (fd); fd = syscall (__NR_signalfd4, -1, &ss, 8, SFD_CLOEXEC); if (fd == -1) { puts ("signalfd4(SFD_CLOEXEC) failed"); return 1; } coe = fcntl (fd, F_GETFD); if (coe == -1) { puts ("fcntl failed"); return 1; } if ((coe & FD_CLOEXEC) == 0) { puts ("signalfd4(SFD_CLOEXEC) does not set close-on-exec flag"); return 1; } close (fd); puts ("OK"); return 0; } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [akpm@linux-foundation.org: add sys_ni stub] Signed-off-by: Ulrich Drepper <drepper@redhat.com> Acked-by: Davide Libenzi <davidel@xmailserver.org> Cc: Michael Kerrisk <mtk.manpages@googlemail.com> Cc: <linux-arch@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 7d9dbca commit 9deb27b

File tree

9 files changed

+34
-6
lines changed

9 files changed

+34
-6
lines changed

arch/x86/ia32/ia32entry.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,4 +826,5 @@ ia32_sys_call_table:
826826
.quad sys32_fallocate
827827
.quad compat_sys_timerfd_settime /* 325 */
828828
.quad compat_sys_timerfd_gettime
829+
.quad compat_sys_signalfd4
829830
ia32_syscall_end:

arch/x86/kernel/syscall_table_32.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,4 @@ ENTRY(sys_call_table)
326326
.long sys_fallocate
327327
.long sys_timerfd_settime /* 325 */
328328
.long sys_timerfd_gettime
329+
.long sys_signalfd4

fs/compat.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,9 +2131,9 @@ asmlinkage long compat_sys_epoll_pwait(int epfd,
21312131

21322132
#ifdef CONFIG_SIGNALFD
21332133

2134-
asmlinkage long compat_sys_signalfd(int ufd,
2135-
const compat_sigset_t __user *sigmask,
2136-
compat_size_t sigsetsize)
2134+
asmlinkage long compat_sys_signalfd4(int ufd,
2135+
const compat_sigset_t __user *sigmask,
2136+
compat_size_t sigsetsize, int flags)
21372137
{
21382138
compat_sigset_t ss32;
21392139
sigset_t tmp;
@@ -2148,9 +2148,15 @@ asmlinkage long compat_sys_signalfd(int ufd,
21482148
if (copy_to_user(ksigmask, &tmp, sizeof(sigset_t)))
21492149
return -EFAULT;
21502150

2151-
return sys_signalfd(ufd, ksigmask, sizeof(sigset_t));
2151+
return sys_signalfd4(ufd, ksigmask, sizeof(sigset_t), flags);
21522152
}
21532153

2154+
asmlinkage long compat_sys_signalfd(int ufd,
2155+
const compat_sigset_t __user *sigmask,
2156+
compat_size_t sigsetsize)
2157+
{
2158+
return compat_sys_signalfd4(ufd, sigmask, sigsetsize, 0);
2159+
}
21542160
#endif /* CONFIG_SIGNALFD */
21552161

21562162
#ifdef CONFIG_TIMERFD

fs/signalfd.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,15 @@ static const struct file_operations signalfd_fops = {
205205
.read = signalfd_read,
206206
};
207207

208-
asmlinkage long sys_signalfd(int ufd, sigset_t __user *user_mask, size_t sizemask)
208+
asmlinkage long sys_signalfd4(int ufd, sigset_t __user *user_mask,
209+
size_t sizemask, int flags)
209210
{
210211
sigset_t sigmask;
211212
struct signalfd_ctx *ctx;
212213

214+
if (flags & ~SFD_CLOEXEC)
215+
return -EINVAL;
216+
213217
if (sizemask != sizeof(sigset_t) ||
214218
copy_from_user(&sigmask, user_mask, sizeof(sigmask)))
215219
return -EINVAL;
@@ -228,7 +232,7 @@ asmlinkage long sys_signalfd(int ufd, sigset_t __user *user_mask, size_t sizemas
228232
* anon_inode_getfd() will install the fd.
229233
*/
230234
ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
231-
0);
235+
flags & O_CLOEXEC);
232236
if (ufd < 0)
233237
kfree(ctx);
234238
} else {
@@ -250,3 +254,9 @@ asmlinkage long sys_signalfd(int ufd, sigset_t __user *user_mask, size_t sizemas
250254

251255
return ufd;
252256
}
257+
258+
asmlinkage long sys_signalfd(int ufd, sigset_t __user *user_mask,
259+
size_t sizemask)
260+
{
261+
return sys_signalfd4(ufd, user_mask, sizemask, 0);
262+
}

include/asm-x86/unistd_32.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@
332332
#define __NR_fallocate 324
333333
#define __NR_timerfd_settime 325
334334
#define __NR_timerfd_gettime 326
335+
#define __NR_signalfd4 327
335336

336337
#ifdef __KERNEL__
337338

include/asm-x86/unistd_64.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,8 @@ __SYSCALL(__NR_timerfd_settime, sys_timerfd_settime)
641641
__SYSCALL(__NR_timerfd_gettime, sys_timerfd_gettime)
642642
#define __NR_paccept 288
643643
__SYSCALL(__NR_paccept, sys_paccept)
644+
#define __NR_signalfd4 289
645+
__SYSCALL(__NR_signalfd4, sys_signalfd4)
644646

645647

646648
#ifndef __NO_STUBS

include/linux/signalfd.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
#ifndef _LINUX_SIGNALFD_H
99
#define _LINUX_SIGNALFD_H
1010

11+
/* For O_CLOEXEC */
12+
#include <linux/fcntl.h>
13+
14+
/* Flags for signalfd4. */
15+
#define SFD_CLOEXEC O_CLOEXEC
1116

1217
struct signalfd_siginfo {
1318
__u32 ssi_signo;

include/linux/syscalls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ asmlinkage long sys_set_robust_list(struct robust_list_head __user *head,
610610
size_t len);
611611
asmlinkage long sys_getcpu(unsigned __user *cpu, unsigned __user *node, struct getcpu_cache __user *cache);
612612
asmlinkage long sys_signalfd(int ufd, sigset_t __user *user_mask, size_t sizemask);
613+
asmlinkage long sys_signalfd4(int ufd, sigset_t __user *user_mask, size_t sizemask, int flags);
613614
asmlinkage long sys_timerfd_create(int clockid, int flags);
614615
asmlinkage long sys_timerfd_settime(int ufd, int flags,
615616
const struct itimerspec __user *utmr,

kernel/sys_ni.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ cond_syscall(sys_ioprio_get);
156156

157157
/* New file descriptors */
158158
cond_syscall(sys_signalfd);
159+
cond_syscall(sys_signalfd4);
159160
cond_syscall(compat_sys_signalfd);
160161
cond_syscall(sys_timerfd_create);
161162
cond_syscall(sys_timerfd_settime);

0 commit comments

Comments
 (0)