Skip to contentSkip to conversation

Add pselect, ppoll system calls.

PATCH David Woodhouse 11 participants 36 messages Jun 10, 2005 – Aug 26, 2005 VFS

lore
Share & tools
lore
#0 lore raw

Says the man page...

The idea of pselect is that if one wants to wait for an event, either a signal or something on a file descriptor, an atomic test is needed to prevent race conditions. (Suppose the signal handler sets a global flag and returns. Then a test of this global flag followed by a call of select() could hang indefinitely if the signal arrived just after the test but just before the call. On the other hand, pselect allows one to first block signals, handle the signals that have come in, then call pselect() with the desired sigmask, avoiding the race.) Since Linux today does not have a pselect() system call, the current glibc2 routine still contains this race.

arch/i386/kernel/syscall_table.S (mode:100644) +2 -0
Index: arch/i386/kernel/syscall_table.S
===================================================================
@@ -289,3 +289,5 @@
 	.long sys_add_key
 	.long sys_request_key
 	.long sys_keyctl
+	.long sys_pselect6
+	.long sys_ppoll			/* 290 */
arch/ppc/kernel/misc.S (mode:100644) +2 -0
Index: arch/ppc/kernel/misc.S
===================================================================
@@ -1441,3 +1441,5 @@
 	.long sys_request_key		/* 270 */
 	.long sys_keyctl
 	.long sys_waitid
+	.long sys_pselect6
+	.long sys_ppoll
arch/ppc64/kernel/misc.S (mode:100644) +6 -2
Index: arch/ppc64/kernel/misc.S
===================================================================
@@ -953,10 +953,12 @@
 	.llong .compat_sys_mq_getsetattr
 	.llong .sys_ni_syscall		/* 268 reserved for sys_kexec_load */
 	.llong .sys32_add_key
-	.llong .sys32_request_key
+	.llong .sys32_request_key	/* 270 */
 	.llong .compat_sys_keyctl
 	.llong .compat_sys_waitid
-
+	.llong .compat_sys_pselect6
+	.llong .compat_sys_ppoll
+	
 	.balign 8
 _GLOBAL(sys_call_table)
 	.llong .sys_restart_syscall	/* 0 */
@@ -1232,3 +1234,5 @@
 	.llong .sys_request_key		/* 270 */
 	.llong .sys_keyctl
 	.llong .sys_waitid
+	.llong .sys_pselect6
+	.llong .sys_ppoll
arch/ppc64/kernel/signal32.c (mode:100644) +0 -11
Index: arch/ppc64/kernel/signal32.c
===================================================================
@@ -112,17 +112,6 @@
 	}
 }
 
-static inline void sigset_from_compat(sigset_t *set, compat_sigset_t *compat)
-{
-	switch (_NSIG_WORDS) {
-	case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32);
-	case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32);
-	case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32);
-	case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32);
-	}
-}
-
-
 /*
  * Save the current user registers on the user stack.
  * We only save the altivec registers if the process has used
fs/compat.c (mode:100644) +70 -0

76 lines not on this page · raw message

fs/select.c (mode:100644) +73 -0

88 lines not on this page · raw message

include/asm-i386/unistd.h (mode:100644) +3 -1
Index: include/asm-i386/unistd.h
===================================================================
@@ -294,8 +294,10 @@
 #define __NR_add_key		286
 #define __NR_request_key	287
 #define __NR_keyctl		288
+#define __NR_pselect6		289
+#define __NR_ppoll		290
 
-#define NR_syscalls 289
+#define NR_syscalls 291
 
 /*
  * user-visible error numbers are in the range -1 - -128: see
include/asm-ppc/unistd.h (mode:100644) +3 -1
Index: include/asm-ppc/unistd.h
===================================================================
@@ -277,8 +277,10 @@
 #define __NR_request_key	270
 #define __NR_keyctl		271
 #define __NR_waitid		272
+#define __NR_pselect6		273
+#define __NR_ppoll		274
 
-#define __NR_syscalls		273
+#define __NR_syscalls		275
 
 #define __NR(n)	#n
 
include/asm-ppc64/unistd.h (mode:100644) +3 -1
Index: include/asm-ppc64/unistd.h
===================================================================
@@ -283,8 +283,10 @@
 #define __NR_request_key	270
 #define __NR_keyctl		271
 #define __NR_waitid		272
+#define __NR_pselect6		273
+#define __NR_ppoll		274
 
-#define __NR_syscalls		273
+#define __NR_syscalls		275
 #ifdef __KERNEL__
 #define NR_syscalls	__NR_syscalls
 #endif
include/linux/compat.h (mode:100644) +1 -0
Index: include/linux/compat.h
===================================================================
@@ -48,6 +48,7 @@
 	compat_sigset_word	sig[_COMPAT_NSIG_WORDS];
 } compat_sigset_t;
 
+extern void sigset_from_compat (sigset_t *set, compat_sigset_t *compat);
 extern int cp_compat_stat(struct kstat *, struct compat_stat __user *);
 extern int get_compat_timespec(struct timespec *, const struct compat_timespec __user *);
 extern int put_compat_timespec(const struct timespec *, struct compat_timespec __user *);
Signature · 1 line
dwmw2
#1 lore raw
Quoted 6 lines · David Woodhouse

The idea of pselect is that if one wants to wait for an event, either a signal or something on a file descriptor, an atomic test is needed to prevent race conditions. (Suppose the signal handler sets a global flag and returns. Then a test of this global flag followed by a call of select() could hang indefinitely if the signal arrived just after the test but just before the call. On the other hand, pselect allows one to

See sleep(), going back to oh V7 unix. It has this avoided nicely in user space using setjmp (nowdays using sigsetjmp).

If glibc has a race why not just fix glibc ?

#2 lore raw
Quoted 2 lines · Alan Cox

If glibc has a race why not just fix glibc ?

.. because glibc tries to implement the "pselect()" interfaces that some other OS's implement.

Whether that is a good idea or not (it's not) is a different issue, but basically it means that apps don't use the longjmp trick exactly because they think pselect() works ok.

Linus

#3 lore raw

Hi.
I realize this is off subject to the mailing list, however its not really off subject to the thread. What is the suggested method for dealing with this? i.e. catching sigint which sets a global variable and then having select() inside the loop, i.e.

while (boolean < 1 ) {
[...]
select(...);
[...]
}

regards,

jnf

Quoted 28 lines · Linus Torvalds

Date: Sun, 12 Jun 2005 17:26:43 -0700 (PDT)
From: Linus Torvalds <[email protected]>
To: Alan Cox <[email protected]>
Cc: David Woodhouse <[email protected]>,
Linux Kernel Mailing List <[email protected]>, [email protected], [email protected]
Subject: Re: Add pselect, ppoll system calls.

Quoted 2 lines · Alan Cox

If glibc has a race why not just fix glibc ?

.. because glibc tries to implement the "pselect()" interfaces that some other OS's implement.

Whether that is a good idea or not (it's not) is a different issue, but basically it means that apps don't use the longjmp trick exactly because they think pselect() works ok.

Linus
-
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/

#4 lore raw
Quoted 8 lines · jnf

Hi. I realize this is off subject to the mailing list, however its not really off subject to the thread. What is the suggested method for dealing with this? i.e. catching sigint which sets a global variable and then having select() inside the loop, i.e.

while (boolean < 1 ) {
   [...]
   select(...);

Nope, that will have a race in between testing "boolean" and the select.

The simplest way is to do

	if (sigsetjmp(..)) {
		... handle signal ...
	}
	for (;;) {
		select(..)
	}

but a lot of people find the control flow very confusing, and I can't much blame them.

One pretty simple alternative is to just make the timeout be a global, and have the signal handler clear it, guaranteeing that if we're just about to hit the select(), we'll exit immediately.

A third alternative that some people prefer (although I personally find it to be the most complex of the bunch and it's also the least efficient, but it works in threaded environments) is to have the signal handler write a byte to a pipe, and have the select() mask contain that pipe as one of the inputs so that the main loop sees the signal even as a pipe readability test.

Anyway, that's three different approaches, all of which are portable and thus preferable to pselect() which is not.

Linus

#5 lore raw
Quoted 2 lines · Linus Torvalds

Anyway, that's three different approaches, all of which are portable and thus preferable to pselect() which is not.

pselect is portable, just not to current and old Linux systems. It's in POSIX for a while and the other Unixes have the interface.

Signature · 1 line
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖
#6 lore raw
Quoted 2 lines · Ulrich Drepper

pselect is portable, just not to current and old Linux systems. It's in POSIX for a while and the other Unixes have the interface.

Indeed - it has been documented since the days of Stevens at least. All unixes that matter have it.

Quoted 1 line

From the application side, I pretty much really want pselect to work. It is

an embarassment to have a broken implementation, if we really think pselect sucks we should deprecate it in glibc.

So, please, consider merging the patches.. ppoll is something else, I never heard about it, but pselect is widely known.

Thanks.

Signature · 2 lines
http://www.PowerDNS.com      Open source, database driven DNS Software 
http://netherlabs.nl              Open and Closed source services
#7 lore raw
Quoted 2 lines · bert hubert

So, please, consider merging the patches.. ppoll is something else, I never heard about it, but pselect is widely known.

It just seemed silly to implement pselect() without doing ppoll() at the same time.

Signature · 1 line
dwmw2
#8 lore raw
Quoted 6 lines · David Woodhouse
Quoted 2 lines · bert hubert

So, please, consider merging the patches.. ppoll is something else, I never heard about it, but pselect is widely known.

It just seemed silly to implement pselect() without doing ppoll() at the same time.

Yes, it kinda makes sense. Question to Uli: would you put ppoll() into glibc as GNU extension? If not adding the syscall is rather poinless unfortunately.

#9 lore raw
Quoted 11 lines · Christoph Hellwig
Quoted 6 lines · David Woodhouse
Quoted 2 lines · bert hubert

So, please, consider merging the patches.. ppoll is something else, I never heard about it, but pselect is widely known.

It just seemed silly to implement pselect() without doing ppoll() at the same time.

Yes, it kinda makes sense. Question to Uli: would you put ppoll() into glibc as GNU extension? If not adding the syscall is rather poinless unfortunately.

And if ppoll is added, the question is also what it's prototype should look like (i.e. have the suboptimal int timeout argument as poll(2), where you can only wait for less than a month or infinitely, or use const struct timespec * like pselect, or struct timeval * like select).

Jakub

#10 lore raw
Quoted 4 lines · Jakub Jelinek

And if ppoll is added, the question is also what it's prototype should look like (i.e. have the suboptimal int timeout argument as poll(2), where you can only wait for less than a month or infinitely, or use const struct timespec * like pselect, or struct timeval * like select).

Eep -- I hadn't noticed that difference. Will update patch accordingly.

The other documented difference (other than the signal mask bit) is that pselect() is documented not to modify the timeout parameter. I'm not sure whether I should preserve that difference, or not.

Signature · 1 line
dwmw2
#11 lore raw
Quoted 1 line · David Woodhouse

Eep -- I hadn't noticed that difference. Will update patch accordingly.

And change it to expect a 64bit value I hope...

Quoted 3 lines

The other documented difference (other than the signal mask bit) is that pselect() is documented not to modify the timeout parameter. I'm not sure whether I should preserve that difference, or not.

As long as there is a configuration where the timeout value is not modified, it doesn't matter. That is the case for select() using a personality switch.

Signature · 1 line
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖
#12 lore raw
Quoted 1 line · Ulrich Drepper

And change it to expect a 64bit value I hope...

64-bit value for which? For seconds? Do we need to support timeouts of longer than 4 milliard seconds? We can't currently come close to that anyway -- we're limited to LONG_MAX / HZ seconds, and if the user asks for more than that then it appears to be silently switched to an infinite timeout.

Quoted 3 lines

As long as there is a configuration where the timeout value is not modified, it doesn't matter. That is the case for select() using a personality switch.

I think it would be best to behave likewise for pselect().

Signature · 1 line
dwmw2
#13 lore raw
Quoted 1 line · David Woodhouse

64-bit value for which? For seconds?

poll()'s timeout value is measrued in milliseconds. Using a 32bit value, as implied by using 'int' for the type, limits the mximum timeout to be 2^31-1 milliseconds, which means about 24 days. Believe it or not, people are complaining about this. Changing the timeout to a 64 bit millisecond timeout would lift the limitation from the API's POV. I don't know what limitations exist in the kernel itself.

Signature · 1 line
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖
#14 lore raw
Quoted 3 lines · Ulrich Drepper

poll()'s timeout value is measrued in milliseconds. Using a 32bit value, as implied by using 'int' for the type, limits the mximum timeout to be 2^31-1 milliseconds, which means about 24 days.

Ah, OK. I thought you were talking about the timespec in pselect(), because that's what you quoted.

Yes, we should make the time for ppoll() a 64-bit value, so you can request a time period longer than 24 days. Shall we also switch to microseconds?

Quoted 4 lines

Believe it or not, people are complaining about this. Changing the
timeout to a 64bit millisecond timeout would lift the limitation from the API's POV. I don't know what limitations exist in the kernel itself.

Indeed. The ABI will be set in stone, but we have some leeway to fix the implementation details of the kernel's limitations -- such as the fact that on a 32-bit box with HZ == 1000, any requested timeout above MAX_LONG milliseconds (24 days) will actually end up being infinite.

Signature · 1 line
dwmw2
#15 lore raw
Quoted 3 lines · David Woodhouse

Yes, we should make the time for ppoll() a 64-bit value, so you can request a time period longer than 24 days. Shall we also switch to microseconds?

I don't see the need for higher precision in this API and a change is confusing. There is nanosleep() for people who only want a delay and that can be done with high precision.

Signature · 1 line
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖
#16 lore raw
Quoted 3 lines · Ulrich Drepper

I don't see the need for higher precision in this API and a change is confusing. There is nanosleep() for people who only want a delay and that can be done with high precision.

If that's the case, might one enquire as to why pselect() has a struct timespec instead of a struct timeval?

Or would the answer just depress me?

Quoted 1 line
Signature · 1 line
dwmw2
#17 lore raw
Quoted 2 lines · David Woodhouse

If that's the case, might one enquire as to why pselect() has a struct timespec instead of a struct timeval?

General alignment. We are not adding any new interfaces using timeval to the specification anymore.

Signature · 1 line
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖
#18 lore raw
In reply to David Woodhouse
Quoted 11 lines · David Woodhouse
Quoted 3 lines · Ulrich Drepper

poll()'s timeout value is measrued in milliseconds. Using a 32bit value, as implied by using 'int' for the type, limits the mximum timeout to be 2^31-1 milliseconds, which means about 24 days.

Ah, OK. I thought you were talking about the timespec in pselect(), because that's what you quoted.

Yes, we should make the time for ppoll() a 64-bit value, so you can request a time period longer than 24 days. Shall we also switch to microseconds?

I think passing const struct timeval * or const struct timespec * (depending if you want micro or nanoseconds) is better and what other functions use for timeouts, then passing int64_t.

Jakub

#19 lore raw
In reply to Ulrich Drepper
Quoted 4 lines · Ulrich Drepper
Quoted 1 line

Eep -- I hadn't noticed that difference. Will update patch

accordingly.

And change [the poll timeout] to expect a 64bit value I hope...

I believe that 64-bit int types in syscalls are a pain on some architectures because of restrictions on precisely which register pairs may be used. I think I'd rather just use a struct timespec, which also makes it consistent with pselect().

Quoted 7 lines
Quoted 3 lines

The other documented difference (other than the signal mask bit) is that pselect() is documented not to modify the timeout parameter. I'm not sure whether I should preserve that difference, or not.

As long as there is a configuration where the timeout value is not modified, it doesn't matter. That is the case for select() using a personality switch.

I've made pselect() consistent with select() by using the same personality switch to control its behaviour.

I've also fixed select() so that timeouts of greater than LONG_MAX jiffies are implemented accurately, instead of being infinite.

Do this look OK?

arch/i386/kernel/syscall_table.S (mode:100644) +2 -0
Index: arch/i386/kernel/syscall_table.S
===================================================================
@@ -289,3 +289,5 @@
 	.long sys_add_key
 	.long sys_request_key
 	.long sys_keyctl
+	.long sys_pselect6
+	.long sys_ppoll			/* 290 */
arch/ppc/kernel/misc.S (mode:100644) +2 -0
Index: arch/ppc/kernel/misc.S
===================================================================
@@ -1441,3 +1441,5 @@
 	.long sys_request_key		/* 270 */
 	.long sys_keyctl
 	.long sys_waitid
+	.long sys_pselect6
+	.long sys_ppoll
arch/ppc64/kernel/misc.S (mode:100644) +6 -2
Index: arch/ppc64/kernel/misc.S
===================================================================
@@ -953,10 +953,12 @@
 	.llong .compat_sys_mq_getsetattr
 	.llong .sys_ni_syscall		/* 268 reserved for sys_kexec_load */
 	.llong .sys32_add_key
-	.llong .sys32_request_key
+	.llong .sys32_request_key	/* 270 */
 	.llong .compat_sys_keyctl
 	.llong .compat_sys_waitid
-
+	.llong .compat_sys_pselect6
+	.llong .compat_sys_ppoll
+	
 	.balign 8
 _GLOBAL(sys_call_table)
 	.llong .sys_restart_syscall	/* 0 */
@@ -1232,3 +1234,5 @@
 	.llong .sys_request_key		/* 270 */
 	.llong .sys_keyctl
 	.llong .sys_waitid
+	.llong .sys_pselect6
+	.llong .sys_ppoll
arch/ppc64/kernel/signal32.c (mode:100644) +0 -11
Index: arch/ppc64/kernel/signal32.c
===================================================================
@@ -112,17 +112,6 @@
 	}
 }
 
-static inline void sigset_from_compat(sigset_t *set, compat_sigset_t *compat)
-{
-	switch (_NSIG_WORDS) {
-	case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32);
-	case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32);
-	case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32);
-	case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32);
-	}
-}
-
-
 /*
  * Save the current user registers on the user stack.
  * We only save the altivec registers if the process has used
fs/compat.c (mode:100644) +170 -37

230 lines not on this page · raw message

fs/select.c (mode:100644) +203 -46

293 lines not on this page · raw message

include/asm-i386/unistd.h (mode:100644) +3 -1
Index: include/asm-i386/unistd.h
===================================================================
@@ -294,8 +294,10 @@
 #define __NR_add_key		286
 #define __NR_request_key	287
 #define __NR_keyctl		288
+#define __NR_pselect6		289
+#define __NR_ppoll		290
 
-#define NR_syscalls 289
+#define NR_syscalls 291
 
 /*
  * user-visible error numbers are in the range -1 - -128: see
include/asm-ppc/unistd.h (mode:100644) +3 -1
Index: include/asm-ppc/unistd.h
===================================================================
@@ -277,8 +277,10 @@
 #define __NR_request_key	270
 #define __NR_keyctl		271
 #define __NR_waitid		272
+#define __NR_pselect6		273
+#define __NR_ppoll		274
 
-#define __NR_syscalls		273
+#define __NR_syscalls		275
 
 #define __NR(n)	#n
 
include/asm-ppc64/unistd.h (mode:100644) +3 -1
Index: include/asm-ppc64/unistd.h
===================================================================
@@ -283,8 +283,10 @@
 #define __NR_request_key	270
 #define __NR_keyctl		271
 #define __NR_waitid		272
+#define __NR_pselect6		273
+#define __NR_ppoll		274
 
-#define __NR_syscalls		273
+#define __NR_syscalls		275
 #ifdef __KERNEL__
 #define NR_syscalls	__NR_syscalls
 #endif
include/linux/compat.h (mode:100644) +1 -0
Index: include/linux/compat.h
===================================================================
@@ -48,6 +48,7 @@
 	compat_sigset_word	sig[_COMPAT_NSIG_WORDS];
 } compat_sigset_t;
 
+extern void sigset_from_compat (sigset_t *set, compat_sigset_t *compat);
 extern int cp_compat_stat(struct kstat *, struct compat_stat __user *);
 extern int get_compat_timespec(struct timespec *, const struct compat_timespec __user *);
 extern int put_compat_timespec(const struct timespec *, struct compat_timespec __user *);
include/linux/poll.h (mode:100644) +1 -0
Index: include/linux/poll.h
===================================================================
@@ -93,6 +93,7 @@
 }
 
 extern int do_select(int n, fd_set_bits *fds, long *timeout);
+extern int do_sys_poll(struct pollfd __user * ufds, unsigned int nfds, int64_t timeout);
 
 #endif /* KERNEL */
 
Signature · 1 line
dwmw2
#20 lore raw

pselect() modifying timeout

Hello David,

By the way, looking at the comments to the last version of the pselect()/ppoll()patch, I see that the treatment of the timeout argument is made dependent on the personality.

http://marc.theaimsgroup.com/?l=linux-kernel&m=111883591220436&w=2

I'm not sure that this is a good idea; my reasons as follows:

1. POSIX made the behaviour of pselect() explicit -- the timeout must not be modified. The idea was to avoid the vagueness of the select() specification; it had to be vague because of existing implementations. By contrast, there were no pre-existing implementations when pselect() was specified. (By the way, although one or two posts in the earlier thread implied that pselect() has long/widely been present on some systems, this is almost certainly not true. The only systems where I believe it is currently implemented are two that were recently Unix 03 certified: Solaris 10 and AIX (5.3?). I know from doing quite a bit of checking that it is not present as a kernel implementation on most (all?) other systems (even though it was already described by POSIX.1g and Richard Stevens 7 years ago))

I haven't tested Solaris 10 and AIX, but I think one can be reasonably sure that they would conform to the letter of POSIX law. Lacking any strong reason to the contrary, Linux should (IMO) too (why gratuitously introduce differences across implementations?).

2. The existing (non-atomic) glibc pselect() implementation does not change the timeout argument.

Please consider making Linux pselect() conform to POSIX on this point.

Cheers,

Michael

Signature · 2 lines
5 GB Mailbox, 50 FreeSMS http://www.gmx.net/de/go/promail
+++ GMX - die erste Adresse für Mail, Message, More +++
#21 lore raw

Re: pselect() modifying timeout

Quoted 2 lines · Michael Kerrisk

Please consider making Linux pselect() conform to POSIX on this point.

There is no question the implementation will conform. But this not dependent on changing the syscall interface. We deliberately decided to not require the kernel interface to be different from select. The userlevel code will take care of the difference. The kernel code is good as proposed.

Signature · 1 line
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖
#22 lore raw

Re: pselect() modifying timeout

Quoted 9 lines
Quoted 2 lines · Michael Kerrisk

Please consider making Linux pselect() conform to POSIX on this point.

There is no question the implementation will conform. But this not dependent on changing the syscall interface. We deliberately decided to not require the kernel interface to be different from select. The userlevel code will take care of the difference. The kernel code is good as proposed.

Okay -- thanks for the info.

Cheers,

Michael

#23 lore raw

Re: pselect() modifying timeout

In reply to Michael Kerrisk
Quoted 4 lines · Michael Kerrisk

1. POSIX made the behaviour of pselect() explicit -- the timeout must not be modified. The idea was to avoid the vagueness of the select() specification; it had to be vague because of existing implementations. By contrast, there were

Unfortunately it made the wrong choice with pselect, as Linux select experience has shown the modified timeout is *very* useful data to some applications. So the patch is better than the POSUX behaviour. The library can wrap it to provide the poorer standards compliant API while not stopping people using the better one for Linux specific apps.

#24 lore raw
In reply to David Woodhouse
Quoted 24 lines

From: David Woodhouse <[email protected]>
Subject: Re: Add pselect, ppoll system calls.
Date: Wed, 15 Jun 2005 12:36:54 +0100

Quoted 4 lines · Ulrich Drepper
Quoted 1 line

Eep -- I hadn't noticed that difference. Will update patch

accordingly.

And change [the poll timeout] to expect a 64bit value I hope...

I believe that 64-bit int types in syscalls are a pain on some architectures because of restrictions on precisely which register pairs may be used. I think I'd rather just use a struct timespec, which also makes it consistent with pselect().

Quoted 8 lines
Quoted 4 lines

The other documented difference (other than the signal mask bit) is that
pselect() is documented not to modify the timeout parameter. I'm not sure whether I should preserve that difference, or not.

As long as there is a configuration where the timeout value is not modified, it doesn't matter. That is the case for select() using a personality switch.

Quoted 5 lines

I've made pselect() consistent with select() by using the same personality switch to control its behaviour.

I've also fixed select() so that timeouts of greater than LONG_MAX jiffies are implemented accurately, instead of being infinite.

Hi David,

I applied the 15 Jun version of your patch agains 2.6.12 and tried a test program. I see some behaviour that puzzles me. When I run the program below, and type control-C, the program tells me that the SIGINT hander was *not* called:

./t_pselect -
Making syscall(SYS_pselect6) call
[Type ^C]
pselect: Unknown error 514 <-- ERESTNOHAND
ready = -1
stdin IS NOT readable
signal handler WAS NOT called

I'm not sure whether this is possibly a mistake in the way I've constructed my test program (I don't think so, but I'm not !00% confident), or some bug in the implementation (I did try making a syscall(SYS__newselect), and that *did* show the signal handler being called. Also, I now have a test result on Solaris 10 for pselect(), and it shows the signal handler is called in this case.)

Cheers,

Michael

/* t_pselect.c

Michael Kerrisk, Aug 2005
*/
#if defined(__linux__) && !defined(NO_SYSCALL)
#define USE_PSELECT_SYSCALL 1
#endif

#ifdef USE_PSELECT_SYSCALL
#define _GNU_SOURCE
#include <syscall.h>
#endif
#include <sys/time.h>
#include <sys/select.h>
#include <signal.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#define errMsg(msg) do { perror(msg); } while (0)

#define errExit(msg)            do { perror(msg); exit(EXIT_FAILURE); \
                                } while (0)

#ifdef USE_PSELECT_SYSCALL
/* Following are for x86 */
#define SYS_pselect6 289
#define SYS_ppoll 290
#endif

sig_atomic_t gotsig = 0;

static void
handler(int sig)

{
    gotsig = 1;
    printf("Caught signal %d\n", sig);  /* UNSAFE (see Section $$$) */
} /* handler */

int
main(int argc, char *argv[])

{
    fd_set readfds;
    int ready, nfds;
    struct timespec timeout;
    struct timespec *pto;
    struct sigaction sa;
#ifdef USE_PSELECT_SYSCALL
    struct {
        sigset_t *sp;
        size_t size;
    } pselarg6;
#endif
    sigset_t empty, all;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s {nsecs|-}\n", argv[1]);
        exit(EXIT_FAILURE);
    }

    setbuf(stdout, NULL);

/* Block all sigs */

    sigfillset(&all);
    if (sigprocmask(SIG_BLOCK, &all, NULL) == -1) errExit("sigprocmask");

    /* Establish hander for SIGINT */

    sa.sa_handler = handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    if (sigaction(SIGINT, &sa, NULL) == -1) errExit("sigaction");

    /* Timeout is specified in argv[1] */

    if (strcmp(argv[1], "-") == 0) {
        pto = NULL;             /* Infinite timeout */
    } else {
        pto = &timeout;
        timeout.tv_sec = atoi(argv[1]);
        timeout.tv_nsec = 0;
    }

    /* Initialize descriptor set */

    nfds = 1;
    FD_ZERO(&readfds);
    FD_SET(STDIN_FILENO, &readfds);

    sigemptyset(&empty);
    /* sigaddset(&empty, SIGINT); */

    /* Make the call */

#ifdef USE_PSELECT_SYSCALL

#if 1
    printf("Making syscall(SYS_pselect6) call\n");
    pselarg6.sp = &empty;
    pselarg6.size = 8; /* sizeof(sigset_t) */
    ready = syscall(SYS_pselect6, nfds, &readfds, NULL, NULL,
                    pto, &pselarg6);
#else
    /* The following is just some testing cruft */
    {
    struct timeval tv;
    struct timeval *ptv;

    if (strcmp(argv[1], "-") == 0) {
        ptv = NULL;             /* Infinite timeout */
    } else {
        ptv = &tv;
        tv.tv_sec = atoi(argv[1]);
        tv.tv_usec = 0;
    }

    if (sigprocmask(SIG_SETMASK, &empty, NULL) == -1)
        errExit("sigprocmask");

    printf("Making syscall(SYS__newselect) call\n");
    ready = syscall(SYS__newselect, nfds, &readfds, NULL, NULL, ptv);
    printf("!!!! Ignore timeout information printed below !!!!\n");
    }

#endif

#else
    /* This is how a "proper" pselect() call looks on other
       implementations, or when calling the non-atomic glibc version */
    printf("Making simple pselect() call\n");
    ready = pselect(nfds, &readfds, NULL, NULL, pto, &empty);

#endif

    /* Now see what happened */

    if (ready == -1) errMsg("pselect");

    printf("ready = %d\n", ready);
    printf("stdin %s readable\n",
                ready > 0 && FD_ISSET(STDIN_FILENO, &readfds) ?
                        "IS" : "IS NOT");
    printf("Signal handler %s called\n", gotsig ? "WAS" : "WAS NOT");

    if (pto != NULL)
        printf("timeout after select(): %ld.%03ld\n",
               (long) timeout.tv_sec, (long) timeout.tv_nsec / 10000000);
    exit(EXIT_SUCCESS);
} /* main */
Signature · 2 lines
5 GB Mailbox, 50 FreeSMS http://www.gmx.net/de/go/promail
+++ GMX - die erste Adresse für Mail, Message, More +++
#25 lore raw

Hi David,

Not sure if your message below was meant as a reply-all or not, but I've brought it back onto the list.

There are some problems with your test program -- it's not actually using pselect() in proper way, which is:

    /* Block signals that will be later unblocked by pselect() */

    sigemptyset(&block);
    sigaddset(&block, SIGINT);
    sigprocmask(SIG_BLOCK, &block, NULL);

    sigemptyset(&set);
    status = my_pselect(1, &s, NULL, NULL, &timeout, &set);

If I change your program to do something like the above, I also do not see a message from the handler -- i.e., it is not being called, and I'm pretty sure it should be.

Below, is my modified version of your program -- it uses SIGINT.

Cheers,

Michael

#define _GNU_SOURCE
#include <syscall.h>
#include <unistd.h>
#include <signal.h>
#include <sys/select.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

#define __NR_pselect6 289

static int my_pselect(int n,
fd_set *readfds, fd_set *writefds, fd_set
*exceptfds, const struct timespec *timeout, sigset_t *sigmask)

{
        struct {
                sigset_t *set;
                size_t size;
        } __attribute__((packed)) arg6;

        arg6.size = 8;
        arg6.set = sigmask;
        return syscall(__NR_pselect6, n, readfds, writefds,
                exceptfds, timeout, &arg6);
}

static void usr1_handler(int signo)

{
        write(1, "handler called\n", 20);
}

int main(void)

{
        fd_set s;
        int status;
        sigset_t set, block;
        struct timespec timeout;

        timeout.tv_sec = 30;
        timeout.tv_nsec = 0;

        FD_ZERO(&s);
        FD_SET(0, &s);

        signal(SIGINT, usr1_handler);

        sigemptyset(&block);
        sigaddset(&block, SIGINT);
        sigprocmask(SIG_BLOCK, &block, NULL);

        sigemptyset(&set);
        status = my_pselect(1, &s, NULL, NULL, &timeout, &set);
        printf("status=%d\n", status);
        if (status == -1) perror("pselect");
}

--- Weitergeleitete Nachricht ---
Von: David Woodhouse <[email protected]>
An: Michael Kerrisk <[email protected]>
Betreff: Re: Add pselect, ppoll system calls.
Datum: Fri, 05 Aug 2005 13:03:14 +0100

This is the test program I used. I send it SIGUSR1 manually.

#define __USE_MISC
#include <unistd.h>
#include <signal.h>
#include <sys/select.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

#define __NR_pselect6 273

int my_pselect(int n, fd_set *readfds, fd_set *writefds, fd_set
*exceptfds, const struct timespec *timeout, const sigset_t *sigmask)

{
        struct {
                sigset_t *set;
                size_t size;
        } __attribute__((packed)) arg6;

        arg6.size = 8;
        arg6.set = sigmask;
        return syscall(__NR_pselect6, n, readfds, writefds, exceptfds,

timeout, &arg6);
}

void usr1_handler(int signo)

{
        write(1, "usr1_handler called\n", 20);
}
int main(void)
{
        fd_set s;
        int p;
        char buf[80];
        sigset_t set;
        fcntl(0, F_SETFL, O_NONBLOCK | fcntl(0, F_GETFL));

        FD_ZERO(&s);
        FD_SET(0, &s);

        sigfillset(&set);

        signal(SIGUSR1, usr1_handler);

        printf("I am %d\n", getpid());

        while (1) {
                printf("Block usr1\n");
                fflush(stdout);
                sigaddset(&set, SIGUSR1);
                my_pselect(1, &s, NULL, NULL, NULL, &set);
                read(0, buf, 80);
                printf("Allow usr1\n");
                fflush(stdout);
                sigdelset(&set, SIGUSR1);
                my_pselect(1, &s, NULL, NULL, NULL, &set);
                read(0, buf, 80);
        }

}

Signature · 2 lines
5 GB Mailbox, 50 FreeSMS http://www.gmx.net/de/go/promail
+++ GMX - die erste Adresse für Mail, Message, More +++
#26 lore raw

(Sorry for delayed response)

Quoted 3 lines · Michael Kerrisk

If I change your program to do something like the above, I also do not see a message from the handler -- i.e., it is not being called, and I'm pretty sure it should be.

Hm, yes. What happens is we come back out of the select() immediately because of the pending signal, but on the way back to userspace we put the old signal mask back... so by the time we check for it, there _is_ no (unblocked) signal pending.

If it's mandatory that we actually call the signal handler, then we need to play tricks like sigsuspend() does to leave the old signal mask on the stack frame. That's a bit painful atm because do_signal is different between architectures.

Signature · 1 line
dwmw2
#27 lore raw
Quoted 4 lines · David Woodhouse

If it's mandatory that we actually call the signal handler, then we need to play tricks like sigsuspend() does to leave the old signal mask on the stack frame. That's a bit painful atm because do_signal is different between architectures.

It is necessary that the handler is called. This is the purpose of these interfaces. If this means more complexity is needed then this is how the cookie crumbles. One use case for pselect would be something like this:

int got_signal;
void sigint_handler(int sig) {
got_signal = 1;
}

{
...

  while (1) {
    if (!got_signal)
      pselect()

    if (got_signal) {
      handle signal
      got_signal = 0;
    }

}
...
}

Signature · 1 line
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖
#28 lore raw
In reply to David Woodhouse

David,

Quoted 11 lines · Michael Kerrisk
Quoted 3 lines

If I change your program to do something like the above, I also do not see a message from the handler -- i.e., it is not being called, and I'm pretty sure it should be.

Hm, yes. What happens is we come back out of the select() immediately because of the pending signal, but on the way back to userspace we put the old signal mask back... so by the time we check for it, there _is_ no (unblocked) signal pending.

If it's mandatory that we actually call the signal handler,

I'm just about to go off on holiday, and don't have a chance to pull up all the relevant standards details at them moment. However, I'm fairly sure that the signal handler should be called. (Try running a modified version of my program on Solaris 10 or the Unix-03 conversion of AIX (5.3?).)

Quoted 4 lines

then we need to
play tricks like sigsuspend() does to leave the old signal mask on the stack frame. That's a bit painful atm because do_signal is different between architectures.

Yes, I'd say the behaviour should in fact be like what sigsuspend() does.

Cheers,

Michael

#29 lore raw
In reply to Christoph Hellwig
Quoted 2 lines · Christoph Hellwig

Yes, it kinda makes sense. Question to Uli: would you put ppoll() into glibc as GNU extension?

Of course. I would rather not add pselect() and deprecate select() than not adding ppoll(). In fact, we just discussed a similar issue in the POSIX base working group. Due to the limitations select() might indeed get the axe in a future revision.

Signature · 1 line
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖
#30 lore raw
In reply to Linus Torvalds
Quoted 4 lines

One pretty simple alternative is to just make the timeout be a global, and have the signal handler clear it, guaranteeing that if we're just about to hit the select(), we'll exit immediately.

That is still racy ... if the signal hits between loading that global to to pass it to select and the actual syscall entry ... pretty narrow but still there.

#31 lore raw
Quoted 3 lines · Benjamin Herrenschmidt

That is still racy ... if the signal hits between loading that global to to pass it to select and the actual syscall entry ... pretty narrow but still there.

We don't load it; we pass a pointer to select. It works, but it's hardly elegant.

Signature · 1 line
dwmw2
#32 lore raw
Quoted 7 lines · David Woodhouse
Quoted 3 lines · Benjamin Herrenschmidt

That is still racy ... if the signal hits between loading that global to to pass it to select and the actual syscall entry ... pretty narrow but still there.

We don't load it; we pass a pointer to select. It works, but it's hardly elegant.

Argh true, blame my own wrapper to select that takes an ulong ..

Who said POSIX was a crap API ? :)

Ben.

#33 lore raw
In reply to Linus Torvalds
Quoted 10 lines · Linus Torvalds
Quoted 2 lines · Alan Cox

If glibc has a race why not just fix glibc ?

.. because glibc tries to implement the "pselect()" interfaces that some other OS's implement.

Whether that is a good idea or not (it's not) is a different issue, but basically it means that apps don't use the longjmp trick exactly because they think pselect() works ok.

Right but why can't glibc be fixed to use the longjmp trick internally. That way pselect works properly for all applications as soon as the glibc bug fix is
applied and the vendor/distro/whatever pushes a new glibc package ?

Alan

#34 lore raw
Quoted 2 lines · Alan Cox

Right but why can't glibc be fixed to use the longjmp trick internally.

Unless I misunderstand what is suggested, for glibc to do that internally would be vile. It'd have to override the signal handler for every signal which is unmasked by the new signal mask, right?

That kind of trick makes some sense to use in an _application_ if the author isn't expecting to run on a POSIX system and just use pselect(), but for glibc to hack it up internally would be horrid.

Signature · 1 line
dwmw2
#35 lore raw
In reply to Alan Cox
Quoted 1 line · Alan Cox

Right but why can't glibc be fixed to use the longjmp trick internally.

How, Alan? Remember, these are not the days of V7. We have threads. And we have signal handlers as a process-wide property, shared by all threads.

Signature · 1 line
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖