Commit c84196e0 authored by Timo Teräs's avatar Timo Teräs

apk: add --wait option to wait for exclusive lock (fixes #26)

parent 1531192c
......@@ -24,7 +24,7 @@
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[] = {
......@@ -38,9 +38,13 @@ static struct apk_option generic_options[] = {
{ '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,
......
......@@ -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
......
......@@ -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,9 +703,25 @@ 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) {
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);
}
}
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment