From 805e51eff86a1c249d80728dcd3f8d70313bc35e Mon Sep 17 00:00:00 2001 From: Kornel Benko Date: Wed, 20 Nov 2013 19:40:32 +0100 Subject: [PATCH] Implement file locking and apply to configure Functions for file locking are added. They are used for ensuring that for specified userdir only one LyX process runs configure. --- configure.ac | 2 +- development/cmake/ConfigureChecks.cmake | 2 +- src/LyX.cpp | 11 +++++++++-- src/support/filetools.cpp | 22 ++++++++++++++++++++++ src/support/filetools.h | 2 ++ 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index b572f4980c..1fe6d9d691 100644 --- a/configure.ac +++ b/configure.ac @@ -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 diff --git a/development/cmake/ConfigureChecks.cmake b/development/cmake/ConfigureChecks.cmake index b6b0b1476b..71291a53a5 100644 --- a/development/cmake/ConfigureChecks.cmake +++ b/development/cmake/ConfigureChecks.cmake @@ -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) diff --git a/src/LyX.cpp b/src/LyX.cpp index 09fcfd5398..b777e55f4e 100644 --- a/src/LyX.cpp +++ b/src/LyX.cpp @@ -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 diff --git a/src/support/filetools.cpp b/src/support/filetools.cpp index f991455a27..387045f974 100644 --- a/src/support/filetools.cpp +++ b/src/support/filetools.cpp @@ -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 diff --git a/src/support/filetools.h b/src/support/filetools.h index 92c6eb57e7..58d11542d4 100644 --- a/src/support/filetools.h +++ b/src/support/filetools.h @@ -292,6 +292,8 @@ typedef std::pair 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