aboutsummaryrefslogtreecommitdiffstats
path: root/src/database.c
diff options
context:
space:
mode:
authorTimo Teras <timo.teras@iki.fi>2009-07-07 10:30:54 +0300
committerTimo Teras <timo.teras@iki.fi>2009-07-07 10:30:54 +0300
commitc84196e0d367d8e82e5a3161394741f86bc32750 (patch)
tree2b6004d8135307451b95f6e1ce0d23aa4cb3b2ad /src/database.c
parent1531192cb9b697a8fc0042b295109bdf5cb52231 (diff)
apk: add --wait option to wait for exclusive lock (fixes #26)
Diffstat (limited to 'src/database.c')
-rw-r--r--src/database.c25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/database.c b/src/database.c
index 55e97fa..63c7d0c 100644
--- a/src/database.c
+++ b/src/database.c
@@ -17,6 +17,7 @@
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
+#include <signal.h>
#include <sys/file.h>
#include "apk_defines.h"
@@ -653,6 +654,10 @@ static int apk_db_create(struct apk_database *db)
return 0;
}
+static void handle_alarm(int sig)
+{
+}
+
int apk_db_open(struct apk_database *db, const char *root, unsigned int flags)
{
const char *apk_repos = getenv("APK_REPOS"), *msg = NULL;
@@ -698,8 +703,24 @@ int apk_db_open(struct apk_database *db, const char *root, unsigned int flags)
}
if (db->lock_fd < 0 ||
flock(db->lock_fd, LOCK_EX | LOCK_NB) < 0) {
- msg = "Unable to lock database";
- goto ret_errno;
+ if (apk_wait) {
+ struct sigaction sa, old_sa;
+
+ apk_message("Waiting for repository lock");
+ memset(&sa, 0, sizeof sa);
+ sa.sa_handler = handle_alarm;
+ sa.sa_flags = SA_ONESHOT;
+ sigaction(SIGALRM, &sa, &old_sa);
+
+ alarm(apk_wait);
+ if (flock(db->lock_fd, LOCK_EX) < 0) {
+ msg = "Unable to lock database";
+ goto ret_errno;
+ }
+
+ alarm(0);
+ sigaction(SIGALRM, &old_sa, NULL);
+ }
}
}
}