OSSP CVS Repository

ossp - Check-in [3335]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 3335
Date: 2003-May-14 18:36:28 (local)
2003-May-14 16:36:28 (UTC)
User:ms
Branch:
Comment: Correctly use section accessors, split script to common and section vector objects, add sectionSetndata (with n length sections), and wrap script dump logic for NULL pointer safety.
Tickets:
Inspections:
Files:
ossp-pkg/rc/rc.h      1.39 -> 1.40     3 inserted, 2 deleted
ossp-pkg/rc/rc_private.h      1.21 -> 1.22     2 inserted, 1 deleted
ossp-pkg/rc/rc_proc.c      1.20 -> 1.21     46 inserted, 40 deleted
ossp-pkg/rc/rc_script.c      1.13 -> 1.14     13 inserted, 9 deleted
ossp-pkg/rc/rc_sect.c      1.2 -> 1.3     26 inserted, 5 deleted

ossp-pkg/rc/rc.h 1.39 -> 1.40

--- rc.h 2003/04/03 12:05:14     1.39
+++ rc.h 2003/05/14 16:36:28     1.40
@@ -123,9 +123,10 @@
 const int sectionGetuid(rc_section_t *);
 const char *sectionGetdata(rc_section_t *);
 size_t sectionGetlen(rc_section_t *);
-rc_return_t sectionSetpri(rc_section_t *, int);
-rc_return_t sectionSetuid(rc_section_t *, int);
+rc_return_t sectionSetpri(rc_section_t *, long);
+rc_return_t sectionSetuid(rc_section_t *, long);
 rc_return_t sectionSetdata(rc_section_t *, const char *);
+rc_return_t sectionSetndata(rc_section_t *, const char *, size_t);
 rc_return_t sectionDelete(rc_section_t *);
 
 /* Processor function prototypes */


ossp-pkg/rc/rc_private.h 1.21 -> 1.22

--- rc_private.h 2003/04/03 12:05:14     1.21
+++ rc_private.h 2003/05/14 16:36:28     1.22
@@ -96,7 +96,8 @@
 /* Processor class */
 typedef struct {
     rc_anal_t *m_pAnal;
-    rc_script_t *m_pScript;
+    rc_script_t *m_pScriptcom;
+    rc_script_t **m_ppScriptvec;
 } rc_proc_t;
 
 #endif /* __OSSPRC_PRIVATE_H__ */


ossp-pkg/rc/rc_proc.c 1.20 -> 1.21

--- rc_proc.c    2003/05/12 16:17:47     1.20
+++ rc_proc.c    2003/05/14 16:36:28     1.21
@@ -45,12 +45,18 @@
 ************************************************/
 rc_proc_t *procNew(void)
 {
+    int nIter = 0;
     rc_proc_t *pNewrc = NULL;
 
     pNewrc = malloc(sizeof(rc_proc_t));
     pNewrc->m_pAnal = analNew();        /* Construct a configuration analyser */
-    pNewrc->m_pScript = scriptNew();    /* Construct a run-command script     */
     analParse(pNewrc->m_pAnal);         /* Preprocess the anal configuration  */
+    pNewrc->m_pScriptcom = scriptNew(); /* Construct a run-command script     */
+
+    /* Logic needed for multiple run-command section combination with priorities */
+    pNewrc->m_ppScriptvec = malloc(sizeof(rc_script_t *) * pNewrc->m_pAnal->m_nRcs);
+    for (nIter = 0; nIter < pNewrc->m_pAnal->m_nRcs; nIter++)
+        pNewrc->m_ppScriptvec[nIter] = scriptNew();
 
     return(pNewrc);
 }
@@ -72,19 +78,17 @@
 ************************************************/
 rc_return_t procPopulate(rc_proc_t *pRc)
 {
-    int i         = 0;
-    int nFdrc     = 0;
-    int nFdfunc   = 0;
-    int nRet      = 0;
-    int nRept     = 0;
-    int nIter     = 0;
+    int i       = 0;
+    int nFdrc   = 0;
+    int nFdfunc = 0;
+    int nRet    = 0;
+    int nIter   = 0;
     ex_t Except;
 
     char         *sBuf        = NULL;
     rc_section_t *pSec        = NULL;
     char         *szLocex     = NULL;
     rc_script_t  *pTempscript = NULL;
-    rc_script_t  **ppParts    = NULL;
 
     assert(*pRc->m_pAnal->m_szRcs);
     sBuf = (char *)calloc(1, RC_READ_BUFSIZE);
@@ -97,25 +101,20 @@
     }
 
     /* Stick on the starting shell id line */
-    scriptAppend(pRc->m_pScript, "#! /bin/sh\n", strlen("#! /bin/sh\n"));
+    scriptAppend(pRc->m_pScriptcom, "#! /bin/sh\n", strlen("#! /bin/sh\n"));
 
     /* Read the func file if it was opened successfully */
     /* We unfortunately make the assumption that 0 is an invalid filedesc */
     if (nFdfunc) {
         /* Read data from the func file */
         while ((nRet = read(nFdfunc, sBuf, RC_READ_BUFSIZE)) > 0) {
-            scriptAppend(pRc->m_pScript, sBuf, nRet);
+            scriptAppend(pRc->m_pScriptcom, sBuf, nRet);
         }
-        scriptAppend(pRc->m_pScript, "\n", sizeof(char));
+        scriptAppend(pRc->m_pScriptcom, "\n", sizeof(char));
         if (nRet == -1) /* Handle read errors */
             RC_THROW(RC_ERR_IO);
     }
 
-    /* Logic needed for multiple section combination with priorities */
-    ppParts = malloc(sizeof(rc_script_t *) * pRc->m_pAnal->m_nRcs);
-    for (nRept = 0; nRept < pRc->m_pAnal->m_nRcs; nRept++)
-        ppParts[nRept] = scriptNew();
-
     /* Iteratively read possibly globbed rc files */
     for (nIter = 0; nIter < pRc->m_pAnal->m_nRcs; nIter++)
     {
@@ -143,8 +142,8 @@
             /* Append config section if it exists */
             pSec = scriptSection(pTempscript, configGetval(RC_NCF_VAL));
             if (pSec) { /* Only operate if the section lookup succeeds */
-                scriptAppend(pRc->m_pScript, sectionGetdata(pSec), strlen(sectionGetdata(pSec)));
-                scriptAppend(pRc->m_pScript, "\n", sizeof(char));
+                scriptAppend(pRc->m_pScriptcom, sectionGetdata(pSec), strlen(sectionGetdata(pSec)));
+                scriptAppend(pRc->m_pScriptcom, "\n", sizeof(char));
                 sectionDelete(pSec);    /* Cleanup */
                 pSec = NULL;            /* Cleanup */
             }
@@ -155,9 +154,9 @@
                 pSec = scriptSection(pTempscript, pRc->m_pAnal->m_pszSecs[i]);
 
                 if (pSec) { /* Only call if the section lookup succeeds */
-                    scriptAppend(pRc->m_pScript, sectionGetdata(pSec),
+                    scriptAppend(pRc->m_ppScriptvec[nIter], sectionGetdata(pSec),
                         sectionGetlen(pSec) - 1);
-                    scriptAppend(pRc->m_pScript, "\n", sizeof(char));
+                    scriptAppend(pRc->m_ppScriptvec[nIter], "\n", sizeof(char));
                 }
                 else if (configGetval(RC_DBG_VAL)) /* Only show if debug set */
                     fprintf(stderr, "#Warning: Missing section '%s' in %s!\n",\
@@ -182,16 +181,6 @@
     close(nFdfunc);     /* Close Func file handle */
 
     /* Memory cleanups */
-    if (ppParts) {
-        for (nRept = pRc->m_pAnal->m_nRcs - 1; nRept >= 0 ; nRept--) {
-            if (ppParts[nRept]) {
-                free(ppParts[nRept]);
-                ppParts[nRept] = NULL;
-            }
-        }
-        free(ppParts);
-        ppParts = NULL;
-    }
     if (sBuf) {
         free(sBuf);
         sBuf = NULL;
@@ -206,6 +195,7 @@
 ************************************************/
 rc_return_t procRun(rc_proc_t *pRc)
 {
+    int   nIter = 0;
     char *pszVec[RC_EXEC_MAXARGS];
 
     /****************************************************/
@@ -214,19 +204,24 @@
     /* Eval  - Print machine evaluatable format         */
     /* Print - Print human readable format              */
     /****************************************************/
-    if (configGetval(RC_EVL_VAL))        /* Evaluate */
+    if (configGetval(RC_EVL_VAL))                              /* Evaluate */
         fprintf(stderr, "Error: Evaluate is not implemented yet.\n"); /* FIX */
-    else if (configGetval(RC_EXC_VAL)) { /* Execute  */
+    else if (configGetval(RC_EXC_VAL)) {                       /* Execute  */
             pszVec[0] = "/bin/sh";
             pszVec[1] = "-c";
-            pszVec[2] = (char *)scriptTostring(pRc->m_pScript);
+            pszVec[2] = (char *)scriptTostring(pRc->m_pScriptcom);
             pszVec[3] = NULL;   /* Add a NULL to mark the end of the chain */
-        if (execvp(*pszVec, pszVec) == -1)  /* launch                   */
+        if (execvp(*pszVec, pszVec) == -1)                       /* launch */
             TRACE("Bad, execvp in child returned -1");
     }
-    else if (configGetval(RC_PRN_VAL))   /* Print */
-        scriptDump(pRc->m_pScript);
-    else                                        /* Something is wrong here */
+    else if (configGetval(RC_PRN_VAL)) {                         /* Print  */
+        scriptDump(pRc->m_pScriptcom);
+        for (nIter = 0; nIter < pRc->m_pAnal->m_nRcs; nIter++) {
+            if (*pRc->m_ppScriptvec[nIter])
+                scriptDump(pRc->m_ppScriptvec[nIter]);
+        }
+    }
+    else /* Something is wrong here */
         return(RC_THROW(RC_ERR_INT));
 
     return(RC_THROW(RC_OK));
@@ -238,9 +233,20 @@
 ************************************************/
 rc_return_t procDelete(rc_proc_t *pRc)
 {
-    scriptDelete(pRc->m_pScript);   /* Destroy the script        */
-    analDelete(pRc->m_pAnal);       /* Destroy the analyser      */
-    free(pRc);                      /* Free the processor itself */
+    int nIter = 0;
+
+    /* Destroy the script vector */
+    for (nIter = pRc->m_pAnal->m_nRcs - 1; nIter >= 0 ; nIter--) {
+        if (pRc->m_ppScriptvec[nIter]) {
+            scriptDelete(pRc->m_ppScriptvec[nIter]);
+            pRc->m_ppScriptvec[nIter] = NULL;
+        }
+    }
+    free(pRc->m_ppScriptvec);
+    pRc->m_ppScriptvec = NULL;
+    scriptDelete(pRc->m_pScriptcom);    /* Destroy the script        */
+    analDelete(pRc->m_pAnal);           /* Destroy the analyser      */
+    free(pRc);                          /* Free the processor itself */
 
     return(RC_THROW(RC_OK));
 }


ossp-pkg/rc/rc_script.c 1.13 -> 1.14

--- rc_script.c  2003/05/12 15:42:45     1.13
+++ rc_script.c  2003/05/14 16:36:28     1.14
@@ -67,9 +67,8 @@
     }
 
     /* Short circuit in case of dumb noop call */
-    if (Size == 0) {
+    if (Size == 0)
         return(RC_THROW(RC_OK));
-    }
 
     /* Add 2 to end of nResize to ensure that a \0 precedes any strings */
     nResize = (*pScript != NULL ? strlen(*pScript) : 0) + Size + 2;
@@ -110,6 +109,8 @@
     char *piBlocend    = NULL; /* Misnomer used to control section looping */
     char *piStart      = NULL;
     char *piEnd        = NULL;
+    long  nPri         = 0;
+    long  nUid         = 0;
     int nOffset        = 0;
     int nFound         = 0;
     int nVecsize       = 0;
@@ -179,9 +180,7 @@
             piStart   = piBlocend + *(pnVec + 6);
             piEnd     = piBlocend + *(pnVec + 7);
             pSec = sectionNew();
-            pSec->szData = malloc(piEnd - piStart + sizeof(char));
-            strncpy(pSec->szData, piStart, piEnd - piStart);
-            *(pSec->szData + (piEnd - piStart)) = NULL; /* Terminate outgoing */
+            sectionSetndata(pSec, piStart, piEnd - piStart);
 
             /* Handle the section priority */
             piStart = piBlocend + *(pnVec + 4);
@@ -190,10 +189,11 @@
             piSubtemp = strnstr(piStart, RC_DEF_PRG, piEnd - piStart);
             if (piSubtemp) { /* Priority pattern found */
 /* FIXME: Remove the 1 in the following line! */
-                pSec->nPri = strtol(piSubtemp + strlen(RC_DEF_PRG) + 1, (char **)NULL, 10);
+                nPri = strtol(piSubtemp + strlen(RC_DEF_PRG) + 1, (char **)NULL, 10);
+                sectionSetpri(pSec, nPri);
             }
             else /* Fallback to default value */
-                pSec->nPri = RC_DEF_PRI;
+                sectionSetpri(pSec, RC_DEF_PRI);
 
 /*fprintf(stderr, "nPri ist %d!\n", pSec->nPri);*/
             /* Handle the section userid   */
@@ -237,8 +237,12 @@
 {
     /* Don't remove this! It encapsulates the script object, */
     /*                    which might not be a simple string */
-    fprintf(stdout, "%s", *pScript);
-    return(RC_THROW(RC_OK));
+    if (pScript) {
+        fprintf(stdout, "%s", *pScript);
+        return(RC_THROW(RC_OK));
+    }
+    else
+        return(RC_THROW(RC_ERR_USE));
 }
 
 /************************************************


ossp-pkg/rc/rc_sect.c 1.2 -> 1.3

--- rc_sect.c    2003/04/03 12:05:14     1.2
+++ rc_sect.c    2003/05/14 16:36:28     1.3
@@ -99,7 +99,7 @@
 * sectionSetXXX(rc_section_t *)                 *
 * Accessor methods                              *
 ************************************************/
-rc_return_t sectionSetpri(rc_section_t *pSec, int nPriority)
+rc_return_t sectionSetpri(rc_section_t *pSec, long nPriority)
 { /* Priority of section, used to order sections during exec|eval|print */
     if (pSec) {
         pSec->nPri = nPriority;
@@ -109,7 +109,7 @@
     return(RC_THROW(RC_ERR_USE));
 }
 
-rc_return_t sectionSetuid(rc_section_t *pSec, int nUserid)
+rc_return_t sectionSetuid(rc_section_t *pSec, long nUserid)
 { /* Userid of section, used with setuid during exec or eval */
     if (pSec) {
         pSec->nUid = nUserid;
@@ -122,18 +122,39 @@
 rc_return_t sectionSetdata(rc_section_t *pSec, const char *kszScript)
 { /* Data of section, this is the script body of the particular section */
     if (pSec) {
-        pSec->Bytes = strlen(kszScript) + sizeof(char); /* Calculate size */
+        pSec->Bytes = strlen(kszScript) * sizeof(char) + sizeof(char);
         if (pSec->szData) { /* The section data is already in use */
             free(pSec->szData);
-            pSec->szData = malloc(pSec->szData);
+            pSec->szData = malloc(pSec->Bytes);
             strcpy(pSec->szData, kszScript);
         }
         else { /* Set the data the usual way */
-            pSec->szData = malloc(pSec->szData);
+            pSec->szData = malloc(pSec->Bytes);
             strcpy(pSec->szData, kszScript);
         }
         return(RC_THROW(RC_OK));
     }
+
+    return(RC_THROW(RC_ERR_USE));
+}
+
+rc_return_t sectionSetndata(rc_section_t *pSec, const char *kszScript, size_t Len)
+{ /* Data of section, this is the script body of the particular section */
+    if (pSec) {
+        pSec->Bytes = Len * sizeof(char) + sizeof(char); /* Set size */
+        if (pSec->szData) { /* The section data is already in use */
+            free(pSec->szData);
+            pSec->szData = malloc(pSec->Bytes);
+            strncpy(pSec->szData, kszScript, Len);
+            *(pSec->szData + Len) = NULL; /* Terminate outgoing */
+        }
+        else { /* Set the data the usual way */
+            pSec->szData = malloc(pSec->Bytes);
+            strncpy(pSec->szData, kszScript, Len);
+            *(pSec->szData + Len) = NULL; /* Terminate outgoing */
+        }
+        return(RC_THROW(RC_OK));
+    }
 
     return(RC_THROW(RC_ERR_USE));
 }

CVSTrac 2.0.1