Browse Source

Use "opening log file" as error stage when appropriate

If the logfile can't be opened the current error stage description is
"setting up standard input/output descriptions", which is technically
accurate but not very meaningful to end users.

In the case of opening a logfile, use "opening log file" instead.
Davin McCall 4 months ago
parent
commit
f4d62a5047
2 changed files with 11 additions and 3 deletions
  1. 5 2
      src/includes/service-constants.h
  2. 6 1
      src/run-child-proc.cc

+ 5 - 2
src/includes/service-constants.h

@@ -69,8 +69,10 @@ inline bool did_finish(stopped_reason_t reason)
 enum class exec_stage {
     ARRANGE_FDS, READ_ENV_FILE, SET_NOTIFYFD_VAR, SETUP_ACTIVATION_SOCKET, SETUP_CONTROL_SOCKET,
     CHDIR, SETUP_STDINOUTERR, ENTER_CGROUP, SET_RLIMITS, SET_UIDGID,
+    OPEN_LOGFILE, // this is used instead of SETUP_STDINOUTERR if output is to logfile
+
     /* values for future expansion: */
-    SPARE1, SPARE2, SPARE3, SPARE4, SPARE5, SPARE6, SPARE7, SPARE8,
+    SPARE2, SPARE3, SPARE4, SPARE5, SPARE6, SPARE7, SPARE8,
     /* must be last: */ DO_EXEC
 };
 
@@ -90,7 +92,8 @@ const char * const exec_stage_descriptions[/* static_cast<int>(exec_stage::DO_EX
         #endif
         "setting resource limits",      // SET_RLIMITS
         "setting user/group ID",        // SET_UIDGID
-        nullptr,                        // SPARE1
+        "opening log file",             // OPEN_LOGFILE
+        // SPARE1 used
         nullptr,                        // SPARE2
         nullptr,                        // SPARE3
         nullptr,                        // SPARE4

+ 6 - 1
src/run-child-proc.cc

@@ -216,7 +216,12 @@ void base_process_service::run_child_proc(run_proc_params params) noexcept
             // take care not to clobber the notify_fd.
             if (output_fd == -1) {
                 output_fd = open(logfile, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
-                if (output_fd == -1) goto failure_out;
+                if (output_fd == -1) {
+                    // On failure to open a log file, change the error stage to give more precise
+                    // information:
+                    if (this->log_type == log_type_id::LOGFILE) err.stage = exec_stage::OPEN_LOGFILE;
+                    goto failure_out;
+                }
                 // Set permission of logfile if present
                 // if log type is NONE, we don't want to change ownership/permissions of /dev/null!
                 if (this->log_type == log_type_id::LOGFILE) {