[Initng-svn] r3837 - in initng/trunk: src

svn at initng.thinktux.net svn at initng.thinktux.net
Tue Apr 18 20:31:31 CEST 2006


Author: jimmy
Date: Tue Apr 18 20:31:29 2006
New Revision: 3837

Modified:
   initng/trunk/plugins/debug_commands/print_service.c
   initng/trunk/src/initng_fd.c
   initng/trunk/src/initng_fork.c
   initng/trunk/src/initng_process_db.h

Log:
Rewrote process buffer handeling.


Modified: initng/trunk/plugins/debug_commands/print_service.c
==============================================================================
--- initng/trunk/plugins/debug_commands/print_service.c	(original)
+++ initng/trunk/plugins/debug_commands/print_service.c	Tue Apr 18 20:31:29 2006
@@ -226,7 +226,7 @@
 		mprintf(string, "\t\tFds read: %i, write: %i\n", p->out_pipe[0],
 			p->out_pipe[1]);
 			
-	if (p->buffer)
+	if (p->buffer && p->buffer_allocated>0)
 	{
 		mprintf(string, "\t\tBuffer (%i): \n##########  BUFFER  ##########\n%s\n##############################\n", p->buffer_allocated,
 				p->buffer);

Modified: initng/trunk/src/initng_fd.c
==============================================================================
--- initng/trunk/src/initng_fd.c	(original)
+++ initng/trunk/src/initng_fd.c	Tue Apr 18 20:31:29 2006
@@ -95,10 +95,9 @@
  */
 void initng_fd_process_read_input(active_db_h * service, process_h * p)
 {
-	char *read_pos = NULL;
-	int chars_read = 0;
-	int old_content_offset = 0;
+	int old_content_offset = p->buffer_len;
 	int read_ret = 0;
+	char *tmp;
 
 	D_("\ninitng_fd_process_read_input(%s, %s, %i);\n", service->name,
 	   p->pt->name);
@@ -113,12 +112,7 @@
 	 * if this are not set to out_pipe, if there is nothing to read. read() will block.
 	 * initng and sit down waiting for input.
 	 */
-	if (p->buffer)
-	{
-		/* get the length of current data in buffer */
-		old_content_offset = strlen(p->buffer);
-	}
-	else
+	if (!p->buffer)
 	{
 		/* initziate buffer fnctl */
 		int fd_flags;
@@ -135,14 +129,14 @@
 		/* OBSERVE, i_realloc may change the path to the data, so dont set buffer_pos to early */
 
 		/* Make sure there is room for 100 more chars */
-		D_("left: %i > %i\n", old_content_offset + chars_read + 100,
+		D_("left: %i > %i\n", p->buffer_len + 100,
 		   p->buffer_allocated);
-		if (old_content_offset + chars_read + 100 > p->buffer_allocated)
+		if (p->buffer_len + 100 >= p->buffer_allocated)
 		{
 			/* do a realloc */
 			D_("Changing size of buffer %p to: %i\n", p->buffer,
 			   p->buffer_allocated + 100 + 1);
-			char *tmp = i_realloc(p->buffer,
+			tmp = i_realloc(p->buffer,
 								  (p->buffer_allocated + 100 +
 								   1) * sizeof(char));
 
@@ -152,6 +146,12 @@
 				D_("p->buffer changes from %p to %p.\n", p->buffer, tmp);
 				p->buffer = tmp;
 				p->buffer_allocated += 100;
+				
+				/*
+				 * make sure it nulls, specially when i_realloc is run for the verry first time
+				 * and maby there is nothing to get by read
+				 */
+				p->buffer[p->buffer_len]='\0';
 			}
 			else
 			{
@@ -160,35 +160,47 @@
 			}
 		}
 
-		/* set read_pos to buffer + chars of old content + chars read so far */
-		read_pos = p->buffer + old_content_offset + chars_read;
-
 		/* read the data */
 		D_("Reading 100 chars.\n");
-		read_ret = read(p->out_pipe[0], read_pos, 100);
+		read_ret = read(p->out_pipe[0], &p->buffer[p->buffer_len], 100);
+		/*printf("read_ret = %i  : \"%.100s\"\n", read_ret, read_pos);*/
 
-		D_("read_ret = %i\n", read_ret);
 		/* make sure read does not return -1 */
 		if (read_ret <= 0)
 			break;
 
-		/* make sure its nulled at end */
-		read_pos[read_ret] = '\0';
-
-		/* increase read_pos */
-		chars_read += read_ret;
+		/* increase buffer_len */
+		p->buffer_len += read_ret;
+		
+		/* make sure its nulled at end */		
+		p->buffer[p->buffer_len]='\0';
 	}
 	/* if read_ret == 100, it migit be more to read, or it got interrupted. */
 	while (read_ret >= 100 || errno == EINTR);
 
+
+
 	/* make sure there is any */
-	if (chars_read > 0)
+	if (p->buffer_len > old_content_offset)
 	{
 		/* let all plugin take part of data */
 		initng_fd_plugin_readpipe(service, p, p->buffer + old_content_offset);
 	}
 
-	/* if EOF close pipes */
+	/*if empty, dont waist memory */
+	if(p->buffer_len==0 && p->buffer)
+	{
+		free(p->buffer);
+		p->buffer=NULL;
+		p->buffer_allocated=0;
+		p->buffer_len=0;
+	}		
+
+
+	/*
+	 * if EOF close pipes.
+	 * Dont free buffer, until the whole process_h frees
+	 */
 	if (read_ret == 0)
 	{
 		D_("Closing fifos for %s.\n", service->name);
@@ -198,14 +210,23 @@
 			close(p->out_pipe[1]);
 		p->out_pipe[0] = -1;
 		p->out_pipe[1] = -1;
+		
+		/* else, realloc to exact size */
+		if(p->buffer && p->buffer_allocated>(p->buffer_len + 1))
+		{
+			tmp = i_realloc(p->buffer, (p->buffer_len + 1) * sizeof(char));
+			if(tmp)
+			{
+				p->buffer=tmp;
+				p->buffer_allocated=p->buffer_len;
+			}
+		}
 		return;
 	}
 
 	/* if buffer reached 10000 chars */
-	if (old_content_offset + chars_read > 10000)
+	if (p->buffer_len > 10000)
 	{
-		char *tmp;
-
 		/* copy the string from 1000 chars, to first */
 		memmove(p->buffer, &p->buffer[1000], 9000 * sizeof(char));
 		/* rezise the buffer */
@@ -215,12 +236,15 @@
 		if (tmp)
 		{
 			p->buffer = tmp;
-			p->buffer_allocated = 9000;
-			p->buffer[9000] = '\0';
+			p->buffer_allocated = 9000;  /* shorted by 1000 chars */
+			p->buffer_len-=1000;         /* shorted by 1000 chars */
+			p->buffer[9000] = '\0';      /* shorted by 1000 chars */
 		}
 		else
 		{
 			/* make the best for the current situation */
+			if(p->buffer)
+				free(p->buffer);
 			p->buffer[0] = '\0';
 			F_("realloc failed, possibly out of memory!\n");
 			return;

Modified: initng/trunk/src/initng_fork.c
==============================================================================
--- initng/trunk/src/initng_fork.c	(original)
+++ initng/trunk/src/initng_fork.c	Tue Apr 18 20:31:29 2006
@@ -127,7 +127,8 @@
 		fcntl(STDERR_FILENO, F_SETFD, 0);
 
 		/* Close the sides of the pipes we don't need, as we're in fork we won't need this part. */
-		close(process->out_pipe[0]);
+		if(process->out_pipe[0]>0)
+			close(process->out_pipe[0]);
 		process->out_pipe[0] = -1;
 
 
@@ -164,7 +165,8 @@
 	{
 
 		/* close the receiving end on pipe, on parent */
-		close(process->out_pipe[1]);
+		if(process->out_pipe[1]>0)
+			close(process->out_pipe[1]);
 		process->out_pipe[1] = -1;
 		if (pid_fork > 0)
 		{

Modified: initng/trunk/src/initng_process_db.h
==============================================================================
--- initng/trunk/src/initng_process_db.h	(original)
+++ initng/trunk/src/initng_process_db.h	Tue Apr 18 20:31:29 2006
@@ -58,7 +58,8 @@
 	int r_code;
 	int out_pipe[2];			/* pipes of process */
 	char *buffer;				/* stdout buffer ## THE BEGINNING ## */
-	int buffer_allocated;
+	int buffer_allocated;       /* chars right now allocated for this buffer */
+	int buffer_len;				/* the count of chars from the beginning in buffer right now */
 	e_pst pst;
 
 	struct list_head list;		/* this process should be in a list */


More information about the Initng-svn mailing list