Use flock instead of fcntl for file locking (#235).
authorDaniel Carl <danielcarl@gmx.de>
Wed, 21 Oct 2015 17:46:50 +0000 (19:46 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Wed, 21 Oct 2015 17:49:16 +0000 (19:49 +0200)
src/cookiejar.c
src/history.c
src/hsts.c
src/main.h
src/util.c

index 23ea525..a16392f 100644 (file)
@@ -20,6 +20,8 @@
 #include "config.h"
 
 #ifdef FEATURE_COOKIE
+#include <fcntl.h>
+#include <sys/file.h>
 #include "main.h"
 #include "cookiejar.h"
 
@@ -47,7 +49,7 @@ SoupCookieJar *cookiejar_new(const char *file, gboolean ro)
 
 static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie, SoupCookie *new_cookie)
 {
-    FLOCK(COOKIEJAR(self)->lock, F_WRLCK);
+    flock(COOKIEJAR(self)->lock, LOCK_EX);
     SoupDate *expire;
     if (new_cookie) {
     /* session-expire-time handling */
@@ -70,7 +72,7 @@ static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie, SoupC
     }
     }
     SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self, old_cookie, new_cookie);
-    FLOCK(COOKIEJAR(self)->lock, F_UNLCK);
+    flock(COOKIEJAR(self)->lock, LOCK_UN);
 }
 
 static void cookiejar_class_init(CookieJarClass *klass)
@@ -96,8 +98,8 @@ static void cookiejar_init(CookieJar *self)
 static void cookiejar_set_property(GObject *self, guint prop_id, const
     GValue *value, GParamSpec *pspec)
 {
-    FLOCK(COOKIEJAR(self)->lock, F_RDLCK);
+    flock(COOKIEJAR(self)->lock, LOCK_SH);
     G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id, value, pspec);
-    FLOCK(COOKIEJAR(self)->lock, F_UNLCK);
+    flock(COOKIEJAR(self)->lock, LOCK_UN);
 }
 #endif
index 0c4eef0..ceeb5e0 100644 (file)
@@ -18,6 +18,8 @@
  */
 
 #include "config.h"
+#include <fcntl.h>
+#include <sys/file.h>
 #include "main.h"
 #include "history.h"
 #include "util.h"
@@ -216,7 +218,7 @@ static void write_to_file(GList *list, const char *file)
 {
     FILE *f;
     if ((f = fopen(file, "w"))) {
-        FLOCK(fileno(f), F_WRLCK);
+        flock(fileno(f), LOCK_EX);
 
         /* overwrite the history file with new unique history items */
         for (GList *link = list; link; link = link->next) {
@@ -228,7 +230,7 @@ static void write_to_file(GList *list, const char *file)
             }
         }
 
-        FLOCK(fileno(f), F_UNLCK);
+        flock(fileno(f), LOCK_UN);
         fclose(f);
     }
 }
index 75fad95..00b25a1 100644 (file)
@@ -22,6 +22,8 @@
 #include "hsts.h"
 #include "util.h"
 #include "main.h"
+#include <fcntl.h>
+#include <sys/file.h>
 #include <string.h>
 #include <glib-object.h>
 #include <libsoup/soup.h>
@@ -440,7 +442,7 @@ static void save_entries(HSTSProvider *provider, const char *file)
     HSTSProviderPrivate *priv = HSTS_PROVIDER_GET_PRIVATE(provider);
 
     if ((f = fopen(file, "w"))) {
-        FLOCK(fileno(f), F_WRLCK);
+        flock(fileno(f), LOCK_EX);
 
         g_hash_table_iter_init(&iter, priv->whitelist);
         while (g_hash_table_iter_next (&iter, (gpointer)&host, (gpointer)&entry)) {
@@ -449,7 +451,7 @@ static void save_entries(HSTSProvider *provider, const char *file)
             g_free(date);
         }
 
-        FLOCK(fileno(f), F_UNLCK);
+        flock(fileno(f), LOCK_UN);
         fclose(f);
     }
 }
index 7e4c035..6179928 100644 (file)
 
 #define LENGTH(x) (sizeof x / sizeof x[0])
 
-#define FLOCK(fd, cmd) { \
-    struct flock lock = {.l_type=cmd,.l_start=0,.l_whence=SEEK_SET,.l_len=0}; \
-    fcntl(fd, F_SETLKW, &lock); \
-}
 
 #ifdef DEBUG
 #define PRINT_DEBUG(...) { \
index dcf2694..21a5709 100644 (file)
@@ -18,6 +18,8 @@
  */
 
 #include "config.h"
+#include <fcntl.h>
+#include <sys/file.h>
 #include <stdio.h>
 #include <pwd.h>
 #include <ctype.h>
@@ -222,13 +224,13 @@ gboolean util_file_append(const char *file, const char *format, ...)
     FILE *f;
 
     if ((f = fopen(file, "a+"))) {
-        FLOCK(fileno(f), F_WRLCK);
+        flock(fileno(f), LOCK_EX);
 
         va_start(args, format);
         vfprintf(f, format, args);
         va_end(args);
 
-        FLOCK(fileno(f), F_UNLCK);
+        flock(fileno(f), LOCK_UN);
         fclose(f);
 
         return true;
@@ -251,7 +253,7 @@ gboolean util_file_prepend(const char *file, const char *format, ...)
 
     content = util_get_file_contents(file, NULL);
     if ((f = fopen(file, "w"))) {
-        FLOCK(fileno(f), F_WRLCK);
+        flock(fileno(f), LOCK_EX);
 
         va_start(args, format);
         /* write new content to the file */
@@ -261,7 +263,7 @@ gboolean util_file_prepend(const char *file, const char *format, ...)
         /* append previous file content */
         fputs(content, f);
 
-        FLOCK(fileno(f), F_UNLCK);
+        flock(fileno(f), LOCK_UN);
         fclose(f);
 
         res = true;