]> git.lyx.org Git - lyx.git/commitdiff
Implement file locking and apply to configure
authorKornel Benko <kornel@lyx.org>
Wed, 20 Nov 2013 18:40:32 +0000 (19:40 +0100)
committerKornel Benko <kornel@lyx.org>
Wed, 20 Nov 2013 18:40:32 +0000 (19:40 +0100)
Functions for file locking are added. They are used for ensuring that
for specified userdir only one LyX process runs configure.

configure.ac
development/cmake/ConfigureChecks.cmake
src/LyX.cpp
src/support/filetools.cpp
src/support/filetools.h

index b572f4980c6d882d0bc22c2c8929f08b54155dd4..1fe6d9d69155a5b6d77c33cc9c97f1c7dfc87849 100644 (file)
@@ -174,7 +174,7 @@ fi
 
 LYX_CHECK_DEF(PATH_MAX, limits.h, [int n = PATH_MAX;])
 
-AC_CHECK_FUNCS(chmod close _close fork getpid _getpid lstat mkfifo open _open pclose _pclose popen _popen readlink putenv setenv strerror unsetenv)
+AC_CHECK_FUNCS(chmod close _close fork getpid _getpid lockf lstat mkfifo open _open pclose _pclose popen _popen readlink putenv setenv strerror unsetenv)
 # Check the form of mkdir()
 AC_FUNC_MKDIR
 AC_FUNC_SELECT_ARGTYPES
index b6b0b1476bd81dd57b3559dca319df24663e9c01..71291a53a5e46e989601534505e90d10b6c4979e 100644 (file)
@@ -40,7 +40,7 @@ configure_file(${TOP_BINARY_DIR}/configIncludes.h.cmake ${TOP_BINARY_DIR}/config
 set(Function_Defines)
 foreach(_f alloca __argz_count __argz_next __argz_stringify
        chmod close _close dcgettext fcntl fork __fsetlocking
-       getcwd getegid getgid getpid _getpid gettext getuid lstat mempcpy mkdir _mkdir
+       getcwd getegid getgid getpid _getpid gettext getuid lstat lockf mempcpy mkdir _mkdir
        mkfifo open _open pclose _pclose popen _popen putenv readlink
        setenv setlocale strcasecmp stpcpy strdup strerror strtoul tsearch unsetenv wcslen)
   string(TOUPPER ${_f} _UF)
index 09fcfd5398e42ba1c6f9d0cc9f3de52dc27a4182..b777e55f4e45c289029333d1c4499a5bd047cb59 100644 (file)
@@ -754,8 +754,15 @@ bool LyX::init()
                prependEnvPath("PATH", replaceEnvironmentPath(lyxrc.path_prefix));
 
        // Check that user LyX directory is ok.
-       if (queryUserLyXDir(package().explicit_user_support()))
-               reconfigureUserLyXDir();
+       {
+               string const lock_file = package().user_support().absFileName() + ".lyx_configure_lock";
+               int fd = fileLock(lock_file.c_str());
+
+               if (queryUserLyXDir(package().explicit_user_support())) {
+                       reconfigureUserLyXDir();
+               }
+               fileUnlock(fd, lock_file.c_str());
+       }
 
        if (!use_gui) {
                // No need for a splash when there is no GUI
index f991455a271f3d0dde199257deb7a08e83ae3038..387045f974515ae1a0b8f937f18cbb3e10b5886a 100644 (file)
@@ -1064,6 +1064,28 @@ bool prefs2prefs(FileName const & filename, FileName const & tempfile, bool lfun
        return true;
 }
 
+int fileLock(const char * lock_file)
+{
+       int fd = -1;
+#if defined(HAVE_LOCKF)
+       fd = open(lock_file, O_CREAT|O_APPEND|O_SYNC|O_RDWR, 0666);
+       if (lockf(fd, F_LOCK, 0) != 0) {
+               close(fd);
+               return(-1);
+       }
+#endif
+       return(fd);
+}
+
+void fileUnlock(int fd, const char * lock_file)
+{
+#if defined(HAVE_LOCKF)
+       if ( fd >= 0) {
+               (void) lockf(fd, F_ULOCK, 0);
+               close(fd);
+       }
+#endif
+}
 
 } //namespace support
 } // namespace lyx
index 92c6eb57e7ec8b50edac51e06051cfb282e40067..58d11542d47505b317e0bcb473ef7e504a15da76 100644 (file)
@@ -292,6 +292,8 @@ typedef std::pair<int, std::string> cmd_ret;
 
 cmd_ret const runCommand(std::string const & cmd);
 
+int fileLock(const char * lock_file);
+void fileUnlock(int fd, const char * lock_file);
 
 } // namespace support
 } // namespace lyx