summaryrefslogtreecommitdiff
path: root/lib/lockfile.c
diff options
context:
space:
mode:
authorBen Pfaff <blp@nicira.com>2012-07-26 14:36:24 -0700
committerBen Pfaff <blp@nicira.com>2012-08-01 10:05:23 -0700
commit77c513a44ab85264aed7c38387ef221ad30e461c (patch)
tree2b733b5bcdbe2bfc1b3c492bd8e4c511d2fcac85 /lib/lockfile.c
parentb322930908706d7db5cb5d1658c5c64f2642300a (diff)
downloadopenvswitch-77c513a44ab85264aed7c38387ef221ad30e461c.tar.gz
lockfile: Fix hang locking through a dangling symlink.
open() with O_CREAT|O_EXCL yields EEXIST if the file being opened is a symlink. lockfile_try_lock() interpreted that error code to mean that some other process had created the lock file in the meantime, so it went around its loop again, which found out the same thing, which led to a hang. This commit fixes the problem by dropping O_EXCL. I don't see any reason that it's actually necessary. That means that the loop itself is unnecessary, so this commit drops that too. Debian bug #681880. CC: 681880@bugs.debian.org Reported-by: Bastian Blank <waldi@debian.org> Signed-off-by: Ben Pfaff <blp@nicira.com> Reviewed-by: Simon Horman <horms@verge.net.au>
Diffstat (limited to 'lib/lockfile.c')
-rw-r--r--lib/lockfile.c50
1 files changed, 16 insertions, 34 deletions
diff --git a/lib/lockfile.c b/lib/lockfile.c
index e0f632871..c55be6616 100644
--- a/lib/lockfile.c
+++ b/lib/lockfile.c
@@ -1,4 +1,4 @@
- /* Copyright (c) 2008, 2009, 2010, 2011 Nicira, Inc.
+ /* Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -221,41 +221,23 @@ lockfile_try_lock(const char *name, bool block, struct lockfile **lockfilep)
*lockfilep = NULL;
- /* Open the lock file, first creating it if necessary. */
- for (;;) {
- /* Check whether we've already got a lock on that file. */
- if (!stat(name, &s)) {
- if (lockfile_find(s.st_dev, s.st_ino)) {
- return EDEADLK;
- }
- } else if (errno != ENOENT) {
- VLOG_WARN("%s: failed to stat lock file: %s",
- name, strerror(errno));
- return errno;
- }
-
- /* Try to open an existing lock file. */
- fd = open(name, O_RDWR);
- if (fd >= 0) {
- break;
- } else if (errno != ENOENT) {
- VLOG_WARN("%s: failed to open lock file: %s",
- name, strerror(errno));
- return errno;
- }
-
- /* Try to create a new lock file. */
- VLOG_INFO("%s: lock file does not exist, creating", name);
- fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
- if (fd >= 0) {
- break;
- } else if (errno != EEXIST) {
- VLOG_WARN("%s: failed to create lock file: %s",
- name, strerror(errno));
- return errno;
+ /* Check whether we've already got a lock on that file. */
+ if (!stat(name, &s)) {
+ if (lockfile_find(s.st_dev, s.st_ino)) {
+ return EDEADLK;
}
+ } else if (errno != ENOENT) {
+ VLOG_WARN("%s: failed to stat lock file: %s",
+ name, strerror(errno));
+ return errno;
+ }
- /* Someone else created the lock file. Try again. */
+ /* Open the lock file. */
+ fd = open(name, O_RDWR | O_CREAT, 0600);
+ if (fd < 0) {
+ VLOG_WARN("%s: failed to open lock file: %s",
+ name, strerror(errno));
+ return errno;
}
/* Get the inode and device number for the lock table. */