[Patch] kqueue(2) <-> procdesc(4): EVFILT_PROCDESC

Konstantin Belousov kostikbel at gmail.com
Sat Apr 5 15:36:34 UTC 2014


On Sat, Apr 05, 2014 at 12:00:43PM +0200, Ed Schouten wrote:
> Hi Robert,
> 
> The other day I was playing around with procdesc(4). Quite a nice
> piece of work. I did notice, though, that there is no way you can use
> kqueue(2) to monitor process descriptors. That's quite a shame,
> because it would be awesome if we could just use EVFILT_PROC on
> process descriptors directly. This is why I thought it would be nice
> to introduce a variant called EVFILT_PROCDESC.
> 
> I initially tried altering kqueue(2) in such a way that
> EVFILT_PROCDESC would use a mixture of filt_fileattach() to attach to
> the descriptor and filt_proc() to watch for events, but this approach
> didn't really work out all that well, for the reason that a kevent
> either has a file descriptor or a process associated; not both.
> 
> In the end I decided to not make things more complex than needed and
> just implement it like a regular file descriptor probe. This means
> that we can get NOTE_EXIT to work, but NOTE_FORK, NOTE_EXEC and
> NOTE_TRACK would require some more work.
> 
> What are your thoughts on the following patch?
> 
> http://80386.nl/pub/kqueue-evfilt-procdesc.txt
> 
> Some notes on this patch:
> 
> - I decided to just reuse the obsolete EVFILT_NETDEV. EVFILT_PROCDESC
> will be used on completely different file descriptor types, so I can't
> think of a way this would cause ABI issues.
> 
> - pd->pd_proc is protected by proctree_lock. It's a bit hard to pick
> this up inside of the kqfilter, so simply make procdesc_exit() copy
> out pd->pd_proc->p_xstat. We also want this copy, because we don't
> want to run into a race condition where wait4() already reaps the
> process before the kqfilter is called. This is works for EVFILT_PROC
> anyway.
> 
> The nice thing about this patch is that even though pdwait4(2) is
> still unimplemented, it does at least allow people to now extract the
> exit code without accessing any global namespaces.

procdesc_kqops_event() should mimic the filt_proc() much more closely
than you do.  In particular:
- I do not see why do you check for NOTE_EXIT in kqops_event().
  If enforcing NOTE_EXIT anywhere, procdesc_kqfilter() is more natural place
  to put the temporal restriction.  I do think that signalling and execing
  are equally useful as exiting notifications, but this is for future.
- The whole structure of filt_proc(), which copies masked sfflags to
  fflags is easier to extend later.  It seems that just repeating
  most of the code from filt_proc() would do what needed. 

I think you must do knlist_clear() before knlist_destroy().
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 834 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-arch/attachments/20140405/13f3abc8/attachment.sig>


More information about the freebsd-arch mailing list