]> git.pond.sub.org Git - empserver/commitdiff
(ef_open): Lock the file. This prevents two programs (servers in
authorMarkus Armbruster <armbru@pond.sub.org>
Wed, 2 Aug 2006 18:05:04 +0000 (18:05 +0000)
committerMarkus Armbruster <armbru@pond.sub.org>
Wed, 2 Aug 2006 18:05:04 +0000 (18:05 +0000)
particular) running on the same game state.  fairland does not use
ef_open() at this time, and thus ignores the lock.

src/lib/common/file.c

index 03f489eb195eef6ba60b07544ea4150fafcda755..84e17c8e891e0053dfc069bbecbeeaed5805c6c8 100644 (file)
@@ -66,6 +66,7 @@ int
 ef_open(int type, int how)
 {
     struct empfile *ep;
+    struct flock lock;
     int oflags, fd, fsiz, size;
 
     if (ef_check(type) < 0)
@@ -90,6 +91,14 @@ ef_open(int type, int how)
        return 0;
     }
 
+    lock.l_type = how & EFF_RDONLY ? F_RDLCK : F_WRLCK;
+    lock.l_whence = SEEK_SET;
+    lock.l_start = lock.l_len = 0;
+    if (fcntl(fd, F_SETLK, &lock) == -1) {
+       logerror("Can't lock %s (%s)", ep->file, strerror(errno));
+       return 0;
+    }
+
     /* get file size */
     fsiz = fsize(fd);
     if (fsiz % ep->size) {