Archived
1
0
Fork 0

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:
Henrik Hautakoski 2013-10-17 15:57:42 +02:00
parent c731173b49
commit 697d512ea6
5 changed files with 9 additions and 16 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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);

View file

@ -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 */
#endif /* LOCKFILE_H */

View file

@ -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);