Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 8 Apr 2012 23:50:15 GMT
From:      David Xu <listlog2011@gmail.com>
To:        freebsd-bugs@FreeBSD.org
Subject:   Re: kern/166706: [libc] sem_open(3) incorrectly returns the already opened named semaphore handle when O_EXCL is used [regression]
Message-ID:  <201204082350.q38NoFqA074592@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR kern/166706; it has been noted by GNATS.

From: David Xu <listlog2011@gmail.com>
To: Jilles Tjoelker <jilles@stack.nl>
Cc: bug-followup@FreeBSD.org, davidxu@FreeBSD.org, igaztanaga@gmail.com
Subject: Re: kern/166706: [libc] sem_open(3) incorrectly returns the already
 opened named semaphore handle when O_EXCL is used [regression]
Date: Mon, 09 Apr 2012 07:49:37 +0800

 On 2012/4/9 1:26, Jilles Tjoelker wrote:
 >> [sem_new.c may return success with O_CREAT|O_EXCL if the semaphore
 >> already exists]
 > The code in sem_new.c will happily add another reference to an already
 > open semaphore, even if O_CREAT|O_EXCL were specified.
 >
 > The below patch fixes this by adding an extra check. It makes the
 > submitter's test program pass again on head.
 >
 > What do you think?
 >
 > Index: lib/libc/gen/sem_new.c
 > ===================================================================
 > --- lib/libc/gen/sem_new.c	(revision 233702)
 > +++ lib/libc/gen/sem_new.c	(working copy)
 > @@ -162,10 +162,16 @@
 >   	_pthread_mutex_lock(&sem_llock);
 >   	LIST_FOREACH(ni,&sem_list, next) {
 >   		if (strcmp(name, ni->name) == 0) {
 > -			ni->open_count++;
 > -			sem = ni->sem;
 > -			_pthread_mutex_unlock(&sem_llock);
 > -			return (sem);
 > +			if ((flags&  (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) {
 > +				_pthread_mutex_unlock(&sem_llock);
 > +				errno = EEXIST;
 > +				return (SEM_FAILED);
 > +			} else {
 > +				ni->open_count++;
 > +				sem = ni->sem;
 > +				_pthread_mutex_unlock(&sem_llock);
 > +				return (sem);
 > +			}
 >   		}
 >   	}
 >
 The patch looks fine to me.
 
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201204082350.q38NoFqA074592>