summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRainer Gerhards <rgerhards@adiscon.com>2007-07-31 09:01:02 +0000
committerRainer Gerhards <rgerhards@adiscon.com>2007-07-31 09:01:02 +0000
commit82747c8d01a775e4a0921f2c839ea410cd48d168 (patch)
treec0c336a548cbd6b2ef2b36cbc50435c9ee869934
parentc0b2b6b1bf3735bb0570523c9529bb1f93e96b86 (diff)
downloadrsyslog-82747c8d01a775e4a0921f2c839ea410cd48d168.tar.gz
rsyslog-82747c8d01a775e4a0921f2c839ea410cd48d168.tar.xz
rsyslog-82747c8d01a775e4a0921f2c839ea410cd48d168.zip
moved code to open config file into separate function processConfFile()
-rw-r--r--rsyslog.h1
-rw-r--r--syslogd.c202
2 files changed, 113 insertions, 90 deletions
diff --git a/rsyslog.h b/rsyslog.h
index 6a9f9500..60d003b2 100644
--- a/rsyslog.h
+++ b/rsyslog.h
@@ -63,6 +63,7 @@ enum rsRetVal_ /** return value. All methods return this if not specified oth
RS_RET_INVALID_INT = -2010,/**< invalid integer */
RS_RET_INVALID_CMD = -2011,/**< invalid command */
RS_RET_VAL_OUT_OF_RANGE = -2012, /**< value out of range */
+ RS_RET_FOPEN_FAILURE = -2013, /**< failure during fopen, for example file not found - see errno */
RS_RET_OK = 0 /**< operation successful */
};
typedef enum rsRetVal_ rsRetVal; /**< friendly type for global return value */
diff --git a/syslogd.c b/syslogd.c
index 115abfd7..b5914b73 100644
--- a/syslogd.c
+++ b/syslogd.c
@@ -3705,20 +3705,127 @@ static void freeSelectors(void)
}
+/* process a configuration file
+ * started with code from init() by rgerhards on 2007-07-31
+ */
+static rsRetVal processConfFile()
+{
+ DEFiRet;
+ int iLnNbr = 0;
+ FILE *cf;
+ selector_t *f;
+ selector_t *nextp = NULL;
+ uchar *p;
+ unsigned int Forwarding = 0;
+#ifdef CONT_LINE
+ uchar cbuf[BUFSIZ];
+ uchar *cline;
+#else
+ uchar cline[BUFSIZ];
+#endif
+
+ if((cf = fopen(ConfFile, "r")) == NULL) {
+ ABORT_FINALIZE(RS_RET_FOPEN_FAILURE);
+ }
+
+ /* Now process the file.
+ */
+#if CONT_LINE
+ cline = cbuf;
+ while (fgets((char*)cline, sizeof(cbuf) - (cline - cbuf), cf) != NULL) {
+#else
+ while (fgets(cline, sizeof(cline), cf) != NULL) {
+#endif
+ ++iLnNbr;
+ /* drop LF - TODO: make it better, replace fgets(), but its clean as it is */
+ if(cline[strlen((char*)cline)-1] == '\n') {
+ cline[strlen((char*)cline) -1] = '\0';
+ }
+ /* check for end-of-section, comments, strip off trailing
+ * spaces and newline character.
+ */
+ p = cline;
+ skipWhiteSpace(&p);
+ if (*p == '\0' || *p == '#')
+ continue;
+
+ if(*p == '$') {
+ if(cfsysline((uchar*) ++p) != RS_RET_OK) {
+ logerrorInt("the last error occured in config file line %d", iLnNbr);
+ }
+ continue;
+ }
+#if CONT_LINE
+ strcpy((char*)cline, (char*)p);
+#endif
+ for (p = (uchar*) strchr((char*)cline, '\0'); isspace((int) *--p););
+#if CONT_LINE
+ if (*p == '\\') {
+ if ((p - cbuf) > BUFSIZ - 30) {
+ /* Oops the buffer is full - what now? */
+ cline = cbuf;
+ } else {
+ *p = 0;
+ cline = p;
+ continue;
+ }
+ } else
+ cline = cbuf;
+#endif
+ *++p = '\0';
+
+ /* allocate next entry and add it */
+ f = (selector_t *)calloc(1, sizeof(selector_t));
+ if(f == NULL) {
+ /* this time, it looks like we really have no point in continuing to run... */
+ logerror("fatal: could not allocated selector\n");
+ ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
+ }
+
+#if CONT_LINE
+ if(cfline((char*)cbuf, f) != RS_RET_OK) {
+#else
+ if(cfline(cline, f) != RS_RET_OK) {
+#endif
+ /* creation of the entry failed, we need to discard it */
+ dprintf("selector line NOT successfully processed\n");
+ free(f);
+ } else {
+ /* successfully created an entry */
+ dprintf("selector line successfully processed\n");
+ if(nextp == NULL) {
+ Files = f;
+ }
+ else {
+ nextp->f_next = f;
+ }
+ nextp = f;
+ if(f->pMod->needUDPSocket(f->pModData) == RS_RET_TRUE) {
+ Forwarding++;
+ }
+ }
+ }
+
+ /* close the configuration file */
+ (void) fclose(cf);
+
+finalize_it:
+ return iRet;
+}
+
+
/* INIT -- Initialize syslogd from configuration table
* init() is called at initial startup AND each time syslogd is HUPed
*/
static void init()
{
+ DEFiRet;
register int i;
- register FILE *cf;
register selector_t *f;
- register selector_t *nextp;
- register char *p;
+ selector_t *nextp;
register unsigned int Forwarding = 0;
#ifdef CONT_LINE
char cbuf[BUFSIZ];
- char *cline;
#else
char cline[BUFSIZ];
#endif
@@ -3788,10 +3895,9 @@ static void init()
resetConfigVariables();
f = NULL;
- nextp = NULL;
/* open the configuration file */
- if ((cf = fopen(ConfFile, "r")) == NULL) {
+ if((iRet = processConfFile(ConfFile)) != RS_RET_OK) {
/* rgerhards: this code is executed to set defaults when the
* config file could not be opened. We might think about
* abandoning the run in this case - but this, too, is not
@@ -3808,90 +3914,6 @@ static void init()
cfline(cbuf, nextp->f_next->f_next);
Initialized = 1;
}
- else { /* we should consider moving this into a separate function, its lengthy... */
- int iLnNbr = 0;
- /*
- * Foreach line in the conf table, open that file.
- */
- #if CONT_LINE
- cline = cbuf;
- while (fgets(cline, sizeof(cbuf) - (cline - cbuf), cf) != NULL) {
- #else
- while (fgets(cline, sizeof(cline), cf) != NULL) {
- #endif
- ++iLnNbr;
- /* drop LF - TODO: make it better, replace fgets(), but its clean as it is */
- if(cline[strlen(cline)-1] == '\n') {
- cline[strlen(cline) -1] = '\0';
- }
- /*
- * check for end-of-section, comments, strip off trailing
- * spaces and newline character.
- */
- for (p = cline; isspace((int) *p); ++p) /*SKIP SPACES*/;
- if (*p == '\0' || *p == '#')
- continue;
-
- if(*p == '$') {
- if(cfsysline((uchar*) ++p) != RS_RET_OK) {
- logerrorInt("error occured in config file line %d", iLnNbr);
- }
- continue;
- }
- #if CONT_LINE
- strcpy(cline, p);
- #endif
- for (p = strchr(cline, '\0'); isspace((int) *--p););
- #if CONT_LINE
- if (*p == '\\') {
- if ((p - cbuf) > BUFSIZ - 30) {
- /* Oops the buffer is full - what now? */
- cline = cbuf;
- } else {
- *p = 0;
- cline = p;
- continue;
- }
- } else
- cline = cbuf;
- #endif
- *++p = '\0';
-
- /* allocate next entry and add it */
- f = (selector_t *)calloc(1, sizeof(selector_t));
- if(f == NULL) {
- /* this time, it looks like we really have no point in continuing to run... */
- logerror("fatal: could not allocated selector\n");
- exit(1); /* TODO: think about it, maybe we can avoid */
- }
-
- #if CONT_LINE
- if(cfline(cbuf, f) != RS_RET_OK) {
- #else
- if(cfline(cline, f) != RS_RET_OK) {
- #endif
- /* creation of the entry failed, we need to discard it */
- dprintf("selector line NOT successfully processed\n");
- free(f);
- } else {
- /* successfully created an entry */
- dprintf("selector line successfully processed\n");
- if(nextp == NULL) {
- Files = f;
- }
- else {
- nextp->f_next = f;
- }
- nextp = f;
- if(f->pMod->needUDPSocket(f->pModData) == RS_RET_TRUE) {
- Forwarding++;
- }
- }
- }
-
- /* close the configuration file */
- (void) fclose(cf);
- }
/* we are now done with reading the configuraton. This is the right time to
* free some objects that were just needed for loading it. rgerhards 2005-10-19