From: Marco van Wieringen Date: Sat, 10 Nov 2012 09:36:10 +0000 (+0100) Subject: Final fix of bug #1943 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=1c6349a3e3a14042f8db6e55934c54040f63199c;p=bacula%2Fbacula Final fix of bug #1943 We cannot check if the db is connected in the library as that has no full B_DB class structure. So we now check the connected state of the database connection in the escape and insert handler which are registered by the director. --- diff --git a/bacula/src/dird/dird.c b/bacula/src/dird/dird.c index 07ad49db54..4596d980e1 100644 --- a/bacula/src/dird/dird.c +++ b/bacula/src/dird/dird.c @@ -65,7 +65,6 @@ extern int job_setattr(PyObject *self, char *attrname, PyObject *value); /* Forward referenced subroutines */ void terminate_dird(int sig); static bool check_resources(); -static void dir_sql_query(JCR *jcr, const char *cmd); static void cleanup_old_files(); /* Exported subroutines */ @@ -117,6 +116,32 @@ static bool check_catalog(cat_op mode); #define CONFIG_FILE "bacula-dir.conf" /* default configuration file */ +/* + * This allows the message handler to operate on the database + * by using a pointer to this function. The pointer is + * needed because the other daemons do not have access + * to the database. If the pointer is + * not defined (other daemons), then writing the database + * is disabled. + */ +static bool dir_sql_query(JCR *jcr, const char *cmd) +{ + if (!jcr || !jcr->db || !jcr->db->is_connected()) { + return false; + } + + return db_sql_query(jcr->db, cmd); +} + +static bool dir_sql_escape(JCR *jcr, B_DB *mdb, char *snew, char *old, int len) +{ + if (!jcr || !jcr->db || !jcr->db->is_connected()) { + return false; + } + + db_escape_string(jcr, mdb, snew, old, len); + return true; +} static void usage() { @@ -313,7 +338,7 @@ int main (int argc, char *argv[]) /* Plug database interface for library routines */ p_sql_query = (sql_query_func)dir_sql_query; - p_sql_escape = (sql_escape_func)db_escape_string; + p_sql_escape = (sql_escape_func)dir_sql_escape; FDConnectTimeout = (int)director->FDConnectTimeout; SDConnectTimeout = (int)director->SDConnectTimeout; @@ -365,22 +390,6 @@ int main (int argc, char *argv[]) return 0; } -/* - * This allows the message handler to operate on the database - * by using a pointer to this function. The pointer is - * needed because the other daemons do not have access - * to the database. If the pointer is - * not defined (other daemons), then writing the database - * is disabled. - */ -static void dir_sql_query(JCR *jcr, const char *cmd) -{ - if (!jcr || !jcr->db) { - return; - } - db_sql_query(jcr->db, cmd, NULL, NULL); -} - /* Cleanup and then exit */ void terminate_dird(int sig) { diff --git a/bacula/src/lib/message.c b/bacula/src/lib/message.c index c510563488..82c5b4ef41 100644 --- a/bacula/src/lib/message.c +++ b/bacula/src/lib/message.c @@ -817,7 +817,7 @@ void dispatch_message(JCR *jcr, int type, utime_t mtime, char *msg) switch (d->dest_code) { case MD_CATALOG: char ed1[50]; - if (!jcr || !jcr->db || !jcr->db->is_connected()) { + if (!jcr || !jcr->db) { break; } if (p_sql_query && p_sql_escape) { @@ -826,12 +826,16 @@ void dispatch_message(JCR *jcr, int type, utime_t mtime, char *msg) int len = strlen(msg) + 1; esc_msg = check_pool_memory_size(esc_msg, len * 2 + 1); - p_sql_escape(jcr, jcr->db, esc_msg, msg, len); - - bstrutime(dt, sizeof(dt), mtime); - Mmsg(cmd, "INSERT INTO Log (JobId, Time, LogText) VALUES (%s,'%s','%s')", - edit_int64(jcr->JobId, ed1), dt, esc_msg); - p_sql_query(jcr, cmd); + if (p_sql_escape(jcr, jcr->db, esc_msg, msg, len)) { + bstrutime(dt, sizeof(dt), mtime); + Mmsg(cmd, "INSERT INTO Log (JobId, Time, LogText) VALUES (%s,'%s','%s')", + edit_int64(jcr->JobId, ed1), dt, esc_msg); + if (!p_sql_query(jcr, cmd)) { + delivery_error(_("Msg delivery error: Unable to store data in database.\n")); + } + } else { + delivery_error(_("Msg delivery error: Unable to store data in database.\n")); + } free_pool_memory(cmd); free_pool_memory(esc_msg); diff --git a/bacula/src/lib/message.h b/bacula/src/lib/message.h index e3fb1e5a49..20cad236c5 100644 --- a/bacula/src/lib/message.h +++ b/bacula/src/lib/message.h @@ -148,8 +148,8 @@ bool get_trace(void); const char *get_basename(const char *pathname); class B_DB; -typedef void (*sql_query_func)(JCR *jcr, const char *cmd); -typedef void (*sql_escape_func)(JCR *jcr, B_DB* db, char *snew, char *old, int len); +typedef bool (*sql_query_func)(JCR *jcr, const char *cmd); +typedef bool (*sql_escape_func)(JCR *jcr, B_DB *db, char *snew, char *old, int len); extern DLL_IMP_EXP sql_query_func p_sql_query; extern DLL_IMP_EXP sql_escape_func p_sql_escape;