From 781afd37d5bd0392ce9bd797f5795ac0f4876803 Mon Sep 17 00:00:00 2001 From: miker Date: Mon, 10 Mar 2008 13:10:59 +0000 Subject: [PATCH] Parially, a patch from Scott McKellar: 1. In socket_open_unix_server() and socket_open_unix_client(), I added checks to make sure that the path parameter doesn't point to aomething too big to fit into the receiving buffer of the struct sockaddr_un. 2. In _socket_handle_client_data() I add a terminal nul to the data received by recv(). Also, reversing the semantics and default of NDEBUG per. To turn on debugging code, set DEBUG to 1 durring the build: $ DEBUG=1 make clean all git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1277 9efc2488-bf62-4759-914b-345cdb29e865 --- include/opensrf/utils.h | 2 +- src/Makefile | 4 ++ src/c-apps/Makefile | 6 ++- src/c-apps/timejson.c | 80 ++++++++++++++++++++++++++++++++++ src/libopensrf/socket_bundle.c | 15 +++++++ 5 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 src/c-apps/timejson.c diff --git a/include/opensrf/utils.h b/include/opensrf/utils.h index a42bc06..dcdba02 100644 --- a/include/opensrf/utils.h +++ b/include/opensrf/utils.h @@ -41,7 +41,7 @@ GNU General Public License for more details. memset( ptr, 0, size );\ } while(0) -#ifndef NDEBUG +#ifdef NDEBUG // The original ... replace with noop once no more errors occur in NDEBUG mode #define osrf_clearbuf( s, n ) memset( s, 0, n ) #else diff --git a/src/Makefile b/src/Makefile index b789ffa..f527987 100644 --- a/src/Makefile +++ b/src/Makefile @@ -12,6 +12,10 @@ export LDLIBS += export LDFLAGS += -Wl,-rpath=$(LIBDIR) -L $(TMPDIR) -L . export CFLAGS += -D_LARGEFILE64_SOURCE -pipe -g -Wall -O2 -fPIC -I ../../include/ -I$(LIBXML2_HEADERS) -I$(APACHE2_HEADERS) -I$(APR_HEADERS) +ifneq ($(DEBUG), 1) +export CGLAGS += -DNDEBUG +endif + ifeq ($(OSRF_LEGACY_JSON), 1) export LDLIBS += -lobjson endif diff --git a/src/c-apps/Makefile b/src/c-apps/Makefile index cd172bd..d566ff3 100644 --- a/src/c-apps/Makefile +++ b/src/c-apps/Makefile @@ -1,12 +1,16 @@ LDLIBS += -lopensrf CFLAGS += -D_LARGEFILE64_SOURCE -DOSRF_LOG_PARAMS -all: osrf_math.so osrf_dbmath.so osrf_version.so +all: osrf_math.so osrf_dbmath.so osrf_version.so timejson +timejson.o: timejson.c osrf_math.o: osrf_math.c osrf_dbmath.o: osrf_dbmath.c osrf_version.o: osrf_version.c +timejson: timejson.o + $(CC) -shared -W1 $(LDLIBS) $(LDFLAGS) timejson.o -o $(TMPDIR)/timejson + osrf_math.so: osrf_math.o $(CC) -shared -W1 $(LDLIBS) $(LDFLAGS) osrf_math.o -o $(TMPDIR)/osrf_math.so diff --git a/src/c-apps/timejson.c b/src/c-apps/timejson.c new file mode 100644 index 0000000..0ee1e94 --- /dev/null +++ b/src/c-apps/timejson.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "opensrf/utils.h" +#include "opensrf/osrf_json.h" + +struct timeval diff_timeval( const struct timeval * begin, + const struct timeval * end ); + +static const char sample_json[] = + "{\"menu\": {\"id\": \"file\", \"value\": \"File\"," + "\"popup\": { \"menuitem\": [ {\"value\": \"New\", " + "\"onclick\": \"CreateNewDoc()\"}," + "{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, " + "{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}"; + +int main( void ) { + int rc = 0; + + struct timezone tz = { 240, 1 }; + struct timeval begin_timeval; + struct timeval end_timeval; + + gettimeofday( &begin_timeval, &tz ); + + long i; + jsonObject * pObj = NULL; + + for( i = 10000000; i; --i ) + { +// pObj = jsonParseString( sample_json ); + pObj = jsonNewObject( NULL ); + jsonObject * p1 = jsonNewObject( NULL ); + jsonObject * p2 = jsonNewObject( NULL ); + jsonObjectFree( p1 ); + jsonObjectFree( p2 ); + jsonObjectFree( pObj ); + } + + jsonObjectFreeUnused(); + + gettimeofday( &end_timeval, &tz ); + + struct timeval elapsed = diff_timeval( &begin_timeval, &end_timeval ); + + printf( "Elapsed time: %ld seconds, %ld microseconds\n", + (long) elapsed.tv_sec, (long) elapsed.tv_usec ); + + struct rlimit rlim; + if( getrlimit( RLIMIT_DATA, &rlim ) ) + printf( "Error calling getrlimit\n" ); + else + printf( "Address space: %lu\n", (unsigned long) rlim.rlim_cur ); + + malloc_stats(); + + return rc; +} + +struct timeval diff_timeval( const struct timeval * begin, const struct timeval * end ) +{ + struct timeval diff; + + diff.tv_sec = end->tv_sec - begin->tv_sec; + diff.tv_usec = end->tv_usec - begin->tv_usec; + + if( diff.tv_usec < 0 ) + { + diff.tv_usec += 1000000; + --diff.tv_sec; + } + + return diff; + +} diff --git a/src/libopensrf/socket_bundle.c b/src/libopensrf/socket_bundle.c index 9829cb2..de9ca93 100644 --- a/src/libopensrf/socket_bundle.c +++ b/src/libopensrf/socket_bundle.c @@ -136,6 +136,13 @@ int socket_open_unix_server(socket_manager* mgr, const char* path) { int sock_fd; struct sockaddr_un server_addr; + if(strlen(path) > sizeof(server_addr.sun_path) - 1) + { + osrfLogWarning( OSRF_LOG_MARK, "socket_open_unix_server(): path too long: %s", + path ); + return -1; + } + errno = 0; sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); if(sock_fd < 0){ @@ -336,6 +343,13 @@ int socket_open_unix_client(socket_manager* mgr, const char* sock_path) { int sock_fd, len; struct sockaddr_un usock; + if(strlen(sock_path) > sizeof(usock.sun_path) - 1) + { + osrfLogWarning( OSRF_LOG_MARK, "socket_open_unix_client(): path too long: %s", + sock_path ); + return -1; + } + errno = 0; if( (sock_fd = socket( AF_UNIX, SOCK_STREAM, 0 )) < 0 ) { osrfLogWarning( OSRF_LOG_MARK, "socket_open_unix_client(): Cannot create UNIX socket: %s", strerror( errno ) ); @@ -713,6 +727,7 @@ static int _socket_handle_client_data(socket_manager* mgr, socket_node* node) { osrfLogInternal( OSRF_LOG_MARK, "%ld : Received data at %f\n", (long) getpid(), get_timestamp_millis()); while( (read_bytes = recv(sock_fd, buf, RBUFSIZE-1, 0) ) > 0 ) { + buf[read_bytes] = '\0'; osrfLogInternal( OSRF_LOG_MARK, "Socket %d Read %d bytes and data: %s", sock_fd, read_bytes, buf); if(mgr->data_received) mgr->data_received(mgr->blob, mgr, sock_fd, buf, node->parent_id); -- 2.43.2