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.
This commit is contained in:
parent
c731173b49
commit
697d512ea6
5 changed files with 9 additions and 16 deletions
|
|
@ -300,7 +300,7 @@ static int parse_config_file(const char *file) {
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
int lockfd, force = 0;
|
int lockfd;
|
||||||
struct lockfile lock = LOCKFILE_INIT;
|
struct lockfile lock = LOCKFILE_INIT;
|
||||||
char filename[4096];
|
char filename[4096];
|
||||||
|
|
||||||
|
|
@ -319,11 +319,7 @@ int main(int argc, char **argv) {
|
||||||
snprintf(filename, sizeof(filename), "%s/%s",
|
snprintf(filename, sizeof(filename), "%s/%s",
|
||||||
env_get_dir(), "config");
|
env_get_dir(), "config");
|
||||||
|
|
||||||
/* Remove lockfile if forced */
|
lockfd = hold_lock(&lock, filename);
|
||||||
if (argc > 1 && !strcmp(argv[1], "-f"))
|
|
||||||
force = 1;
|
|
||||||
|
|
||||||
lockfd = hold_lock(&lock, filename, force);
|
|
||||||
if (lockfd < 0)
|
if (lockfd < 0)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
|
|
||||||
2
dlhist.c
2
dlhist.c
|
|
@ -245,7 +245,7 @@ int dlhist_open() {
|
||||||
"%s/%s", env_get_dir(), STORAGE_FILE);
|
"%s/%s", env_get_dir(), STORAGE_FILE);
|
||||||
|
|
||||||
/* try lockin the file */
|
/* try lockin the file */
|
||||||
if (hold_lock(&lock, filename, 0) < 0)
|
if (hold_lock(&lock, filename) < 0)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
fd = open(filename, O_CREAT | O_RDONLY, 0600);
|
fd = open(filename, O_CREAT | O_RDONLY, 0600);
|
||||||
|
|
|
||||||
|
|
@ -109,13 +109,10 @@ static double get_lock_time(const char *path) {
|
||||||
return difftime(time(NULL), st.st_ctime);
|
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;
|
int fd, mask = O_WRONLY | O_TRUNC | O_CREAT | O_EXCL;
|
||||||
|
|
||||||
if (force)
|
|
||||||
mask &= ~O_EXCL;
|
|
||||||
|
|
||||||
fd = open(path, mask, 0600);
|
fd = open(path, mask, 0600);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
/* Force open if lockfile exists
|
/* Force open if lockfile exists
|
||||||
|
|
@ -130,7 +127,7 @@ static int open_lock(const char *path, int force) {
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
int hold_lock(struct lockfile *lock, const char *filename, int force) {
|
int hold_lock(struct lockfile *lock, const char *filename) {
|
||||||
|
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
|
|
@ -143,7 +140,7 @@ int hold_lock(struct lockfile *lock, const char *filename, int force) {
|
||||||
if (rc > sizeof(lock->name))
|
if (rc > sizeof(lock->name))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
lock->fd = open_lock(lock->name, force);
|
lock->fd = open_lock(lock->name);
|
||||||
if (lock->fd < 0) {
|
if (lock->fd < 0) {
|
||||||
if (errno == EEXIST)
|
if (errno == EEXIST)
|
||||||
return error("'%s' is locked", filename);
|
return error("'%s' is locked", filename);
|
||||||
|
|
|
||||||
|
|
@ -33,10 +33,10 @@ struct lockfile {
|
||||||
|
|
||||||
#define is_locked(x) ((x)->fd >= 0)
|
#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 commit_lock(struct lockfile *lock);
|
||||||
|
|
||||||
int release_lock(struct lockfile *lock);
|
int release_lock(struct lockfile *lock);
|
||||||
|
|
||||||
#endif /* LOCKFILE_H */
|
#endif /* LOCKFILE_H */
|
||||||
|
|
|
||||||
|
|
@ -163,7 +163,7 @@ int proc_cache_open() {
|
||||||
"%s/%s", env_get_dir(), STORAGE_FILE);
|
"%s/%s", env_get_dir(), STORAGE_FILE);
|
||||||
|
|
||||||
/* try lockin the file */
|
/* try lockin the file */
|
||||||
if (hold_lock(&lock, filename, 0) < 0)
|
if (hold_lock(&lock, filename) < 0)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
fd = open(filename, O_CREAT | O_RDONLY, 0600);
|
fd = open(filename, O_CREAT | O_RDONLY, 0600);
|
||||||
|
|
|
||||||
Reference in a new issue