]> www.infradead.org Git - users/mchehab/rasdaemon.git/commitdiff
rasdaemon: add closure and cleanups for the database
authorShiju Jose <shiju.jose@huawei.com>
Wed, 16 Oct 2019 16:34:00 +0000 (17:34 +0100)
committerMauro Carvalho Chehab <mchehab+huawei@kernel.org>
Wed, 20 Nov 2019 04:16:24 +0000 (05:16 +0100)
This patch adds closure and cleanups for the sqlite3 database.

Signed-off-by: Shiju Jose <shiju.jose@huawei.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
ras-events.c
ras-non-standard-handler.c
ras-non-standard-handler.h
ras-record.c
ras-record.h

index f912daed03ccdd46fa1dc6a6e7baed68ff00b43d..d155caad6556e2e635d315c975eb1739602d90c7 100644 (file)
@@ -418,7 +418,7 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata,
                        size = read(fds[i].fd, page, pdata[i].ras->page_size);
                        if (size < 0) {
                                log(TERM, LOG_WARNING, "read\n");
-                               goto error;
+                               goto cleanup;
                        } else if (size > 0) {
                                kbuffer_load_subbuffer(kbuf, page);
 
@@ -452,6 +452,13 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata,
        /* poll() is not supported. We need to fallback to the old way */
        log(TERM, LOG_INFO,
            "Old kernel detected. Stop listening and fall back to pthread way.\n");
+
+cleanup:
+       if (pdata[0].ras->record_events) {
+               unregister_ns_dec_tab();
+               ras_mc_event_closedb(pdata[0].cpu, pdata[0].ras);
+       }
+
 error:
        kbuffer_free(kbuf);
        free(page);
@@ -540,6 +547,11 @@ static void *handle_ras_events_cpu(void *priv)
 
        read_ras_event(fd, pdata, kbuf, page);
 
+       if (pdata->ras->record_events) {
+               unregister_ns_dec_tab();
+               ras_mc_event_closedb(pdata->cpu, pdata->ras);
+       }
+
        close(fd);
        kbuffer_free(kbuf);
        free(page);
index 1b5d67a2f7bb843bee5662b5431c43a1a304841e..d92fd420b22395feb6ced18e49e992cdc06b40a1 100644 (file)
@@ -41,6 +41,22 @@ int register_ns_dec_tab(const p_ns_dec_tab tab)
 void unregister_ns_dec_tab(void)
 {
        if (ns_dec_tab) {
+#ifdef HAVE_SQLITE3
+               p_ns_dec_tab dec_tab;
+               int i, count;
+
+               for (count = 0; count < dec_tab_count; count++) {
+                       dec_tab = ns_dec_tab[count];
+                       for (i = 0; dec_tab[i].decode; i++) {
+                               if (dec_tab[i].stmt_dec_record) {
+                                       ras_mc_finalize_vendor_table(
+                                               dec_tab[i].stmt_dec_record);
+                                       dec_tab[i].stmt_dec_record = NULL;
+                               }
+                       }
+               }
+#endif
+
                free(ns_dec_tab);
                ns_dec_tab = NULL;
                dec_tab_count = 0;
index fd9dd9296478b80e0352498e26f5d00fc72e09e4..2b9bf40ea25db56dffc4df11348cc3e5a8be20a9 100644 (file)
@@ -36,8 +36,12 @@ int ras_non_standard_event_handler(struct trace_seq *s,
 
 void print_le_hex(struct trace_seq *s, const uint8_t *buf, int index);
 
+#ifdef HAVE_NON_STANDARD
 int register_ns_dec_tab(const p_ns_dec_tab tab);
-
 void unregister_ns_dec_tab(void);
+#else
+static inline int register_ns_dec_tab(const p_ns_dec_tab tab) { return 0; };
+static inline void unregister_ns_dec_tab(void) { return; };
+#endif
 
 #endif
index 8f1c550f98b177d0d0b644a3558fdcbbbc091765..c6cf61af8d590997bfb8abfffa0c2c645c725105 100644 (file)
@@ -595,6 +595,18 @@ int ras_mc_add_vendor_table(struct ras_events *ras,
        return rc;
 }
 
+int ras_mc_finalize_vendor_table(sqlite3_stmt *stmt)
+{
+       int rc;
+
+       rc = sqlite3_finalize(stmt);
+       if (rc != SQLITE_OK)
+               log(TERM, LOG_ERR,
+                   "Failed to finalize sqlite: error = %d\n", rc);
+
+       return rc;
+}
+
 int ras_mc_event_opendb(unsigned cpu, struct ras_events *ras)
 {
        int rc;
@@ -692,3 +704,111 @@ int ras_mc_event_opendb(unsigned cpu, struct ras_events *ras)
                ras->db_priv = priv;
        return 0;
 }
+
+int ras_mc_event_closedb(unsigned int cpu, struct ras_events *ras)
+{
+       int rc;
+       sqlite3 *db;
+       struct sqlite3_priv *priv = ras->db_priv;
+
+       printf("Calling %s()\n", __func__);
+
+       if (!priv)
+               return -1;
+
+       db = priv->db;
+       if (!db)
+               return -1;
+
+       if (priv->stmt_mc_event) {
+               rc = sqlite3_finalize(priv->stmt_mc_event);
+               if (rc != SQLITE_OK)
+                       log(TERM, LOG_ERR,
+                           "cpu %u: Failed to finalize mc_event sqlite: error = %d\n",
+                           cpu, rc);
+       }
+
+#ifdef HAVE_AER
+       if (priv->stmt_aer_event) {
+               rc = sqlite3_finalize(priv->stmt_aer_event);
+               if (rc != SQLITE_OK)
+                       log(TERM, LOG_ERR,
+                           "cpu %u: Failed to finalize aer_event sqlite: error = %d\n",
+                           cpu, rc);
+       }
+#endif
+
+#ifdef HAVE_EXTLOG
+       if (priv->stmt_extlog_record) {
+               rc = sqlite3_finalize(priv->stmt_extlog_record);
+               if (rc != SQLITE_OK)
+                       log(TERM, LOG_ERR,
+                           "cpu %u: Failed to finalize extlog_record sqlite: error = %d\n",
+                           cpu, rc);
+       }
+#endif
+
+
+#ifdef HAVE_MCE
+       if (priv->stmt_mce_record) {
+               rc = sqlite3_finalize(priv->stmt_mce_record);
+               if (rc != SQLITE_OK)
+                       log(TERM, LOG_ERR,
+                           "cpu %u: Failed to finalize mce_record sqlite: error = %d\n",
+                           cpu, rc);
+       }
+#endif
+
+#ifdef HAVE_NON_STANDARD
+       if (priv->stmt_non_standard_record) {
+               rc = sqlite3_finalize(priv->stmt_non_standard_record);
+               if (rc != SQLITE_OK)
+                       log(TERM, LOG_ERR,
+                           "cpu %u: Failed to finalize non_standard_record sqlite: error = %d\n",
+                           cpu, rc);
+       }
+#endif
+
+#ifdef HAVE_ARM
+       if (priv->stmt_arm_record) {
+               rc = sqlite3_finalize(priv->stmt_arm_record);
+               if (rc != SQLITE_OK)
+                       log(TERM, LOG_ERR,
+                           "cpu %u: Failed to finalize arm_record sqlite: error = %d\n",
+                           cpu, rc);
+       }
+#endif
+
+#ifdef HAVE_DEVLINK
+       if (priv->stmt_devlink_event) {
+               rc = sqlite3_finalize(priv->stmt_devlink_event);
+               if (rc != SQLITE_OK)
+                       log(TERM, LOG_ERR,
+                           "cpu %u: Failed to finalize devlink_event sqlite: error = %d\n",
+                           cpu, rc);
+       }
+#endif
+
+#ifdef HAVE_DISKERROR
+       if (priv->stmt_diskerror_event) {
+               rc = sqlite3_finalize(priv->stmt_diskerror_event);
+               if (rc != SQLITE_OK)
+                       log(TERM, LOG_ERR,
+                           "cpu %u: Failed to finalize diskerror_event sqlite: error = %d\n",
+                           cpu, rc);
+       }
+#endif
+
+       rc = sqlite3_close_v2(db);
+       if (rc != SQLITE_OK)
+               log(TERM, LOG_ERR,
+                   "cpu %u: Failed to close sqlite: error = %d\n", cpu, rc);
+
+       rc = sqlite3_shutdown();
+       if (rc != SQLITE_OK)
+               log(TERM, LOG_ERR,
+                   "cpu %u: Failed to shutdown sqlite: error = %d\n", cpu, rc);
+       free(priv);
+
+       return 0;
+}
index c9af5ae83f944fbf50d194452f1b6f01e6279ce4..a67b1938ea11a22dd2a8a68b98df501d09ef1c09 100644 (file)
@@ -147,8 +147,10 @@ struct db_table_descriptor {
 };
 
 int ras_mc_event_opendb(unsigned cpu, struct ras_events *ras);
+int ras_mc_event_closedb(unsigned int cpu, struct ras_events *ras);
 int ras_mc_add_vendor_table(struct ras_events *ras, sqlite3_stmt **stmt,
                            const struct db_table_descriptor *db_tab);
+int ras_mc_finalize_vendor_table(sqlite3_stmt *stmt);
 int ras_store_mc_event(struct ras_events *ras, struct ras_mc_event *ev);
 int ras_store_aer_event(struct ras_events *ras, struct ras_aer_event *ev);
 int ras_store_mce_record(struct ras_events *ras, struct mce_event *ev);
@@ -160,6 +162,7 @@ int ras_store_diskerror_event(struct ras_events *ras, struct diskerror_event *ev
 
 #else
 static inline int ras_mc_event_opendb(unsigned cpu, struct ras_events *ras) { return 0; };
+static inline int ras_mc_event_closedb(unsigned int cpu, struct ras_events *ras) { return 0; };
 static inline int ras_store_mc_event(struct ras_events *ras, struct ras_mc_event *ev) { return 0; };
 static inline int ras_store_aer_event(struct ras_events *ras, struct ras_aer_event *ev) { return 0; };
 static inline int ras_store_mce_record(struct ras_events *ras, struct mce_event *ev) { return 0; };