From 697d512ea679363db68012bd3829dca9eb0daed8 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 17 Oct 2013 15:57:42 +0200 Subject: [PATCH] lockfile: Remove force parameter from hold_lock() If user wants to 'force' a lock, he/she will have to remove the file manually. some wierd race conditions may happen if different processes optains the same lock at any given time (thats why lockfile is implemented in the first place) So lockfile API should not in any circumstances directly provide a way to force taking a lock. --- compile.c | 8 ++------ dlhist.c | 2 +- lockfile.c | 9 +++------ lockfile.h | 4 ++-- proc-cache.c | 2 +- 5 files changed, 9 insertions(+), 16 deletions(-) diff --git a/compile.c b/compile.c index 8c72980..11be6f9 100644 --- a/compile.c +++ b/compile.c @@ -300,7 +300,7 @@ static int parse_config_file(const char *file) { int main(int argc, char **argv) { - int lockfd, force = 0; + int lockfd; struct lockfile lock = LOCKFILE_INIT; char filename[4096]; @@ -319,11 +319,7 @@ int main(int argc, char **argv) { snprintf(filename, sizeof(filename), "%s/%s", env_get_dir(), "config"); - /* Remove lockfile if forced */ - if (argc > 1 && !strcmp(argv[1], "-f")) - force = 1; - - lockfd = hold_lock(&lock, filename, force); + lockfd = hold_lock(&lock, filename); if (lockfd < 0) return 1; diff --git a/dlhist.c b/dlhist.c index cbfa4da..61f49f3 100644 --- a/dlhist.c +++ b/dlhist.c @@ -245,7 +245,7 @@ int dlhist_open() { "%s/%s", env_get_dir(), STORAGE_FILE); /* try lockin the file */ - if (hold_lock(&lock, filename, 0) < 0) + if (hold_lock(&lock, filename) < 0) goto error; fd = open(filename, O_CREAT | O_RDONLY, 0600); diff --git a/lockfile.c b/lockfile.c index 9e6f463..181ea9f 100644 --- a/lockfile.c +++ b/lockfile.c @@ -109,13 +109,10 @@ static double get_lock_time(const char *path) { return difftime(time(NULL), st.st_ctime); } -static int open_lock(const char *path, int force) { +static int open_lock(const char *path) { int fd, mask = O_WRONLY | O_TRUNC | O_CREAT | O_EXCL; - if (force) - mask &= ~O_EXCL; - fd = open(path, mask, 0600); if (fd < 0) { /* Force open if lockfile exists @@ -130,7 +127,7 @@ static int open_lock(const char *path, int force) { return fd; } -int hold_lock(struct lockfile *lock, const char *filename, int force) { +int hold_lock(struct lockfile *lock, const char *filename) { int rc; @@ -143,7 +140,7 @@ int hold_lock(struct lockfile *lock, const char *filename, int force) { if (rc > sizeof(lock->name)) return -1; - lock->fd = open_lock(lock->name, force); + lock->fd = open_lock(lock->name); if (lock->fd < 0) { if (errno == EEXIST) return error("'%s' is locked", filename); diff --git a/lockfile.h b/lockfile.h index 1d9bba6..5d31725 100644 --- a/lockfile.h +++ b/lockfile.h @@ -33,10 +33,10 @@ struct lockfile { #define is_locked(x) ((x)->fd >= 0) -int hold_lock(struct lockfile *lock, const char *filename, int force); +int hold_lock(struct lockfile *lock, const char *filename); int commit_lock(struct lockfile *lock); int release_lock(struct lockfile *lock); -#endif /* LOCKFILE_H */ \ No newline at end of file +#endif /* LOCKFILE_H */ diff --git a/proc-cache.c b/proc-cache.c index 6a64cdd..72e24ff 100644 --- a/proc-cache.c +++ b/proc-cache.c @@ -163,7 +163,7 @@ int proc_cache_open() { "%s/%s", env_get_dir(), STORAGE_FILE); /* try lockin the file */ - if (hold_lock(&lock, filename, 0) < 0) + if (hold_lock(&lock, filename) < 0) goto error; fd = open(filename, O_CREAT | O_RDONLY, 0600);