From c7662795f519d2b6797e4b1ac7fa4a22afa26310 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Thu, 27 Jul 2006 23:18:35 +0000 Subject: * merge - This is my current patch queue. I think that all of these are safe to apply. This is just under half of the pending changes in chris/general (the easy half). --- lib/backupclient/BackupClientFileAttributes.cpp | 2 + lib/backupclient/BackupStoreFile.cpp | 2 + lib/backupclient/BackupStoreObjectDump.cpp | 8 ++- lib/backupstore/BackupStoreAccounts.cpp | 5 +- lib/backupstore/BackupStoreCheck.cpp | 7 ++- lib/common/BoxPlatform.h | 9 +++- lib/common/FdGetLine.h | 4 ++ lib/common/Guards.h | 2 +- lib/common/UnixUser.cpp | 4 ++ lib/common/makeexception.pl.in | 11 ++-- lib/raidfile/RaidFileRead.cpp | 31 ++++++++--- lib/raidfile/RaidFileWrite.cpp | 69 +++++++++++++++++++++---- lib/server/Daemon.cpp | 50 ++++++++++++++---- lib/server/ServerStream.h | 18 ++++++- lib/server/SocketStream.cpp | 47 ++++++++++++----- lib/server/SocketStream.h | 2 + lib/server/SocketStreamTLS.cpp | 8 ++- lib/server/makeprotocol.pl.in | 18 +++++-- 18 files changed, 241 insertions(+), 56 deletions(-) (limited to 'lib') diff --git a/lib/backupclient/BackupClientFileAttributes.cpp b/lib/backupclient/BackupClientFileAttributes.cpp index 974db3c9..ace72cf8 100644 --- a/lib/backupclient/BackupClientFileAttributes.cpp +++ b/lib/backupclient/BackupClientFileAttributes.cpp @@ -642,6 +642,7 @@ void BackupClientFileAttributes::WriteAttributes(const char *Filename) const } // If working as root, set user IDs + #ifndef WIN32 if(::geteuid() == 0) { #ifndef HAVE_LCHOWN @@ -661,6 +662,7 @@ void BackupClientFileAttributes::WriteAttributes(const char *Filename) const } #endif } + #endif if(static_cast(xattrOffset+sizeof(u_int32_t))<=mpClearAttributes->GetSize()) { diff --git a/lib/backupclient/BackupStoreFile.cpp b/lib/backupclient/BackupStoreFile.cpp index f5a55207..278bf50a 100644 --- a/lib/backupclient/BackupStoreFile.cpp +++ b/lib/backupclient/BackupStoreFile.cpp @@ -289,6 +289,8 @@ void BackupStoreFile::DecodeFile(IOStream &rEncodedFile, const char *DecodedFile // Copy it out to the file stream->CopyStreamTo(out); } + + out.Close(); // Write the attributes stream->GetAttributes().WriteAttributes(DecodedFilename); diff --git a/lib/backupclient/BackupStoreObjectDump.cpp b/lib/backupclient/BackupStoreObjectDump.cpp index b4ccc731..d3d9cc17 100644 --- a/lib/backupclient/BackupStoreObjectDump.cpp +++ b/lib/backupclient/BackupStoreObjectDump.cpp @@ -113,7 +113,13 @@ void BackupStoreDirectory::Dump(void *clibFileHandle, bool ToTrace) // Output item int16_t f = (*i)->GetFlags(); - OutputLine(file, ToTrace, "%06llx %4lld %016llx %4d %3d %4d%s%s%s%s%s%s\n", +#ifdef WIN32 + OutputLine(file, ToTrace, + "%06I64x %4I64d %016I64x %4d %3d %4d%s%s%s%s%s%s\n", +#else + OutputLine(file, ToTrace, + "%06llx %4lld %016llx %4d %3d %4d%s%s%s%s%s%s\n", +#endif (*i)->GetObjectID(), (*i)->GetSizeInBlocks(), (*i)->GetAttributesHash(), diff --git a/lib/backupstore/BackupStoreAccounts.cpp b/lib/backupstore/BackupStoreAccounts.cpp index 36d9cad3..eb10b385 100644 --- a/lib/backupstore/BackupStoreAccounts.cpp +++ b/lib/backupstore/BackupStoreAccounts.cpp @@ -141,8 +141,9 @@ void BackupStoreAccounts::GetAccountRoot(int32_t ID, std::string &rRootDirOut, i std::string BackupStoreAccounts::MakeAccountRootDir(int32_t ID, int DiscSet) const { char accid[64]; // big enough! - ::sprintf(accid, "%08x/", ID); - return std::string(std::string(BOX_RAIDFILE_ROOT_BBSTORED DIRECTORY_SEPARATOR) + accid); + ::sprintf(accid, "%08x" DIRECTORY_SEPARATOR, ID); + return std::string(std::string(BOX_RAIDFILE_ROOT_BBSTORED + DIRECTORY_SEPARATOR) + accid); } diff --git a/lib/backupstore/BackupStoreCheck.cpp b/lib/backupstore/BackupStoreCheck.cpp index fb48c3da..16d397a2 100644 --- a/lib/backupstore/BackupStoreCheck.cpp +++ b/lib/backupstore/BackupStoreCheck.cpp @@ -328,7 +328,8 @@ void BackupStoreCheck::CheckObjectsDir(int64_t StartID) std::string dirName; StoreStructure::MakeObjectFilename(StartID, mStoreRoot, mDiscSetNumber, dirName, false /* don't make sure the dir exists */); // Check expectations - ASSERT(dirName.size() > 4 && dirName[dirName.size() - 4] == '/'); + ASSERT(dirName.size() > 4 && + dirName[dirName.size() - 4] == DIRECTORY_SEPARATOR_ASCHAR); // Remove the filename from it dirName.resize(dirName.size() - 4); // four chars for "/o00" @@ -377,7 +378,9 @@ void BackupStoreCheck::CheckObjectsDir(int64_t StartID) if(!fileOK) { // Unexpected or bad file, delete it - ::printf("Spurious file %s/%s found%s\n", dirName.c_str(), (*i).c_str(), mFixErrors?", deleting":""); + ::printf("Spurious file %s" DIRECTORY_SEPARATOR "%s " + "found%s\n", dirName.c_str(), (*i).c_str(), + mFixErrors?", deleting":""); ++mNumberErrorsFound; if(mFixErrors) { diff --git a/lib/common/BoxPlatform.h b/lib/common/BoxPlatform.h index b766706a..cb83f7a9 100644 --- a/lib/common/BoxPlatform.h +++ b/lib/common/BoxPlatform.h @@ -40,8 +40,8 @@ #endif #endif -// Slight hack; disable interception on Darwin within raidfile test -#ifdef __APPLE__ +// Slight hack; disable interception in raidfile test on Darwin and Windows +#if defined __APPLE__ || defined WIN32 // TODO: Replace with autoconf test #define PLATFORM_CLIB_FNS_INTERCEPTION_IMPOSSIBLE #endif @@ -138,6 +138,11 @@ #define INFTIM -1 #endif +// for Unix compatibility with Windows :-) +#if !HAVE_DECL_O_BINARY + #define O_BINARY 0 +#endif + #ifdef WIN32 typedef u_int64_t InodeRefType; #else diff --git a/lib/common/FdGetLine.h b/lib/common/FdGetLine.h index fecb0371..a18007a3 100644 --- a/lib/common/FdGetLine.h +++ b/lib/common/FdGetLine.h @@ -14,6 +14,10 @@ #ifdef NDEBUG #define FDGETLINE_BUFFER_SIZE 1024 +#elif defined WIN32 + // need enough space for at least one unicode character + // in UTF-8 when calling console_read() from bbackupquery + #define FDGETLINE_BUFFER_SIZE 5 #else #define FDGETLINE_BUFFER_SIZE 4 #endif diff --git a/lib/common/Guards.h b/lib/common/Guards.h index 17d73b3f..b1bca0fa 100644 --- a/lib/common/Guards.h +++ b/lib/common/Guards.h @@ -24,7 +24,7 @@ #include "MemLeakFindOn.h" -template +template class FileHandleGuard { public: diff --git a/lib/common/UnixUser.cpp b/lib/common/UnixUser.cpp index df2d0ddd..d0fd337b 100644 --- a/lib/common/UnixUser.cpp +++ b/lib/common/UnixUser.cpp @@ -75,6 +75,7 @@ UnixUser::UnixUser(uid_t UID, gid_t GID) // -------------------------------------------------------------------------- UnixUser::~UnixUser() { +#ifndef WIN32 if(mRevertOnDestruction) { // Revert to "real" user and group id of the process @@ -84,6 +85,7 @@ UnixUser::~UnixUser() THROW_EXCEPTION(CommonException, CouldNotRestoreProcessUser) } } +#endif } @@ -98,6 +100,7 @@ UnixUser::~UnixUser() // -------------------------------------------------------------------------- void UnixUser::ChangeProcessUser(bool Temporary) { +#ifndef WIN32 if(Temporary) { // Change temporarily (change effective only) @@ -119,6 +122,7 @@ void UnixUser::ChangeProcessUser(bool Temporary) THROW_EXCEPTION(CommonException, CouldNotChangeProcessUser) } } +#endif } diff --git a/lib/common/makeexception.pl.in b/lib/common/makeexception.pl.in index 1564b75b..c03b8277 100755 --- a/lib/common/makeexception.pl.in +++ b/lib/common/makeexception.pl.in @@ -1,9 +1,11 @@ #!@PERL@ +use lib "../../infrastructure"; +use BoxPlatform; + # global exception list file my $global_list = '../../ExceptionCodes.txt'; - my @exception; my @exception_desc; my $class; @@ -46,8 +48,8 @@ close EXCEPTION_DESC; # write the code print "Generating $class exception...\n"; -open CPP,">autogen_${class}Exception.cpp" or die "Can't open cpp file for writing"; -open H,">autogen_${class}Exception.h" or die "Can't open h file for writing"; +open CPP,">autogen_${class}Exception.cpp.new" or die "Can't open cpp file for writing"; +open H,">autogen_${class}Exception.h.new" or die "Can't open h file for writing"; # write header file my $guardname = uc 'AUTOGEN_'.$class.'EXCEPTION_H'; @@ -200,6 +202,9 @@ __E close H; close CPP; +update_if_changed("autogen_${class}Exception.cpp"); +update_if_changed("autogen_${class}Exception.h"); + # update the global exception list my $list_before; my $list_after; diff --git a/lib/raidfile/RaidFileRead.cpp b/lib/raidfile/RaidFileRead.cpp index d60936fc..bc728113 100644 --- a/lib/raidfile/RaidFileRead.cpp +++ b/lib/raidfile/RaidFileRead.cpp @@ -14,10 +14,20 @@ #include #include #include + +#ifdef HAVE_SYS_UIO_H #include +#endif + +#ifdef HAVE_SYSLOG_H #include +#endif + #include + +#ifdef HAVE_DIRENT_H #include +#endif #include #include @@ -583,7 +593,8 @@ void RaidFileRead_Raid::AttemptToRecoverFromIOError(bool Stripe1) // Open the parity file std::string parityFilename(RaidFileUtil::MakeRaidComponentName(rdiscSet, mFilename, (2 + startDisc) % READ_NUMBER_DISCS_REQUIRED)); - mParityHandle = ::open(parityFilename.c_str(), O_RDONLY, 0555); + mParityHandle = ::open(parityFilename.c_str(), + O_RDONLY | O_BINARY, 0555); if(mParityHandle == -1) { THROW_EXCEPTION(RaidFileException, OSError) @@ -1017,7 +1028,8 @@ std::auto_ptr RaidFileRead::Open(int SetNumber, const std::string std::string writeFilename(RaidFileUtil::MakeWriteFileName(rdiscSet, Filename)); // Attempt to open - int osFileHandle = ::open(writeFilename.c_str(), O_RDONLY, 0); + int osFileHandle = ::open(writeFilename.c_str(), + O_RDONLY | O_BINARY, 0); if(osFileHandle == -1) { THROW_EXCEPTION(RaidFileException, ErrorOpeningFileForRead) @@ -1055,13 +1067,15 @@ std::auto_ptr RaidFileRead::Open(int SetNumber, const std::string try { // Open stripe1 - stripe1 = ::open(stripe1Filename.c_str(), O_RDONLY, 0555); + stripe1 = ::open(stripe1Filename.c_str(), + O_RDONLY | O_BINARY, 0555); if(stripe1 == -1) { stripe1errno = errno; } // Open stripe2 - stripe2 = ::open(stripe2Filename.c_str(), O_RDONLY, 0555); + stripe2 = ::open(stripe2Filename.c_str(), + O_RDONLY | O_BINARY, 0555); if(stripe2 == -1) { stripe2errno = errno; @@ -1169,7 +1183,8 @@ std::auto_ptr RaidFileRead::Open(int SetNumber, const std::string // Open stripe1? if(existingFiles & RaidFileUtil::Stripe1Exists) { - stripe1 = ::open(stripe1Filename.c_str(), O_RDONLY, 0555); + stripe1 = ::open(stripe1Filename.c_str(), + O_RDONLY | O_BINARY, 0555); if(stripe1 == -1) { THROW_EXCEPTION(RaidFileException, OSError) @@ -1178,14 +1193,16 @@ std::auto_ptr RaidFileRead::Open(int SetNumber, const std::string // Open stripe2? if(existingFiles & RaidFileUtil::Stripe2Exists) { - stripe2 = ::open(stripe2Filename.c_str(), O_RDONLY, 0555); + stripe2 = ::open(stripe2Filename.c_str(), + O_RDONLY | O_BINARY, 0555); if(stripe2 == -1) { THROW_EXCEPTION(RaidFileException, OSError) } } // Open parity - parity = ::open(parityFilename.c_str(), O_RDONLY, 0555); + parity = ::open(parityFilename.c_str(), + O_RDONLY | O_BINARY, 0555); if(parity == -1) { THROW_EXCEPTION(RaidFileException, OSError) diff --git a/lib/raidfile/RaidFileWrite.cpp b/lib/raidfile/RaidFileWrite.cpp index e30162fa..b3c98701 100644 --- a/lib/raidfile/RaidFileWrite.cpp +++ b/lib/raidfile/RaidFileWrite.cpp @@ -104,7 +104,8 @@ void RaidFileWrite::Open(bool AllowOverwrite) writeFilename += 'X'; // Attempt to open - mOSFileHandle = ::open(writeFilename.c_str(), O_WRONLY | O_CREAT, + mOSFileHandle = ::open(writeFilename.c_str(), + O_WRONLY | O_CREAT | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if(mOSFileHandle == -1) { @@ -115,7 +116,7 @@ void RaidFileWrite::Open(bool AllowOverwrite) #ifdef HAVE_FLOCK int errnoBlock = EWOULDBLOCK; if(::flock(mOSFileHandle, LOCK_EX | LOCK_NB) != 0) -#else +#elif HAVE_DECL_F_SETLK int errnoBlock = EAGAIN; struct flock desc; desc.l_type = F_WRLCK; @@ -123,6 +124,9 @@ void RaidFileWrite::Open(bool AllowOverwrite) desc.l_start = 0; desc.l_len = 0; if(::fcntl(mOSFileHandle, F_SETLK, &desc) != 0) +#else + int errnoBlock = ENOSYS; + if (0) #endif { // Lock was not obtained. @@ -242,23 +246,46 @@ void RaidFileWrite::Commit(bool ConvertToRaidNow) } // Rename it into place -- BEFORE it's closed so lock remains + +#ifdef WIN32 + // Except on Win32 which doesn't allow renaming open files + // Close file... + if(::close(mOSFileHandle) != 0) + { + THROW_EXCEPTION(RaidFileException, OSError) + } + mOSFileHandle = -1; +#endif // WIN32 + RaidFileController &rcontroller(RaidFileController::GetController()); RaidFileDiscSet rdiscSet(rcontroller.GetDiscSet(mSetNumber)); // Get the filename for the write file std::string renameTo(RaidFileUtil::MakeWriteFileName(rdiscSet, mFilename)); // And the current name std::string renameFrom(renameTo + 'X'); + +#ifdef WIN32 + // need to delete the target first + if(::unlink(renameTo.c_str()) != 0 && + GetLastError() != ERROR_FILE_NOT_FOUND) + { + THROW_EXCEPTION(RaidFileException, OSError) + } +#endif + if(::rename(renameFrom.c_str(), renameTo.c_str()) != 0) { THROW_EXCEPTION(RaidFileException, OSError) } +#ifndef WIN32 // Close file... if(::close(mOSFileHandle) != 0) { THROW_EXCEPTION(RaidFileException, OSError) } mOSFileHandle = -1; +#endif // !WIN32 // Raid it? if(ConvertToRaidNow) @@ -292,8 +319,15 @@ void RaidFileWrite::Discard() writeFilename += 'X'; // Unlink and close it - if((::unlink(writeFilename.c_str()) != 0) - || (::close(mOSFileHandle) != 0)) + +#ifdef WIN32 + // On Win32 we must close it first + if (::close(mOSFileHandle) != 0 || + ::unlink(writeFilename.c_str()) != 0) +#else // !WIN32 + if (::unlink(writeFilename.c_str()) != 0 || + ::close(mOSFileHandle) != 0) +#endif // !WIN32 { THROW_EXCEPTION(RaidFileException, OSError) } @@ -388,13 +422,13 @@ void RaidFileWrite::TransformToRaidStorage() try { #if HAVE_DECL_O_EXLOCK - FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_EXLOCK)> stripe1(stripe1FilenameW.c_str()); - FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_EXLOCK)> stripe2(stripe2FilenameW.c_str()); - FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_EXLOCK)> parity(parityFilenameW.c_str()); + FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_EXLOCK | O_BINARY)> stripe1(stripe1FilenameW.c_str()); + FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_EXLOCK | O_BINARY)> stripe2(stripe2FilenameW.c_str()); + FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_EXLOCK | O_BINARY)> parity(parityFilenameW.c_str()); #else - 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()); + FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_BINARY)> stripe1(stripe1FilenameW.c_str()); + FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_BINARY)> stripe2(stripe2FilenameW.c_str()); + FileHandleGuard<(O_WRONLY | O_CREAT | O_EXCL | O_BINARY)> parity(parityFilenameW.c_str()); #endif // Then... read in data... @@ -530,6 +564,21 @@ void RaidFileWrite::TransformToRaidStorage() parity.Close(); stripe2.Close(); stripe1.Close(); + +#ifdef WIN32 + // Must delete before renaming + #define CHECK_UNLINK(file) \ + { \ + if (::unlink(file) != 0 && errno != ENOENT) \ + { \ + THROW_EXCEPTION(RaidFileException, OSError); \ + } \ + } + CHECK_UNLINK(stripe1Filename.c_str()); + CHECK_UNLINK(stripe2Filename.c_str()); + CHECK_UNLINK(parityFilename.c_str()); + #undef CHECK_UNLINK +#endif // Rename them into place if(::rename(stripe1FilenameW.c_str(), stripe1Filename.c_str()) != 0 diff --git a/lib/server/Daemon.cpp b/lib/server/Daemon.cpp index 2f902473..2131bdcb 100644 --- a/lib/server/Daemon.cpp +++ b/lib/server/Daemon.cpp @@ -23,6 +23,10 @@ #include #endif +#ifdef WIN32 + #include +#endif + #include "Daemon.h" #include "Configuration.h" #include "ServerException.h" @@ -142,7 +146,7 @@ int Daemon::Main(const char *DefaultConfigFile, int argc, const char *argv[]) { fprintf(stderr, "%s: failed to start: " "failed to open configuration file: " - "%s", DaemonName(), + "%s\n", DaemonName(), mConfigFileName.c_str()); #ifdef WIN32 ::syslog(LOG_ERR, "%s: failed to start: " @@ -189,6 +193,7 @@ int Daemon::Main(const char *DefaultConfigFile, int argc, const char *argv[]) { THROW_EXCEPTION(ServerException, DaemoniseFailed) } +#endif // !WIN32 // Server configuration const Configuration &serverConfig( @@ -197,7 +202,8 @@ int Daemon::Main(const char *DefaultConfigFile, int argc, const char *argv[]) // Open PID file for writing pidFileName = serverConfig.GetKeyValue("PidFile"); FileHandleGuard<(O_WRONLY | O_CREAT | O_TRUNC), (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)> pidFile(pidFileName.c_str()); - + +#ifndef WIN32 // Handle changing to a different user if(serverConfig.KeyExists("User")) { @@ -267,20 +273,25 @@ int Daemon::Main(const char *DefaultConfigFile, int argc, const char *argv[]) // open the log ::openlog(DaemonName(), LOG_PID, LOG_LOCAL6); + // Log the start message ::syslog(LOG_INFO, "Starting daemon (config: %s) (version " BOX_VERSION ")", mConfigFileName.c_str()); -#ifndef WIN32 // Write PID to file char pid[32]; + +#ifdef WIN32 + int pidsize = sprintf(pid, "%d", (int)GetCurrentProcessId()); +#else int pidsize = sprintf(pid, "%d", (int)getpid()); +#endif + if(::write(pidFile, pid, pidsize) != pidsize) { ::syslog(LOG_ERR, "can't write pid file"); THROW_EXCEPTION(ServerException, DaemoniseFailed) } -#endif // Set up memory leak reporting #ifdef BOX_MEMORY_LEAK_TESTING @@ -352,6 +363,22 @@ int Daemon::Main(const char *DefaultConfigFile, int argc, const char *argv[]) #endif return 1; } + +#ifdef WIN32 + // Under win32 we must initialise the Winsock library + // before using sockets + + WSADATA info; + + if (WSAStartup(0x0101, &info) == SOCKET_ERROR) + { + // will not run without sockets + ::syslog(LOG_ERR, "Failed to initialise Windows Sockets"); + THROW_EXCEPTION(CommonException, Internal) + } +#endif + + int retcode = 0; // Main Daemon running try @@ -381,7 +408,8 @@ int Daemon::Main(const char *DefaultConfigFile, int argc, const char *argv[]) mConfigFileName.c_str(), errors.c_str()); // And give up - return 1; + retcode = 1; + break; } // delete old configuration @@ -409,22 +437,26 @@ int Daemon::Main(const char *DefaultConfigFile, int argc, const char *argv[]) ::syslog(LOG_ERR, "%s: terminating due to exception %s " "(%d/%d)", DaemonName(), e.what(), e.GetType(), e.GetSubType()); - return 1; + retcode = 1; } catch(std::exception &e) { ::syslog(LOG_ERR, "%s: terminating due to exception %s", DaemonName(), e.what()); - return 1; + retcode = 1; } catch(...) { ::syslog(LOG_ERR, "%s: terminating due to unknown exception", DaemonName()); - return 1; + retcode = 1; } + +#ifdef WIN32 + WSACleanup(); +#endif - return 0; + return retcode; } // -------------------------------------------------------------------------- diff --git a/lib/server/ServerStream.h b/lib/server/ServerStream.h index 8dafccae..745c3ccb 100644 --- a/lib/server/ServerStream.h +++ b/lib/server/ServerStream.h @@ -56,6 +56,10 @@ public: return "generic-stream-server"; } + #ifdef WIN32 + virtual void OnIdle() { } + #endif + virtual void Run() { // Set process title as appropraite @@ -215,6 +219,7 @@ public: if(connection.get()) { // Since this is a template parameter, the if() will be optimised out by the compiler + #ifndef WIN32 // no fork on Win32 if(ForkToHandleRequests) { pid_t pid = ::fork(); @@ -255,14 +260,20 @@ public: } else { + #endif // !WIN32 // Just handle in this connection SetProcessTitle("handling"); HandleConnection(*connection); SetProcessTitle("idle"); + #ifndef WIN32 } + #endif // !WIN32 } } - + + #ifdef WIN32 + OnIdle(); + #else // !WIN32 // Clean up child processes (if forking daemon) if(ForkToHandleRequests) { @@ -277,6 +288,7 @@ public: } } while(p > 0); } + #endif // !WIN32 } } catch(...) @@ -301,7 +313,11 @@ protected: // depends on the forking model in case someone changes it later. bool WillForkToHandleRequests() { + #ifdef WIN32 + return false; + #else return ForkToHandleRequests; + #endif // WIN32 } private: diff --git a/lib/server/SocketStream.cpp b/lib/server/SocketStream.cpp index aa3825bf..61d3846f 100644 --- a/lib/server/SocketStream.cpp +++ b/lib/server/SocketStream.cpp @@ -36,7 +36,7 @@ // // -------------------------------------------------------------------------- SocketStream::SocketStream() - : mSocketHandle(-1), + : mSocketHandle(INVALID_SOCKET_VALUE), mReadClosed(false), mWriteClosed(false), mBytesRead(0), @@ -85,7 +85,7 @@ SocketStream::SocketStream(const SocketStream &rToCopy) { THROW_EXCEPTION(ServerException, BadSocketHandle); } - if(mSocketHandle == -1) + if(mSocketHandle == INVALID_SOCKET_VALUE) { THROW_EXCEPTION(ServerException, DupError); } @@ -101,7 +101,7 @@ SocketStream::SocketStream(const SocketStream &rToCopy) // -------------------------------------------------------------------------- SocketStream::~SocketStream() { - if(mSocketHandle != -1) + if(mSocketHandle != INVALID_SOCKET_VALUE) { Close(); } @@ -117,7 +117,10 @@ SocketStream::~SocketStream() // -------------------------------------------------------------------------- void SocketStream::Attach(int socket) { - if(mSocketHandle != -1) {THROW_EXCEPTION(ServerException, SocketAlreadyOpen)} + if(mSocketHandle != INVALID_SOCKET_VALUE) + { + THROW_EXCEPTION(ServerException, SocketAlreadyOpen) + } mSocketHandle = socket; ResetCounters(); @@ -134,7 +137,10 @@ void SocketStream::Attach(int socket) // -------------------------------------------------------------------------- void SocketStream::Open(int Type, const char *Name, int Port) { - if(mSocketHandle != -1) {THROW_EXCEPTION(ServerException, SocketAlreadyOpen)} + if(mSocketHandle != INVALID_SOCKET_VALUE) + { + THROW_EXCEPTION(ServerException, SocketAlreadyOpen) + } // Setup parameters based on type, looking up names if required int sockDomain = 0; @@ -144,7 +150,7 @@ void SocketStream::Open(int Type, const char *Name, int Port) // Create the socket mSocketHandle = ::socket(sockDomain, SOCK_STREAM, 0 /* let OS choose protocol */); - if(mSocketHandle == -1) + if(mSocketHandle == INVALID_SOCKET_VALUE) { THROW_EXCEPTION(ServerException, SocketOpenError) } @@ -158,7 +164,7 @@ void SocketStream::Open(int Type, const char *Name, int Port) #else ::close(mSocketHandle); #endif - mSocketHandle = -1; + mSocketHandle = INVALID_SOCKET_VALUE; THROW_EXCEPTION(ConnectionException, Conn_SocketConnectError) } ResetCounters(); @@ -174,7 +180,10 @@ void SocketStream::Open(int Type, const char *Name, int Port) // -------------------------------------------------------------------------- int SocketStream::Read(void *pBuffer, int NBytes, int Timeout) { - if(mSocketHandle == -1) {THROW_EXCEPTION(ServerException, BadSocketHandle)} + if(mSocketHandle == INVALID_SOCKET_VALUE) + { + THROW_EXCEPTION(ServerException, BadSocketHandle) + } if(Timeout != IOStream::TimeOutInfinite) { @@ -247,7 +256,10 @@ int SocketStream::Read(void *pBuffer, int NBytes, int Timeout) // -------------------------------------------------------------------------- void SocketStream::Write(const void *pBuffer, int NBytes) { - if(mSocketHandle == -1) {THROW_EXCEPTION(ServerException, BadSocketHandle)} + if(mSocketHandle == INVALID_SOCKET_VALUE) + { + THROW_EXCEPTION(ServerException, BadSocketHandle) + } // Buffer in byte sized type. ASSERT(sizeof(char) == 1); @@ -311,7 +323,10 @@ void SocketStream::Write(const void *pBuffer, int NBytes) // -------------------------------------------------------------------------- void SocketStream::Close() { - if(mSocketHandle == -1) {THROW_EXCEPTION(ServerException, BadSocketHandle)} + if(mSocketHandle == INVALID_SOCKET_VALUE) + { + THROW_EXCEPTION(ServerException, BadSocketHandle) + } #ifdef WIN32 if(::closesocket(mSocketHandle) == -1) #else @@ -320,7 +335,7 @@ void SocketStream::Close() { THROW_EXCEPTION(ServerException, SocketCloseError) } - mSocketHandle = -1; + mSocketHandle = INVALID_SOCKET_VALUE; } // -------------------------------------------------------------------------- @@ -333,7 +348,10 @@ void SocketStream::Close() // -------------------------------------------------------------------------- void SocketStream::Shutdown(bool Read, bool Write) { - if(mSocketHandle == -1) {THROW_EXCEPTION(ServerException, BadSocketHandle)} + if(mSocketHandle == INVALID_SOCKET_VALUE) + { + THROW_EXCEPTION(ServerException, BadSocketHandle) + } // Do anything? if(!Read && !Write) return; @@ -388,7 +406,10 @@ bool SocketStream::StreamClosed() // -------------------------------------------------------------------------- tOSSocketHandle SocketStream::GetSocketHandle() { - if(mSocketHandle == -1) {THROW_EXCEPTION(ServerException, BadSocketHandle)} + if(mSocketHandle == INVALID_SOCKET_VALUE) + { + THROW_EXCEPTION(ServerException, BadSocketHandle) + } return mSocketHandle; } diff --git a/lib/server/SocketStream.h b/lib/server/SocketStream.h index 7f1cb741..ca07434e 100644 --- a/lib/server/SocketStream.h +++ b/lib/server/SocketStream.h @@ -14,8 +14,10 @@ #ifdef WIN32 typedef SOCKET tOSSocketHandle; + #define INVALID_SOCKET_VALUE (tOSSocketHandle)(-1) #else typedef int tOSSocketHandle; + #define INVALID_SOCKET_VALUE -1 #endif // -------------------------------------------------------------------------- diff --git a/lib/server/SocketStreamTLS.cpp b/lib/server/SocketStreamTLS.cpp index 94aa3868..af4ad460 100644 --- a/lib/server/SocketStreamTLS.cpp +++ b/lib/server/SocketStreamTLS.cpp @@ -137,8 +137,12 @@ void SocketStreamTLS::Handshake(const TLSContext &rContext, bool IsServer) THROW_EXCEPTION(ServerException, TLSAllocationFailed) } -#ifndef WIN32 // Make the socket non-blocking so timeouts on Read work + +#ifdef WIN32 + u_long nonblocking = 1; + ioctlsocket(socket, FIONBIO, &nonblocking); +#else // !WIN32 // This is more portable than using ioctl with FIONBIO int statusFlags = 0; if(::fcntl(socket, F_GETFL, &statusFlags) < 0 @@ -309,7 +313,7 @@ int SocketStreamTLS::Read(void *pBuffer, int NBytes, int Timeout) case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_WRITE: - // wait for the requried data + // wait for the required data // Will only get once around this loop, so don't need to calculate timeout values if(WaitWhenRetryRequired(se, Timeout) == false) { diff --git a/lib/server/makeprotocol.pl.in b/lib/server/makeprotocol.pl.in index 3dce6118..efe6519d 100755 --- a/lib/server/makeprotocol.pl.in +++ b/lib/server/makeprotocol.pl.in @@ -1,6 +1,9 @@ #!@PERL@ use strict; +use lib "../../infrastructure"; +use BoxPlatform; + # Make protocol C++ classes from a protocol description file # built in type info (values are is basic type, C++ typename) @@ -167,8 +170,8 @@ close IN; # open files my $h_filename = 'autogen_'.$protocol_name.'Protocol'.$type.'.h'; -open CPP,'>autogen_'.$protocol_name.'Protocol'.$type.'.cpp'; -open H,">$h_filename"; +open CPP,'>autogen_'.$protocol_name.'Protocol'.$type.'.cpp.new'; +open H,">$h_filename.new"; print CPP <<__E; @@ -912,6 +915,8 @@ __E close H; close CPP; +update_if_changed('autogen_'.$protocol_name.'Protocol'.$type.'.cpp'); +update_if_changed($h_filename); sub obj_is_type { @@ -981,8 +986,15 @@ sub make_log_strings { # need to translate it my ($format,$arg) = @{$log_display_types{$ty}}; - push @str,$format; $arg =~ s/VAR/m$nm/g; + + if ($format eq "0x%llx" and $target_windows) + { + $format = "0x%I64x"; + $arg = "(uint64_t)$arg"; + } + + push @str,$format; push @arg,$arg; } else -- cgit v1.2.3