From 47603c485b47965f00dc1d9d9119d03cfeba8e71 Mon Sep 17 00:00:00 2001 From: Nicolas Boichat Date: Sat, 8 May 2004 09:49:55 +0000 Subject: [PATCH] parse_conf now returns a status if wanted. git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@1347 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/lib/lex.c | 1 + bacula/src/lib/message.c | 14 ++++++++++++-- bacula/src/lib/parse_conf.c | 38 ++++++++++++++++++++++++++----------- bacula/src/lib/parse_conf.h | 2 +- bacula/src/lib/protos.h | 2 +- 5 files changed, 42 insertions(+), 15 deletions(-) diff --git a/bacula/src/lib/lex.c b/bacula/src/lib/lex.c index 52e3da69df..42a7ab9833 100644 --- a/bacula/src/lib/lex.c +++ b/bacula/src/lib/lex.c @@ -120,6 +120,7 @@ LEX *lex_open_file(LEX *lf, const char *filename, LEX_ERROR_HANDLER *scan_error) if ((fd = fopen(fname, "r")) == NULL) { Emsg2(M_ERROR_TERM, 0, _("Cannot open config file %s: %s\n"), fname, strerror(errno)); + return NULL; /* Never reached if exit_on_error == 1 */ } Dmsg1(900, "Open config file: %s\n", fname); nf = (LEX *)malloc(sizeof(LEX)); diff --git a/bacula/src/lib/message.c b/bacula/src/lib/message.c index fe7ddfaa38..24d1f90fd5 100755 --- a/bacula/src/lib/message.c +++ b/bacula/src/lib/message.c @@ -89,6 +89,9 @@ static bool trace = false; static MSGS *daemon_msgs; /* global messages */ +/* Define if e_msg must exit when M_ERROR_TERM is received */ +static int exit_on_error = 1; + /* * Set daemon name. Also, find canonical execution * path. Note, exepath has spare room for tacking on @@ -930,7 +933,7 @@ e_msg(const char *file, int line, int type, int level, const char *fmt,...) char *p = 0; p[0] = 0; /* generate segmentation violation */ } - if (type == M_ERROR_TERM) { + if ((type == M_ERROR_TERM) && exit_on_error) { exit(1); } } @@ -1027,7 +1030,7 @@ Jmsg(JCR *jcr, int type, int level, const char *fmt,...) char *p = 0; p[0] = 0; /* generate segmentation violation */ } - if (type == M_ERROR_TERM) { + if ((type == M_ERROR_TERM) && exit_on_error) { exit(1); } } @@ -1201,3 +1204,10 @@ void q_msg(const char *file, int line, JCR *jcr, int type, int level, const char Qmsg(jcr, type, level, "%s", pool_buf); free_memory(pool_buf); } + +/* + * Define if e_msg must exit when M_ERROR_TERM is received + */ +void set_exit_on_error(int value) { + exit_on_error = value; +} diff --git a/bacula/src/lib/parse_conf.c b/bacula/src/lib/parse_conf.c index bba1cf46bc..56d7696d5a 100755 --- a/bacula/src/lib/parse_conf.c +++ b/bacula/src/lib/parse_conf.c @@ -680,11 +680,13 @@ enum parse_state { /********************************************************************* * * Parse configuration file - * + * + * Return 0 if reading failed, 1 otherwise */ -void -parse_config(const char *cf) +int +parse_config(const char *cf, int exit_on_error) { + set_exit_on_error(exit_on_error); LEX *lc = NULL; int token, i, pass; int res_type = 0; @@ -698,7 +700,10 @@ parse_config(const char *cf) Dmsg0(300, "Enter parse_config()\n"); for (pass=1; pass <= 2; pass++) { Dmsg1(300, "parse_config pass %d\n", pass); - lc = lex_open_file(lc, cf, NULL); + if ((lc = lex_open_file(lc, cf, NULL)) == NULL) { + set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ + return 0; + } while ((token=lex_get_token(lc, T_ALL)) != T_EOF) { Dmsg1(300, "parse got token=%s\n", lex_tok_to_str(token)); switch (state) { @@ -708,7 +713,8 @@ parse_config(const char *cf) } if (token != T_IDENTIFIER) { scan_err1(lc, _("Expected a Resource name identifier, got: %s"), lc->str); - /* NOT REACHED */ + set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ + return 0; } for (i=0; resources[i].name; i++) if (strcasecmp(resources[i].name, lc->str) == 0) { @@ -720,7 +726,8 @@ parse_config(const char *cf) } if (state == p_none) { scan_err1(lc, _("expected resource name, got: %s"), lc->str); - /* NOT REACHED */ + set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ + return 0; } break; case p_resource: @@ -731,7 +738,8 @@ parse_config(const char *cf) case T_IDENTIFIER: if (level != 1) { scan_err1(lc, _("not in resource definition: %s"), lc->str); - /* NOT REACHED */ + set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ + return 0; } for (i=0; items[i].name; i++) { if (strcasecmp(items[i].name, lc->str) == 0) { @@ -742,7 +750,8 @@ parse_config(const char *cf) Dmsg1 (300, "in T_IDENT got token=%s\n", lex_tok_to_str(token)); if (token != T_EQUALS) { scan_err1(lc, _("expected an equals, got: %s"), lc->str); - /* NOT REACHED */ + set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ + return 0; } } Dmsg1(300, "calling handler for %s\n", items[i].name); @@ -757,7 +766,8 @@ parse_config(const char *cf) Dmsg1(300, "Keyword = %s\n", lc->str); scan_err1(lc, _("Keyword \"%s\" not permitted in this resource.\n" "Perhaps you left the trailing brace off of the previous resource."), lc->str); - /* NOT REACHED */ + set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ + return 0; } break; @@ -774,16 +784,20 @@ parse_config(const char *cf) default: scan_err2(lc, _("unexpected token %d %s in resource definition"), token, lex_tok_to_str(token)); - /* NOT REACHED */ + set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ + return 0; } break; default: scan_err1(lc, _("Unknown parser state %d\n"), state); - /* NOT REACHED */ + set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ + return 0; } } if (state != p_none) { scan_err0(lc, _("End of conf file reached with unclosed resource.")); + set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ + return 0; } if (debug_level > 50 && pass == 2) { int i; @@ -794,6 +808,8 @@ parse_config(const char *cf) lc = lex_close_file(lc); } Dmsg0(300, "Leave parse_config()\n"); + set_exit_on_error(1); + return 1; } /********************************************************************* diff --git a/bacula/src/lib/parse_conf.h b/bacula/src/lib/parse_conf.h index 45f7ca38fc..ad91d45994 100644 --- a/bacula/src/lib/parse_conf.h +++ b/bacula/src/lib/parse_conf.h @@ -97,7 +97,7 @@ union CURES { /* Configuration routines */ -void parse_config(const char *cf); +int parse_config(const char *cf, int exit_on_error = 1); void free_config_resources(void); RES **save_config_resources(void); diff --git a/bacula/src/lib/protos.h b/bacula/src/lib/protos.h index d11c9dbd75..564807db65 100644 --- a/bacula/src/lib/protos.h +++ b/bacula/src/lib/protos.h @@ -152,7 +152,7 @@ void init_console_msg (char *wd); void free_msgs_res (MSGS *msgs); void dequeue_messages (JCR *jcr); void set_trace (int trace_flag); - +void set_exit_on_error (int value); /* bnet_server.c */ void bnet_thread_server(char *bind_addr, int port, int max_clients, workq_t *client_wq, -- 2.39.5