linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: A signal fairy tale
@ 2001-06-28  2:49 Daniel R. Kegel
  2001-06-29  8:18 ` Christopher Smith
  0 siblings, 1 reply; 28+ messages in thread
From: Daniel R. Kegel @ 2001-06-28  2:49 UTC (permalink / raw)
  To: balbir.singh; +Cc: Linux kernel mailing list

Balbir Singh <balbir.singh@wipro.com> wrote:

>Shouldn't there be a sigclose() and other operations to make the API
>orthogonal.

No, plain old close() on the file descriptor returned by sigopen()
would do the trick.

>sigopen() should be selective about the signals it allows
>as argument. Try and make sigopen() thread specific, so that if one
>thread does a sigopen(), it does not imply it will do all the signal
>handling for all the threads.

IMHO sigopen()/read() should behave just like sigwait() with respect 
to threads.  That means that in Posix, it would not be thread specific,
but in Linux, it would be thread specific, because that's how signals
and threads work there at the moment.

>Does using sigopen() imply that signal(), sigaction(), etc cannot be used.
>In the same process one could do a sigopen() in the library, but the
>process could use sigaction()/signal() without knowing what the library
>does (which signals it handles, etc).

Between sigopen() and close(), calling signal() or sigaction() on that 
signal would probably return EBUSY.   A well-behaved program already
looks for an unoccupied signal using sigaction (as Jamie Lokier
points out), so they shouldn't try to reuse a signal in use by sigopen().

- Dan


^ permalink raw reply	[flat|nested] 28+ messages in thread
[parent not found: <fa.d69j5vv.ej8irj@ifi.uio.no>]
* Re: A signal fairy tale
@ 2001-06-28 20:11 Daniel R. Kegel
  2001-06-29  8:31 ` Christopher Smith
  0 siblings, 1 reply; 28+ messages in thread
From: Daniel R. Kegel @ 2001-06-28 20:11 UTC (permalink / raw)
  To: lk; +Cc: linux-kernel, x

Jamie wrote:
> Daniel R. Kegel wrote:
> > Christopher Smith <x@xman.org> wrote:
> > > Jamie Lokier <lk@tantalophile.demon.co.uk> wrote:
> > > > Btw, this functionality is already available using sigaction().  Just
> > > > search for a signal whose handler is SIG_DFL.  If you then block that
> > > > signal before changing, checking the result, and unblocking the signal,
> > > > you can avoid race conditions too.  (This is what my programs do).
> > > 
> > > It's more than whether a signal is blocked or not, unfortunately. Lots of 
> > > applications will invoke sigwaitinfo() on whatever the current signal mask 
> > > is, which means you can't rely on sigaction to solve your problems. :-(
> > 
> > As Chris points out, allocating a signal by the scheme Jamie
> > describes is neccessary but not sufficient.  The problem Chris
> > ran into is that he allocated a signal fair and square, only to find
> > the application picking it up via sigwaitinfo()!
> 
> I check that the handler is not SIG_DFL, but perhaps my assumption that
> any sigwaitinfo() user of a signal would set SA_SIGINFO and set the
> handler to non-SIG_DFL is mistaken?
 
I think your assumption is correct.  The problem is that the
application in question (Sun's JDK 1.4 beta) does something like this:
	sigprocmask(0, NULL, &oldset);
	sigwaitinfo(&oldset, &info);
So even though Chris did set the handler for his signal to non-SIG_DFL,
the application didn't care, and sucked all his signal notifications
away from him.

> > Yes, this is a bug in the application -- but it's interesting that this
> > bug only shows up when you try to integrate a new, well-behaved, library 
> > into the app.  It's a fragile part of the Unix API.  sigopen() is
> > a way for libraries to defend themselves against misuse of sigwaitinfo()
> > by big applications over which you have no control.
> > 
> > So sigopen() is a technological fix to a social problem, I guess.
> 
> Requiring all libraries to use the sigopen() as you specified it just
> isn't going to work, because you would have to make big changes to the
> libraries.

I didn't mean to require any library to change at all.  This is
an optional thing; a library can use this technique if it wants to
insulate itself from badly behaved applications.

> Sometimes you actually do need SIGRTxxx signals to be delivered using
> signal handlers!

No objection there, I agree.
 
> Also as it was specified, you are reduced to reading one type of signal
> at a time, or using select().  Often you wish to check several signals.
> For example, in my programs sigwaitinfo() calls check for SIGIO, SIGURG
> and SIGRTxxx at least.  Therefore siginfo(), if implemented, should take
> a sigset_t, not a signal number.
 
I have no objection to sigopen() taking a sigset_t *.

> The problem of when you actually want to receive an allocated signal
> through a handler is, IMHO, best solved by permitting sigaction() and
> signal delivery on signals that have been opened with sigopen().

sigopen() essentially installs a special signal handler (say, SIG_OPEN).
If sigaction() can override that, it should probably close the file descriptor, too.

I can buy that, perhaps, even though it makes libraries using sigopen()
somewhat more vulnerable to poorly behaved applications.  I think the
present application doesn't misbehave badly enough that it would try to
install a signal handler over Chris's.
 
> However, it would be ok to require a flag SA_SIGOPEN to sigaction() to
> prevent it from returning EBUSY.

That'd be ok.

Another issue someone raised: 

> would read() on this fd require the kernel to copy every byte of the siginfo_t?  

IMHO no; read() would leave undefined any bytes that would not have been set by 
sigwaitinfo().  The kernel could set them to zero or leave them untouched,
as desired.

Another issue:

AFAIK, there's no 'read with a timeout' system call for file descriptors, so
if you needed the equivalent of sigtimedwait(),
you might end up doing a select on the sigopen fd, which is an extra
system call.  (I wish Posix had invented sigopen() and readtimedwait() instead of 
sigtimedwait...)

- Dan

^ permalink raw reply	[flat|nested] 28+ messages in thread
* Re: A signal fairy tale
@ 2001-06-28  3:04 Daniel R. Kegel
  2001-06-28 14:46 ` Jamie Lokier
  0 siblings, 1 reply; 28+ messages in thread
From: Daniel R. Kegel @ 2001-06-28  3:04 UTC (permalink / raw)
  To: Jamie Lokier, x; +Cc: linux-kernel

Christopher Smith <x@xman.org> wrote:

> Jamie Lokier <lk@tantalophile.demon.co.uk> wrote:
> > Btw, this functionality is already available using sigaction().  Just
> > search for a signal whose handler is SIG_DFL.  If you then block that
> > signal before changing, checking the result, and unblocking the signal,
> > you can avoid race conditions too.  (This is what my programs do).
> 
> It's more than whether a signal is blocked or not, unfortunately. Lots of 
> applications will invoke sigwaitinfo() on whatever the current signal mask 
> is, which means you can't rely on sigaction to solve your problems. :-(

As Chris points out, allocating a signal by the scheme Jamie
describes is neccessary but not sufficient.  The problem Chris
ran into is that he allocated a signal fair and square, only to find
the application picking it up via sigwaitinfo()!
Yes, this is a bug in the application -- but it's interesting that this
bug only shows up when you try to integrate a new, well-behaved, library 
into the app.  It's a fragile part of the Unix API.  sigopen() is
a way for libraries to defend themselves against misuse of sigwaitinfo()
by big applications over which you have no control.

So sigopen() is a technological fix to a social problem, I guess.
- Dan

^ permalink raw reply	[flat|nested] 28+ messages in thread
* Re: A signal fairy tale
@ 2001-06-28  2:57 Daniel R. Kegel
  2001-06-29  8:19 ` Christopher Smith
  0 siblings, 1 reply; 28+ messages in thread
From: Daniel R. Kegel @ 2001-06-28  2:57 UTC (permalink / raw)
  To: linux-kernel, x

From: Christopher Smith <x@xman.org>
>> [ sigopen() proposal ]
>... From a programming standpoint, this 
>looks like a really nice approach. I must say I prefer this approach to the 
>various "event" strategies I've seen to date, as it fixes the primary 
>problem with signals, while still allowing us to hook in to all the 
>standard POSIX API's that already use signals. 

Thanks.  I'm sure people already thought of this long ago, it's basically just 
the usual "In Unix, everything's a file".  Until you ran into that
problem where the jvm was stealing your signals, though, I hadn't seen
a situation where having signals behave like files would be important.

>It'd be nice if I could pass 
>in a 0 for signum and have the kernel select from unused signals (problem 
>being that "unused" is not necessarily easy to define), althouh I guess an 
>inefficient version of this could be handled in userland.

There is a (non-threadsafe) convention that Jamie Lokier pointed out
for finding an unused thread.   If we allowed sigopen(-1) to allocate
a new signal for you, it'd probably just use the same scheme...

>I presume the fd could be shared between threads and otherwise behave like 
>a normal fd, which would be sooooper nice.

Yes.

>I guess the main thing I'm thinking is this could require some significant 
>changes to the way the kernel behaves. Still, it's worth taking a "try it 
>and see approach". If anyone else thinks this is a good idea I may hack 
>together a sample patch and give it a whirl.

What's the biggest change you see?  From my (two-martini-lunch-tainted)
viewpoint, it's just another kind of signal masking, sorta...

>Thanks again good fairy Dan/Eunice. ;-)

You're welcome (and I'll tell Eunice you liked her idea!).

- Dan


^ permalink raw reply	[flat|nested] 28+ messages in thread
* A signal fairy tale
@ 2001-06-26 12:54 Dan Kegel
  2001-06-27  3:56 ` Christopher Smith
                   ` (4 more replies)
  0 siblings, 5 replies; 28+ messages in thread
From: Dan Kegel @ 2001-06-26 12:54 UTC (permalink / raw)
  To: linux-kernel

Once upon a time a hacker named Xman
wrote a library that used aio, and decided
to use sigtimedwait() to pick up completion
notifications.  It worked well, and his I/O
was blazing fast (since was using a copy
of Linux that was patched to have good aio).
But when he tried to integrate his library
into a large application someone else had
written, woe! that application's use of signals
conflicted with his library.  "Fsck!" said Xman.
At that moment a fairy appeared, and said
"Young man, watch your language, or I'm going to
have to turn you into a goon!  I'm the good fairy Eunice.  
Can I help you?"  Xman explained his problem to Eunice,
who smiled and said "All you need is right here,
just type 'man 2 sigopen'".  Xman did, and saw:

 
SIGOPEN(2)        Linux Programmer's Manual           SIGOPEN(2)
 
NAME
       sigopen - open a signal as a file descriptor
 
SYNOPSIS
       #include <signal.h>
 
       int sigopen(int signum);
 
DESCRIPTION
       The sigopen system call opens signal number signum as a file descriptor.
       That signal is no longer delivered normally or available for pickup
       with sigwait() et al.  Instead, it must be picked up by calling
       read() on the file descriptor returned by sigwait(); the buffer passed to
       read() must have a size which is a multiple of sizeof(siginfo_t).
       Multiple signals may be picked up with a single call to read().
       When that file descriptor is closed, the signal is available once more 
       for traditional use.
       A signal number cannot be opened more than once concurrently; sigopen() 
       thus provides a way to avoid signal usage clashes in large programs.

RETURN VALUE
       signal returns the new file descriptor, or -1 on error (in which case, errno
       is set appropriately).

ERRORS
       EWOULDBLOCK signal is already open

NOTES                                
       read() will block when reading from a file descriptor opened by sigopen()
       until a signal is available unless fcntl(fd, F_SETFL, O_NONBLOCK) is called
       to set it into nonblocking mode.

HISTORY
       sigopen() first appeared in the 2.5.2 Linux kernel.

Linux                      July, 2001                         1           

When he finished reading, he knew just how to solve his
problem, and he lived happily ever after.  

The End.

- Dan

^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2001-07-02 22:35 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-06-28  2:49 A signal fairy tale Daniel R. Kegel
2001-06-29  8:18 ` Christopher Smith
2001-06-29  9:05   ` Dan Kegel
     [not found] <fa.d69j5vv.ej8irj@ifi.uio.no>
     [not found] ` <fa.h2rpibv.87m5bp@ifi.uio.no>
2001-06-28 14:59   ` Dan Maas
2001-06-28 15:21     ` Alan Cox
2001-06-29  8:26   ` Christopher Smith
2001-06-29 11:56     ` Chris Wedgwood
2001-06-30 10:02     ` Jan Hudec
  -- strict thread matches above, loose matches on Subject: below --
2001-06-28 20:11 Daniel R. Kegel
2001-06-29  8:31 ` Christopher Smith
2001-06-28  3:04 Daniel R. Kegel
2001-06-28 14:46 ` Jamie Lokier
2001-06-28  2:57 Daniel R. Kegel
2001-06-29  8:19 ` Christopher Smith
2001-06-29  9:29   ` Dan Kegel
2001-06-29 18:46     ` Dan Kegel
2001-07-02 22:33       ` Christopher Smith
2001-06-26 12:54 Dan Kegel
2001-06-27  3:56 ` Christopher Smith
2001-06-27  6:21 ` Balbir Singh
2001-06-27 18:11   ` Christopher Smith
2001-06-28  3:28     ` Balbir Singh
2001-06-27  9:18 ` Jamie Lokier
2001-06-27 18:16   ` Christopher Smith
2001-06-28 12:58 ` John Fremlin
2001-06-28 16:21   ` Jamie Lokier
2001-06-29  8:22 ` Christopher Smith
2001-06-29 11:47   ` John Fremlin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).