summaryrefslogtreecommitdiff
path: root/usr.sbin/pkg_add/OpenBSD
diff options
context:
space:
mode:
authorMarc Espie <espie@cvs.openbsd.org>2005-01-14 02:25:13 +0000
committerMarc Espie <espie@cvs.openbsd.org>2005-01-14 02:25:13 +0000
commit40e9a6cdf34436562481587eb43511900cf73931 (patch)
treecd112ed1823e8268abb2ee8515de820c890188e6 /usr.sbin/pkg_add/OpenBSD
parent1ddaf5f286b2ad047bc37bb05b9b860c1850f714 (diff)
`big lock' model: lock the whole db for reading/writing.
design checked with millert@, relying on process termination for unlock at his suggestion. Perf optimization: don't try to reinstall installed packages in kitchensink mode if !-r.
Diffstat (limited to 'usr.sbin/pkg_add/OpenBSD')
-rw-r--r--usr.sbin/pkg_add/OpenBSD/PackageInfo.pm28
1 files changed, 27 insertions, 1 deletions
diff --git a/usr.sbin/pkg_add/OpenBSD/PackageInfo.pm b/usr.sbin/pkg_add/OpenBSD/PackageInfo.pm
index e5db40c7500..49e7072dedd 100644
--- a/usr.sbin/pkg_add/OpenBSD/PackageInfo.pm
+++ b/usr.sbin/pkg_add/OpenBSD/PackageInfo.pm
@@ -1,5 +1,5 @@
# ex:ts=8 sw=4:
-# $OpenBSD: PackageInfo.pm,v 1.15 2004/12/27 22:16:13 espie Exp $
+# $OpenBSD: PackageInfo.pm,v 1.16 2005/01/14 02:25:12 espie Exp $
#
# Copyright (c) 2003-2004 Marc Espie <espie@openbsd.org>
#
@@ -20,6 +20,7 @@ use warnings;
package OpenBSD::PackageInfo;
our @ISA=qw(Exporter);
our @EXPORT=qw(installed_packages installed_info installed_name info_names is_info_name
+ lock_db unlock_db
add_installed delete_installed is_installed borked_package CONTENTS COMMENT DESC INSTALL DEINSTALL REQUIRE
REQUIRED_BY REQUIRING DISPLAY UNDISPLAY MTREE_DIRS);
@@ -37,6 +38,7 @@ use constant {
UNDISPLAY => '+UNDISPLAY',
MTREE_DIRS => '+MTREE_DIRS' };
+use Fcntl qw/:flock/;
my $pkg_db = $ENV{"PKG_DBDIR"} || '/var/db/pkg';
our $list;
@@ -166,4 +168,28 @@ sub is_info_name($)
return $info{$name};
}
+my $dlock;
+
+sub lock_db($)
+{
+ my $shared = shift;
+ my $mode = $shared ? LOCK_SH : LOCK_EX;
+ open($dlock, '<', $pkg_db) or return;
+ if (flock($dlock, $mode | LOCK_NB)) {
+ return;
+ }
+ print "Package database already locked... awaiting release\n";
+ while (!flock($dlock, $mode)) {
+ }
+ return;
+}
+
+sub unlock_db()
+{
+ if (defined $dlock) {
+ flock($dlock, LOCK_UN);
+ close($dlock);
+ }
+}
+
1;