[Box Backup-commit] COMMIT r2115 - in box/trunk: bin/bbackupd bin/bbackupquery lib/backupclient lib/common lib/server

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Fri Mar 28 22:18:44 GMT 2008


Author: chris
Date: 2008-03-28 22:18:44 +0000 (Fri, 28 Mar 2008)
New Revision: 2115

Modified:
   box/trunk/bin/bbackupd/BackupClientContext.cpp
   box/trunk/bin/bbackupd/BackupDaemon.cpp
   box/trunk/bin/bbackupd/Win32ServiceFunctions.cpp
   box/trunk/bin/bbackupquery/BackupQueries.cpp
   box/trunk/bin/bbackupquery/bbackupquery.cpp
   box/trunk/lib/backupclient/BackupClientRestore.cpp
   box/trunk/lib/common/BoxTime.cpp
   box/trunk/lib/common/EventWatchFilesystemObject.cpp
   box/trunk/lib/common/FileStream.cpp
   box/trunk/lib/common/FileStream.h
   box/trunk/lib/common/Guards.h
   box/trunk/lib/common/Logging.h
   box/trunk/lib/server/Daemon.cpp
   box/trunk/lib/server/ServerControl.h
   box/trunk/lib/server/SocketListen.h
   box/trunk/lib/server/SocketStream.cpp
Log:
Improve logging with macros that consistently output strerror(errno) and
errno, replacing almost all use of strerror() in the main code.

Log a more detailed error message before throwing an exception for some
more system call failures.

Make FileStream store its filename on all platforms, not just Windows.

Wrap some long lines at less than 80 characters to improve readability.

Fix some minor violations of coding standard (white space) and a typo
in a comment.


Modified: box/trunk/bin/bbackupd/BackupClientContext.cpp
===================================================================
--- box/trunk/bin/bbackupd/BackupClientContext.cpp	2008-03-28 21:51:32 UTC (rev 2114)
+++ box/trunk/bin/bbackupd/BackupClientContext.cpp	2008-03-28 22:18:44 UTC (rev 2115)
@@ -146,8 +146,8 @@
 
 			if (!mpExtendedLogFileHandle)
 			{
-				BOX_ERROR("Failed to open extended log "
-					"file: " << strerror(errno));
+				BOX_LOG_SYS_ERROR("Failed to open extended "
+					"log file: " << mExtendedLogFile);
 			}
 			else
 			{

Modified: box/trunk/bin/bbackupd/BackupDaemon.cpp
===================================================================
--- box/trunk/bin/bbackupd/BackupDaemon.cpp	2008-03-28 21:51:32 UTC (rev 2114)
+++ box/trunk/bin/bbackupd/BackupDaemon.cpp	2008-03-28 22:18:44 UTC (rev 2115)
@@ -1812,10 +1812,9 @@
 				if(::statfs(apLoc->mPath.c_str(), &s) != 0)
 #endif // HAVE_STRUCT_STATVFS_F_MNTONNAME
 				{
-					BOX_WARNING("Failed to stat location "
+					BOX_LOG_SYS_WARNING("Failed to stat location "
 						"path '" << apLoc->mPath <<
-						"' (" << strerror(errno) <<
-						"), skipping location '" <<
+						"', skipping location '" <<
 						apLoc->mName << "'");
 					continue;
 				}
@@ -2189,9 +2188,8 @@
 #endif
 		if(::rename(newmap.c_str(), target.c_str()) != 0)
 		{
-			BOX_ERROR("failed to rename ID map: " << newmap
-				<< " to " << target << ": " 
-				<< strerror(errno));
+			BOX_LOG_SYS_ERROR("Failed to rename ID map: " <<
+				newmap << " to " << target);
 			THROW_EXCEPTION(CommonException, OSFileError)
 		}
 	}
@@ -3055,9 +3053,10 @@
 	// Check to see if the file exists
 	if(!FileExists(storeObjectInfoFile.c_str()))
 	{
-		// File doesn't exist -- so can't be deleted. But something isn't quite right, so log a message
-		BOX_WARNING("Store object info file did not exist when it "
-			"was supposed to. (" << storeObjectInfoFile << ")");
+		// File doesn't exist -- so can't be deleted. But something
+		// isn't quite right, so log a message
+		BOX_WARNING("StoreObjectInfoFile did not exist when it "
+			"was supposed to: " << storeObjectInfoFile);
 
 		// Return true to stop things going around in a loop
 		return true;
@@ -3066,8 +3065,8 @@
 	// Actually delete it
 	if(::unlink(storeObjectInfoFile.c_str()) != 0)
 	{
-		BOX_ERROR("Failed to delete the old store object info file: "
-			<< storeObjectInfoFile << ": "<< strerror(errno));
+		BOX_LOG_SYS_ERROR("Failed to delete the old "
+			"StoreObjectInfoFile: " << storeObjectInfoFile);
 		return false;
 	}
 

Modified: box/trunk/bin/bbackupd/Win32ServiceFunctions.cpp
===================================================================
--- box/trunk/bin/bbackupd/Win32ServiceFunctions.cpp	2008-03-28 21:51:32 UTC (rev 2114)
+++ box/trunk/bin/bbackupd/Win32ServiceFunctions.cpp	2008-03-28 22:18:44 UTC (rev 2115)
@@ -207,8 +207,8 @@
 
 		if (emu_stat(pConfigFileName, &st) != 0)
 		{
-			BOX_ERROR("Failed to open configuration file '" <<
-				pConfigFileName << "': " << strerror(errno));
+			BOX_LOG_SYS_ERROR("Failed to open configuration file "
+				"'" << pConfigFileName << "'");
 			return 1;
 		}
 
@@ -221,7 +221,7 @@
 		}
 	}
 
-	SC_HANDLE scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
+	SC_HANDLE scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
 
 	if (!scm) 
 	{

Modified: box/trunk/bin/bbackupquery/BackupQueries.cpp
===================================================================
--- box/trunk/bin/bbackupquery/BackupQueries.cpp	2008-03-28 21:51:32 UTC (rev 2114)
+++ box/trunk/bin/bbackupquery/BackupQueries.cpp	2008-03-28 22:18:44 UTC (rev 2115)
@@ -810,8 +810,8 @@
 		}
 		else
 		{
-			BOX_ERROR("Error changing to directory '" <<
-				args[0] << ": " << strerror(errno));
+			BOX_LOG_SYS_ERROR("Failed to change to directory "
+				"'" << args[0] << "'");
 		}
 
 		SetReturnCode(COMMAND_RETURN_ERROR);
@@ -822,8 +822,7 @@
 	char wd[PATH_MAX];
 	if(::getcwd(wd, PATH_MAX) == 0)
 	{
-		BOX_ERROR("Error getting current directory: " <<
-			strerror(errno));
+		BOX_LOG_SYS_ERROR("Error getting current directory");
 		SetReturnCode(COMMAND_RETURN_ERROR);
 		return;
 	}
@@ -1399,9 +1398,8 @@
 		}
 		else
 		{
-			BOX_WARNING("Failed to access local directory '" <<
-				localDirDisplay << ": " << strerror(errno) <<
-				"'.");
+			BOX_LOG_SYS_WARNING("Failed to access local directory "
+				"'" << localDirDisplay << "'");
 			rParams.mUncheckedFiles ++;
 		}
 		return;
@@ -1449,8 +1447,8 @@
 	DIR *dirhandle = ::opendir(rLocalDir.c_str());
 	if(dirhandle == 0)
 	{
-		BOX_WARNING("Failed to open local directory '" << 
-			localDirDisplay << "': " << strerror(errno));
+		BOX_LOG_SYS_WARNING("Failed to open local directory '" << 
+			localDirDisplay << "'");
 		rParams.mUncheckedFiles ++;
 		return;
 	}
@@ -1518,8 +1516,8 @@
 		// Close directory
 		if(::closedir(dirhandle) != 0)
 		{
-			BOX_ERROR("Failed to close local directory '" <<
-				localDirDisplay << "': " << strerror(errno));
+			BOX_LOG_SYS_ERROR("Failed to close local directory "
+				"'" << localDirDisplay << "'");
 		}
 		dirhandle = 0;
 	

Modified: box/trunk/bin/bbackupquery/bbackupquery.cpp
===================================================================
--- box/trunk/bin/bbackupquery/bbackupquery.cpp	2008-03-28 21:51:32 UTC (rev 2114)
+++ box/trunk/bin/bbackupquery/bbackupquery.cpp	2008-03-28 22:18:44 UTC (rev 2115)
@@ -174,8 +174,8 @@
 			logFile = ::fopen(optarg, "w");
 			if(logFile == 0)
 			{
-				BOX_ERROR("Failed to open log file '" <<
-					optarg << "': " << strerror(errno));
+				BOX_LOG_SYS_ERROR("Failed to open log file "
+					"'" << optarg << "'");
 			}
 			break;
 

Modified: box/trunk/lib/backupclient/BackupClientRestore.cpp
===================================================================
--- box/trunk/lib/backupclient/BackupClientRestore.cpp	2008-03-28 21:51:32 UTC (rev 2114)
+++ box/trunk/lib/backupclient/BackupClientRestore.cpp	2008-03-28 22:18:44 UTC (rev 2115)
@@ -267,14 +267,14 @@
 					"restore this object.");
 				if(::unlink(rLocalDirectoryName.c_str()) != 0)
 				{
-					BOX_ERROR("Failed to delete file " <<
-						rLocalDirectoryName << ": " <<
-						strerror(errno));
+					BOX_LOG_SYS_ERROR("Failed to delete "
+						"file '" << 
+						rLocalDirectoryName << "'");
 					return Restore_UnknownError;
 				}
 				BOX_TRACE("In restore, directory name " 
-					"collision with file " <<
-					rLocalDirectoryName);
+					"collision with file '" <<
+					rLocalDirectoryName << "'");
 			}
 			break;
 		case ObjectExists_NoObject:
@@ -378,9 +378,8 @@
 		exists == ObjectExists_File) && 
 		::mkdir(rLocalDirectoryName.c_str(), S_IRWXU) != 0)
 	{
-		BOX_ERROR("Failed to create directory '" <<
-			rLocalDirectoryName << "': " << 
-			strerror(errno));
+		BOX_LOG_SYS_ERROR("Failed to create directory '" <<
+			rLocalDirectoryName << "'");
 		return Restore_UnknownError;
 	}
 
@@ -451,7 +450,9 @@
 			{
 				// Local name
 				BackupStoreFilenameClear nm(en->GetName());
-				std::string localFilename(rLocalDirectoryName + DIRECTORY_SEPARATOR_ASCHAR + nm.GetClearFilename());
+				std::string localFilename(rLocalDirectoryName +
+					DIRECTORY_SEPARATOR_ASCHAR +
+					nm.GetClearFilename());
 				
 				// Unlink anything which already exists:
 				// For resuming restores, we can't overwrite
@@ -459,20 +460,23 @@
 				if(ObjectExists(localFilename) != ObjectExists_NoObject &&
 					::unlink(localFilename.c_str()) != 0)
 				{
-					BOX_ERROR("Failed to delete file '" <<
-						localFilename << "': " <<
-						strerror(errno));
+					BOX_LOG_SYS_ERROR("Failed to delete "
+						"file '" << localFilename << 
+						"'");
 					return Restore_UnknownError;
 				}
 				
 				// Request it from the store
-				rConnection.QueryGetFile(DirectoryID, en->GetObjectID());
+				rConnection.QueryGetFile(DirectoryID,
+					en->GetObjectID());
 		
 				// Stream containing encoded file
-				std::auto_ptr<IOStream> objectStream(rConnection.ReceiveStream());
+				std::auto_ptr<IOStream> objectStream(
+					rConnection.ReceiveStream());
 		
-				// Decode the file -- need to do different things depending on whether 
-				// the directory entry has additional attributes
+				// Decode the file -- need to do different
+				// things depending on whether the directory
+				// entry has additional attributes
 				try
 				{
 					if(en->HasAttributes())

Modified: box/trunk/lib/common/BoxTime.cpp
===================================================================
--- box/trunk/lib/common/BoxTime.cpp	2008-03-28 21:51:32 UTC (rev 2114)
+++ box/trunk/lib/common/BoxTime.cpp	2008-03-28 22:18:44 UTC (rev 2115)
@@ -39,8 +39,8 @@
 		struct timeval tv;
 		if (gettimeofday(&tv, NULL) != 0)
 		{
-			BOX_ERROR("Failed to gettimeofday(), dropping "
-				"precision: " << strerror(errno));
+			BOX_LOG_SYS_ERROR("Failed to gettimeofday(), "
+				"dropping precision");
 		}
 		else
 		{

Modified: box/trunk/lib/common/EventWatchFilesystemObject.cpp
===================================================================
--- box/trunk/lib/common/EventWatchFilesystemObject.cpp	2008-03-28 21:51:32 UTC (rev 2114)
+++ box/trunk/lib/common/EventWatchFilesystemObject.cpp	2008-03-28 22:18:44 UTC (rev 2115)
@@ -26,9 +26,10 @@
 // --------------------------------------------------------------------------
 //
 // Function
-//		Name:    EventWatchFilesystemObject::EventWatchFilesystemObject(const char *)
-//		Purpose: Constructor -- opens the file object
-//		Created: 12/3/04
+//	Name:    EventWatchFilesystemObject::EventWatchFilesystemObject
+//		 (const char *)
+//	Purpose: Constructor -- opens the file object
+//	Created: 12/3/04
 //
 // --------------------------------------------------------------------------
 EventWatchFilesystemObject::EventWatchFilesystemObject(const char *Filename)
@@ -39,9 +40,8 @@
 #ifdef HAVE_KQUEUE
 	if(mDescriptor == -1)
 	{
-		BOX_ERROR("EventWatchFilesystemObject: "
-			"Failed to open file '" << Filename << "': " <<
-			strerror(errno));
+		BOX_LOG_SYS_ERROR("EventWatchFilesystemObject: "
+			"Failed to open file '" << Filename << "'");
 		THROW_EXCEPTION(CommonException, OSFileOpenError)
 	}
 #else
@@ -53,9 +53,9 @@
 // --------------------------------------------------------------------------
 //
 // Function
-//		Name:    EventWatchFilesystemObject::~EventWatchFilesystemObject()
-//		Purpose: Destructor
-//		Created: 12/3/04
+//	Name:    EventWatchFilesystemObject::~EventWatchFilesystemObject()
+//	Purpose: Destructor
+//	Created: 12/3/04
 //
 // --------------------------------------------------------------------------
 EventWatchFilesystemObject::~EventWatchFilesystemObject()
@@ -70,12 +70,14 @@
 // --------------------------------------------------------------------------
 //
 // Function
-//		Name:    EventWatchFilesystemObject::EventWatchFilesystemObject(const EventWatchFilesystemObject &)
-//		Purpose: Copy constructor
-//		Created: 12/3/04
+//	Name:    EventWatchFilesystemObject::EventWatchFilesystemObject
+//		 (const EventWatchFilesystemObject &)
+//	Purpose: Copy constructor
+//	Created: 12/3/04
 //
 // --------------------------------------------------------------------------
-EventWatchFilesystemObject::EventWatchFilesystemObject(const EventWatchFilesystemObject &rToCopy)
+EventWatchFilesystemObject::EventWatchFilesystemObject(
+	const EventWatchFilesystemObject &rToCopy)
 	: mDescriptor(::dup(rToCopy.mDescriptor))
 {
 	if(mDescriptor == -1)
@@ -89,17 +91,20 @@
 // --------------------------------------------------------------------------
 //
 // Function
-//		Name:    EventWatchFilesystemObject::FillInKEvent(struct kevent &, int)
-//		Purpose: For WaitForEvent
-//		Created: 12/3/04
+//	Name:    EventWatchFilesystemObject::FillInKEvent(struct kevent &, int)
+//	Purpose: For WaitForEvent
+//	Created: 12/3/04
 //
 // --------------------------------------------------------------------------
-void EventWatchFilesystemObject::FillInKEvent(struct kevent &rEvent, int Flags) const
+void EventWatchFilesystemObject::FillInKEvent(struct kevent &rEvent,
+	int Flags) const
 {
-	EV_SET(&rEvent, mDescriptor, EVFILT_VNODE, EV_CLEAR, NOTE_DELETE | NOTE_WRITE, 0, (void*)this);
+	EV_SET(&rEvent, mDescriptor, EVFILT_VNODE, EV_CLEAR,
+		NOTE_DELETE | NOTE_WRITE, 0, (void*)this);
 }
 #else
-void EventWatchFilesystemObject::FillInPoll(int &fd, short &events, int Flags) const
+void EventWatchFilesystemObject::FillInPoll(int &fd, short &events,
+	int Flags) const
 {
 	THROW_EXCEPTION(CommonException, KQueueNotSupportedOnThisPlatform)
 }

Modified: box/trunk/lib/common/FileStream.cpp
===================================================================
--- box/trunk/lib/common/FileStream.cpp	2008-03-28 21:51:32 UTC (rev 2114)
+++ box/trunk/lib/common/FileStream.cpp	2008-03-28 22:18:44 UTC (rev 2115)
@@ -30,7 +30,8 @@
 #else
 	: mOSFileHandle(::open(Filename, flags, mode)),
 #endif
-	  mIsEOF(false)
+	  mIsEOF(false),
+	  mFileName(Filename)
 {
 #ifdef WIN32
 	if(mOSFileHandle == INVALID_HANDLE_VALUE)
@@ -49,9 +50,6 @@
 			THROW_EXCEPTION(CommonException, OSFileOpenError)
 		}
 	}
-#ifdef WIN32
-	this->fileName = Filename;
-#endif
 }
 
 
@@ -65,7 +63,8 @@
 // --------------------------------------------------------------------------
 FileStream::FileStream(tOSFileHandle FileDescriptor)
 	: mOSFileHandle(FileDescriptor),
-	  mIsEOF(false)
+	  mIsEOF(false),
+	  mFileName("HANDLE")
 {
 #ifdef WIN32
 	if(mOSFileHandle == INVALID_HANDLE_VALUE)
@@ -77,9 +76,6 @@
 		BOX_ERROR("FileStream: called with invalid file handle");
 		THROW_EXCEPTION(CommonException, OSFileOpenError)
 	}
-#ifdef WIN32
-	this->fileName = "HANDLE";
-#endif
 }
 
 #if 0
@@ -150,27 +146,32 @@
 		NULL
 		);
 
-	if ( valid )
+	if(valid)
 	{
 		r = numBytesRead;
 	}
-	else if (GetLastError() == ERROR_BROKEN_PIPE)
+	else if(GetLastError() == ERROR_BROKEN_PIPE)
 	{
 		r = 0;
 	}
 	else
 	{
-		BOX_ERROR("Failed to read from file: " <<
-			GetErrorMessage(GetLastError()));
+		BOX_LOG_WIN_ERROR("Failed to read from file: " << mFileName);
 		r = -1;
 	}
 #else
 	int r = ::read(mOSFileHandle, pBuffer, NBytes);
+	if(r == -1)
+	{
+		BOX_LOG_SYS_ERROR("Failed to read from file: " << mFileName);
+	}
 #endif
+
 	if(r == -1)
 	{
 		THROW_EXCEPTION(CommonException, OSFileReadError)
 	}
+
 	if(r == 0)
 	{
 		mIsEOF = true;

Modified: box/trunk/lib/common/FileStream.h
===================================================================
--- box/trunk/lib/common/FileStream.h	2008-03-28 21:51:32 UTC (rev 2114)
+++ box/trunk/lib/common/FileStream.h	2008-03-28 22:18:44 UTC (rev 2115)
@@ -57,10 +57,8 @@
 	bool mIsEOF;
 	FileStream(const FileStream &rToCopy) { /* do not call */ }
 
-#ifdef WIN32
 	// for debugging..
-	std::string fileName;
-#endif
+	std::string mFileName;
 };
 
 

Modified: box/trunk/lib/common/Guards.h
===================================================================
--- box/trunk/lib/common/Guards.h	2008-03-28 21:51:32 UTC (rev 2114)
+++ box/trunk/lib/common/Guards.h	2008-03-28 22:18:44 UTC (rev 2115)
@@ -37,8 +37,8 @@
 	{
 		if(mOSFileHandle < 0)
 		{
-			BOX_ERROR("FileHandleGuard: failed to open file '" <<
-				rFilename << "': " << strerror(errno));
+			BOX_LOG_SYS_ERROR("FileHandleGuard: failed to open " 
+				"file '" << rFilename << "'");
 			THROW_EXCEPTION(CommonException, OSFileOpenError)
 		}
 	}

Modified: box/trunk/lib/common/Logging.h
===================================================================
--- box/trunk/lib/common/Logging.h	2008-03-28 21:51:32 UTC (rev 2114)
+++ box/trunk/lib/common/Logging.h	2008-03-28 22:18:44 UTC (rev 2115)
@@ -48,6 +48,20 @@
 	if (Logging::IsEnabled(Log::TRACE)) \
 	{ BOX_LOG(Log::TRACE, stuff) }
 
+#define BOX_LOG_SYS_WARNING(stuff) \
+	BOX_WARNING(stuff << ": " << strerror(errno) << " (" << errno << ")")
+#define BOX_LOG_SYS_ERROR(stuff) \
+	BOX_ERROR(stuff << ": " << strerror(errno) << " (" << errno << ")")
+#define BOX_LOG_SYS_FATAL(stuff) \
+	BOX_FATAL(stuff << ": " << strerror(errno) << " (" << errno << ")")
+
+#ifdef WIN32
+	#define BOX_LOG_WIN_ERROR(stuff) \
+		BOX_ERROR(stuff << ": " << GetErrorMessage(GetLastError()))
+	#define BOX_LOG_WIN_ERROR_NUMBER(stuff, number) \
+		BOX_ERROR(stuff << ": " << GetErrorMessage(number))
+#endif
+
 #define BOX_FORMAT_ACCOUNT(accno) \
 	std::hex << \
 	std::showbase << \

Modified: box/trunk/lib/server/Daemon.cpp
===================================================================
--- box/trunk/lib/server/Daemon.cpp	2008-03-28 21:51:32 UTC (rev 2114)
+++ box/trunk/lib/server/Daemon.cpp	2008-03-28 22:18:44 UTC (rev 2115)
@@ -465,8 +465,7 @@
 			// Set new session
 			if(::setsid() == -1)
 			{
-				BOX_ERROR("Failed to setsid(): " << 
-					strerror(errno));
+				BOX_LOG_SYS_ERROR("Failed to setsid()");
 				THROW_EXCEPTION(ServerException, DaemoniseFailed)
 			}
 
@@ -475,6 +474,7 @@
 			{
 			case -1:
 				// error
+				BOX_LOG_SYS_ERROR("Failed to fork() a child");
 				THROW_EXCEPTION(ServerException, DaemoniseFailed)
 				break;
 
@@ -497,9 +497,11 @@
 		struct sigaction sa;
 		sa.sa_handler = SignalHandler;
 		sa.sa_flags = 0;
-		sigemptyset(&sa.sa_mask);		// macro
-		if(::sigaction(SIGHUP, &sa, NULL) != 0 || ::sigaction(SIGTERM, &sa, NULL) != 0)
+		sigemptyset(&sa.sa_mask); // macro
+		if(::sigaction(SIGHUP, &sa, NULL) != 0 ||
+			::sigaction(SIGTERM, &sa, NULL) != 0)
 		{
+			BOX_LOG_SYS_ERROR("Failed to set signal handlers");
 			THROW_EXCEPTION(ServerException, DaemoniseFailed)
 		}
 #endif // !WIN32
@@ -515,7 +517,8 @@
 
 		if(::write(pidFile, pid, pidsize) != pidsize)
 		{
-			BOX_FATAL("can't write pid file");
+			BOX_LOG_SYS_FATAL("Failed to write PID file: " <<
+				pidFileName);
 			THROW_EXCEPTION(ServerException, DaemoniseFailed)
 		}
 		
@@ -544,6 +547,7 @@
 			int devnull = ::open(PLATFORM_DEV_NULL, O_RDWR, 0);
 			if(devnull == -1)
 			{
+				BOX_LOG_SYS_ERROR("Failed to open /dev/null");
 				THROW_EXCEPTION(CommonException, OSFileError);
 			}
 			// Then duplicate them to all three handles
@@ -890,6 +894,8 @@
 		{
 			return 0;
 		}
+		BOX_LOG_SYS_ERROR("Failed to stat configuration file: " <<
+			GetConfigFileName());
 		THROW_EXCEPTION(CommonException, OSFileError)
 	}
 	

Modified: box/trunk/lib/server/ServerControl.h
===================================================================
--- box/trunk/lib/server/ServerControl.h	2008-03-28 21:51:32 UTC (rev 2114)
+++ box/trunk/lib/server/ServerControl.h	2008-03-28 22:18:44 UTC (rev 2115)
@@ -142,8 +142,7 @@
 	bool killed = (::kill(pid, SIGTERM) == 0);
 	if (!killed)
 	{
-		BOX_ERROR("Failed to kill process " << pid << ": " <<
-			strerror(errno));
+		BOX_LOG_SYS_ERROR("Failed to kill process " << pid);
 	}
 	TEST_THAT(killed);
 	return killed;

Modified: box/trunk/lib/server/SocketListen.h
===================================================================
--- box/trunk/lib/server/SocketListen.h	2008-03-28 21:51:32 UTC (rev 2114)
+++ box/trunk/lib/server/SocketListen.h	2008-03-28 22:18:44 UTC (rev 2115)
@@ -108,45 +108,57 @@
 			if(::close(mSocketHandle) == -1)
 #endif
 			{
-				THROW_EXCEPTION(ServerException, SocketCloseError)
+				BOX_LOG_SYS_ERROR("Failed to close network "
+					"socket");
+				THROW_EXCEPTION(ServerException,
+					SocketCloseError)
 			}
 		}
 		mSocketHandle = -1;
 	}
 
-	// --------------------------------------------------------------------------
+	// ------------------------------------------------------------------
 	//
 	// Function
 	//		Name:    SocketListen::Listen(int, char*, int)
 	//		Purpose: Initialises, starts the socket listening.
 	//		Created: 2003/07/31
 	//
-	// --------------------------------------------------------------------------
+	// ------------------------------------------------------------------
 	void Listen(int Type, const char *Name, int Port = 0)
 	{
-		if(mSocketHandle != -1) {THROW_EXCEPTION(ServerException, SocketAlreadyOpen)}
+		if(mSocketHandle != -1)
+		{
+			THROW_EXCEPTION(ServerException, SocketAlreadyOpen);
+		}
 		
 		// Setup parameters based on type, looking up names if required
 		int sockDomain = 0;
 		SocketAllAddr addr;
 		int addrLen = 0;
-		Socket::NameLookupToSockAddr(addr, sockDomain, Type, Name, Port, addrLen);
+		Socket::NameLookupToSockAddr(addr, sockDomain, Type, Name,
+			Port, addrLen);
 	
 		// Create the socket
-		mSocketHandle = ::socket(sockDomain, SOCK_STREAM, 0 /* let OS choose protocol */);
+		mSocketHandle = ::socket(sockDomain, SOCK_STREAM,
+			0 /* let OS choose protocol */);
 		if(mSocketHandle == -1)
 		{
+			BOX_LOG_SYS_ERROR("Failed to create a network socket");
 			THROW_EXCEPTION(ServerException, SocketOpenError)
 		}
 		
 		// Set an option to allow reuse (useful for -HUP situations!)
 #ifdef WIN32
-		if(::setsockopt(mSocketHandle, SOL_SOCKET, SO_REUSEADDR, "", 0) == -1)
+		if(::setsockopt(mSocketHandle, SOL_SOCKET, SO_REUSEADDR, "",
+			0) == -1)
 #else
 		int option = true;
-		if(::setsockopt(mSocketHandle, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)) == -1)
+		if(::setsockopt(mSocketHandle, SOL_SOCKET, SO_REUSEADDR,
+			&option, sizeof(option)) == -1)
 #endif
 		{
+			BOX_LOG_SYS_ERROR("Failed to set socket options");
 			THROW_EXCEPTION(ServerException, SocketOpenError)
 		}
 
@@ -161,19 +173,25 @@
 		}	
 	}
 	
-	// --------------------------------------------------------------------------
+	// ------------------------------------------------------------------
 	//
 	// Function
 	//		Name:    SocketListen::Accept(int)
-	//		Purpose: Accepts a connection, returning a pointer to a class of
-	//				 the specified type. May return a null pointer if a signal happens,
-	//				 or there's a timeout. Timeout specified in milliseconds, defaults to infinite time.
+	//		Purpose: Accepts a connection, returning a pointer to
+	//			 a class of the specified type. May return a
+	//			 null pointer if a signal happens, or there's
+	//			 a timeout. Timeout specified in
+	//			 milliseconds, defaults to infinite time.
 	//		Created: 2003/07/31
 	//
-	// --------------------------------------------------------------------------
-	std::auto_ptr<SocketType> Accept(int Timeout = INFTIM, std::string *pLogMsg = 0)
+	// ------------------------------------------------------------------
+	std::auto_ptr<SocketType> Accept(int Timeout = INFTIM,
+		std::string *pLogMsg = 0)
 	{
-		if(mSocketHandle == -1) {THROW_EXCEPTION(ServerException, BadSocketHandle)}
+		if(mSocketHandle == -1)
+		{
+			THROW_EXCEPTION(ServerException, BadSocketHandle);
+		}
 		
 		// Do the accept, using the supplied locking type
 		int sock;
@@ -185,8 +203,10 @@
 			
 			if(!socklock.HaveLock())
 			{
-				// Didn't get the lock for some reason. Wait a while, then
-				// return nothing.
+				// Didn't get the lock for some reason.
+				// Wait a while, then return nothing.
+				BOX_ERROR("Failed to get a lock on incoming "
+					"connection");
 				::sleep(1);
 				return std::auto_ptr<SocketType>();
 			}
@@ -202,12 +222,18 @@
 				// signal?
 				if(errno == EINTR)
 				{
+					BOX_ERROR("Failed to accept "
+						"connection: interrupted by "
+						"signal");
 					// return nothing
 					return std::auto_ptr<SocketType>();
 				}
 				else
 				{
-					THROW_EXCEPTION(ServerException, SocketPollError)
+					BOX_LOG_SYS_ERROR("Failed to poll "
+						"connection");
+					THROW_EXCEPTION(ServerException,
+						SocketPollError)
 				}
 				break;
 			case 0:	// timed out
@@ -220,16 +246,19 @@
 			
 			sock = ::accept(mSocketHandle, &addr, &addrlen);
 		}
-		// Got socket (or error), unlock (implcit in destruction)
+
+		// Got socket (or error), unlock (implicit in destruction)
 		if(sock == -1)
 		{
+			BOX_LOG_SYS_ERROR("Failed to accept connection");
 			THROW_EXCEPTION(ServerException, SocketAcceptError)
 		}
 
 		// Log it
 		if(pLogMsg)
 		{
-			*pLogMsg = Socket::IncomingConnectionLogMessage(&addr, addrlen);
+			*pLogMsg = Socket::IncomingConnectionLogMessage(&addr,
+				addrlen);
 		}
 		else
 		{
@@ -243,27 +272,29 @@
 	// Functions to allow adding to WaitForEvent class, for efficient waiting
 	// on multiple sockets.
 #ifdef HAVE_KQUEUE
-	// --------------------------------------------------------------------------
+	// ------------------------------------------------------------------
 	//
 	// Function
 	//		Name:    SocketListen::FillInKEevent
 	//		Purpose: Fills in a kevent structure for this socket
 	//		Created: 9/3/04
 	//
-	// --------------------------------------------------------------------------
+	// ------------------------------------------------------------------
 	void FillInKEvent(struct kevent &rEvent, int Flags = 0) const
 	{
-		EV_SET(&rEvent, mSocketHandle, EVFILT_READ, 0, 0, 0, (void*)this);
+		EV_SET(&rEvent, mSocketHandle, EVFILT_READ, 0, 0, 0,
+			(void*)this);
 	}
 #else
-	// --------------------------------------------------------------------------
+	// ------------------------------------------------------------------
 	//
 	// Function
 	//		Name:    SocketListen::FillInPoll
-	//		Purpose: Fills in the data necessary for a poll operation
+	//		Purpose: Fills in the data necessary for a poll
+	//			 operation
 	//		Created: 9/3/04
 	//
-	// --------------------------------------------------------------------------
+	// ------------------------------------------------------------------
 	void FillInPoll(int &fd, short &events, int Flags = 0) const
 	{
 		fd = mSocketHandle;

Modified: box/trunk/lib/server/SocketStream.cpp
===================================================================
--- box/trunk/lib/server/SocketStream.cpp	2008-03-28 21:51:32 UTC (rev 2114)
+++ box/trunk/lib/server/SocketStream.cpp	2008-03-28 22:18:44 UTC (rev 2115)
@@ -150,9 +150,11 @@
 	Socket::NameLookupToSockAddr(addr, sockDomain, Type, Name, Port, addrLen);
 
 	// Create the socket
-	mSocketHandle = ::socket(sockDomain, SOCK_STREAM, 0 /* let OS choose protocol */);
+	mSocketHandle = ::socket(sockDomain, SOCK_STREAM,
+		0 /* let OS choose protocol */);
 	if(mSocketHandle == INVALID_SOCKET_VALUE)
 	{
+		BOX_LOG_SYS_ERROR("Failed to create a network socket");
 		THROW_EXCEPTION(ServerException, SocketOpenError)
 	}
 	
@@ -163,23 +165,17 @@
 #ifdef WIN32
 		DWORD err = WSAGetLastError();
 		::closesocket(mSocketHandle);
-#else
+		BOX_LOG_WIN_ERROR_NUMBER("Failed to connect to socket " 
+			"(type " << Type << ", name " << Name <<
+			", port " << Port << ")", err);
+#else // !WIN32
 		int err = errno;
 		::close(mSocketHandle);
-#endif
+		BOX_LOG_SYS_ERROR("Failed to connect to socket (type " <<
+			Type << ", name " << Name << ", port " << Port <<
+			")");
+#endif // WIN32
 
-#ifdef WIN32
-		BOX_ERROR("Failed to connect to socket (type " << Type <<
-			", name " << Name << ", port " << Port << "): " <<
-				GetErrorMessage(err)
-			);
-#else
-		BOX_ERROR("Failed to connect to socket (type " << Type <<
-			", name " << Name << ", port " << Port << "): " <<
-				strerror(err) << " (" << err << ")"
-			);
-#endif
-
 		mSocketHandle = INVALID_SOCKET_VALUE;
 		THROW_EXCEPTION(ConnectionException, Conn_SocketConnectError)
 	}
@@ -220,7 +216,9 @@
 			else
 			{
 				// Bad!
-				THROW_EXCEPTION(ServerException, SocketPollError)
+				BOX_LOG_SYS_ERROR("Failed to poll socket");
+				THROW_EXCEPTION(ServerException,
+					SocketPollError)
 			}
 			break;
 			
@@ -250,9 +248,12 @@
 		else
 		{
 			// Other error
-			THROW_EXCEPTION(ConnectionException, Conn_SocketReadError)
+			BOX_LOG_SYS_ERROR("Failed to read from socket");
+			THROW_EXCEPTION(ConnectionException,
+				Conn_SocketReadError);
 		}
 	}
+
 	// Closed for reading?
 	if(r == 0)
 	{
@@ -297,7 +298,9 @@
 		{
 			// Error.
 			mWriteClosed = true;	// assume can't write again
-			THROW_EXCEPTION(ConnectionException, Conn_SocketWriteError)
+			BOX_LOG_SYS_ERROR("Failed to write to socket");
+			THROW_EXCEPTION(ConnectionException,
+				Conn_SocketWriteError);
 		}
 		
 		// Knock off bytes sent
@@ -310,7 +313,9 @@
 		// Need to wait until it can send again?
 		if(bytesLeft > 0)
 		{
-			TRACE3("Waiting to send data on socket %d, (%d to send of %d)\n", mSocketHandle, bytesLeft, NBytes);
+			BOX_TRACE("Waiting to send data on socket " << 
+				mSocketHandle << " (" << bytesLeft <<
+				" of " << NBytes << " bytes left)");
 			
 			// Wait for data to send.
 			struct pollfd p;
@@ -323,7 +328,10 @@
 				// Don't exception if it's just a signal
 				if(errno != EINTR)
 				{
-					THROW_EXCEPTION(ServerException, SocketPollError)
+					BOX_LOG_SYS_ERROR("Failed to poll "
+						"socket");
+					THROW_EXCEPTION(ServerException,
+						SocketPollError)
 				}
 			}
 		}
@@ -350,6 +358,7 @@
 	if(::close(mSocketHandle) == -1)
 #endif
 	{
+		BOX_LOG_SYS_ERROR("Failed to close socket");
 		THROW_EXCEPTION(ServerException, SocketCloseError)
 	}
 	mSocketHandle = INVALID_SOCKET_VALUE;
@@ -380,6 +389,7 @@
 	// Shut it down!
 	if(::shutdown(mSocketHandle, how) == -1)
 	{
+		BOX_LOG_SYS_ERROR("Failed to shutdown socket");
 		THROW_EXCEPTION(ConnectionException, Conn_SocketShutdownError)
 	}
 }
@@ -458,12 +468,15 @@
 	struct ucred cred;
 	socklen_t credLen = sizeof(cred);
 
-	if(::getsockopt(mSocketHandle, SOL_SOCKET, SO_PEERCRED, &cred, &credLen) == 0)
+	if(::getsockopt(mSocketHandle, SOL_SOCKET, SO_PEERCRED, &cred,
+		&credLen) == 0)
 	{
 		rUidOut = cred.uid;
 		rGidOut = cred.gid;
 		return true;
 	}
+
+	BOX_LOG_SYS_ERROR("Failed to get peer credentials on socket");
 #endif
 
 	// Not available




More information about the Boxbackup-commit mailing list