Subject: pkg/7590: Package locking for pkgsrc
To: None <gnats-bugs@gnats.netbsd.org>
From: David Maxwell <david@fundy.net>
List: netbsd-bugs
Date: 05/16/1999 13:50:56
>Number:         7590
>Category:       pkg
>Synopsis:       code to allow multiple parallel pkgsrc makes friendlier
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    pkg-manager (NetBSD software packages system bug manager)
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Sun May 16 13:50:01 1999
>Last-Modified:
>Originator:     David Maxwell
>Organization:
	
	Just me.
>Release:        <NetBSD-current source date> 1.4
>Environment:
	
System: NetBSD newbox 1.4 NetBSD 1.4 (NEWBOX) #5: Sat May 15 13:02:28 ADT 1999 root@newbox:/usr/src/sys/arch/i386/compile/NEWBOX i386


>Description:
	
pkgsrc doesn't restrict multiple makes from trying to build the same package
at the same time. Since packages depend on each other all over the place,
it's easy to open a couple shells and start building packages that both
require say - perl, and have them clobber each other.
>How-To-Repeat:
	
Open two shells. cd /usr/pkgsrc/lang/perl5 in both, and make in both.

>Fix:
	
Here's some 'make code' that will fix this. I don't pretend to know the pksrc
system well enough to put it into the right part of bsd.pkg.mk

This will run standalone for testing purposes, as is. Remove the noted lines
for use in bsd.pkg.mk. Change the 10 seconds to something reasonable.

For CVS purposes, or whatever:
David Maxwell's (david@maxwell.net) implementation of an idea from Mike
Pelley (mike@pelley.com)

------------------------------------------------------------------------

# These are the basic variables used from bsd.pkg.mk
# They are only needed here for standalone testing of course.

ECHO?=          /bin/echo
ECHO_MSG?=              ${ECHO}
WRKDIR_BASENAME?=       work
TAIL?=		/usr/bin/tail	

# This one isn't in bsd.pkg.mk, but will be needed by this.
# About line 547

PS?=            /bin/ps

# Don't know why it would be, but might as well test.
# The Make is the parent of the shell.

.ifndef PPID
PPID!= ${PS} -p $$$$ -o ppid | ${TAIL} -1
.endif

# Just a single target for standalone testing. Move into
# Whatever section is appropriate.

# The environment variable PKG_NOWAITFORLOCK can be set if
# you prefer to have the build error out, rather than wait if
# it finds a locked package.

test:
.ifdef PKG_NOWAITFORLOCK
	@ shlock -f ${WRKDIR_BASENAME}/lockfile -p ${PPID} || ( ${ECHO_MSG} "The package is currently being built by another process." && exit 1 )
.else
	@-	while ( ! shlock -f ${WRKDIR_BASENAME}/lockfile -p ${PPID} ) ;  do \
		sleep 10 ; \
	echo "The package is currently being built by another process. Will retry in 10 seconds." ; done
.endif

	@ echo Managed to lock the package.

#	Sleep 30 seconds for standalone testing. Remove for bsd.pkg.mk
	@ sleep 30

#	Cleanup at end of process.
	@- rm ${WRKDIR_BASENAME}/lockfile


>Audit-Trail:
>Unformatted: