00001
00002 #include "system.h"
00003
00004 #include <rpmlib.h>
00005 #include <rpmmacro.h>
00006
00007 #include "rpmts.h"
00008 #include "rpmlock.h"
00009
00010 #include "debug.h"
00011
00012
00013
00014 #define RPMLOCK_PATH "/var/lock/rpm/transaction"
00015
00016 static const char * rpmlock_path_default = "%{?_rpmlock_path}";
00017
00018 static const char * rpmlock_path = NULL;
00019
00020 enum {
00021 RPMLOCK_READ = 1 << 0,
00022 RPMLOCK_WRITE = 1 << 1,
00023 RPMLOCK_WAIT = 1 << 2,
00024 };
00025
00026 typedef struct {
00027 int fd;
00028 int openmode;
00029 } * rpmlock;
00030
00031
00032 static rpmlock rpmlock_new( const char *rootdir)
00033
00034
00035 {
00036 rpmlock lock = (rpmlock) malloc(sizeof(*lock));
00037
00038
00039 if (rpmlock_path == NULL) {
00040 char * t = rpmExpand(rpmlock_path_default, NULL);
00041 if (t == NULL || *t == '\0' || *t == '%')
00042 t = RPMLOCK_PATH;
00043 rpmlock_path = xstrdup(t);
00044 t = _free(t);
00045 }
00046 if (lock != NULL) {
00047 mode_t oldmask = umask(022);
00048 lock->fd = open(rpmlock_path, O_RDWR|O_CREAT, 0644);
00049 (void) umask(oldmask);
00050
00051
00052 if (lock->fd == -1) {
00053 lock->fd = open(rpmlock_path, O_RDONLY);
00054 if (lock->fd == -1) {
00055 free(lock);
00056 lock = NULL;
00057 } else {
00058
00059 lock->openmode = RPMLOCK_READ;
00060
00061 }
00062 } else {
00063
00064 lock->openmode = RPMLOCK_WRITE | RPMLOCK_READ;
00065
00066 }
00067
00068 }
00069
00070 return lock;
00071
00072 }
00073
00074 static void rpmlock_free( rpmlock lock)
00075
00076
00077 {
00078 if (lock) {
00079 (void) close(lock->fd);
00080 free(lock);
00081 }
00082 }
00083
00084 static int rpmlock_acquire( rpmlock lock, int mode)
00085
00086 {
00087 int res = 0;
00088 if (lock && (mode & lock->openmode)) {
00089 struct flock info;
00090 int cmd;
00091 if (mode & RPMLOCK_WAIT)
00092 cmd = F_SETLKW;
00093 else
00094 cmd = F_SETLK;
00095 if (mode & RPMLOCK_READ)
00096 info.l_type = F_RDLCK;
00097 else
00098 info.l_type = F_WRLCK;
00099 info.l_whence = SEEK_SET;
00100 info.l_start = 0;
00101 info.l_len = 0;
00102 info.l_pid = 0;
00103 if (fcntl(lock->fd, cmd, &info) != -1)
00104 res = 1;
00105 }
00106 return res;
00107 }
00108
00109 static void rpmlock_release( rpmlock lock)
00110
00111
00112 {
00113 if (lock) {
00114 struct flock info;
00115 info.l_type = F_UNLCK;
00116 info.l_whence = SEEK_SET;
00117 info.l_start = 0;
00118 info.l_len = 0;
00119 info.l_pid = 0;
00120 (void) fcntl(lock->fd, F_SETLK, &info);
00121 }
00122 }
00123
00124
00125
00126
00127 void *rpmtsAcquireLock(rpmts ts)
00128 {
00129 const char *rootDir = rpmtsRootDir(ts);
00130 rpmlock lock;
00131
00132 if (!rootDir)
00133 rootDir = "/";
00134 lock = rpmlock_new(rootDir);
00135
00136 if (!lock) {
00137 rpmMessage(RPMMESS_ERROR, _("can't create transaction lock on %s\n"), rpmlock_path);
00138 } else if (!rpmlock_acquire(lock, RPMLOCK_WRITE)) {
00139 if (lock->openmode & RPMLOCK_WRITE)
00140 rpmMessage(RPMMESS_WARNING,
00141 _("waiting for transaction lock on %s\n"), rpmlock_path);
00142 if (!rpmlock_acquire(lock, RPMLOCK_WRITE|RPMLOCK_WAIT)) {
00143 rpmMessage(RPMMESS_ERROR,
00144 _("can't create transaction lock on %s\n"), rpmlock_path);
00145 rpmlock_free(lock);
00146 lock = NULL;
00147 }
00148 }
00149
00150 return lock;
00151 }
00152
00153 void rpmtsFreeLock(void *lock)
00154 {
00155 rpmlock_release((rpmlock)lock);
00156 rpmlock_free((rpmlock)lock);
00157 }
00158
00159