From d4cc6fb5e0f51f0272fb108c6e0caf6f68caa128 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sun, 24 Nov 2013 00:27:10 +0100 Subject: [PATCH] Improve compatibility with Solaris. Fixes #5129 --- lib/base/application.cpp | 9 ++++++++- lib/base/unix.h | 1 + lib/base/unixsocket.cpp | 24 ++++++++++++------------ 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/base/application.cpp b/lib/base/application.cpp index 354c7aebb..548b7ade7 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -525,7 +525,14 @@ void Application::UpdatePidFile(const String& filename) Utility::SetCloExec(fd); - if (flock(fd, LOCK_EX | LOCK_NB) < 0) { + struct flock lock; + + lock.l_start = 0; + lock.l_len = 0; + lock.l_type = F_WRLCK; + lock.l_whence = SEEK_SET; + + if (fcntl(fd, F_SETLK, &lock) < 0) { Log(LogCritical, "base", "Could not lock PID file. Make sure that only one instance of the application is running."); _exit(EXIT_FAILURE); diff --git a/lib/base/unix.h b/lib/base/unix.h index ee6022839..7c4d0ed61 100644 --- a/lib/base/unix.h +++ b/lib/base/unix.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include diff --git a/lib/base/unixsocket.cpp b/lib/base/unixsocket.cpp index 8623a09a0..e18c31fe8 100644 --- a/lib/base/unixsocket.cpp +++ b/lib/base/unixsocket.cpp @@ -40,13 +40,13 @@ void UnixSocket::Bind(const String& path) { unlink(path.CStr()); - sockaddr_un sun; - memset(&sun, 0, sizeof(sun)); - sun.sun_family = AF_UNIX; - strncpy(sun.sun_path, path.CStr(), sizeof(sun.sun_path)); - sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; + sockaddr_un s_un; + memset(&s_un, 0, sizeof(s_un)); + s_un.sun_family = AF_UNIX; + strncpy(s_un.sun_path, path.CStr(), sizeof(s_un.sun_path)); + s_un.sun_path[sizeof(s_un.sun_path) - 1] = '\0'; - if (bind(GetFD(), (sockaddr *)&sun, SUN_LEN(&sun)) < 0) { + if (bind(GetFD(), (sockaddr *)&s_un, SUN_LEN(&s_un)) < 0) { BOOST_THROW_EXCEPTION(posix_error() << boost::errinfo_api_function("bind") << boost::errinfo_errno(errno)); @@ -55,13 +55,13 @@ void UnixSocket::Bind(const String& path) void UnixSocket::Connect(const String& path) { - sockaddr_un sun; - memset(&sun, 0, sizeof(sun)); - sun.sun_family = AF_UNIX; - strncpy(sun.sun_path, path.CStr(), sizeof(sun.sun_path)); - sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; + sockaddr_un s_un; + memset(&s_un, 0, sizeof(s_un)); + s_un.sun_family = AF_UNIX; + strncpy(s_un.sun_path, path.CStr(), sizeof(s_un.sun_path)); + s_un.sun_path[sizeof(s_un.sun_path) - 1] = '\0'; - if (connect(GetFD(), (sockaddr *)&sun, SUN_LEN(&sun)) < 0 && errno != EINPROGRESS) { + if (connect(GetFD(), (sockaddr *)&s_un, SUN_LEN(&s_un)) < 0 && errno != EINPROGRESS) { BOOST_THROW_EXCEPTION(posix_error() << boost::errinfo_api_function("connect") << boost::errinfo_errno(errno));