[RFC PATCH 5/6] Report error exit reason to systemd

Victor Westerhuis victor@westerhu.is
Sun Mar 7 12:49:57 GMT 2021


Signed-off-by: Victor Westerhuis <victor@westerhu.is>
---
 debuginfod/debuginfod.cxx | 74 ++++++++++++++++++++++++---------------
 1 file changed, 46 insertions(+), 28 deletions(-)

diff --git a/debuginfod/debuginfod.cxx b/debuginfod/debuginfod.cxx
index 9542c5e2..35b5fc18 100644
--- a/debuginfod/debuginfod.cxx
+++ b/debuginfod/debuginfod.cxx
@@ -120,6 +120,24 @@ using namespace std;
 #endif
 
 
+#ifdef ENABLE_SYSTEMD
+#define report_error(status, errnum, format, ...) \
+  do \
+    { \
+      int s = (status), e = (errnum); \
+      if (s != 0 && e != 0) \
+        { \
+          (void) sd_notifyf (false, "ERRNO=%d", e); \
+          (void) sd_notifyf (false, "STATUS=" format, ##__VA_ARGS__); \
+        } \
+      error (s, e, format, ##__VA_ARGS__); \
+    } \
+  while (false)
+#else
+#define report_error(...) error(__VA_ARGS__)
+#endif
+
+
 inline bool
 string_endswith(const string& haystack, const string& needle)
 {
@@ -3316,10 +3334,10 @@ main (int argc, char *argv[])
 #endif
   int rc = regcomp (& file_include_regex, ".*", REG_EXTENDED|REG_NOSUB); // match everything
   if (rc != 0)
-    error (EXIT_FAILURE, 0, "regcomp failure: %d", rc);
+    report_error (EXIT_FAILURE, 0, "regcomp failure: %d", rc);
   rc = regcomp (& file_exclude_regex, "^$", REG_EXTENDED|REG_NOSUB); // match nothing
   if (rc != 0)
-    error (EXIT_FAILURE, 0, "regcomp failure: %d", rc);
+    report_error (EXIT_FAILURE, 0, "regcomp failure: %d", rc);
 
   // default parameters for fdcache are computed from system stats
   struct statfs sfs;
@@ -3337,8 +3355,8 @@ main (int argc, char *argv[])
   argp_program_version_hook = print_version; // this works
   (void) argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &remaining, NULL);
   if (remaining != argc)
-      error (EXIT_FAILURE, 0,
-             "unexpected argument: %s", argv[remaining]);
+      report_error (EXIT_FAILURE, 0,
+                    "unexpected argument: %s", argv[remaining]);
 
   if (scan_archives.size()==0 && !scan_files && source_paths.size()>0)
     obatched(clog) << "warning: without -F -R -U -Z, ignoring PATHs" << endl;
@@ -3362,13 +3380,13 @@ main (int argc, char *argv[])
   if (rc == SQLITE_CORRUPT)
     {
       (void) unlink (db_path.c_str());
-      error (EXIT_FAILURE, 0,
-             "cannot open %s, deleted database: %s", db_path.c_str(), sqlite3_errmsg(db));
+      report_error (EXIT_FAILURE, 0,
+                    "cannot open %s, deleted database: %s", db_path.c_str(), sqlite3_errmsg(db));
     }
   else if (rc)
     {
-      error (EXIT_FAILURE, 0,
-             "cannot open %s, consider deleting database: %s", db_path.c_str(), sqlite3_errmsg(db));
+      report_error (EXIT_FAILURE, 0,
+                    "cannot open %s, consider deleting database: %s", db_path.c_str(), sqlite3_errmsg(db));
     }
 
   // open the readonly query variant
@@ -3381,8 +3399,8 @@ main (int argc, char *argv[])
                         NULL);
   if (rc)
     {
-      error (EXIT_FAILURE, 0,
-             "cannot open %s, consider deleting database: %s", db_path.c_str(), sqlite3_errmsg(dbq));
+      report_error (EXIT_FAILURE, 0,
+                    "cannot open %s, consider deleting database: %s", db_path.c_str(), sqlite3_errmsg(dbq));
     }
 
 
@@ -3393,16 +3411,16 @@ main (int argc, char *argv[])
   rc = sqlite3_create_function(dbq, "sharedprefix", 2, SQLITE_UTF8, NULL,
                                & sqlite3_sharedprefix_fn, NULL, NULL);
   if (rc != SQLITE_OK)
-    error (EXIT_FAILURE, 0,
-           "cannot create sharedprefix function: %s", sqlite3_errmsg(dbq));
+    report_error (EXIT_FAILURE, 0,
+                  "cannot create sharedprefix function: %s", sqlite3_errmsg(dbq));
 
   if (verbose > 3)
     obatched(clog) << "ddl: " << DEBUGINFOD_SQLITE_DDL << endl;
   rc = sqlite3_exec (db, DEBUGINFOD_SQLITE_DDL, NULL, NULL, NULL);
   if (rc != SQLITE_OK)
     {
-      error (EXIT_FAILURE, 0,
-             "cannot run database schema ddl: %s", sqlite3_errmsg(db));
+      report_error (EXIT_FAILURE, 0,
+                    "cannot run database schema ddl: %s", sqlite3_errmsg(db));
     }
 
   vector<MHD_Daemon *> daemons;
@@ -3413,7 +3431,7 @@ main (int argc, char *argv[])
       if (fds <= 0)
         {
           close_databases ();
-          error (EXIT_FAILURE, -fds, "cannot get fds from systemd");
+          report_error (EXIT_FAILURE, -fds, "cannot get fds from systemd");
         }
 
       for (int i = 0; i < fds; ++i)
@@ -3424,13 +3442,13 @@ main (int argc, char *argv[])
           if (rc < 0)
             {
               close_databases ();
-              error (EXIT_FAILURE, -rc, "unable to determine fd type");
+              report_error (EXIT_FAILURE, -rc, "unable to determine fd type");
             }
           else if (rc == 0)
             {
               close_databases ();
-              error (EXIT_FAILURE, 0,
-                     "fd %d is not of the correct socket type", fd);
+              report_error (EXIT_FAILURE, 0,
+                            "fd %d is not of the correct socket type", fd);
             }
 
           MHD_Daemon *d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
@@ -3450,8 +3468,8 @@ main (int argc, char *argv[])
           if (d == NULL)
             {
               close_databases ();
-              error (EXIT_FAILURE, 0,
-                    "cannot start http server on fd %d", fd);
+              report_error (EXIT_FAILURE, 0,
+                            "cannot start http server on fd %d", fd);
             }
 
           obatched(clog) << "started http server on fd " << fd << endl;
@@ -3491,7 +3509,7 @@ main (int argc, char *argv[])
       if (d4 == NULL && d6 == NULL) // neither ipv4 nor ipv6? boo
         {
           close_databases ();
-          error (EXIT_FAILURE, 0, "cannot start http server at port %d", http_port);
+          report_error (EXIT_FAILURE, 0, "cannot start http server at port %d", http_port);
         }
 
       obatched(clog) << "started http server on "
@@ -3531,8 +3549,8 @@ main (int argc, char *argv[])
         obatched(clog) << "extra ddl:\n" << i << endl;
       rc = sqlite3_exec (db, i.c_str(), NULL, NULL, NULL);
       if (rc != SQLITE_OK && rc != SQLITE_DONE && rc != SQLITE_ROW)
-        error (0, 0,
-               "warning: cannot run database extra ddl %s: %s", i.c_str(), sqlite3_errmsg(db));
+        report_error (0, 0,
+                      "warning: cannot run database extra ddl %s: %s", i.c_str(), sqlite3_errmsg(db));
     }
 
   if (maxigroom)
@@ -3563,7 +3581,7 @@ main (int argc, char *argv[])
   pthread_t pt;
   rc = pthread_create (& pt, NULL, thread_main_groom, NULL);
   if (rc)
-    error (EXIT_FAILURE, rc, "cannot spawn thread to groom database\n");
+    report_error (EXIT_FAILURE, rc, "cannot spawn thread to groom database\n");
   else
     all_threads.push_back(pt);
 
@@ -3571,13 +3589,13 @@ main (int argc, char *argv[])
     {
       rc = pthread_create (& pt, NULL, thread_main_fts_source_paths, NULL);
       if (rc)
-        error (EXIT_FAILURE, rc, "cannot spawn thread to traverse source paths\n");
+        report_error (EXIT_FAILURE, rc, "cannot spawn thread to traverse source paths\n");
       all_threads.push_back(pt);
       for (unsigned i=0; i<concurrency; i++)
         {
           rc = pthread_create (& pt, NULL, thread_main_scanner, NULL);
           if (rc)
-            error (EXIT_FAILURE, rc, "cannot spawn thread to scan source files / archives\n");
+            report_error (EXIT_FAILURE, rc, "cannot spawn thread to scan source files / archives\n");
           all_threads.push_back(pt);
         }
     }
@@ -3610,8 +3628,8 @@ main (int argc, char *argv[])
   rc = sqlite3_exec (db, DEBUGINFOD_SQLITE_CLEANUP_DDL, NULL, NULL, NULL);
   if (rc != SQLITE_OK)
     {
-      error (0, 0,
-             "warning: cannot run database cleanup ddl: %s", sqlite3_errmsg(db));
+      report_error (0, 0,
+                    "warning: cannot run database cleanup ddl: %s", sqlite3_errmsg(db));
     }
 
   // NB: no problem with unconditional free here - an earlier failed regcomp would exit program
-- 
2.30.1


More information about the Elfutils-devel mailing list