[PATCH 3/3] platform: fix missing sigisemptyset

Rich Felker dalias at aerifal.cx
Thu Nov 28 01:01:49 UTC 2013


On Thu, Nov 28, 2013 at 01:54:21AM +0100, Denys Vlasenko wrote:
> On Wednesday 27 November 2013 02:46, Rich Felker wrote:
> > On Tue, Nov 26, 2013 at 10:01:36PM +0200, Daniel Borca wrote:
> > > +#ifndef HAVE_SIGISEMPTYSET
> > > +int sigisemptyset(sigset_t *set)
> > > +{
> > > +	sigset_t empty;
> > > +	int ret = sigemptyset(&empty);
> > > +	if (ret == 0) {
> > > +		ret = !memcmp(&empty, set, sizeof(sigset_t));
> > > +	}
> > > +	return ret;
> > > +}
> > > +#endif
> > 
> > This is not a suitable fallback implementation. It's not needed on
> > musl (we provide sigisemptyset), but if this version were used, it
> > would give the wrong results, because musl's sigemptyset only fills
> > the first _NSIG-1 bits and ignores the remaining ~900 bits of junk in
> > sigset_t.
> > 
> > A valid fallback for sigisemptyset would be something like:
> > 
> > 	int empty = 1, i;
> > 	for (i=1; i<_NSIG; i++) {
> > 		if (sigismember(set, i)>0) {
> > 			empty = 0;
> > 			break;
> > 		}
> > 	}
> 
> My eyes!  8(

It's not *that* bad, is it? :)

> > If _NSIG is unknown (not defined), sizeof(sigset_t)*8+1 could work in
> > its place, assuming signals are numbered sequentially.
> > 
> > Note that in the case of an invalid signal number, sigismember should
> > return either 0 (not a member) or -1 (with errno set); the above code
> > I suggested handles this case (for example, if you try signal numbers
> > greater than _NSIG that are not actually reflected in the sigset_t).
> 
> I would say it's better to open-code a conditional replacement everywhere
> sigisemptyset is used (total of two callsites): usually, the caller expects
> not-braindead looping implementation.

At least in hush, the problem is that busybox is using sigset_t not as
part of an interface to the standard library functions that use it,
but to store its own internal representation of which signals it's
seen, then stuck with the fact that the standard sigset_t API (which
lacks sigisemptyset) can't efficiently provide it the information it
wants. Of course this may still be a reasonable trade-off since
reusing the sigset_t API is size-efficient and the code is probably
not performance-critical.

Rich


More information about the busybox mailing list