phosphor-sel-logger clears SEL

the sel-logger is now responsible to clear the SEL and it is no
longer configurable.

- the meson option 'clears-sel' is removed

- function saveClearSelTimestamp called to persist the timestamp
  when the SEL was cleared.

Tested: together with the other topic changes. SEL Add/Clear works.

Change-Id: I04449e0c5c335ec1f2b7a54895c5b8337faf6071
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/meson.build b/meson.build
index 72a1c7e..e239c02 100644
--- a/meson.build
+++ b/meson.build
@@ -36,9 +36,6 @@
 
   deps += dependency('phosphor-logging')
 endif
-if get_option('clears-sel')
-  cpp_args += '-DSEL_LOGGER_CLEARS_SEL'
-endif
 
 executable(
   'sel-logger',
diff --git a/meson.options b/meson.options
index 81ab2f6..1c0866c 100644
--- a/meson.options
+++ b/meson.options
@@ -10,5 +10,3 @@
        description: 'Automatically log SEL records for host error events')
 option('send-to-logger', type: 'boolean',
        description: 'Automatically log events to Redfish for pulse type assert-deassert sensor events')
-option('clears-sel', type: 'boolean',
-       description: 'Expose method for SEL Logger to clear the SEL records stored in ipmi_sel')
diff --git a/src/sel_logger.cpp b/src/sel_logger.cpp
index 98c0530..3cc183e 100644
--- a/src/sel_logger.cpp
+++ b/src/sel_logger.cpp
@@ -111,11 +111,30 @@
     return std::stoul(newestEntryFields[1]);
 }
 
-#ifdef SEL_LOGGER_CLEARS_SEL
 static unsigned int recordId = initializeRecordId();
 
+static void saveClearSelTimestamp()
+{
+    int fd = open("/var/lib/ipmi/sel_erase_time",
+                  O_WRONLY | O_CREAT | O_CLOEXEC, 0644);
+    if (fd < 0)
+    {
+        std::cerr << "Failed to open file\n";
+        return;
+    }
+
+    if (futimens(fd, NULL) < 0)
+    {
+        std::cerr << "Failed to update SEL cleared timestamp: "
+                  << std::string(strerror(errno));
+    }
+    close(fd);
+}
+
 void clearSelLogFiles()
 {
+    saveClearSelTimestamp();
+
     // Clear the SEL by deleting the log files
     std::vector<std::filesystem::path> selLogFiles;
     if (getSELLogFiles(selLogFiles))
@@ -145,21 +164,9 @@
         std::cerr << e.what() << "\n";
     }
 }
-#endif
 
 static unsigned int getNewRecordId(void)
 {
-#ifndef SEL_LOGGER_CLEARS_SEL
-    static unsigned int recordId = initializeRecordId();
-
-    // If the log has been cleared, also clear the current ID
-    std::vector<std::filesystem::path> selLogFiles;
-    if (!getSELLogFiles(selLogFiles))
-    {
-        recordId = selInvalidRecID;
-    }
-#endif
-
     if (++recordId >= selInvalidRecID)
     {
         recordId = 1;
@@ -297,12 +304,10 @@
         return selAddOemRecord(conn, message, selData, recordType);
     });
 
-#ifdef SEL_LOGGER_CLEARS_SEL
 #ifndef SEL_LOGGER_SEND_TO_LOGGING_SERVICE
     // Clear SEL entries
     ifaceAddSel->register_method("Clear", []() { clearSelLogFiles(); });
 #endif
-#endif
     ifaceAddSel->initialize();
 
 #ifdef SEL_LOGGER_MONITOR_THRESHOLD_EVENTS