Skip to contentSkip to conversation

Desperately missing a working "pselect()" or similar...

DISCUSSION [email protected] 7 participants 11 messages Oct 5, 2001

lore
Share & tools
lore
#0 lore raw

Hi,

I'm currently looking for a decent method to wait on either an I/O event _or_ a signal coming from another process.

Alas, it seems that the Linux kernel does not have any appropriate system call to support what for example "pselect()" tries to do: Atomically enable some signals and entering a select.

Without a proper pselect() implementation (the one in glibc is just a mock-up that doesn't prevent the race condition) I'm currently unable to come up with a good idea on how to wait on both types of events.

A somewhat bizarre solution would be to have the process create a pipe-pair, select on the reading end, and let the signal-handler write a byte to the pipe - but this has at least the drawback you always spoil one "select-cycle" for each signal you get - as the first return from the select() call happenes without any fds being flagged as readable, only when you enter select() once more the pipe will cause the return and tell you what happened...

Arguments against other options I considered:

- Using just signals is at least prevented by SIGIO not being delivered for pipes, and I'm not eager to find out about all the other problems that may arise by devices not behaving as expected

- Unix domain sockets would be awkward to use due to the fact I'd need to come up with some "filenames" for them to bind to, and both security considerations and the danger of "leaking" files that remain on disk forever make me shudder...

Any ideas?
Anyone capable of implementing a system-call for pselect() (or ppoll) ?

Regards,

Lutz Vieweg

--

 Dipl. Phys. Lutz Vieweg | email: [email protected]
 Innovative Software AG  | Phone/Fax: +49-69-505030 -120/-505
 Feuerbachstrasse 26-32  | http://www.isg.de/people/lkv/
 60325 Frankfurt am Main | ^^^ PGP key available here ^^^
#1 lore raw
Quoted 7 lines · On Fri, 5 Oct 2001 [email protected] wrote:

A somewhat bizarre solution would be to have the process create a pipe-pair, select on the reading end, and let the signal-handler write a byte to the pipe - but this has at least the drawback you always spoil one "select-cycle" for each signal you get - as the first return from the select() call happenes without any fds being flagged as readable, only when you enter select() once more the pipe will cause the return and tell you what happened...

fork() is cheap. Create a child, have a pipe between child and parent and do select() on the other end of pipe. I.e. signal handler writes into pipe and that triggers select() in the second process.

#2 lore raw
Quoted 11 lines · Alexander Viro
Quoted 7 lines · On Fri, 5 Oct 2001 [email protected] wrote:

A somewhat bizarre solution would be to have the process create a pipe-pair, select on the reading end, and let the signal-handler write a byte to the pipe - but this has at least the drawback you always spoil one "select-cycle" for each signal you get - as the first return from the select() call happenes without any fds being flagged as readable, only when you enter select() once more the pipe will cause the return and tell you what happened...

Quoted 3 lines

fork() is cheap. Create a child, have a pipe between child and parent and do select() on the other end of pipe. I.e. signal handler writes into pipe and that triggers select() in the second process.

What exactly would be the advantage of doubling the number of processes running just to introduce this indirection? An additional context-switch surely doesn't speed up things... or am I misinterpreting your proposal completely?

Regards,

Lutz Vieweg

--

 Dipl. Phys. Lutz Vieweg | email: [email protected]
 Innovative Software AG  | Phone/Fax: +49-69-505030 -120/-505
 Feuerbachstrasse 26-32  | http://www.isg.de/people/lkv/
 60325 Frankfurt am Main | ^^^ PGP key available here ^^^
#3 lore raw
In reply to [email protected]
Quoted 10 lines · [email protected] wrote:

Hi,

I'm currently looking for a decent method to wait on either an I/O event _or_ a signal coming from another process.

- Unix domain sockets would be awkward to use due to the fact I'd need to come up with some "filenames" for them to bind to, and both security considerations and the danger of "leaking" files that remain on disk forever make me shudder...

If you use a named socket in the abstract namespace, then it can't "leak" to disk....

Chris

Signature · 4 lines
Chris Friesen                    | MailStop: 043/33/F10  
Nortel Networks                  | work: (613) 765-0557
3500 Carling Avenue              | fax:  (613) 765-2986
Nepean, ON K2H 8E9 Canada        | email: [email protected]
#4 lore raw
In reply to [email protected]
Quoted 10 lines · Christopher Friesen
Quoted 7 lines

I'm currently looking for a decent method to wait on either an I/O event _or_ a signal coming from another process.

- Unix domain sockets would be awkward to use due to the fact I'd need to come up with some "filenames" for them to bind to, and both security considerations and the danger of "leaking" files that remain on disk forever make me shudder...

If you use a named socket in the abstract namespace, then it can't "leak" to disk....

Ok, but man 7 unix says:

SCM_CREDENTIALS and the abstract namespace were introduced with Linux 2.2 and should not be used in portable programs

... and I really do want to write portable programs...

Regards,

Lutz Vieweg

--

 Dipl. Phys. Lutz Vieweg | email: [email protected]
 Innovative Software AG  | Phone/Fax: +49-69-505030 -120/-505
 Feuerbachstrasse 26-32  | http://www.isg.de/people/lkv/
 60325 Frankfurt am Main | ^^^ PGP key available here ^^^
#5 lore raw
In reply to [email protected]
Quoted 4 lines · In article <[email protected]> you wrote:

Without a proper pselect() implementation (the one in glibc is just a mock-up that doesn't prevent the race condition) I'm currently unable to come up with a good idea on how to wait on both types of events.

Isnt select() returning with EINTR?

Quoted 4 lines

A somewhat bizarre solution would be to have the process create a pipe-pair, select on the reading end, and let the signal-handler write a byte to the pipe - but this has at least the drawback you always spoil one "select-cycle" for each signal you get

Well, you can use the pipe instead of the signal. What kind of signal do you try to trap? Looks like you want to do critical high load stuff with a signal.

Greetings
Bernd

#6 lore raw
In reply to [email protected]
Quoted 7 lines · Bernd Eckenfels
Quoted 4 lines · In article <[email protected]> you wrote:

Without a proper pselect() implementation (the one in glibc is just a mock-up that doesn't prevent the race condition) I'm currently unable to come up with a good idea on how to wait on both types of events.

Isnt select() returning with EINTR?

The select system call doesn't return EINTR when the signal is caught prior to entry into select.

The problem is if you have a select loop and small signal handlers setting flags for the loop, a signal could come in after the flag is tested but before select is called. Instead of acting on this signal right away, the process blocks in select.

The pselect system call offers a solution. The process blocks signals in the select loop; pselect unblocks those signals and does a select. The race condition mentioned earlier disappears: the signal that arrives after the flag test is blocked. The pselect system call unblocks the signal, so the deferred signal acts just like it arrived while the process is blocked in select.

Quoted 8 lines
Quoted 4 lines

A somewhat bizarre solution would be to have the process create a pipe-pair, select on the reading end, and let the signal-handler write a byte to the pipe - but this has at least the drawback you always spoil one "select-cycle" for each signal you get

Well, you can use the pipe instead of the signal. What kind of signal do you try to trap? Looks like you want to do critical high load stuff with a signal.

He just wants to handle signals properly 100% of the time.

#7 lore raw
Quoted 2 lines

The select system call doesn't return EINTR when the signal is caught prior to entry into select.

Your friend there is siglongjmp/sigsetjmp - the same problem was true with old versions of alarm that did

	alarm(num)
	pause()

on a heavily loaded box.

Using siglongjmp cures that

#8 lore raw
In reply to Alex Pennace
Quoted 6 lines · Alan Cox
Quoted 2 lines

The select system call doesn't return EINTR when the signal is caught prior to entry into select.

Your friend there is siglongjmp/sigsetjmp - the same problem was true with old versions of alarm that did

Quoted 6 lines
	alarm(num)
	pause()

on a heavily loaded box.

Using siglongjmp cures that

I was naive about siglongjump bouncing through the stack over a system call, but it appears to be safe. Excellent.

#9 lore raw
In reply to [email protected]
Quoted 6 lines · Neil Brown
Quoted 2 lines · On Friday October 5, [email protected] wrote:

The select system call doesn't return EINTR when the signal is caught prior to entry into select.

A technique I used in a similar situation once went something like:

Quoted 7 lines

tv.tv_sec=bignum;
tv.tv_usec = 0;
enable_signals();
select(nfds, &readfds,&writefds,0,&tv);

and have the signal handlers set tv.tv_sec to 0. (tv is a global variable).

I've thought about that, but I haven't been able to find any guarantee that there will be no user space futzing around with &tv, like a library wrapper that copies tv to another spot in memory and invokes the syscall with that address.

#10 lore raw
Quoted 9 lines
Quoted 8 lines

A technique I used in a similar situation once went something like:

tv.tv_sec=bignum;
tv.tv_usec = 0;
enable_signals();
select(nfds, &readfds,&writefds,0,&tv);

Quoted 7 lines
Quoted 2 lines

and have the signal handlers set tv.tv_sec to 0. (tv is a global variable).

I've thought about that, but I haven't been able to find any guarantee that there will be no user space futzing around with &tv, like a library wrapper that copies tv to another spot in memory and invokes the syscall with that address.

This will commonly happen if, for example, the user-side timeval structure
contains seconds and microseconds and the kernel-side structure contains seconds and nanoseconds. The signal might occur after the library has performed the structure conversion.

DS