2004-02-26 03:54:58 +08:00
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
|
|
|
|
#include <rpmlib.h>
|
2005-01-26 12:05:34 +08:00
|
|
|
#include <rpmmacro.h>
|
2004-02-26 03:54:58 +08:00
|
|
|
|
|
|
|
#include "rpmts.h"
|
|
|
|
#include "rpmlock.h"
|
|
|
|
|
2005-01-26 12:05:34 +08:00
|
|
|
#include "debug.h"
|
|
|
|
|
2004-02-26 03:54:58 +08:00
|
|
|
/* Internal interface */
|
|
|
|
|
2005-01-26 12:05:34 +08:00
|
|
|
#define RPMLOCK_PATH "/var/lock/rpm/transaction"
|
|
|
|
/*@unchecked@*/ /*@observer@*/
|
|
|
|
static const char * rpmlock_path_default = "%{?_rpmlock_path}";
|
|
|
|
/*@unchecked@*/
|
|
|
|
static const char * rpmlock_path = NULL;
|
2004-02-26 03:54:58 +08:00
|
|
|
|
|
|
|
enum {
|
|
|
|
RPMLOCK_READ = 1 << 0,
|
|
|
|
RPMLOCK_WRITE = 1 << 1,
|
|
|
|
RPMLOCK_WAIT = 1 << 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int fd;
|
|
|
|
int openmode;
|
2004-02-26 09:20:52 +08:00
|
|
|
} * rpmlock;
|
2004-02-26 03:54:58 +08:00
|
|
|
|
2004-02-26 09:20:52 +08:00
|
|
|
/*@null@*/
|
2004-03-27 01:27:57 +08:00
|
|
|
static rpmlock rpmlock_new(/*@unused@*/ const char *rootdir)
|
2004-02-26 09:20:52 +08:00
|
|
|
/*@globals fileSystem @*/
|
|
|
|
/*@modifies fileSystem @*/
|
2004-02-26 03:54:58 +08:00
|
|
|
{
|
2004-02-26 09:20:52 +08:00
|
|
|
rpmlock lock = (rpmlock) malloc(sizeof(*lock));
|
2005-01-26 12:05:34 +08:00
|
|
|
|
|
|
|
/* XXX oneshot to determine path for fcntl lock. */
|
|
|
|
if (rpmlock_path == NULL) {
|
2007-07-12 14:20:09 +08:00
|
|
|
const char * t = rpmGenPath(rootdir, rpmlock_path_default, NULL);
|
2005-01-26 12:05:34 +08:00
|
|
|
if (t == NULL || *t == '\0' || *t == '%')
|
2007-03-13 18:32:40 +08:00
|
|
|
t = strdup(RPMLOCK_PATH);
|
2005-01-26 12:05:34 +08:00
|
|
|
rpmlock_path = xstrdup(t);
|
|
|
|
t = _free(t);
|
|
|
|
}
|
|
|
|
if (lock != NULL) {
|
2004-02-26 03:54:58 +08:00
|
|
|
mode_t oldmask = umask(022);
|
2005-01-26 12:05:34 +08:00
|
|
|
lock->fd = open(rpmlock_path, O_RDWR|O_CREAT, 0644);
|
2004-02-26 09:20:52 +08:00
|
|
|
(void) umask(oldmask);
|
|
|
|
|
|
|
|
/*@-branchstate@*/
|
2004-02-26 03:54:58 +08:00
|
|
|
if (lock->fd == -1) {
|
2005-01-26 12:05:34 +08:00
|
|
|
lock->fd = open(rpmlock_path, O_RDONLY);
|
2004-02-26 03:54:58 +08:00
|
|
|
if (lock->fd == -1) {
|
|
|
|
free(lock);
|
|
|
|
lock = NULL;
|
|
|
|
} else {
|
2004-03-27 01:27:57 +08:00
|
|
|
/*@-nullderef@*/ /* XXX splint on crack */
|
2004-02-26 03:54:58 +08:00
|
|
|
lock->openmode = RPMLOCK_READ;
|
2004-03-27 01:27:57 +08:00
|
|
|
/*@=nullderef@*/
|
2004-02-26 03:54:58 +08:00
|
|
|
}
|
|
|
|
} else {
|
2004-03-27 01:27:57 +08:00
|
|
|
/*@-nullderef@*/ /* XXX splint on crack */
|
2004-02-26 03:54:58 +08:00
|
|
|
lock->openmode = RPMLOCK_WRITE | RPMLOCK_READ;
|
2004-03-27 01:27:57 +08:00
|
|
|
/*@=nullderef@*/
|
2004-02-26 03:54:58 +08:00
|
|
|
}
|
2004-02-26 09:20:52 +08:00
|
|
|
/*@=branchstate@*/
|
2004-02-26 03:54:58 +08:00
|
|
|
}
|
2004-02-26 09:20:52 +08:00
|
|
|
/*@-compdef@*/
|
2004-02-26 03:54:58 +08:00
|
|
|
return lock;
|
2004-02-26 09:20:52 +08:00
|
|
|
/*@=compdef@*/
|
2004-02-26 03:54:58 +08:00
|
|
|
}
|
|
|
|
|
2004-02-26 09:20:52 +08:00
|
|
|
static void rpmlock_free(/*@only@*/ /*@null@*/ rpmlock lock)
|
|
|
|
/*@globals fileSystem, internalState @*/
|
|
|
|
/*@modifies lock, fileSystem, internalState @*/
|
2004-02-26 03:54:58 +08:00
|
|
|
{
|
|
|
|
if (lock) {
|
2004-02-26 09:20:52 +08:00
|
|
|
(void) close(lock->fd);
|
2004-02-26 03:54:58 +08:00
|
|
|
free(lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-26 09:20:52 +08:00
|
|
|
static int rpmlock_acquire(/*@null@*/ rpmlock lock, int mode)
|
|
|
|
/*@*/
|
2004-02-26 03:54:58 +08:00
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
if (lock && (mode & lock->openmode)) {
|
|
|
|
struct flock info;
|
|
|
|
int cmd;
|
|
|
|
if (mode & RPMLOCK_WAIT)
|
|
|
|
cmd = F_SETLKW;
|
|
|
|
else
|
|
|
|
cmd = F_SETLK;
|
|
|
|
if (mode & RPMLOCK_READ)
|
|
|
|
info.l_type = F_RDLCK;
|
|
|
|
else
|
|
|
|
info.l_type = F_WRLCK;
|
|
|
|
info.l_whence = SEEK_SET;
|
|
|
|
info.l_start = 0;
|
|
|
|
info.l_len = 0;
|
2004-02-26 09:20:52 +08:00
|
|
|
info.l_pid = 0;
|
2004-02-26 03:54:58 +08:00
|
|
|
if (fcntl(lock->fd, cmd, &info) != -1)
|
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-02-26 09:20:52 +08:00
|
|
|
static void rpmlock_release(/*@null@*/ rpmlock lock)
|
|
|
|
/*@globals internalState @*/
|
|
|
|
/*@modifies internalState @*/
|
2004-02-26 03:54:58 +08:00
|
|
|
{
|
|
|
|
if (lock) {
|
|
|
|
struct flock info;
|
|
|
|
info.l_type = F_UNLCK;
|
|
|
|
info.l_whence = SEEK_SET;
|
|
|
|
info.l_start = 0;
|
|
|
|
info.l_len = 0;
|
2004-02-26 09:20:52 +08:00
|
|
|
info.l_pid = 0;
|
|
|
|
(void) fcntl(lock->fd, F_SETLK, &info);
|
2004-02-26 03:54:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* External interface */
|
|
|
|
|
|
|
|
void *rpmtsAcquireLock(rpmts ts)
|
|
|
|
{
|
|
|
|
const char *rootDir = rpmtsRootDir(ts);
|
2004-02-26 09:20:52 +08:00
|
|
|
rpmlock lock;
|
|
|
|
|
2007-02-22 22:15:30 +08:00
|
|
|
if (!rootDir || rpmtsChrootDone(ts))
|
2004-02-26 03:54:58 +08:00
|
|
|
rootDir = "/";
|
|
|
|
lock = rpmlock_new(rootDir);
|
2004-02-26 09:20:52 +08:00
|
|
|
/*@-branchstate@*/
|
2004-02-26 03:54:58 +08:00
|
|
|
if (!lock) {
|
2005-01-26 12:05:34 +08:00
|
|
|
rpmMessage(RPMMESS_ERROR, _("can't create transaction lock on %s\n"), rpmlock_path);
|
2004-02-26 03:54:58 +08:00
|
|
|
} else if (!rpmlock_acquire(lock, RPMLOCK_WRITE)) {
|
|
|
|
if (lock->openmode & RPMLOCK_WRITE)
|
|
|
|
rpmMessage(RPMMESS_WARNING,
|
2005-01-26 12:05:34 +08:00
|
|
|
_("waiting for transaction lock on %s\n"), rpmlock_path);
|
2004-02-26 03:54:58 +08:00
|
|
|
if (!rpmlock_acquire(lock, RPMLOCK_WRITE|RPMLOCK_WAIT)) {
|
|
|
|
rpmMessage(RPMMESS_ERROR,
|
2005-01-26 12:05:34 +08:00
|
|
|
_("can't create transaction lock on %s\n"), rpmlock_path);
|
2004-02-26 03:54:58 +08:00
|
|
|
rpmlock_free(lock);
|
|
|
|
lock = NULL;
|
|
|
|
}
|
|
|
|
}
|
2004-02-26 09:20:52 +08:00
|
|
|
/*@=branchstate@*/
|
2004-02-26 03:54:58 +08:00
|
|
|
return lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpmtsFreeLock(void *lock)
|
|
|
|
{
|
2004-02-26 09:20:52 +08:00
|
|
|
rpmlock_release((rpmlock)lock); /* Not really needed here. */
|
|
|
|
rpmlock_free((rpmlock)lock);
|
2004-02-26 03:54:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|