[Initng-svn] r3186 - in initng/trunk: plugins/cpout plugins/history plugins/logfile plugins/ngcs plugins/reload plugins/stcmd plugins/syslog src

svn at initng.thinktux.net svn at initng.thinktux.net
Sat Mar 4 01:18:02 CET 2006


Author: jimmy
Date: Sat Mar  4 01:18:02 2006
New Revision: 3186

Modified:
   initng/trunk/plugins/cpout/initng_colorprint_out.c
   initng/trunk/plugins/history/initng_history.c
   initng/trunk/plugins/logfile/initng_logfile.c
   initng/trunk/plugins/ngcs/initng_ngcs_cmds.c
   initng/trunk/plugins/reload/initng_reload.c
   initng/trunk/plugins/stcmd/print_service.c
   initng/trunk/plugins/syslog/initng_syslog.c
   initng/trunk/src/initng_fd.c
   initng/trunk/src/initng_fd.h
   initng/trunk/src/initng_fork.c
   initng/trunk/src/initng_kill_handler.c
   initng/trunk/src/initng_plugin.h
   initng/trunk/src/initng_process_db.h
Log:
Implenting variable process output buffers, will now realloc with a 100 chars heap.


Modified: initng/trunk/plugins/cpout/initng_colorprint_out.c
==============================================================================
--- initng/trunk/plugins/cpout/initng_colorprint_out.c	(original)
+++ initng/trunk/plugins/cpout/initng_colorprint_out.c	Sat Mar  4 01:18:02 2006
@@ -252,7 +252,7 @@
     D_("print_system_state(): new system state: %i\n", state);
 }
 
-static int print_program_output(active_db_h * service, process_h * x)
+static int print_program_output(active_db_h * service, process_h * x, char *buffer_pos)
 {
     /*
        TODO here:
@@ -261,8 +261,7 @@
        That way when this function is called we can print every full line from plugin_pos.
        This way fsck will look nice, along with an "internal" database of write positions we can cache data so we print every 5 seconds or on int forceflush.
      */
-    /*int skipline = FALSE; */
-    int i;
+    int i = 0;
 
     assert(service);
     assert(service->name);
@@ -275,25 +274,24 @@
 
     D_(" from service \"%s\"\n", service->name);
     /*
-       printf("buffer_pos: %i\n", x->buffer_pos);
+       printf("buffer_pos: %i\n", buffer_pos);
        printf("datalen: %i\n", datalen);
        printf("Buffer: \n################\n%s\n##########\n\n",x->buffer);
      */
-    i = x->buffer_pos;
     /* a first while loop that sorts out crap */
-    while (x->buffer[i] != '\0')
+    while (buffer_pos[i] != '\0')
     {
         /*  remove lines with " [2]  Done " that bash generates. */
-        if (x->buffer[i] == '[' && x->buffer[i + 2] == ']')
+        if (buffer_pos[i] == '[' && buffer_pos[i + 2] == ']')
         {
             /* jump to next line */
-            while (x->buffer[i] && x->buffer[i] != '\n')
+            while (buffer_pos[i] && buffer_pos[i] != '\n')
                 i++;
         }
 
         /* if there are stupid tokens, go to next char, and run while again. */
-        if (x->buffer[i] == ' ' || x->buffer[i] == '\n'
-            || x->buffer[i] == '\t')
+        if (buffer_pos[i] == ' ' || buffer_pos[i] == '\n'
+            || buffer_pos[i] == '\t')
         {
             i++;
             continue;
@@ -304,7 +302,7 @@
     }
 
     /* Make sure that there is anything left to write */
-    if (strlen(&x->buffer[i]) < 2)
+    if (strlen(&buffer_pos[i]) < 2)
     {
         /* its okay anyway */
         return (TRUE);
@@ -329,17 +327,17 @@
 
 
     /* while buffer lasts */
-    while (x->buffer[i] != '\0')
+    while (buffer_pos[i] != '\0')
     {
         /*  remove lines with " [2]  Done " that bash generates. */
-        if (x->buffer[i] == '[' && x->buffer[i + 2] == ']')
+        if (buffer_pos[i] == '[' && buffer_pos[i + 2] == ']')
         {
-            while (x->buffer[i] && x->buffer[i] != '\n')
+            while (buffer_pos[i] && buffer_pos[i] != '\n')
                 i++;
         }
 
         /* if this are a newline */
-        if (x->buffer[i] == '\n')
+        if (buffer_pos[i] == '\n')
         {
             /* print our special indented newline instead */
             putchar('\n');
@@ -347,14 +345,14 @@
             putchar(' ');
             i++;
             /* skip spaces, on newline. */
-            while (x->buffer[i]
-                   && (x->buffer[i] == ' ' || x->buffer[i] == '\t'))
+            while (buffer_pos[i]
+                   && (buffer_pos[i] == ' ' || buffer_pos[i] == '\t'))
                 i++;
             continue;
         }
 
         /* ok, now put the char, and go to next. */
-        putchar(x->buffer[i]);
+        putchar(buffer_pos[i]);
         i++;
     }
     return (TRUE);

Modified: initng/trunk/plugins/history/initng_history.c
==============================================================================
--- initng/trunk/plugins/history/initng_history.c	(original)
+++ initng/trunk/plugins/history/initng_history.c	Sat Mar  4 01:18:02 2006
@@ -39,7 +39,6 @@
 
 history_h history_db;
 
-static int fetch_output(active_db_h * service, process_h * process);
 static void cmd_history(char *arg, FILE * fd);
 static void cmd_log(char *arg, FILE * fd);
 
@@ -198,7 +197,7 @@
     return (TRUE);
 }
 
-static int fetch_output(active_db_h * service, process_h * process)
+static int fetch_output(active_db_h * service, process_h * process, char *buffer_pos)
 {
     history_h *tmp_e = NULL;
 
@@ -213,7 +212,7 @@
     tmp_e->service = service;
     tmp_e->name = NULL;
     gettimeofday(&tmp_e->time, NULL);
-    tmp_e->data = i_strdup(&process->buffer[process->buffer_pos]);
+    tmp_e->data = i_strdup(buffer_pos);
     tmp_e->action = NULL;
 
     /* add to history struct */

Modified: initng/trunk/plugins/logfile/initng_logfile.c
==============================================================================
--- initng/trunk/plugins/logfile/initng_logfile.c	(original)
+++ initng/trunk/plugins/logfile/initng_logfile.c	Sat Mar  4 01:18:02 2006
@@ -38,7 +38,7 @@
     { "logfile", STRING, NULL, "An extra output of service output." };
 
 
-static int program_output(active_db_h * service, process_h * x)
+static int program_output(active_db_h * service, process_h * x, char *buffer_pos)
 {
     const char *filename = NULL;
     char *filename_fixed = NULL;
@@ -73,9 +73,9 @@
 
     /* Write data to logfile */
     D_("Writing data...\n");
-    len = strlen(&x->buffer[x->buffer_pos]);
+    len = strlen(buffer_pos);
 
-    if (write(fd, &x->buffer[x->buffer_pos], len) != len)
+    if (write(fd, buffer_pos, len) != len)
         F_("Error writing to %s's log, err : %s\n", service->name,
            strerror(errno));
 

Modified: initng/trunk/plugins/ngcs/initng_ngcs_cmds.c
==============================================================================
--- initng/trunk/plugins/ngcs/initng_ngcs_cmds.c	(original)
+++ initng/trunk/plugins/ngcs/initng_ngcs_cmds.c	Sat Mar  4 01:18:02 2006
@@ -101,7 +101,7 @@
     return TRUE;
 }
 
-static int service_output_watch(active_db_h * service, process_h * x)
+static int service_output_watch(active_db_h * service, process_h * x, char *buffer_pos)
 {
     ngcs_watch *watch, *nextwatch;
     ngcs_data dat[2];
@@ -113,7 +113,7 @@
     dat[0].d.s = service->name;
     dat[1].type = NGCS_TYPE_STRING;
     dat[1].len = -1;
-    dat[1].d.s = x->buffer + x->buffer_pos;
+    dat[1].d.s = buffer_pos;
     list_for_each_entry_prev_safe(watch, nextwatch, &watches.list, list)
     {
         if (watch->name == NULL || strcmp(watch->name, service->name) == 0)

Modified: initng/trunk/plugins/reload/initng_reload.c
==============================================================================
--- initng/trunk/plugins/reload/initng_reload.c	(original)
+++ initng/trunk/plugins/reload/initng_reload.c	Sat Mar  4 01:18:02 2006
@@ -141,7 +141,6 @@
                 process->r_code = entry.process[pnr].rcode;
 
                 /* allocate a new output buffer for this process */
-                process->buffer_pos = 0;
                 process->buffer = NULL;
 
                 /* add this process to the list */

Modified: initng/trunk/plugins/stcmd/print_service.c
==============================================================================
--- initng/trunk/plugins/stcmd/print_service.c	(original)
+++ initng/trunk/plugins/stcmd/print_service.c	Sat Mar  4 01:18:02 2006
@@ -202,11 +202,8 @@
             p->out_pipe[1]);
     if (p->buffer)
     {
-        fprintf(fd, "\t\tBuffer: \"");
-        print_max_20(p->buffer, fd);
-        fprintf(fd, "\"\n");
+        fprintf(fd, "\t\tBuffer (%i): \"%s\"\n", p->buffer_allocated, p->buffer);
     }
-    fprintf(fd, "\t\tBuffer-pos: %i\n\n", p->buffer_pos);
 
 }
 

Modified: initng/trunk/plugins/syslog/initng_syslog.c
==============================================================================
--- initng/trunk/plugins/syslog/initng_syslog.c	(original)
+++ initng/trunk/plugins/syslog/initng_syslog.c	Sat Mar  4 01:18:02 2006
@@ -37,7 +37,6 @@
 #include "../../src/initng_plugin_hook.h"
 #include "../../src/initng_static_states.h"
 
-static int syslog_fetch_output(active_db_h * service, process_h * process);
 static void syslog_print_system_state(h_sys_state state);
 static int syslog_print_status_change(active_db_h * service);
 static void check_syslog(void);
@@ -229,26 +228,26 @@
     return;
 }
 
-static int syslog_fetch_output(active_db_h * service, process_h * process)
+static int syslog_fetch_output(active_db_h * service, process_h * process, char *buffer_pos)
 {
     char log[201];
-    int pos = process->buffer_pos;
+    int pos = 0;
     int i;
 
     assert(service);
     assert(service->name);
 
     /* print every line, ending with a '\n' as an own syslog */
-    while (process->buffer[pos])
+    while (buffer_pos[pos])
     {
         i = 0;
         /* count the number of char before '\n' */
-        while (process->buffer[pos + i] && process->buffer[pos + i] != '\n'
+        while (buffer_pos[pos + i] && buffer_pos[pos + i] != '\n'
                && i < 200)
             i++;
 
         /* copy that many chars to our temporary log array */
-        strncpy(log, &process->buffer[pos], i);
+        strncpy(log, &buffer_pos[pos], i);
         log[i] = '\0';
 
         /* send it to syslog */
@@ -258,7 +257,7 @@
         pos += i;
 
         /* and skip the newline if any */
-        if (process->buffer[pos])
+        if (buffer_pos[pos])
             pos++;
 
     }

Modified: initng/trunk/src/initng_fd.c
==============================================================================
--- initng/trunk/src/initng_fd.c	(original)
+++ initng/trunk/src/initng_fd.c	Sat Mar  4 01:18:02 2006
@@ -61,7 +61,7 @@
  * be printed to screen anyway.
  */
 static void initng_fd_plugin_readpipe(active_db_h * service,
-                                      process_h * process)
+                                      process_h * process, char *buffer_pos)
 {
     s_call *current = NULL;
     int delivered = FALSE;
@@ -72,7 +72,7 @@
     while_list(current, &g.PIPEWATCHERS)
     {
         D_("Calling pipewatcher plugin.\n");
-        if ((*current->c.pipewatcher) (service, process) == TRUE)
+        if ((*current->c.pipewatcher) (service, process, buffer_pos) == TRUE)
             delivered = TRUE;
 #ifdef DEBUG
         else
@@ -84,127 +84,109 @@
 
     /* make sure someone handled this */
     if (delivered != TRUE)
-        fprintf(stdout, "%s", &process->buffer[process->buffer_pos]);
+        fprintf(stdout, "%s", buffer_pos);
 }
 
-/*
- * This does the actual read from pipe, all checks have been done here.
- *
- */
-static int initng_fd_process_readpipe_read(active_db_h * service,
-                                           process_h * p, int flush_buffer)
-{
-    int len = 0;                /* length of data read */
-
-    S_;
-
-    /* check if buffer exits, or have to be created */
-    if (!p->buffer)
-    {
-        /* allocate space for the buffer */
-        p->buffer = i_calloc(MAX_BUFFER + 1, sizeof(char));
-        p->buffer_pos = 0;
-    }
-
-    /* read data from process, and continue again after a interrupt */
-    do
-    {
-        errno = 0;
-        /* can max read MAX_BUFFER (once allocated) - len (all currently read) - buffer_pos */
-        if ((MAX_BUFFER - len - p->buffer_pos) < 1)
-        {
-            F_("Can't read more, buffer is out!\n");
-            break;
-        }
-        len += read(p->out_pipe[0], &p->buffer[p->buffer_pos],
-                    (MAX_BUFFER - len - p->buffer_pos));
-    }
-    while (errno == EINTR);
-
-    /* make sure we got something */
-    if (len <= 0)
-        return (len);
-
-    /* if we got something. */
-
-    /* null the end to make sure we don't get an overflow */
-    p->buffer[p->buffer_pos + len] = '\0';
-
-    /* if max buffer is reached, make sure it flushes it */
-    if (p->buffer_pos + len >= MAX_BUFFER)
-        flush_buffer = TRUE;
-
-    /* let all plugin take part of data */
-    initng_fd_plugin_readpipe(service, p);
-
-    /* increase the buffer position */
-    if (flush_buffer)
-        p->buffer_pos = 0;
-    else
-        p->buffer_pos = p->buffer_pos + len;
-
-    /* return length of data read */
-    return (len);
-}
 
 /*
  * This function is called when data is polled below,
  * or when a process is freed ( with flush_buffer set)
  */
-void initng_fd_process_read_input(active_db_h * service, process_h * p,
-                                  int flush_buffer)
+void initng_fd_process_read_input(active_db_h * service, process_h * p)
 {
-    int len = 0;
+    char *read_pos = NULL;
+    int chars_read=0;
+    int old_content_offset = 0;
+    int read_ret = 0;
 
-    S_;
+    D_("\ninitng_fd_process_read_input(%s, %s, %i);\n", service->name, p->pt->name);
 
-    /*
-     * flush_buffer is set, and readpipe is called just
-     * before process are being freed, when the process
-     * is dead.
+    if(p->out_pipe[0]<=0)
+    {
+	F_("FIFO, can be read! NOT OPEN!\n");
+	return;
+    }
+
+    /* INITZIATE
+     * 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 (flush_buffer)
+    if(p->buffer)
     {
+	/* get the lenght of current data in buffer */
+	old_content_offset = strlen(p->buffer);
+    } else {
+	/* initziate buffer fnctl */
         int fd_flags;
 
         fd_flags = fcntl(p->out_pipe[0], F_GETFL, 0);
         fcntl(p->out_pipe[0], F_SETFL, fd_flags | O_NONBLOCK);
     }
-
-    /* get data from process, it return length of data got */
-    len = initng_fd_process_readpipe_read(service, p, flush_buffer);
-
-    if (!flush_buffer)
+    
+    /* read data from process, and continue again after a interrupt */
+    do
     {
-        /* We have an error other than EAGAIN, set flush_buffer */
-        if ((len == -1) && (errno != EAGAIN))
-        {
-            flush_buffer = TRUE;
-            F_("Error reading from pipe for %s (%s)\n", service->name,
-               strerror(errno));
-        }
-
-        /* Why flush buffer if we got nothing to read? */
-        /* End of file */
-        if (len == 0)
-            flush_buffer = TRUE;
-    }
-
-    /* If i < 1 we either have 0 (end of file) or -1 (error) */
-    if (flush_buffer == FALSE)
-        return;
-
-    /* free data, close and reset fds */
-    D_("Freeing buffer for %s\n", service->name);
-    if (p->out_pipe[0] > 0)
-        close(p->out_pipe[0]);
-    if (p->out_pipe[1] > 0)
-        close(p->out_pipe[1]);
-    if (p->buffer)
-        free(p->buffer);
-    p->out_pipe[0] = -1;
-    p->out_pipe[1] = -1;
-    p->buffer = NULL;
+        errno = 0;
+	
+	/* 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, p->buffer_allocated);
+	if(old_content_offset + chars_read + 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, (p->buffer_allocated + 100 + 1) * sizeof(char));
+	    
+	    /* make sure realloc suceeded */
+	    if(tmp)
+	    {
+		D_("p->buffer changes from %p to %p.\n", p->buffer, tmp);
+		p->buffer=tmp;
+		p->buffer_allocated += 100;
+	    } else {
+		F_("realloc failed, possibly out of memory!\n");
+		return;
+	    }
+	}
+
+	/* 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);
+	
+	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;
+    }
+    /* 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)
+    {
+        /* let all plugin take part of data */
+        initng_fd_plugin_readpipe(service, p, p->buffer + old_content_offset);
+    }
+    
+    /* if buffer reached 10000 chars */
+    if(old_content_offset + chars_read > 10000)
+    {
+	/* copy the string from 1000 chars, to first */
+	memcpy(p->buffer, &p->buffer[1000], 9000 * sizeof(char));
+	/* rezise the buffer */
+	i_realloc(p->buffer, 9000 * sizeof(char));
+	p->buffer_allocated = 9000;
+    }
 }
 
 
@@ -369,7 +351,7 @@
                 D_("Will read from %s->start_process on fd #%i\n",
                    currentA->name, currentP->out_pipe[0]);
                 /* Do the actual read from pipe */
-                initng_fd_process_read_input(currentA, currentP, FALSE);
+                initng_fd_process_read_input(currentA, currentP);
                 /* If we've been interrupted, return to main loop (program might have ended) */
                 if (g.interrupt)
                     return (TRUE);

Modified: initng/trunk/src/initng_fd.h
==============================================================================
--- initng/trunk/src/initng_fd.h	(original)
+++ initng/trunk/src/initng_fd.h	Sat Mar  4 01:18:02 2006
@@ -17,7 +17,6 @@
  * Boston, MA  02110-1301, USA.
  */
 
-void initng_fd_process_read_input(active_db_h * service, process_h * p,
-                                  int flush_buffer);
+void initng_fd_process_read_input(active_db_h * service, process_h * p);
 void initng_fd_close_all(void);
 int initng_fd_plugin_poll(int timeout);

Modified: initng/trunk/src/initng_fork.c
==============================================================================
--- initng/trunk/src/initng_fork.c	(original)
+++ initng/trunk/src/initng_fork.c	Sat Mar  4 01:18:02 2006
@@ -67,7 +67,7 @@
     {
         free(process->buffer);
         process->buffer = NULL;
-        process->buffer_pos = 0;
+	process->buffer_allocated = 0;
     }
 
 

Modified: initng/trunk/src/initng_kill_handler.c
==============================================================================
--- initng/trunk/src/initng_kill_handler.c	(original)
+++ initng/trunk/src/initng_kill_handler.c	Sat Mar  4 01:18:02 2006
@@ -95,10 +95,23 @@
     }
 
     /*
-     * calling initng_process_read_input, with flush true
-     * makes it close read all fd, and close them, that is important.
+     * calling initng_process_read_input, Make sure all buffers read, before closing them.
      */
-    initng_fd_process_read_input(service, process, TRUE);
+    initng_fd_process_read_input(service, process);
+
+    /*
+     * Close the process output fifos to initng.
+     */
+    if (process->out_pipe[0] > 0)
+    {
+	close(process->out_pipe[0]);
+	process->out_pipe[0]=0;
+    }
+    if (process->out_pipe[1] > 0)
+    {
+	close(process->out_pipe[1]);
+	process->out_pipe[1]=0;
+    }
 
     /* launch a kill_handler if any */
     if (process->pt && process->pt->kill_handler)

Modified: initng/trunk/src/initng_plugin.h
==============================================================================
--- initng/trunk/src/initng_plugin.h	(original)
+++ initng/trunk/src/initng_plugin.h	Sat Mar  4 01:18:02 2006
@@ -41,7 +41,7 @@
     int (*status_change) (active_db_h * service);
     service_cache_h *(*parser) (const char *name);
     void (*swatcher) (h_sys_state state);
-    int (*pipewatcher) (active_db_h * service, process_h * process);
+    int (*pipewatcher) (active_db_h * service, process_h * process, char *buffer_pos);
     int (*launch) (active_db_h * service, process_h * process);
     int (*af_launcher) (active_db_h * service, process_h * process);
     int (*handle_killed) (active_db_h * service, process_h * process);

Modified: initng/trunk/src/initng_process_db.h
==============================================================================
--- initng/trunk/src/initng_process_db.h	(original)
+++ initng/trunk/src/initng_process_db.h	Sat Mar  4 01:18:02 2006
@@ -21,8 +21,6 @@
 #ifndef PROCESS_DB_H
 #define PROCESS_DB_H
 
-#define MAX_BUFFER 1024                     /* This is the maximum size of buffer in process_h */
-
 #include <sys/types.h>
 #include <unistd.h>
 #include "initng_active_db.h"
@@ -46,8 +44,8 @@
     pid_t pid;                  /* pid of process */
     int r_code;
     int out_pipe[2];            /* pipes of process */
-    char *buffer;               /* stdout buffer */
-    int buffer_pos;             /* Our current position in the buffer */
+    char *buffer;               /* stdout buffer ## THE BEGINNING ## */
+    int buffer_allocated;
 
     struct list_head list;      /* this process should be in a list */
 };


More information about the Initng-svn mailing list