]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
lxclock: use thread-safe *_OFD_* fcntl() locks
authorChristian Brauner <christian.brauner@ubuntu.com>
Fri, 30 Mar 2018 04:54:40 +0000 (06:54 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Thu, 23 Aug 2018 20:51:28 +0000 (22:51 +0200)
If they aren't available fallback to BSD flock()s.

Closes #2245.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/lxclock.c
src/lxc/lxclock.h

index b5ecdcb5b53c6da9b7a2e161d1d86f8ea4a7b629..31dbd1c13aec01e08d0bc7c24b626aab71f71365 100644 (file)
@@ -54,8 +54,8 @@ static inline void dump_stacktrace(void)
        size = backtrace(array, MAX_STACKDEPTH);
        strings = backtrace_symbols(array, size);
 
-       // Using fprintf here as our logging module is not thread safe
-       fprintf(stderr, "\tObtained %zu stack frames.\n", size);
+       /* Using fprintf here as our logging module is not thread safe. */
+       fprintf(stderr, "\tObtained %zu stack frames\n", size);
 
        for (i = 0; i < size; i++)
                fprintf(stderr, "\t\t%s\n", strings[i]);
@@ -195,7 +195,7 @@ int lxclock(struct lxc_lock *l, int timeout)
        case LXC_LOCK_ANON_SEM:
                if (!timeout) {
                        ret = sem_wait(l->u.sem);
-                       if (ret == -1)
+                       if (ret < 0)
                                saved_errno = errno;
                } else {
                        struct timespec ts;
@@ -205,7 +205,7 @@ int lxclock(struct lxc_lock *l, int timeout)
                        }
                        ts.tv_sec += timeout;
                        ret = sem_timedwait(l->u.sem, &ts);
-                       if (ret == -1)
+                       if (ret < 0)
                                saved_errno = errno;
                }
                break;
@@ -220,21 +220,22 @@ int lxclock(struct lxc_lock *l, int timeout)
                        goto out;
                }
                if (l->u.f.fd == -1) {
-                       l->u.f.fd = open(l->u.f.fname, O_RDWR|O_CREAT,
-                                       S_IWUSR | S_IRUSR);
+                       l->u.f.fd = open(l->u.f.fname, O_CREAT | O_RDWR | O_NOFOLLOW | O_CLOEXEC | O_NOCTTY, S_IWUSR | S_IRUSR);
                        if (l->u.f.fd == -1) {
                                ERROR("Error opening %s", l->u.f.fname);
                                saved_errno = errno;
                                goto out;
                        }
                }
+               memset(&lk, 0, sizeof(struct flock));
                lk.l_type = F_WRLCK;
                lk.l_whence = SEEK_SET;
-               lk.l_start = 0;
-               lk.l_len = 0;
-               ret = fcntl(l->u.f.fd, F_SETLKW, &lk);
-               if (ret == -1)
+               ret = fcntl(l->u.f.fd, F_OFD_SETLKW, &lk);
+               if (ret < 0) {
+                       if (errno == EINVAL)
+                               ret = flock(l->u.f.fd, LOCK_EX);
                        saved_errno = errno;
+               }
                break;
        }
 
@@ -259,13 +260,15 @@ int lxcunlock(struct lxc_lock *l)
                break;
        case LXC_LOCK_FLOCK:
                if (l->u.f.fd != -1) {
+                       memset(&lk, 0, sizeof(struct flock));
                        lk.l_type = F_UNLCK;
                        lk.l_whence = SEEK_SET;
-                       lk.l_start = 0;
-                       lk.l_len = 0;
-                       ret = fcntl(l->u.f.fd, F_SETLK, &lk);
-                       if (ret < 0)
+                       ret = fcntl(l->u.f.fd, F_OFD_SETLK, &lk);
+                       if (ret < 0) {
+                               if (errno == EINVAL)
+                                       ret = flock(l->u.f.fd, LOCK_EX | LOCK_NB);
                                saved_errno = errno;
+                       }
                        close(l->u.f.fd);
                        l->u.f.fd = -1;
                } else
index e097216eeae801d8153ff3cae4f9c81ef318a86b..32cb1dd679fd4761000177e421f852ccd0856082 100644 (file)
 #ifndef __LXC_LXCLOCK_H
 #define __LXC_LXCLOCK_H
 
-#include <fcntl.h>           /* For O_* constants */
-#include <sys/stat.h>        /* For mode constants */
-#include <sys/file.h>
+#include <fcntl.h>
 #include <semaphore.h>
 #include <string.h>
+#include <sys/stat.h>
+#include <sys/file.h>
 #include <time.h>
+#include <unistd.h>
+
+#ifndef F_OFD_GETLK
+#define F_OFD_GETLK    36
+#endif
+
+#ifndef F_OFD_SETLK
+#define F_OFD_SETLK    37
+#endif
+
+#ifndef F_OFD_SETLKW
+#define F_OFD_SETLKW   38
+#endif
 
 #define LXC_LOCK_ANON_SEM 1 /*!< Anonymous semaphore lock */
 #define LXC_LOCK_FLOCK    2 /*!< flock(2) lock */