diff --git a/service/dconf-rebuilder.c b/service/dconf-rebuilder.c
index d7f4cb3..96ffad9 100644
--- a/service/dconf-rebuilder.c
+++ b/service/dconf-rebuilder.c
@@ -22,6 +22,8 @@
 #include "dconf-rebuilder.h"
 
 #include <string.h>
+#include <fcntl.h>
+#include <errno.h>
 
 #include "gvdb-reader.h"
 #include "gvdb-builder.h"
@@ -191,9 +193,33 @@ dconf_rebuilder_rebuild (const gchar  *filename,
                                 0, keys, values, n_items };
   gboolean success;
   GvdbTable *old;
+  struct flock lock;
+  int          lock_fd = -1;
 
   state.table = gvdb_hash_table_new (NULL, NULL);
 
+  if ((lock_fd = open (filename, O_WRONLY | O_SYNC)) != -1)
+    {
+      lock.l_whence = SEEK_SET;
+      lock.l_start = 0;
+      lock.l_len = 0;
+      lock.l_type = F_WRLCK;
+
+      /* Create a lock on the file 'filename' */
+      if (fcntl (lock_fd, F_SETLKW, &lock) == -1)
+        {
+          g_warning ("Can not get lock on '%s': %s",
+                     filename,
+                     g_strerror (errno));
+        }
+    }
+  else
+    {
+      g_warning ("Can not open '%s': %s",
+                 filename,
+                 g_strerror (errno));
+    }
+
   if ((old = gvdb_table_new (filename, FALSE, NULL)))
     {
       gvdb_table_walk (old, "/",
@@ -208,6 +234,12 @@ dconf_rebuilder_rebuild (const gchar  *filename,
     dconf_rebuilder_put_item (&state);
 
   success = gvdb_table_write_contents (state.table, filename, FALSE, error);
+
+  if (lock_fd != -1)
+    {
+      close (lock_fd);
+    }
+
   g_hash_table_unref (state.table);
 
   return success;
