diff options
author | Martin Ebourne <martin@ebourne.me.uk> | 2005-11-30 23:14:40 +0000 |
---|---|---|
committer | Martin Ebourne <martin@ebourne.me.uk> | 2005-11-30 23:14:40 +0000 |
commit | 01ca97865fa9d6ed1a967e970927265f1c348289 (patch) | |
tree | 5e7e1f70b38b156994c3802ba3e77fbf288fe937 /lib | |
parent | 99f8ce096bc5569adbfea1911dbcda24c28d8d8b (diff) |
Merged martin/solaris at r9 to trunk
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/common/BoxPlatform.h | 49 | ||||
-rwxr-xr-x | lib/common/NamedLock.cpp | 30 | ||||
-rwxr-xr-x | lib/raidfile/RaidFileRead.cpp | 30 | ||||
-rwxr-xr-x | lib/raidfile/RaidFileUtil.cpp | 8 | ||||
-rwxr-xr-x | lib/raidfile/RaidFileWrite.cpp | 23 | ||||
-rwxr-xr-x | lib/server/Daemon.cpp | 14 | ||||
-rwxr-xr-x | lib/server/Protocol.cpp | 31 | ||||
-rwxr-xr-x | lib/server/SocketStreamTLS.cpp | 17 |
8 files changed, 157 insertions, 45 deletions
diff --git a/lib/common/BoxPlatform.h b/lib/common/BoxPlatform.h index 716366d2..3b75f992 100755 --- a/lib/common/BoxPlatform.h +++ b/lib/common/BoxPlatform.h @@ -133,7 +133,7 @@ #define PLATFORM_stat_SHORT_mtime #define PLATFORM_stat_NO_st_flags #define PLATFORM_USES_MTAB_FILE_FOR_MOUNTS - #define PLATFORM_open_NO_O_EXLOCK + #define PLATFORM_open_USE_flock #define PLATFORM_sockaddr_NO_len #define PLATFORM_RANDOM_DEVICE "/dev/urandom" @@ -145,6 +145,41 @@ #endif // PLATFORM_LINUX +#ifdef PLATFORM_SUNOS + + #include <sys/types.h> + + // for ntohl etc... + #include <netinet/in.h> + + // types 'missing' + typedef uint8_t u_int8_t; +// typedef signed char int8_t; + typedef uint32_t u_int32_t; + typedef uint16_t u_int16_t; + typedef uint64_t u_int64_t; + + // not defined in Solaris, a BSD thing + #define INFTIM -1 + + //#define LLONG_MAX 9223372036854775807LL + //#define LLONG_MIN (-LLONG_MAX - 1LL) + + #define PLATFORM_STATIC_TEMP_DIRECTORY_NAME "/tmp" + + #define PLATFORM_BERKELEY_DB_NOT_SUPPORTED + #define PLATFORM_KQUEUE_NOT_SUPPORTED // This may be in Solaris 10 + #define PLATFORM_dirent_BROKEN_d_type // Well, no d_type at all actually + #define PLATFORM_stat_SHORT_mtime + #define PLATFORM_stat_NO_st_flags + #define PLATFORM_USES_MTAB_FILE_FOR_MOUNTS + #define PLATFORM_open_USE_fcntl + #define PLATFORM_sockaddr_NO_len + + #define PLATFORM_RANDOM_DEVICE "/dev/urandom" + +#endif // PLATFORM_SUNOS + #ifdef PLATFORM_CYGWIN #define PLATFORM_BERKELEY_DB_NOT_SUPPORTED @@ -154,7 +189,7 @@ #define PLATFORM_stat_SHORT_mtime #define PLATFORM_stat_NO_st_flags #define PLATFORM_USES_MTAB_FILE_FOR_MOUNTS - #define PLATFORM_open_NO_O_EXLOCK + #define PLATFORM_open_USE_flock #define PLATFORM_sockaddr_NO_len #define PLATFORM_NO_BUILT_IN_SWAP64 @@ -192,6 +227,16 @@ #endif // PLATFORM_CYGWIN +// Check the processor type +#ifdef PLATFORM_SPARC + #define PLATFORM_ALIGN_INT +#endif + +#ifdef PLATFORM_ARM + #define PLATFORM_ALIGN_INT +#endif + + // Find out if credentials on UNIX sockets can be obtained #ifndef PLATFORM_HAVE_getpeereid #ifndef PLATFORM_HAVE_getsockopt_SO_PEERCRED diff --git a/lib/common/NamedLock.cpp b/lib/common/NamedLock.cpp index 1f6e038a..8953db89 100755 --- a/lib/common/NamedLock.cpp +++ b/lib/common/NamedLock.cpp @@ -12,12 +12,9 @@ #include <fcntl.h> #include <errno.h> #include <unistd.h> -#ifdef PLATFORM_LINUX +#ifdef PLATFORM_open_USE_flock #include <sys/file.h> -#endif // PLATFORM_LINUX -#ifdef PLATFORM_CYGWIN - #include <sys/file.h> -#endif // PLATFORM_CYGWIN +#endif // PLATFORM_open_USE_flock #include "NamedLock.h" #include "CommonException.h" @@ -71,12 +68,14 @@ bool NamedLock::TryAndGetLock(const char *Filename, int mode) } // See if the lock can be got -#ifdef PLATFORM_open_NO_O_EXLOCK +#if defined(PLATFORM_open_USE_flock) || defined(PLATFORM_open_USE_fcntl) int fd = ::open(Filename, O_WRONLY | O_CREAT | O_TRUNC, mode); if(fd == -1) { THROW_EXCEPTION(CommonException, OSFileError) } + +#ifdef PLATFORM_open_USE_flock if(::flock(fd, LOCK_EX | LOCK_NB) != 0) { ::close(fd); @@ -89,6 +88,25 @@ bool NamedLock::TryAndGetLock(const char *Filename, int mode) THROW_EXCEPTION(CommonException, OSFileError) } } +#else + struct flock desc; + desc.l_type = F_WRLCK; + desc.l_whence = SEEK_SET; + desc.l_start = 0; + desc.l_len = 0; + if(::fcntl(fd, F_SETLK, &desc) != 0) + { + ::close(fd); + if(errno == EAGAIN) + { + return false; + } + else + { + THROW_EXCEPTION(CommonException, OSFileError) + } + } +#endif // Success mFileDescriptor = fd; diff --git a/lib/raidfile/RaidFileRead.cpp b/lib/raidfile/RaidFileRead.cpp index fed22ac0..6314ba90 100755 --- a/lib/raidfile/RaidFileRead.cpp +++ b/lib/raidfile/RaidFileRead.cpp @@ -317,11 +317,7 @@ RaidFileRead_Raid::RaidFileRead_Raid(int SetNumber, const std::string &Filename, mEOF(false) { // Make sure size of the IOStream::pos_type matches the pos_type used -#ifdef PLATFORM_LINUX ASSERT(sizeof(pos_type) >= sizeof(off_t)); -#else - ASSERT(sizeof(pos_type) == sizeof(off_t)); -#endif // Sanity check handles if(mStripe1Handle != -1 && mStripe2Handle != -1) @@ -1546,18 +1542,22 @@ bool RaidFileRead::ReadDirectoryContents(int SetNumber, const std::string &rDirN continue; } + // Entry... + std::string name; + unsigned int countToAdd = 1; + // stat the file to find out what type it is -/* struct stat st; +#ifdef PLATFORM_SUNOS + struct stat st; std::string fullName(dn + DIRECTORY_SEPARATOR + en->d_name); - if(::stat(fullName.c_str(), &st) != 0) + if(::lstat(fullName.c_str(), &st) != 0) { THROW_EXCEPTION(RaidFileException, OSError) - }*/ - - // Entry... - std::string name; - unsigned int countToAdd = 1; - if(DirReadType == DirReadType_FilesOnly && en->d_type == DT_REG) // (st.st_mode & S_IFDIR) == 0) + } + if(DirReadType == DirReadType_FilesOnly && (st.st_mode & S_IFDIR) == 0) +#else + if(DirReadType == DirReadType_FilesOnly && en->d_type == DT_REG) +#endif { // File. Complex, need to check the extension int dot = -1; @@ -1585,7 +1585,11 @@ bool RaidFileRead::ReadDirectoryContents(int SetNumber, const std::string &rDirN } } } - if(DirReadType == DirReadType_DirsOnly && en->d_type == DT_DIR) // (st.st_mode & S_IFDIR)) +#ifdef PLATFORM_SUNOS + if(DirReadType == DirReadType_DirsOnly && (st.st_mode & S_IFDIR)) +#else + if(DirReadType == DirReadType_DirsOnly && en->d_type == DT_DIR) +#endif { // Directory, and we want directories name = en->d_name; diff --git a/lib/raidfile/RaidFileUtil.cpp b/lib/raidfile/RaidFileUtil.cpp index 9177c8ba..c71bb2df 100755 --- a/lib/raidfile/RaidFileUtil.cpp +++ b/lib/raidfile/RaidFileUtil.cpp @@ -54,7 +54,7 @@ RaidFileUtil::ExistType RaidFileUtil::RaidFileExists(RaidFileDiscSet &rDiscSet, if(pRevisionID != 0) { (*pRevisionID) = FileModificationTime(st); -#ifdef PLATFORM_LINUX +#ifdef PLATFORM_stat_SHORT_mtime // On linux, the time resolution is very low for modification times. // So add the size to it to give a bit more chance of it changing. // TODO: Make this better. @@ -71,7 +71,7 @@ RaidFileUtil::ExistType RaidFileUtil::RaidFileExists(RaidFileDiscSet &rDiscSet, int64_t revisionID = 0; int setSize = rDiscSet.size(); int rfCount = 0; -#ifdef PLATFORM_LINUX +#ifdef PLATFORM_stat_SHORT_mtime // TODO: replace this with better linux revision ID detection int64_t revisionIDplus = 0; #endif @@ -92,7 +92,7 @@ RaidFileUtil::ExistType RaidFileUtil::RaidFileExists(RaidFileDiscSet &rDiscSet, { int64_t rid = FileModificationTime(st); if(rid > revisionID) revisionID = rid; -#ifdef PLATFORM_LINUX +#ifdef PLATFORM_stat_SHORT_mtime revisionIDplus += st.st_size; #endif } @@ -101,7 +101,7 @@ RaidFileUtil::ExistType RaidFileUtil::RaidFileExists(RaidFileDiscSet &rDiscSet, if(pRevisionID != 0) { (*pRevisionID) = revisionID; -#ifdef PLATFORM_LINUX +#ifdef PLATFORM_stat_SHORT_mtime (*pRevisionID) += revisionIDplus; #endif } diff --git a/lib/raidfile/RaidFileWrite.cpp b/lib/raidfile/RaidFileWrite.cpp index 414f24e6..987aa22c 100755 --- a/lib/raidfile/RaidFileWrite.cpp +++ b/lib/raidfile/RaidFileWrite.cpp @@ -112,10 +112,21 @@ void RaidFileWrite::Open(bool AllowOverwrite) } // Get a lock on the write file +#ifdef PLATFORM_open_USE_fcntl + int errnoBlock = EAGAIN; + struct flock desc; + desc.l_type = F_WRLCK; + desc.l_whence = SEEK_SET; + desc.l_start = 0; + desc.l_len = 0; + if(::fcntl(mOSFileHandle, F_SETLK, &desc) != 0) +#else + int errnoBlock = EWOULDBLOCK; if(::flock(mOSFileHandle, LOCK_EX | LOCK_NB) != 0) +#endif { // Lock was not obtained. - bool wasLocked = (errno == EWOULDBLOCK); + bool wasLocked = (errno == errnoBlock); // Close the file ::close(mOSFileHandle); mOSFileHandle = -1; @@ -376,7 +387,7 @@ void RaidFileWrite::TransformToRaidStorage() // Then open them all for writing (in strict order) try { -#ifdef PLATFORM_LINUX +#if defined(PLATFORM_open_USE_flock) || defined(PLATFORM_open_USE_fcntl) FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL)> stripe1(stripe1FilenameW.c_str()); FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL)> stripe2(stripe2FilenameW.c_str()); FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL)> parity(parityFilenameW.c_str()); @@ -448,11 +459,7 @@ void RaidFileWrite::TransformToRaidStorage() { // XOR in the size at the end of the parity block ASSERT(sizeof(RaidFileRead::FileSizeType) == (2*sizeof(unsigned int))); -#ifdef PLATFORM_LINUX ASSERT(sizeof(RaidFileRead::FileSizeType) >= sizeof(off_t)); -#else - ASSERT(sizeof(RaidFileRead::FileSizeType) == sizeof(off_t)); -#endif int sizePos = (blockSize/sizeof(unsigned int)) - 2; RaidFileRead::FileSizeType sw = hton64(writeFileStat.st_size); unsigned int *psize = (unsigned int *)(&sw); @@ -509,11 +516,7 @@ void RaidFileWrite::TransformToRaidStorage() // if it can't be worked out some other means -- size is required to rebuild the file if one of the stripe files is missing if(sizeRecordRequired) { -#ifdef PLATFORM_LINUX ASSERT(sizeof(writeFileStat.st_size) <= sizeof(RaidFileRead::FileSizeType)); -#else - ASSERT(sizeof(writeFileStat.st_size) == sizeof(RaidFileRead::FileSizeType)); -#endif RaidFileRead::FileSizeType sw = hton64(writeFileStat.st_size); ASSERT((::lseek(parity, 0, SEEK_CUR) % blockSize) == 0); if(::write(parity, &sw, sizeof(sw)) != sizeof(sw)) diff --git a/lib/server/Daemon.cpp b/lib/server/Daemon.cpp index 31997fb6..1885c6c8 100755 --- a/lib/server/Daemon.cpp +++ b/lib/server/Daemon.cpp @@ -141,7 +141,11 @@ int Daemon::Main(const char *DefaultConfigFile, int argc, const char *argv[]) SetupInInitialProcess(); // Set signal handler - if(::signal(SIGHUP, SignalHandler) == SIG_ERR || ::signal(SIGTERM, SignalHandler) == SIG_ERR) + struct sigaction sa; + sa.sa_handler = SignalHandler; + sa.sa_flags = 0; + ::sigemptyset(&sa.sa_mask); + if(::sigaction(SIGHUP, &sa, NULL) != 0 || ::sigaction(SIGTERM, &sa, NULL) != 0) { THROW_EXCEPTION(ServerException, DaemoniseFailed) } @@ -354,8 +358,12 @@ int Daemon::Main(const char *DefaultConfigFile, int argc, const char *argv[]) void Daemon::EnterChild() { // Unset signal handlers - ::signal(SIGHUP, SIG_DFL); - ::signal(SIGTERM, SIG_DFL); + struct sigaction sa; + sa.sa_handler = SIG_DFL; + sa.sa_flags = 0; + ::sigemptyset(&sa.sa_mask); + ::sigaction(SIGHUP, &sa, NULL); + ::sigaction(SIGTERM, &sa, NULL); } diff --git a/lib/server/Protocol.cpp b/lib/server/Protocol.cpp index ca89bdf1..690c2ec0 100755 --- a/lib/server/Protocol.cpp +++ b/lib/server/Protocol.cpp @@ -423,7 +423,14 @@ void Protocol::Read(int64_t &rOut) READ_START_CHECK READ_CHECK_BYTES_AVAILABLE(sizeof(int64_t)) - rOut = ntoh64(*((int64_t*)(mpBuffer + mReadOffset))); +#ifdef PLATFORM_ALIGN_INT + int64_t nvalue; + memcpy(&nvalue, mpBuffer + mReadOffset, sizeof(int64_t)); +#else + int64_t nvalue = *((int64_t*)(mpBuffer + mReadOffset)); +#endif + rOut = ntoh64(nvalue); + mReadOffset += sizeof(int64_t); } @@ -440,7 +447,13 @@ void Protocol::Read(int32_t &rOut) READ_START_CHECK READ_CHECK_BYTES_AVAILABLE(sizeof(int32_t)) - rOut = ntohl(*((int32_t*)(mpBuffer + mReadOffset))); +#ifdef PLATFORM_ALIGN_INT + int32_t nvalue; + memcpy(&nvalue, mpBuffer + mReadOffset, sizeof(int32_t)); +#else + int32_t nvalue = *((int32_t*)(mpBuffer + mReadOffset)); +#endif + rOut = ntohl(nvalue); mReadOffset += sizeof(int32_t); } @@ -545,7 +558,12 @@ void Protocol::Write(int64_t Value) WRITE_START_CHECK WRITE_ENSURE_BYTES_AVAILABLE(sizeof(int64_t)) - *((int64_t*)(mpBuffer + mWriteOffset)) = hton64(Value); + int64_t nvalue = hton64(Value); +#ifdef PLATFORM_ALIGN_INT + memcpy(mpBuffer + mWriteOffset, &nvalue, sizeof(int64_t)); +#else + *((int64_t*)(mpBuffer + mWriteOffset)) = nvalue; +#endif mWriteOffset += sizeof(int64_t); } @@ -563,7 +581,12 @@ void Protocol::Write(int32_t Value) WRITE_START_CHECK WRITE_ENSURE_BYTES_AVAILABLE(sizeof(int32_t)) - *((int32_t*)(mpBuffer + mWriteOffset)) = htonl(Value); + int32_t nvalue = htonl(Value); +#ifdef PLATFORM_ALIGN_INT + memcpy(mpBuffer + mWriteOffset, &nvalue, sizeof(int32_t)); +#else + *((int32_t*)(mpBuffer + mWriteOffset)) = nvalue; +#endif mWriteOffset += sizeof(int32_t); } diff --git a/lib/server/SocketStreamTLS.cpp b/lib/server/SocketStreamTLS.cpp index 63ac7bb5..7d82f82c 100755 --- a/lib/server/SocketStreamTLS.cpp +++ b/lib/server/SocketStreamTLS.cpp @@ -14,7 +14,7 @@ #include <openssl/bio.h> #include <poll.h> #include <errno.h> -#include <sys/ioctl.h> +#include <fcntl.h> #include "SocketStreamTLS.h" #include "SSLLib.h" @@ -132,12 +132,23 @@ void SocketStreamTLS::Handshake(const TLSContext &rContext, bool IsServer) } // Make the socket non-blocking so timeouts on Read work - int nonblocking = true; - if(::ioctl(socket, FIONBIO, &nonblocking) == -1) + // This is more portable than using ioctl with FIONBIO + int statusFlags = 0; + if(::fcntl(socket, F_GETFL, &statusFlags) < 0 + || ::fcntl(socket, F_SETFL, statusFlags | O_NONBLOCK) == -1) { THROW_EXCEPTION(ServerException, SocketSetNonBlockingFailed) } + // FIXME: This is less portable than the above. However, it MAY be needed + // for cygwin, which has/had bugs with fcntl + // + // int nonblocking = true; + // if(::ioctl(socket, FIONBIO, &nonblocking) == -1) + // { + // THROW_EXCEPTION(ServerException, SocketSetNonBlockingFailed) + // } + // Set the two to know about each other ::SSL_set_bio(mpSSL, mpBIO, mpBIO); |