waitfd: file descriptor to wait on child processes
RFC PATCH 1/1 waitfd: file descriptor to wait on child processes
This is essentially my first kernel patch, so be nice :)
Linux already has signalfd, timerfd, and eventfd to expose signals, timers, and events via a file descriptor. This patch is a working prototype for a fourth: waitfd. It pretty much does what the name suggests: reading from it yields a series of status ints (as would be written into the second argument of waitpid) for child processes that have changed state. It takes essentially the same arguments as waitpid (for now) and supports the same set of features.
This is far from ready for merge. Some things that are wrong with it:
* Waitpid's argument scheme probably isn't the best for this. By default it makes it easiest to wait on a single child, which is not often useful in this case. Waiting on all children or children in a particular process group is possible, but not a particular, explicit set of children, which we probably want (and which will complicate the implementation significantly).
* The prototype for peek_waitpid is obviously in the wrong place, but I haven't found a good home for it.
* Waitid's semantics have slightly altered: passing NULL as the pointer to siginfo_t with WNOWAIT will now return successfully instead of throwing EFAULT. I don't know if that means I broke it or fixed it :)
* peek_waitpid may not be required at all now. I can probably trick sys_wait4 or sys_waitid into giving me what I want (or I could always just make do_wait non-static).
Please provide thoughts.
--CJD
arch/x86/include/asm/unistd_32.h | 1 + arch/x86/include/asm/unistd_64.h | 2 + arch/x86/kernel/syscall_table_32.S | 1 + fs/Makefile | 1 + fs/waitfd.c | 113 ++++++++++++++++++++++++++++++++++++ init/Kconfig | 10 +++ kernel/exit.c | 59 +++++++++++++++---- kernel/sys_ni.c | 1 + 8 files changed, 176 insertions(+), 12 deletions(-)
arch/x86/include/asm/unistd_32.h +0 -0
@@ -338,6 +338,7 @@
#define __NR_dup3 330
#define __NR_pipe2 331
#define __NR_inotify_init1 332
+#define __NR_waitfd 333
#ifdef __KERNEL__
arch/x86/include/asm/unistd_64.h +0 -0
@@ -653,6 +653,8 @@ __SYSCALL(__NR_dup3, sys_dup3)
__SYSCALL(__NR_pipe2, sys_pipe2)
#define __NR_inotify_init1 294__SYSCALL(__NR_inotify_init1, sys_inotify_init1)
+#define __NR_waitfd 295
+__SYSCALL(__NR_waitfd, sys_waitfd)
#ifndef __NO_STUBS
arch/x86/kernel/syscall_table_32.S +1 -0
@@ -332,3 +332,4 @@ ENTRY(sys_call_table) .long sys_dup3 /* 330 */ .long sys_pipe2 .long sys_inotify_init1 + .long sys_waitfd
fs/Makefile +0 -0
@@ -25,6 +25,7 @@ obj-$(CONFIG_INOTIFY_USER) += inotify_user.o
obj-$(CONFIG_EPOLL) += eventpoll.o
obj-$(CONFIG_ANON_INODES) += anon_inodes.o
obj-$(CONFIG_SIGNALFD) += signalfd.o
+obj-$(CONFIG_WAITFD) += waitfd.o obj-$(CONFIG_TIMERFD) += timerfd.o obj-$(CONFIG_EVENTFD) += eventfd.o
obj-$(CONFIG_AIO) += aio.o
fs/waitfd.c new file +113 -0
113 lines not on this page · raw message
init/Kconfig +10 -0
@@ -683,6 +683,16 @@ config EPOLL Disabling this option will cause the kernel to be built without support for epoll family of system calls. +config WAITFD + bool "Enable waitfd() system call" if EMBEDDED + select ANON_INODES + default y + help + Enable the waitfd() system call that allows receving child state + changes from a file descriptor. + + If unsure, say Y. +
config SIGNALFD
bool "Enable signalfd() system call" if EMBEDDED select ANON_INODES
kernel/exit.c +14 -12
@@ -1233,18 +1233,20 @@ static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid,int retval = rusagep ?getrusage (p,RUSAGE_BOTH , rusagep) :0 ;put_task_struct (p); -if (!retval) - retval =put_user (SIGCHLD , &infop->si_signo); -if (!retval) - retval =put_user (0 , &infop->si_errno); -if (!retval) - retval =put_user ((short )why, &infop->si_code); -if (!retval) - retval =put_user (pid, &infop->si_pid); -if (!retval) - retval =put_user (uid, &infop->si_uid); -if (!retval) - retval =put_user (status, &infop->si_status); +if (infop) { +if (!retval) + retval =put_user (SIGCHLD , &infop->si_signo); +if (!retval) + retval =put_user (0 , &infop->si_errno); +if (!retval) + retval =put_user ((short )why, &infop->si_code); +if (!retval) + retval =put_user (pid, &infop->si_pid); +if (!retval) + retval =put_user (uid, &infop->si_uid); +if (!retval) + retval =put_user (status, &infop->si_status); + }if (!retval) retval = pid;return retval;
@@ -1794,6 +1796,39 @@ asmlinkage long sys_waitid(int which, pid_t upid,return ret;
}
+long peek_waitpid(pid_t upid, int options)
+{
+ struct pid *pid = NULL; + enum pid_type type; + long ret;
+
+ if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
+ __WNOTHREAD|__WCLONE|__WALL))
+ return -EINVAL;
+
+ options |= WNOHANG | WNOWAIT;
+
+ if (upid == -1)
+ type = PIDTYPE_MAX;
+ else if (upid < 0) {
+ type = PIDTYPE_PGID;
+ pid = find_get_pid(-upid);
+ } else if (upid == 0) {
+ type = PIDTYPE_PGID;
+ pid = get_pid(task_pgrp(current));
+ } else /* upid > 0 */ {
+ type = PIDTYPE_PID;
+ pid = find_get_pid(upid);
+ }
+
+ ret = do_wait(type, pid, options | WEXITED, NULL, NULL, NULL);
+ put_pid(pid);
+
+ /* avoid REGPARM breakage on x86: */ + asmlinkage_protect(4, ret, upid, options); + return ret;
+}
+
asmlinkage long sys_wait4(pid_t upid, int __user *stat_addr,
int options, struct rusage __user *ru)
{
kernel/sys_ni.c +0 -0
@@ -163,6 +163,7 @@ cond_syscall(sys_ioprio_set);
cond_syscall(sys_ioprio_get);
/* New file descriptors */
+cond_syscall(sys_waitfd);cond_syscall(sys_signalfd);cond_syscall(sys_signalfd4);cond_syscall(compat_sys_signalfd);
Quoted 5 lines
prototype for a fourth: waitfd. It pretty much does what the name
suggests: reading from it yields a series of status ints (as would be written into the second argument of waitpid) for child processes that have changed state. It takes essentially the same arguments as waitpid (for now) and supports the same set of features.
This propogates the fundamental braindamage of waitpid - the fact the notification only works on child process trees.
Here is a more elegant suggestion - use epoll, inotify and friends fully on /proc process nodes.
Alan
Quoted 3 lines · Alan Cox
Here is a more elegant suggestion - use epoll, inotify and friends fully on /proc process nodes.
Could you give an example?
What is supported?
Scott
Signature · 2 lines
Have you ever, ever felt like this? Had strange things happen? Are you going round the twist?
Quoted 9 lines · Alan Cox
Quoted 2 lines
prototype for a fourth: waitfd. It pretty much does what the name suggests: reading from it yields a series of status ints (as would be written into the second argument of waitpid) for child processes that have changed state. It takes essentially the same arguments as waitpid (for now) and supports the same set of features.
This propogates the fundamental braindamage of waitpid - the fact the notification only works on child process trees.
Here is a more elegant suggestion - use epoll, inotify and friends fully on /proc process nodes.
Quoted 9 lines
Alan
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Last I checked inotify was not supported in /proc, or at least most of it. What kind of work load is it to change that?
--CJD
Quoted 8 lines
Quoted 5 lines
This propogates the fundamental braindamage of waitpid - the fact the notification only works on child process trees.
Here is a more elegant suggestion - use epoll, inotify and friends fully on /proc process nodes.
Last I checked inotify was not supported in /proc, or at least most of it. What kind of work load is it to change that?
I don't know but I think it would be the better approach to find it. That also separates notification of state to parents from the general problem of wanting to know when a service has died, which seems to be an ever more common point of interest on the desktop in particular.
File content change notification for /proc is hard because the contents don't exist in the normal way and get updated but can be done if there is a wait queue for the job. Actual changes to structure (new directories etc) is in part a similar problem but there are clear points already in existence when the proc nodes are created/destroyed and thus notification can occur.
Quoted 8 lines · Alan Cox
Quoted 6 lines
Quoted 6 lines
This propogates the fundamental braindamage of waitpid - the fact the notification only works on child process trees.
Here is a more elegant suggestion - use epoll, inotify and friends fully on /proc process nodes.
Quoted 9 lines
Quoted 2 lines
Last I checked inotify was not supported in /proc, or at least most of it. What kind of work load is it to change that?
I don't know but I think it would be the better approach to find it. That also separates notification of state to parents from the general problem of wanting to know when a service has died, which seems to be an ever more common point of interest on the desktop in particular.
Of course, using inotify on proc will not (and should not) actually reap dead processes, meaning waitpid() isn't obviated by the change (though now it is always called on a specific pid and is never expected to block or EAGAIN). We also introduce a new gotcha for userspace programs: this mechanism works identically for child and non-child processes, so a process may or may not be waitable when returned. A simple "do not shoot self in foot" should suffice for this though.
Also, it doesn't work if /proc hasn't been mounted, which just so happens to matter for my particular use cases :)
Quoted 8 lines
File content change notification for /proc is hard because the contents don't exist in the normal way and get updated but can be done if there is a wait queue for the job. Actual changes to structure (new directories etc) is in part a similar problem but there are clear points already in existence when the proc nodes are created/destroyed and thus notification can occur.
Changes to structure are more interesting in terms of this particular problem anyway, and definitely simpler to capture.
--CJD
Quoted 24 lines · Casey Dahlin
This is essentially my first kernel patch, so be nice :)
Linux already has signalfd, timerfd, and eventfd to expose signals, timers, and events via a file descriptor. This patch is a working prototype for a
fourth: waitfd. It pretty much does what the name suggests: reading from it yields a series of status ints (as would be written into the second argument of waitpid) for child processes that have changed state. It takes essentially the same arguments as waitpid (for now) and supports the same set of features.This is far from ready for merge. Some things that are wrong with it:
* Waitpid's argument scheme probably isn't the best for this. By default it makes it easiest to wait on a single child, which is not often useful in this case. Waiting on all children or children in a particular process group is possible, but not a particular, explicit set of children, which we probably want (and which will complicate the implementation significantly).
* The prototype forpeek_waitpidis obviously in the wrong place, but I haven't found a good home for it.
* Waitid's semantics have slightly altered: passing NULL as the pointer tosiginfo_twith WNOWAIT will now return successfully instead of throwing EFAULT. I don't know if that means I broke it or fixed it :)
*peek_waitpidmay not be required at all now. I can probably tricksys_wait4orsys_waitidinto giving me what I want (or I could always just makedo_waitnon-static).
Quoted 1 line
Please provide thoughts.
What's wrong in having a signalfd on SIGCHLD, than doing waitpid() once you get the signal? Do you have cases where this wouldn't be OK? About the code, eventually, you really want to report the PID of the exited child, together with the status. So maybe a non-compat-requiring struct would be better to be returned by read(2).
Also ...
fs/waitfd.c line 45
Quoted 13 lines
fs/waitfd.c new file +13
-0+static unsigned int waitfd_poll (struct file *file,poll_table *wait) +{ +struct waitfd_ctx *ctx = file->private_data; +long value; + +poll_wait (file, ¤t->signal->wait_chldexit, wait); + + value =peek_waitpid (ctx->upid, ctx->ops); +if (value >0 ) { +return POLLIN ; + }if (value == -ECHILD ) { +return POLLIN ; + }
Trust the compiler, it's pretty good in not having you to add Perl-like extra brackets ;)
This also looks wierd:
} if (value == -ECHILD) {So maybe:
return value > 0 || value == -ECHILD ? POLLIN: 0;
Quoted 5 lines
+/*
+ * Returns a multiple of the size of a "structwaitfd_siginfo", or a negative
+ * error code. The "count" parameter must be at least the size of a
+ * "structwaitfd_siginfo".
+ */
Really? ...
Quoted 19 lines
fs/waitfd.c new file +19
-0+static ssize_t waitfd_read (struct file *file,char __user *buf,size_t count, +loff_t *ppos) +{ +struct waitfd_ctx *ctx = file->private_data; +int __user *stat_addr = (int *)buf; +int nonblock = file->f_flags &O_NONBLOCK ?WNOHANG :0 ; +ssize_t ret, total =0 ; + + count /=sizeof (int ); +if (!count) +return -EINVAL ; + + do { + ret = sys_wait4(ctx->upid, stat_addr, ctx->ops | nonblock, + NULL); + if (ret == 0) + ret = -EAGAIN; + if (ret == -ECHILD) + ret = 0;
fs/waitfd.c line 83
Quoted 10 lines
fs/waitfd.c new file +10
-0+if (ret <=0 ) +break ; + + stat_addr++; + total +=sizeof (struct siginfo ); + nonblock =WNOHANG ; + }while (--count); + +return total ? total: ret; +}
... looks like you're returning a sequence of status ints, with wrong data size returned by read(2).
fs/waitfd.c line 91
Quoted 8 lines
fs/waitfd.c new file +8
-0+ +static const struct file_operations waitfd_fops = { + .release = waitfd_release, + .poll = waitfd_poll, + .read = waitfd_read, +}; + +asmlinkage long sys_waitfd (pid_t upid,int ops)
Please leave space for extra flags for fds, otherwise Uli will have to make another sys_waitfd3().
Quoted 22 lines
(quoted diff) +22
-0+long peek_waitpid(pid_t upid, int options) +{ + struct pid *pid = NULL; + enum pid_type type; + long ret; + + if (options & ~(WNOHANG|WUNTRACED|WCONTINUED| + __WNOTHREAD|__WCLONE|__WALL)) + return -EINVAL; + + options |= WNOHANG | WNOWAIT; + + if (upid == -1) + type = PIDTYPE_MAX; + else if (upid < 0) { + type = PIDTYPE_PGID; + pid = find_get_pid(-upid); + } else if (upid == 0) { + type = PIDTYPE_PGID; + pid = get_pid(task_pgrp(current)); + } else /* upid > 0 */ { + type = PIDTYPE_PID;
Review
Quoted 10 lines
(quoted diff) +10
-0+ pid = find_get_pid(upid); + } + + ret = do_wait(type, pid, options | WEXITED, NULL, NULL, NULL); + put_pid(pid); + + /* avoid REGPARM breakage on x86: */ + asmlinkage_protect(4, ret, upid, options); + return ret; +}
Given that you blinded copied this from sys_wait4(), you may want to at least try to make sys_wait4() to re-use the new function. Also, your patch does not apply to latest Linus tree. Which one was the base?
- Davide
Quoted 27 lines · Davide Libenzi
Quoted 24 lines · Casey Dahlin
This is essentially my first kernel patch, so be nice :)
Linux already has signalfd, timerfd, and eventfd to expose signals, timers, and events via a file descriptor. This patch is a working prototype for a
fourth: waitfd. It pretty much does what the name suggests: reading from it yields a series of status ints (as would be written into the second argument of waitpid) for child processes that have changed state. It takes essentially the same arguments as waitpid (for now) and supports the same set of features.This is far from ready for merge. Some things that are wrong with it:
* Waitpid's argument scheme probably isn't the best for this. By default it makes it easiest to wait on a single child, which is not often useful in this case. Waiting on all children or children in a particular process group is possible, but not a particular, explicit set of children, which we probably want (and which will complicate the implementation significantly).
* The prototype forpeek_waitpidis obviously in the wrong place, but I haven't found a good home for it.
* Waitid's semantics have slightly altered: passing NULL as the pointer tosiginfo_twith WNOWAIT will now return successfully instead of throwing EFAULT. I don't know if that means I broke it or fixed it :)
*peek_waitpidmay not be required at all now. I can probably tricksys_wait4orsys_waitidinto giving me what I want (or I could always just makedo_waitnon-static).
Quoted 5 lines
Quoted 2 lines
Please provide thoughts.
What's wrong in having a signalfd on SIGCHLD, than doing
waitpid()once you get the signal? Do you have cases where this wouldn't be OK?
When the child doesn't send SIGCHLD (clone() lets you do evil things :)
Seriously though, that may be an option. Scott (CC'd ) is primarily the consumer of this, so he can better comment.
Quoted 25 lines
About the code, eventually, you really want to report the PID of the exited child, together with the status. So maybe a non-compat-requiring struct would be better to be returned by read(2). Also ...
Quoted 4 lines
fs/waitfd.c new file +3
-0+static unsigned int waitfd_poll (struct file *file,poll_table *wait) +{ +struct waitfd_ctx *ctx = file->private_data;fs/waitfd.c line 45
Quoted 10 lines
fs/waitfd.c new file +10
-0+long value; + +poll_wait (file, ¤t->signal->wait_chldexit, wait); + + value =peek_waitpid (ctx->upid, ctx->ops); +if (value >0 ) { +return POLLIN ; + }if (value == -ECHILD ) { +return POLLIN ; + }Trust the compiler, it's pretty good in not having you to add Perl-like extra brackets ;) This also looks wierd:
Quoted 8 lines
} if (value == -ECHILD) {So maybe:
return value > 0 || value == -ECHILD ? POLLIN: 0;
Yeah, I ripped this up because during debugging poll was behaving oddly, and I had been instrumenting the code with printks. It can go back to being 1 line now.
Quoted 14 lines
Quoted 9 lines
+/*
+ * Returns a multiple of the size of a "structwaitfd_siginfo", or a negative
+ * error code. The "count" parameter must be at least the size of a
+ * "structwaitfd_siginfo".
+ */Really? ...
Wow... and I read this patch before I sent it, too *facepalm*
The function did briefly return a siginfo_t (I implemented on top of waitid first).
Quoted 25 lines
Quoted 25 lines
fs/waitfd.c new file +24
-0+static ssize_t waitfd_read (struct file *file,char __user *buf,size_t count, +loff_t *ppos) +{ +struct waitfd_ctx *ctx = file->private_data; +int __user *stat_addr = (int *)buf; +int nonblock = file->f_flags &O_NONBLOCK ?WNOHANG :0 ; +ssize_t ret, total =0 ; + + count /=sizeof (int ); +if (!count) +return -EINVAL ; + + do { + ret = sys_wait4(ctx->upid, stat_addr, ctx->ops | nonblock, + NULL); + if (ret == 0) + ret = -EAGAIN; + if (ret == -ECHILD) + ret = 0; + if (ret <= 0) + break; + + stat_addr++; + total += sizeof(struct siginfo);
Quoted 11 lines
fs/waitfd.c line 83
Quoted 6 lines
fs/waitfd.c new file +5
-0+ nonblock =WNOHANG ; + }while (--count); + +return total ? total: ret; +}... looks like you're returning a sequence of status ints, with wrong data size returned by read(2).
More leftovers from above.
Quoted 16 lines
fs/waitfd.c line 91
Quoted 11 lines
fs/waitfd.c new file +8
-0+ +static const struct file_operations waitfd_fops = { + .release = waitfd_release, + .poll = waitfd_poll, + .read = waitfd_read, +}; + +asmlinkage long sys_waitfd (pid_t upid,int ops)Please leave space for extra flags for fds, otherwise Uli will have to make another
sys_waitfd3().
ack'd.
Quoted 28 lines
Quoted 28 lines
(quoted diff) +27
-0+long peek_waitpid(pid_t upid, int options) +{ + struct pid *pid = NULL; + enum pid_type type; + long ret; + + if (options & ~(WNOHANG|WUNTRACED|WCONTINUED| + __WNOTHREAD|__WCLONE|__WALL)) + return -EINVAL; + + options |= WNOHANG | WNOWAIT; + + if (upid == -1) + type = PIDTYPE_MAX; + else if (upid < 0) { + type = PIDTYPE_PGID; + pid = find_get_pid(-upid); + } else if (upid == 0) { + type = PIDTYPE_PGID; + pid = get_pid(task_pgrp(current)); + } else /* upid > 0 */ { + type = PIDTYPE_PID; + pid = find_get_pid(upid); + } + + ret = do_wait(type, pid, options | WEXITED, NULL, NULL, NULL); + put_pid(pid);
Quoted 9 lines
Review
Quoted 6 lines
(quoted diff) +5
-0+ + /* avoid REGPARM breakage on x86: */ + asmlinkage_protect(4, ret, upid, options); + return ret; +}Given that you blinded copied this from
sys_wait4(), you may want to at least try to makesys_wait4()to re-use the new function.
Good idea.
Quoted 3 lines
Also, your patch does not apply to latest Linus tree. Which one was the base?
This is the last commit before mine in my git repo:
commit f7a8db89c1f42e504bb12d2ae399cd96f755a7db
Merge: 6f84b4d... c49b9f2...
Author: Linus Torvalds <[email protected]>
Date: Mon Dec 8 19:52:43 2008 -0800
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
* git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6:
tproxy: fixe a possible read from an invalid location in the
socket match
zd1211rw: use unaligned safe memcmp() in-place of compare_ether_addr()
(...lots and lots of changes snipped...)
--CJD
Quoted 9 lines · Davide Libenzi
Quoted 7 lines · Casey Dahlin
Linux already has signalfd, timerfd, and eventfd to expose signals, timers, and events via a file descriptor. This patch is a working prototype for a
fourth: waitfd. It pretty much does what the name suggests: reading from it yields a series of status ints (as would be written into the second argument of waitpid) for child processes that have changed state. It takes essentially the same arguments as waitpid (for now) and supports the same set of features.
Quoted 3 lines
What's wrong in having a signalfd on SIGCHLD, than doing
waitpid()once you get the signal?
Because SIGCHLD isn't a POSIX realtime signal, only one copy of it will be queued at any one time -- even with signalfd(), and even though they have different (useful) siginfo_t.
So if you have three children die in rapid succession, you only get the siginfo for the first one. Thus you still have to call waitid()/waitpid() in a loop, and wait on everything.
Could the fact that you don't get signalfd notification of the additional signals be considered a bug? Or possibly a useful additional feature?
If we were able to read all the queued SIGCHLD signals with signalfd (preserving the one pending only behaviour of ordinary delivery), then a loop like the following would be possible:
sigemptyset (&mask);
sigaddset (&mask, SIGCHLD);
sfd = signalfd (-1, &mask, 0);
for (;;) {
read (sfd, &fdsi, sizeof (struct signalfd_siginfo));waitpid (fdsi.ssi_pid, 0, 0);
}
So you only need to wait for each one individually.
Scott
Signature · 2 lines
Have you ever, ever felt like this? Had strange things happen? Are you going round the twist?
Quoted 38 lines · Scott James Remnant
Quoted 9 lines · Davide Libenzi
Quoted 7 lines · Casey Dahlin
Linux already has signalfd, timerfd, and eventfd to expose signals, timers, and events via a file descriptor. This patch is a working prototype for a
fourth: waitfd. It pretty much does what the name suggests: reading from it yields a series of status ints (as would be written into the second argument of waitpid) for child processes that have changed state. It takes essentially the same arguments as waitpid (for now) and supports the same set of features.Quoted 3 lines
What's wrong in having a signalfd on SIGCHLD, than doing
waitpid()once you get the signal?Because SIGCHLD isn't a POSIX realtime signal, only one copy of it will be queued at any one time -- even with
signalfd(), and even though they have different (useful)siginfo_t.So if you have three children die in rapid succession, you only get the siginfo for the first one. Thus you still have to call waitid()/waitpid() in a loop, and wait on everything.
Could the fact that you don't get signalfd notification of the additional signals be considered a bug? Or possibly a useful additional feature?
If we were able to read all the queued SIGCHLD signals with signalfd (preserving the one pending only behaviour of ordinary delivery), then a loop like the following would be possible:
sigemptyset (&mask); sigaddset (&mask, SIGCHLD); sfd = signalfd (-1, &mask, 0); for (;;) { read (sfd, &fdsi, sizeof (struct signalfd_siginfo));
Quoted 2 lines
waitpid (fdsi.ssi_pid, 0, 0);}
And how about this?
sfd = signalfd(SIGCHLD);
for (;;) {
poll(sfd, POLLIN);
while ((pid = waitpid(0, &status, WNOHANG)) != -1)
process_child_death(pid);
}- Davide
Quoted 10 lines · Davide Libenzi
And how about this?
sfd = signalfd(SIGCHLD); for (;;) { poll(sfd, POLLIN); while ((pid = waitpid(0, &status, WNOHANG)) != -1) process_child_death(pid); }
At this point, why have signalfd()'s read() return siginfo_t at all?
You have to discard the entire information since it's only ever the first signal that matched, all subsequent ones are thrown away.
Scott
Signature · 2 lines
Have you ever, ever felt like this? Had strange things happen? Are you going round the twist?
Quoted 12 lines · Scott James Remnant
Quoted 10 lines · Davide Libenzi
And how about this?
sfd = signalfd(SIGCHLD); for (;;) { poll(sfd, POLLIN); while ((pid = waitpid(0, &status, WNOHANG)) != -1) process_child_death(pid); }
Quoted 4 lines
At this point, why have signalfd()'s
read()returnsiginfo_tat all?You have to discard the entire information since it's only ever the first signal that matched, all subsequent ones are thrown away.
Signalfd is a wakeup gate available to the poll/select/epoll subsystems. Signals that are queued inside the kernel, get de-queueing behavior, while signals that are overlapping, get overlapping behavior.
The code above enable you to wait for signals inside a poll/select/epoll dispatch loop, and get all the info waitpid() can return to you - w/out missing anything.
In a real app case, you're probably waiting for more than simple SIGCHLD, so you likely will have one (or more) read(2) of the signalfd to find out which signal you got, and do the waitpid(WNOHANG) loop in case of SIGCHLD.
- Davide