Another attempt to fix the MySQL server has gone away error. Fixed gcc 7.1+ compiler warnings.

Mon, 02 Dec 2019 20:56:01 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 02 Dec 2019 20:56:01 +0100
changeset 558
a8e065a9f851
parent 557
ee76d0bfcdaf
child 559
10cc2400b5d5

Another attempt to fix the MySQL server has gone away error. Fixed gcc 7.1+ compiler warnings.

bmsd/co2meters.c file | annotate | diff | comparison | revisions
bmsd/fermenters.c file | annotate | diff | comparison | revisions
bmsd/mysql.c file | annotate | diff | comparison | revisions
--- a/bmsd/co2meters.c	Mon Dec 02 16:53:56 2019 +0100
+++ b/bmsd/co2meters.c	Mon Dec 02 20:56:01 2019 +0100
@@ -235,9 +235,9 @@
     jobj = json_tokener_parse(payload);
 
     timestamp = time(NULL);
-    log->datetime = malloc(21);
+    log->datetime = malloc(73);
     mytime = localtime(&timestamp);
-    snprintf(log->datetime, 20, "%04d-%02d-%02d %02d:%02d:%02d", 
+    snprintf(log->datetime, 72, "%04d-%02d-%02d %02d:%02d:%02d",
 	mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
 
     if (json_object_object_get_ex(jobj, "metric", &metric)) {
--- a/bmsd/fermenters.c	Mon Dec 02 16:53:56 2019 +0100
+++ b/bmsd/fermenters.c	Mon Dec 02 20:56:01 2019 +0100
@@ -444,9 +444,9 @@
 
     if (json_object_object_get_ex(jobj, "timestamp", &val)) {
 	timestamp = json_object_get_int(val);
-    	log->datetime = malloc(21);
+    	log->datetime = malloc(73);
     	mytime = localtime(&timestamp);
-    	snprintf(log->datetime, 20, "%04d-%02d-%02d %02d:%02d:%02d", 
+    	snprintf(log->datetime, 73, "%04d-%02d-%02d %02d:%02d:%02d",
 	    mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
     }
 
--- a/bmsd/mysql.c	Mon Dec 02 16:53:56 2019 +0100
+++ b/bmsd/mysql.c	Mon Dec 02 20:56:01 2019 +0100
@@ -397,18 +397,20 @@
 int bms_mysql_query(const char *query)
 {
     int		rc = mysql_query(con, query);
+    int		err = mysql_errno(con);
 
     if (rc) {
-        syslog(LOG_NOTICE, "MySQL: error %u (%s)", mysql_errno(con), mysql_error(con));
+        syslog(LOG_NOTICE, "MySQL: error %u (%s)", err, mysql_error(con));
         syslog(LOG_NOTICE, query);
     } else {
 	return 0;
     }
 
     /* Any error execpt server gone away */
-    if (rc != 2006)
+    if (err != 2006)
 	return rc;
 
+    syslog(LOG_NOTICE, "Trying to reconnect");
     /* Try to reconnect and do the query again */
     mysql_close(con);
     if (mysql_real_connect(con, Config.mysql_host, Config.mysql_user, Config.mysql_pass, Config.mysql_database, Config.mysql_port, NULL, 0) == NULL) {
@@ -434,14 +436,14 @@
 
 void node_mysql_insert(sys_node_list *node)
 {
-    char	*query = malloc(1024), first[21], last[21];
+    char	*query = malloc(1024), first[73], last[73];
     struct tm	*mytime;
 
     mytime = localtime(&node->firstseen);
-    snprintf(first, 20, "%04d-%02d-%02d %02d:%02d:%02d", 
+    snprintf(first, 72, "%04d-%02d-%02d %02d:%02d:%02d",
 		    mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
     mytime = localtime(&node->lastseen);
-    snprintf(last, 20, "%04d-%02d-%02d %02d:%02d:%02d",
+    snprintf(last, 72, "%04d-%02d-%02d %02d:%02d:%02d",
 		mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
 
     snprintf(query, 1023,
@@ -465,11 +467,11 @@
 
 void node_mysql_update(sys_node_list *node)
 {
-    char        *query = malloc(1024), last[21];
+    char        *query = malloc(1024), last[65];
     struct tm   *mytime;
 
     mytime = localtime(&node->lastseen);
-    snprintf(last, 20, "%04d-%02d-%02d %02d:%02d:%02d",
+    snprintf(last, 64, "%04d-%02d-%02d %02d:%02d:%02d",
     	mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
 
     snprintf(query, 1023,

mercurial