[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Cluster-devel] conga/ricci common/ClientSocket.cpp common/Log ...



CVSROOT:	/cvs/cluster
Module name:	conga
Changes by:	rmccabe sourceware org	2007-09-11 00:26:52

Modified files:
	ricci/common   : ClientSocket.cpp Logger.cpp Module.cpp 
	                 sys_util.c 
	ricci/include  : Logger.h 
	ricci/modules/cluster: Virt.cpp 
	ricci/modules/cluster/clumon/src/common: ClusterMonitor.cpp 
	ricci/ricci    : Makefile Ricci.cpp RicciWorker.cpp 
	                 SSLInstance.cpp 

Log message:
	Cleanup and refactor some of the network and file io

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/ClientSocket.cpp.diff?cvsroot=cluster&r1=1.9&r2=1.10
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/Logger.cpp.diff?cvsroot=cluster&r1=1.4&r2=1.5
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/Module.cpp.diff?cvsroot=cluster&r1=1.7&r2=1.8
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/sys_util.c.diff?cvsroot=cluster&r1=1.1&r2=1.2
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/include/Logger.h.diff?cvsroot=cluster&r1=1.3&r2=1.4
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/cluster/Virt.cpp.diff?cvsroot=cluster&r1=1.5&r2=1.6
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/cluster/clumon/src/common/ClusterMonitor.cpp.diff?cvsroot=cluster&r1=1.3&r2=1.4
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/ricci/Makefile.diff?cvsroot=cluster&r1=1.22&r2=1.23
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/ricci/Ricci.cpp.diff?cvsroot=cluster&r1=1.27&r2=1.28
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/ricci/RicciWorker.cpp.diff?cvsroot=cluster&r1=1.13&r2=1.14
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/ricci/SSLInstance.cpp.diff?cvsroot=cluster&r1=1.10&r2=1.11

--- conga/ricci/common/ClientSocket.cpp	2007/08/30 22:50:13	1.9
+++ conga/ricci/common/ClientSocket.cpp	2007/09/11 00:26:52	1.10
@@ -34,6 +34,9 @@
 #include <String.h>
 #include <netdb.h>
 
+extern "C" {
+	#include "sys_util.h"
+}
 
 ClientSocket::ClientSocket() :
 	Socket(-1)
@@ -169,29 +172,27 @@
 	if (_sock == -1)
 		throw String("ClientSocket::recv(): socket already closed");
 
-	while (true) {
-		char buffer[4096];
-		int ret = ::recv(_sock, buffer, sizeof(buffer), 0);
-		if (ret == -1) {
-			if (errno == EINTR)
-				continue;
-			else if (errno == EAGAIN)
-				return "";
-			throw String("ClientSocket::recv(): recv error: ")
-					+ String(strerror(errno));
-		}
-
-		if (ret == 0) {
-			close();
-			throw String("ClientSocket::recv(): socket has been shutdown");
-		}
+	char buffer[4096];
+	int ret;
 
-		//log(String("received ") + ret + " bytes from socket " + _sock,
-		//LogLevel(LogSocket|LogTransfer));
-		String data(buffer, ret);
-		shred(buffer, ret);
-		return data;
-	}
+	ret = read_restart(_sock, buffer, sizeof(buffer));
+	if (ret < 0) {
+		if (ret == -EAGAIN)
+			return "";
+		throw String("ClientSocket::recv(): recv error: ")
+				+ String(strerror(-ret));
+	}
+
+	if (ret == 0) {
+		close();
+		throw String("ClientSocket::recv(): socket has been shutdown");
+	}
+
+	//log(String("received ") + ret + " bytes from socket " + _sock,
+	//LogLevel(LogSocket|LogTransfer));
+	String data(buffer, ret);
+	memset(buffer, 0, ret);
+	return data;
 }
 
 String
@@ -212,21 +213,17 @@
 	if (_sock == -1)
 		throw String("ClientSocket::send(): socket already closed");
 
-	while (true) {
-		int ret = ::send(_sock, msg.c_str(), msg.size(), 0);
-		if (ret == -1) {
-			if (errno == EINTR)
-				continue;
-			else if (errno == EAGAIN || errno == EWOULDBLOCK)
-				return msg;
-			throw String("ClientSocket::send(): socket error: ")
-					+ String(strerror(errno));
-		}
-
-		//log(String("sent ") + ret + " bytes thru socket " + _sock,
-		//LogLevel(LogSocket|LogTransfer));
-		return msg.substr(ret);
+	int ret = write_restart(_sock, msg.c_str(), msg.size());
+	if (ret < 0) {
+		if (ret == -EAGAIN || ret == -EWOULDBLOCK)
+			return msg;
+		throw String("ClientSocket::send(): socket error: ")
+				+ String(strerror(-ret));
 	}
+
+	//log(String("sent ") + ret + " bytes thru socket " + _sock,
+	//LogLevel(LogSocket|LogTransfer));
+	return msg.substr(ret);
 }
 
 String
--- conga/ricci/common/Logger.cpp	2007/09/07 19:07:21	1.4
+++ conga/ricci/common/Logger.cpp	2007/09/11 00:26:52	1.5
@@ -22,16 +22,20 @@
 
 
 #include "Logger.h"
-#include "Time.h"
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
 #include <unistd.h>
-#include <errno.h>
 #include <stdlib.h>
-#include "String.h"
+#include <time.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
+extern "C" { 
+	#include "sys_util.h"
+}
+
+#include "String.h"
 
 using namespace std;
 
@@ -98,49 +102,48 @@
 	free(_domain_c);
 }
 
-void
+ssize_t
 Logger::log(const String& msg, LogLevel level)
 {
-	log_sigsafe(msg.c_str(), level);
+	return log_sigsafe(msg.c_str(), level);
 }
 
-void
+ssize_t
 Logger::log_sigsafe(const char *msg, LogLevel level)
 {
 	if (_fd > 0 && (_level & level)) {
-		char time[64];
-		time_t t = time_sec();
+		char cur_time[64];
+		time_t t = time(NULL);
+		char buf[4096];
 		char *p;
 		int ret;
+		size_t buflen;
 
-		ctime_r(&t, time);
-		time[sizeof(time) - 1] = '\0';
+		ctime_r(&t, cur_time);
+		cur_time[sizeof(cur_time) - 1] = '\0';
 
-		p = strchr(time, '\n');
+		p = strchr(cur_time, '\n');
 		if (p != NULL)
 			*p = '\0';		
 
-		char m[4096] = { 0, };
-		if (_fd > 2 && _domain_c != NULL)
-			ret = snprintf(m, sizeof(m), "%s %s: %s\n", time, _domain_c, msg);
-		else
-			ret = snprintf(m, sizeof(m), "%s: %s\n", time, msg);
-
-		if (ret < 0 || (size_t) ret >= sizeof(m)) {
-			m[sizeof(m) - 1] = '\0';
-			ret = strlen(m);
-		}
-
-		while (ret > 0) {
-			ssize_t w = write(_fd, m, ret);
-			if (w < 0) {
-				if (errno == EINTR)
-					continue;
-				break;
-			}
-			ret -= w;
+		if (_fd > 2 && _domain_c != NULL) {
+			ret = snprintf(buf, sizeof(buf), "%s %s: %s\n",
+					cur_time, _domain_c, msg);
+		} else
+			ret = snprintf(buf, sizeof(buf), "%s: %s\n", cur_time, msg);
+
+		if (ret < 0)
+			return -ENOMEM;
+		buflen = (size_t) ret;
+
+		if (buflen >= sizeof(buf)) {
+			buf[sizeof(buf) - 1] = '\0';
+			buflen = strlen(buf);
 		}
+		return write_restart(_fd, buf, buflen);
 	}
+
+	return 0;
 }
 
 
@@ -162,16 +165,16 @@
 	return String(buff) + s;
 }
 
-void
+ssize_t
 log(const String& msg, LogLevel level)
 {
-	logger->log(msg, level);
+	return logger->log(msg, level);
 }
 
-void
+ssize_t
 log_sigsafe(const char *msg, LogLevel level)
 {
-	logger->log_sigsafe(msg, level);
+	return logger->log_sigsafe(msg, level);
 }
 
 void
--- conga/ricci/common/Module.cpp	2007/08/31 04:57:37	1.7
+++ conga/ricci/common/Module.cpp	2007/09/11 00:26:52	1.8
@@ -31,6 +31,11 @@
 #include <sys/poll.h>
 #include <errno.h>
 #include <unistd.h>
+
+extern "C" {
+	#include "sys_util.h"
+}
+
 typedef struct pollfd poll_fd;
 
 #include <iostream>
@@ -223,7 +228,7 @@
 		}
 	}
 
-	int old_err;
+	int old_err = -1;
 	if (!display_err) {
 		// redirect stderr to /dev/null
 		old_err = dup(2);
@@ -290,16 +295,13 @@
 			char buff[4096];
 			int ret;
 
-			ret = read(poll_data.fd, buff, sizeof(buff));
-			if (ret == -1) {
-				if (errno == EINTR)
-					continue;
-				throw String("error reading stdin: ") + String(strerror(errno));
-			}
+			ret = read_restart(poll_data.fd, buff, sizeof(buff));
+			if (ret < 0)
+				throw String("error reading stdin: ") + String(strerror(-ret));
 
 			if (ret > 0) {
 				data.append(buff, ret);
-				shred(buff, sizeof(buff));
+				memset(buff, 0, sizeof(buff));
 			}
 
 			if ((size_t) ret < sizeof(buff)) {
@@ -315,9 +317,8 @@
 
 		if (poll_data.revents & (POLLERR | POLLHUP | POLLNVAL))
 			throw String("stdin error: ") + String(strerror(errno));
-	} // while
+	}
 
-	// cout << data << endl;
 	throw String("invalid input");
 }
 
--- conga/ricci/common/sys_util.c	2007/09/09 18:20:00	1.1
+++ conga/ricci/common/sys_util.c	2007/09/11 00:26:52	1.2
@@ -34,12 +34,15 @@
 	while (len > 0) {
 		size_t ret = read(fd, buf, len);
 		if (ret < 0) {
-			if (errno == EINTR || errno == EAGAIN)
+			if (errno == EINTR)
 				continue;
+			if (errno == EAGAIN)
+				break;
 			return -errno;
 		}
+
 		if (ret == 0)
-			return -EPIPE;
+			break;
 
 		buf += ret;
 		len -= (size_t) ret;
@@ -53,12 +56,15 @@
 	while (len > 0) {
 		size_t ret = write(fd, buf, len);
 		if (ret < 0) {
-			if (errno == EINTR || errno == EAGAIN)
+			if (errno == EINTR)
 				continue;
+			if (errno == EAGAIN)
+				break;
 			return -errno;
 		}
+
 		if (ret == 0)
-			return -EPIPE;
+			break;
 
 		buf += ret;
 		len -= (size_t) ret;
--- conga/ricci/include/Logger.h	2007/08/31 13:32:36	1.3
+++ conga/ricci/include/Logger.h	2007/09/11 00:26:52	1.4
@@ -49,8 +49,8 @@
 		Logger(int fd, const String& domain, LogLevel level);
 		virtual ~Logger();
 
-		void log(const String& msg, LogLevel level=LogBasic);
-		void log_sigsafe(const char* msg, LogLevel level=LogBasic);
+		ssize_t log(const String& msg, LogLevel level=LogBasic);
+		ssize_t log_sigsafe(const char* msg, LogLevel level=LogBasic);
 		void operator<< (const String& msg) { log(msg); }
 
 	private:
@@ -67,8 +67,8 @@
 // helper functions
 String operator+ (const String&, int);
 String operator+ (int, const String&);
-void log(const String& msg, LogLevel level=LogBasic);
-void log_sigsafe(const char* msg, LogLevel level=LogBasic);
+ssize_t log(const String& msg, LogLevel level=LogBasic);
+ssize_t log_sigsafe(const char* msg, LogLevel level=LogBasic);
 void set_logger(counting_auto_ptr<Logger>);
 
 #endif
--- conga/ricci/modules/cluster/Virt.cpp	2007/08/23 15:30:27	1.5
+++ conga/ricci/modules/cluster/Virt.cpp	2007/09/11 00:26:52	1.6
@@ -24,6 +24,8 @@
 #include <sys/stat.h>
 #include <string.h>
 #include <errno.h>
+
+#include "sys_util.h"
 #include "base64.h"
 }
 
@@ -59,9 +61,9 @@
 	char *buf = NULL;
 	size_t keylen;
 	size_t keylen_dec = 0;
-	ssize_t ret;
 	bool decoded = false;
 	int fd;
+	ssize_t ret;
 	mode_t old_mask;
 	char tmpname[] = "/etc/cluster/.fence_xvm.keyXXXXXX";
 
@@ -89,14 +91,13 @@
 	umask(old_mask);
 
 	fchmod(fd, 0600);
-	ret = write(fd, buf, keylen_dec);
-	if (ret < 0 || (size_t) ret != keylen_dec) {
-		int err = errno;
+	ret = write_restart(fd, buf, keylen_dec);
+	if (ret < 0) {
 		unlink(tmpname);
 		close(fd);
 		memset(buf, 0, keylen_dec);
 		free(buf);
-		throw String("error setting new key: ") + String(strerror(err));
+		throw String("error setting new key: ") + String(strerror(-ret));
 	}
 
 	close(fd);
@@ -125,22 +126,20 @@
 	if (fd < 0)
 		throw String("error generating key: ") + String(strerror(errno));
 
-	ret = read(fd, buf, keylen);
-	err = errno;
+	ret = read_restart(fd, buf, keylen);
 	close(fd);
-	if ((size_t) ret != keylen)
-		throw String("error generating key: ") + String(strerror(err));
+	if (ret < 0)
+		throw String("error generating key: ") + String(strerror(-ret));
 
 	fd = open(XVM_KEY_PATH, O_WRONLY | O_EXCL | O_CREAT, 0600);
 	if (fd < 0)
 		throw String("error generating key: ") + String(strerror(errno));
 
-	ret = write(fd, buf, keylen);
-	err = errno;
+	ret = write_restart(fd, buf, keylen);
 	close(fd);
-	if ((size_t) ret != keylen) {
+	if (ret < 0) {
 		unlink(XVM_KEY_PATH);
-		throw String("error generating key: ") + String(strerror(err));
+		throw String("error generating key: ") + String(strerror(-ret));
 	}
 	return (true);
 }
@@ -164,12 +163,11 @@
 		throw String("error retrieving key: ") + String(strerror(errno));
 	}
 
-	ret = read(fd, buf, sizeof(buf));
-	err = errno;
+	ret = read_restart(fd, buf, sizeof(buf));
 	close(fd);
 	if (ret < 0 || (off_t) ret != st.st_size) {
 		memset(buf, 0, sizeof(buf));
-		throw String("error retrieving key: ") + String(strerror(err));
+		throw String("error retrieving key: ") + String(strerror(-ret));
 	}
 	keylen_bin = (size_t) ret;
 
--- conga/ricci/modules/cluster/clumon/src/common/ClusterMonitor.cpp	2007/09/04 18:28:40	1.3
+++ conga/ricci/modules/cluster/clumon/src/common/ClusterMonitor.cpp	2007/09/11 00:26:52	1.4
@@ -57,7 +57,9 @@
 			poll_data.revents = 0;
 
 			unsigned int time_start = time_mil();
+
 			int ret = poll(&poll_data, 1, timeout);
+			int err = errno;
 			timeout -= (time_mil() - time_start);
 			if (ret == 0)
 				continue;
@@ -66,7 +68,7 @@
 					continue;
 				else {
 					throw String("get_cluster(): poll() error")
-							+ String(strerror(errno));
+							+ String(strerror(err));
 				}
 			}
 
--- conga/ricci/ricci/Makefile	2007/09/07 19:07:23	1.22
+++ conga/ricci/ricci/Makefile	2007/09/11 00:26:52	1.23
@@ -36,7 +36,7 @@
 
 PARANOID=1
 
-INCLUDE += `pkg-config --cflags dbus-1`
+INCLUDE += `pkg-config --cflags dbus-1` -I../common
 CFLAGS +=
 CXXFLAGS += -DDBUS_MAJOR_VERSION="${dbus_major_version}" -DDBUS_MINOR_VERSION="${dbus_minor_version}" -DPARANOIA=$(PARANOID)
 LDFLAGS += `pkg-config --libs dbus-1`
--- conga/ricci/ricci/Ricci.cpp	2007/09/09 01:06:15	1.27
+++ conga/ricci/ricci/Ricci.cpp	2007/09/11 00:26:52	1.28
@@ -39,6 +39,9 @@
 #include <errno.h>
 #include <dirent.h>
 
+extern "C" {
+	#include "sys_util.h"
+}
 
 #include <fstream>
 using namespace std;
@@ -298,15 +301,10 @@
 	try {
 		// save request
 		String xml_str(generateXML(_report));
-		while (xml_str.size()) {
-			ssize_t ret = write(fd, xml_str.c_str(), xml_str.size());
-			if (ret < 0) {
-				if (errno == EINTR)
-					continue;
-				throw String("unable to write batch request: ")
-						+ String(strerror(errno));
-			}
-			xml_str = xml_str.substr(ret);
+		ssize_t ret = write_restart(fd, xml_str.c_str(), xml_str.size());
+		if (ret < 0) {
+			throw String("unable to write batch request: ")
+					+ String(strerror(-ret));
 		}
 
 		close(fd);
@@ -347,7 +345,7 @@
 			size_t res = fread(buff, 1, sizeof(buff), file);
 			int err = errno;
 			batch.append(buff, res);
-			shred(buff, sizeof(buff));
+			memset(buff, 0, sizeof(buff));
 
 			if (res < sizeof(buff)) {
 				if (ferror(file)) {
--- conga/ricci/ricci/RicciWorker.cpp	2007/08/30 17:08:44	1.13
+++ conga/ricci/ricci/RicciWorker.cpp	2007/09/11 00:26:52	1.14
@@ -35,6 +35,9 @@
 #include <errno.h>
 #include <unistd.h>
 
+extern "C" {
+	#include "sys_util.h"
+}
 
 #include <iostream>
 using namespace std;
@@ -277,19 +280,15 @@
 		// read file
 		String xml_str;
 		char buff[4096];
-		int res;
+		ssize_t res;
 
-		while ((res = read(_fd, buff, sizeof(buff))) != 0) {
-			if (res > 0)
-				xml_str.append(buff, res);
-			else {
-				if (errno != EINTR) {
-					throw String("failure reading batch file: ")
-							+ String(strerror(errno));
-				}
-			}
+		res = read_restart(_fd, buff, sizeof(buff));
+		if (res <= 0) {
+			throw String("error reading batch file: ")
+					+ String(strerror(-res));
 		}
-		shred(buff, sizeof(buff));
+		xml_str.append(buff, res);
+		memset(buff, 0, sizeof(buff));
 
 		// _xml
 		_xml = parseXML(xml_str);
--- conga/ricci/ricci/SSLInstance.cpp	2007/09/09 01:06:15	1.10
+++ conga/ricci/ricci/SSLInstance.cpp	2007/09/11 00:26:52	1.11
@@ -35,6 +35,10 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+extern "C" {
+	#include "sys_util.h"
+}
+
 #include <iostream>
 #include <list>
 #include <set>
@@ -290,7 +294,7 @@
 		int ret = SSL_read(_ssl, buff, sizeof(buff));
 		if (ret > 0) {
 			String data(buff, ret);
-			shred(buff, sizeof(buff));
+			memset(buff, 0, sizeof(buff));
 			return data;
 		} else {
 			bool want_read, want_write;
@@ -398,11 +402,12 @@
 	f_name += "/client_cert_XXXXXX";
 
 	int fd = -1;
-	char* buff = new char[f_name.size() + 1];
+	char *buff = new char[f_name.size() + 1];
 
 	try {
 		// pick a filename
 		strcpy(buff, f_name.c_str());
+
 		if ((fd = mkstemp(buff)) == -1)
 			throw String("unable to generate random file");
 		f_name = buff;
@@ -411,14 +416,12 @@
 		buff = NULL;
 
 		String data(_cert_pem);
-		while (data.size()) {
-			ssize_t i = write(fd, data.c_str(), data.size());
-			if (i == -1) {
-				if (errno != EINTR)
-					throw String("error writing certificate");
-			} else
-				data = data.substr(i);
+		ssize_t i = write_restart(fd, data.c_str(), data.size());
+		if (i < 0) {
+			throw String("error writing certificate: ")
+					+ String(strerror(-i));
 		}
+
 		while (close(fd) && errno == EINTR)
 			;
 	} catch ( ... ) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]