From 1f625e69e9609bedde9b071d87849ed518b43d22 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Thu, 22 Apr 2010 11:01:27 +0300 Subject: [PATCH] Further generalize the rpmlock interface - Move transaction lock path handling into rpmts.c, export new low level rpmlockAcquire() function to actually grab a lock - Rename rpmtsFreeLock() to rpmlockFree() and return NULL in the general rpm style --- lib/rpmlock.c | 42 ++++++++++-------------------------------- lib/rpmlock.h | 9 +++++++-- lib/rpmts.c | 28 +++++++++++++++++++++++++--- lib/rpmts_internal.h | 3 +++ lib/transaction.c | 2 +- 5 files changed, 46 insertions(+), 38 deletions(-) diff --git a/lib/rpmlock.c b/lib/rpmlock.c index 473669f..cf9947e 100644 --- a/lib/rpmlock.c +++ b/lib/rpmlock.c @@ -1,13 +1,10 @@ #include "system.h" -#include #include #include -#include #include -#include #include "lib/rpmlock.h" @@ -98,39 +95,19 @@ static void rpmlock_release(rpmlock lock) /* External interface */ -#define RPMLOCK_PATH LOCALSTATEDIR "/rpm/.rpm.lock" -rpmlock rpmtsAcquireLock(rpmts ts) +rpmlock rpmlockAcquire(const char *lock_path, const char *descr) { - static const char * const rpmlock_path_default = "%{?_rpmlock_path}"; - static const char * rpmlock_path = NULL; - const char *rootDir = rpmtsRootDir(ts); - rpmlock lock; - - if (!rootDir || rpmtsChrootDone(ts)) - rootDir = "/"; - /* XXX oneshot to determine path for fcntl lock. */ - if (rpmlock_path == NULL) { - char * t = rpmGenPath(rootDir, rpmlock_path_default, NULL); - if (t == NULL || *t == '\0' || *t == '%') - t = xstrdup(RPMLOCK_PATH); - rpmlock_path = xstrdup(t); - (void) rpmioMkpath(dirname(t), 0755, getuid(), getgid()); - t = _free(t); - } - - lock = rpmlock_new(rpmlock_path); + rpmlock lock = rpmlock_new(lock_path); if (!lock) { - rpmlog(RPMLOG_ERR, - _("can't create transaction lock on %s (%s)\n"), - rpmlock_path, strerror(errno)); + rpmlog(RPMLOG_ERR, _("can't create %s lock on %s (%s)\n"), + descr, lock_path, strerror(errno)); } else if (!rpmlock_acquire(lock, RPMLOCK_WRITE)) { if (lock->openmode & RPMLOCK_WRITE) - rpmlog(RPMLOG_WARNING, - _("waiting for transaction lock on %s\n"), rpmlock_path); + rpmlog(RPMLOG_WARNING, _("waiting for %s lock on %s\n"), + descr, lock_path); if (!rpmlock_acquire(lock, RPMLOCK_WRITE|RPMLOCK_WAIT)) { - rpmlog(RPMLOG_ERR, - _("can't create transaction lock on %s (%s)\n"), - rpmlock_path, strerror(errno)); + rpmlog(RPMLOG_ERR, _("can't create %s lock on %s (%s)\n"), + descr, lock_path, strerror(errno)); rpmlock_free(lock); lock = NULL; } @@ -138,10 +115,11 @@ rpmlock rpmtsAcquireLock(rpmts ts) return lock; } -void rpmtsFreeLock(rpmlock lock) +rpmlock rpmlockFree(rpmlock lock) { rpmlock_release(lock); /* Not really needed here. */ rpmlock_free(lock); + return NULL; } diff --git a/lib/rpmlock.h b/lib/rpmlock.h index f527c4d..96f6de2 100644 --- a/lib/rpmlock.h +++ b/lib/rpmlock.h @@ -1,9 +1,14 @@ #ifndef RPMLOCK_H #define RPMLOCK_H +#include + typedef struct rpmlock_s * rpmlock; -rpmlock rpmtsAcquireLock(rpmts ts); -void rpmtsFreeLock(rpmlock lock); +RPM_GNUC_INTERNAL +rpmlock rpmlockAcquire(const char *lock_path, const char *descr); + +RPM_GNUC_INTERNAL +rpmlock rpmlockFree(rpmlock lock); #endif diff --git a/lib/rpmts.c b/lib/rpmts.c index aeb472b..f1ecda5 100644 --- a/lib/rpmts.c +++ b/lib/rpmts.c @@ -5,6 +5,7 @@ #include "system.h" #include +#include #include #include /* rpmReadPackage etc */ @@ -21,7 +22,6 @@ #include "rpmio/digest.h" #include "lib/rpmal.h" -#include "lib/rpmlock.h" #include "lib/rpmts_internal.h" #include "lib/misc.h" @@ -98,7 +98,7 @@ int rpmtsInitDB(rpmts ts, int dbmode) int rc = -1; if (lock) rc = rpmdbInit(ts->rootDir, dbmode); - rpmtsFreeLock(lock); + rpmlockFree(lock); return rc; } @@ -129,7 +129,7 @@ int rpmtsRebuildDB(rpmts ts) rc = rpmdbRebuild(ts->rootDir, ts, headerCheck); else rc = rpmdbRebuild(ts->rootDir, NULL, NULL); - rpmtsFreeLock(lock); + rpmlockFree(lock); return rc; } @@ -971,3 +971,25 @@ rpmte rpmtsiNext(rpmtsi tsi, rpmElementType type) } return te; } + +#define RPMLOCK_PATH LOCALSTATEDIR "/rpm/.rpm.lock" +rpmlock rpmtsAcquireLock(rpmts ts) +{ + static const char * const rpmlock_path_default = "%{?_rpmlock_path}"; + static const char * rpmlock_path = NULL; + const char *rootDir = rpmtsRootDir(ts); + + if (!rootDir || rpmtsChrootDone(ts)) + rootDir = "/"; + /* XXX oneshot to determine path for fcntl lock. */ + if (rpmlock_path == NULL) { + char * t = rpmGenPath(rootDir, rpmlock_path_default, NULL); + if (t == NULL || *t == '\0' || *t == '%') + t = xstrdup(RPMLOCK_PATH); + rpmlock_path = xstrdup(t); + (void) rpmioMkpath(dirname(t), 0755, getuid(), getgid()); + t = _free(t); + } + return rpmlockAcquire(rpmlock_path, _("transaction")); +} + diff --git a/lib/rpmts_internal.h b/lib/rpmts_internal.h index d73d2b0..c42e2a1 100644 --- a/lib/rpmts_internal.h +++ b/lib/rpmts_internal.h @@ -6,6 +6,7 @@ #include "lib/rpmal.h" /* XXX availablePackage */ #include "lib/rpmhash.h" /* XXX hashTable */ #include "lib/fprint.h" +#include "lib/rpmlock.h" typedef struct diskspaceInfo_s * rpmDiskSpaceInfo; @@ -78,4 +79,6 @@ tsMembers rpmtsMembers(rpmts ts); RPM_GNUC_INTERNAL int rpmtsSolve(rpmts ts, rpmds key); +RPM_GNUC_INTERNAL +rpmlock rpmtsAcquireLock(rpmts ts); #endif /* _RPMTS_INTERNAL_H */ diff --git a/lib/transaction.c b/lib/transaction.c index ee91c28..93903fb 100644 --- a/lib/transaction.c +++ b/lib/transaction.c @@ -1421,6 +1421,6 @@ int rpmtsRun(rpmts ts, rpmps okProbs, rpmprobFilterFlags ignoreSet) exit: tsprobs = rpmpsFree(tsprobs); - rpmtsFreeLock(lock); + rpmlockFree(lock); return rc; } -- 2.7.4