From c84196e0d367d8e82e5a3161394741f86bc32750 Mon Sep 17 00:00:00 2001 From: Timo Teras Date: Tue, 7 Jul 2009 10:30:54 +0300 Subject: [PATCH] apk: add --wait option to wait for exclusive lock (fixes #26) --- src/apk.c | 19 +++++++++++++------ src/apk_defines.h | 2 +- src/database.c | 25 +++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/apk.c b/src/apk.c index 024c11d..84c2d04 100644 --- a/src/apk.c +++ b/src/apk.c @@ -24,23 +24,27 @@ const char *apk_root; struct apk_repository_url apk_repository_list; -int apk_verbosity = 1, apk_cwd_fd; +int apk_verbosity = 1, apk_cwd_fd, apk_wait; unsigned int apk_flags = 0; static struct apk_option generic_options[] = { { 'h', "help", "Show generic help or applet specific help" }, { 'p', "root", "Install packages to DIR", - required_argument, "DIR" }, + required_argument, "DIR" }, { 'X', "repository", "Use packages from REPO", - required_argument, "REPO" }, + required_argument, "REPO" }, { 'q', "quiet", "Print less information" }, { 'v', "verbose", "Print more information" }, { 'V', "version", "Print program version and exit" }, { 'f', "force", "Do what was asked even if it looks dangerous" }, { 0x101, "progress", "Show a progress bar" }, - { 0x102, "clean-protected", - "Do not create .apk-new files to configuration dirs" }, - { 0x104, "simulate", "Show what would be done without actually doing it" }, + { 0x102, "clean-protected", "Do not create .apk-new files to " + "configuration dirs" }, + { 0x104, "simulate", "Show what would be done without actually " + "doing it" }, + { 0x105, "wait", "Wait for TIME seconds to get an exclusive " + "repository lock before failing", + required_argument, "TIME" }, }; void apk_log(const char *prefix, const char *format, ...) @@ -305,6 +309,9 @@ int main(int argc, char **argv) case 0x104: apk_flags |= APK_SIMULATE; break; + case 0x105: + apk_wait = atoi(optarg); + break; default: if (applet == NULL || applet->parse == NULL || applet->parse(ctx, r, diff --git a/src/apk_defines.h b/src/apk_defines.h index f530e51..8dcd400 100644 --- a/src/apk_defines.h +++ b/src/apk_defines.h @@ -50,7 +50,7 @@ extern csum_t bad_checksum; #define csum_valid(buf) memcmp(buf, bad_checksum, sizeof(csum_t)) #endif -extern int apk_cwd_fd, apk_verbosity; +extern int apk_cwd_fd, apk_verbosity, apk_wait; extern unsigned int apk_flags; #define APK_FORCE 0x0001 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 #include #include +#include #include #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); + } } } }