X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Flockfile.c;h=43e55927ea68bf8ecc8308019971e09e5d42f010;hb=bd3ad2c3508aaf7283f3ee041ecd3e5108471b21;hp=db84aebb40cec38dd0bc590b22109104a0556eac;hpb=e368cad8ecf6dbf272b2a3775b2e3e5e2dc6a5cf;p=sliver-openvswitch.git diff --git a/lib/lockfile.c b/lib/lockfile.c index db84aebb4..43e55927e 100644 --- a/lib/lockfile.c +++ b/lib/lockfile.c @@ -1,4 +1,4 @@ - /* Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc. + /* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ #include "coverage.h" #include "hash.h" #include "hmap.h" +#include "ovs-thread.h" #include "timeval.h" #include "util.h" #include "vlog.h" @@ -52,10 +53,14 @@ struct lockfile { * descriptor for a file on which a process holds a lock drops *all* locks on * that file. That means that we can't afford to open a lockfile more than * once. */ -static struct hmap lock_table = HMAP_INITIALIZER(&lock_table); +static struct ovs_mutex lock_table_mutex = OVS_MUTEX_INITIALIZER; +static struct hmap lock_table__ = HMAP_INITIALIZER(&lock_table__); +static struct hmap *const lock_table OVS_GUARDED_BY(lock_table_mutex) + = &lock_table__; static void lockfile_unhash(struct lockfile *); -static int lockfile_try_lock(const char *name, struct lockfile **lockfilep); +static int lockfile_try_lock(const char *name, pid_t *pidp, + struct lockfile **lockfilep); /* Returns the name of the lockfile that would be created for locking a file * named 'filename_'. The caller is responsible for freeing the returned name, @@ -98,21 +103,29 @@ lockfile_lock(const char *file, struct lockfile **lockfilep) * stylized ways such that any number of readers may access a file while it * is being written. */ char *lock_name; + pid_t pid; int error; COVERAGE_INC(lockfile_lock); lock_name = lockfile_name(file); - error = lockfile_try_lock(lock_name, lockfilep); + ovs_mutex_lock(&lock_table_mutex); + error = lockfile_try_lock(lock_name, &pid, lockfilep); + ovs_mutex_unlock(&lock_table_mutex); if (error) { COVERAGE_INC(lockfile_error); if (error == EACCES) { error = EAGAIN; } - VLOG_WARN("%s: failed to lock file: %s", - lock_name, strerror(error)); + if (pid) { + VLOG_WARN("%s: cannot lock file because it is already locked by " + "pid %ld", lock_name, (long int) pid); + } else { + VLOG_WARN("%s: failed to lock file: %s", + lock_name, ovs_strerror(error)); + } } free(lock_name); @@ -125,8 +138,11 @@ void lockfile_unlock(struct lockfile *lockfile) { if (lockfile) { - COVERAGE_INC(lockfile_unlock); + ovs_mutex_lock(&lock_table_mutex); lockfile_unhash(lockfile); + ovs_mutex_unlock(&lock_table_mutex); + + COVERAGE_INC(lockfile_unlock); free(lockfile->name); free(lockfile); } @@ -140,12 +156,14 @@ lockfile_postfork(void) { struct lockfile *lockfile; - HMAP_FOR_EACH (lockfile, hmap_node, &lock_table) { + ovs_mutex_lock(&lock_table_mutex); + HMAP_FOR_EACH (lockfile, hmap_node, lock_table) { if (lockfile->fd >= 0) { VLOG_WARN("%s: child does not inherit lock", lockfile->name); lockfile_unhash(lockfile); } } + ovs_mutex_unlock(&lock_table_mutex); } static uint32_t @@ -156,12 +174,12 @@ lockfile_hash(dev_t device, ino_t inode) } static struct lockfile * -lockfile_find(dev_t device, ino_t inode) +lockfile_find(dev_t device, ino_t inode) OVS_REQUIRES(&lock_table_mutex) { struct lockfile *lockfile; HMAP_FOR_EACH_WITH_HASH (lockfile, hmap_node, - lockfile_hash(device, inode), &lock_table) { + lockfile_hash(device, inode), lock_table) { if (lockfile->device == device && lockfile->inode == inode) { return lockfile; } @@ -170,17 +188,18 @@ lockfile_find(dev_t device, ino_t inode) } static void -lockfile_unhash(struct lockfile *lockfile) +lockfile_unhash(struct lockfile *lockfile) OVS_REQUIRES(&lock_table_mutex) { if (lockfile->fd >= 0) { close(lockfile->fd); lockfile->fd = -1; - hmap_remove(&lock_table, &lockfile->hmap_node); + hmap_remove(lock_table, &lockfile->hmap_node); } } static struct lockfile * lockfile_register(const char *name, dev_t device, ino_t inode, int fd) + OVS_REQUIRES(&lock_table_mutex) { struct lockfile *lockfile; @@ -195,13 +214,14 @@ lockfile_register(const char *name, dev_t device, ino_t inode, int fd) lockfile->device = device; lockfile->inode = inode; lockfile->fd = fd; - hmap_insert(&lock_table, &lockfile->hmap_node, + hmap_insert(lock_table, &lockfile->hmap_node, lockfile_hash(device, inode)); return lockfile; } static int -lockfile_try_lock(const char *name, struct lockfile **lockfilep) +lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep) + OVS_REQUIRES(&lock_table_mutex) { struct flock l; struct stat s; @@ -209,6 +229,7 @@ lockfile_try_lock(const char *name, struct lockfile **lockfilep) int fd; *lockfilep = NULL; + *pidp = 0; /* Check whether we've already got a lock on that file. */ if (!stat(name, &s)) { @@ -217,7 +238,7 @@ lockfile_try_lock(const char *name, struct lockfile **lockfilep) } } else if (errno != ENOENT) { VLOG_WARN("%s: failed to stat lock file: %s", - name, strerror(errno)); + name, ovs_strerror(errno)); return errno; } @@ -225,13 +246,14 @@ lockfile_try_lock(const char *name, struct lockfile **lockfilep) fd = open(name, O_RDWR | O_CREAT, 0600); if (fd < 0) { VLOG_WARN("%s: failed to open lock file: %s", - name, strerror(errno)); + name, ovs_strerror(errno)); return errno; } /* Get the inode and device number for the lock table. */ if (fstat(fd, &s)) { - VLOG_ERR("%s: failed to fstat lock file: %s", name, strerror(errno)); + VLOG_ERR("%s: failed to fstat lock file: %s", + name, ovs_strerror(errno)); close(fd); return errno; } @@ -243,13 +265,14 @@ lockfile_try_lock(const char *name, struct lockfile **lockfilep) l.l_start = 0; l.l_len = 0; - time_disable_restart(); error = fcntl(fd, F_SETLK, &l) == -1 ? errno : 0; - time_enable_restart(); if (!error) { *lockfilep = lockfile_register(name, s.st_dev, s.st_ino, fd); } else { + if (!fcntl(fd, F_GETLK, &l) && l.l_type != F_UNLCK) { + *pidp = l.l_pid; + } close(fd); } return error;