Blame SOURCES/bz1078078-pcmk-lrmd_provide_stderr_output_from_agents_if_available_otherwise_fall_back_to_stdout.patch

7ebc05
commit 2c6e6e1ad3ecbb186f9a43f89280d0d4391277e5
7ebc05
Author: Andrew Beekhof <andrew@beekhof.net>
7ebc05
Date:   Tue Apr 8 12:13:44 2014 +1000
7ebc05
7ebc05
    Fix: lrmd: Provide stderr output from agents if available, otherwise fall back to stdout
7ebc05
    
7ebc05
    (cherry picked from commit d9cc751625ff3adfa158a2f769ab9038f6e317fd)
7ebc05
7ebc05
diff --git a/lib/services/services_linux.c b/lib/services/services_linux.c
7ebc05
index ac0138f..534e362 100644
7ebc05
--- a/lib/services/services_linux.c
7ebc05
+++ b/lib/services/services_linux.c
7ebc05
@@ -57,34 +57,37 @@ set_fd_opts(int fd, int opts)
7ebc05
 }
7ebc05
 
7ebc05
 static gboolean
7ebc05
-read_output(int fd, svc_action_t * op)
7ebc05
+svc_read_output(int fd, svc_action_t * op, bool is_stderr)
7ebc05
 {
7ebc05
     char *data = NULL;
7ebc05
     int rc = 0, len = 0;
7ebc05
-    gboolean is_err = FALSE;
7ebc05
     char buf[500];
7ebc05
     static const size_t buf_read_len = sizeof(buf) - 1;
7ebc05
 
7ebc05
-    crm_trace("%p", op);
7ebc05
 
7ebc05
     if (fd < 0) {
7ebc05
+        crm_trace("No fd for %s", op->id);
7ebc05
         return FALSE;
7ebc05
     }
7ebc05
 
7ebc05
-    if (fd == op->opaque->stderr_fd) {
7ebc05
-        is_err = TRUE;
7ebc05
-        if (op->stderr_data) {
7ebc05
-            len = strlen(op->stderr_data);
7ebc05
-            data = op->stderr_data;
7ebc05
-        }
7ebc05
-    } else if (op->stdout_data) {
7ebc05
+    if (is_stderr && op->stderr_data) {
7ebc05
+        len = strlen(op->stderr_data);
7ebc05
+        data = op->stderr_data;
7ebc05
+        crm_trace("Reading %s stderr into offset %d", op->id, len);
7ebc05
+
7ebc05
+    } else if (is_stderr == FALSE && op->stdout_data) {
7ebc05
         len = strlen(op->stdout_data);
7ebc05
         data = op->stdout_data;
7ebc05
+        crm_trace("Reading %s stdout into offset %d", op->id, len);
7ebc05
+
7ebc05
+    } else {
7ebc05
+        crm_trace("Reading %s %s", op->id, is_stderr?"stderr":"stdout", len);
7ebc05
     }
7ebc05
 
7ebc05
     do {
7ebc05
         rc = read(fd, buf, buf_read_len);
7ebc05
         if (rc > 0) {
7ebc05
+            crm_trace("Got %d characters starting with %.20s", rc, buf);
7ebc05
             buf[rc] = 0;
7ebc05
             data = realloc(data, len + rc + 1);
7ebc05
             sprintf(data + len, "%s", buf);
7ebc05
@@ -99,7 +102,7 @@ read_output(int fd, svc_action_t * op)
7ebc05
 
7ebc05
     } while (rc == buf_read_len || rc < 0);
7ebc05
 
7ebc05
-    if (data != NULL && is_err) {
7ebc05
+    if (data != NULL && is_stderr) {
7ebc05
         op->stderr_data = data;
7ebc05
     } else if (data != NULL) {
7ebc05
         op->stdout_data = data;
7ebc05
@@ -113,7 +116,7 @@ dispatch_stdout(gpointer userdata)
7ebc05
 {
7ebc05
     svc_action_t *op = (svc_action_t *) userdata;
7ebc05
 
7ebc05
-    return read_output(op->opaque->stdout_fd, op);
7ebc05
+    return svc_read_output(op->opaque->stdout_fd, op, FALSE);
7ebc05
 }
7ebc05
 
7ebc05
 static int
7ebc05
@@ -121,7 +124,7 @@ dispatch_stderr(gpointer userdata)
7ebc05
 {
7ebc05
     svc_action_t *op = (svc_action_t *) userdata;
7ebc05
 
7ebc05
-    return read_output(op->opaque->stderr_fd, op);
7ebc05
+    return svc_read_output(op->opaque->stderr_fd, op, TRUE);
7ebc05
 }
7ebc05
 
7ebc05
 static void
7ebc05
@@ -264,18 +267,23 @@ operation_finished(mainloop_child_t * p, pid_t pid, int core, int signo, int exi
7ebc05
     op->status = PCMK_LRM_OP_DONE;
7ebc05
     CRM_ASSERT(op->pid == pid);
7ebc05
 
7ebc05
+    crm_trace("%s %p %p", prefix, op->opaque->stderr_gsource, op->opaque->stdout_gsource);
7ebc05
     if (op->opaque->stderr_gsource) {
7ebc05
         /* Make sure we have read everything from the buffer.
7ebc05
          * Depending on the priority mainloop gives the fd, operation_finished
7ebc05
          * could occur before all the reads are done.  Force the read now.*/
7ebc05
+        crm_trace("%s dispatching stderr", prefix);
7ebc05
         dispatch_stderr(op);
7ebc05
+        crm_trace("%s: %p", op->stderr_data);
7ebc05
     }
7ebc05
 
7ebc05
     if (op->opaque->stdout_gsource) {
7ebc05
         /* Make sure we have read everything from the buffer.
7ebc05
          * Depending on the priority mainloop gives the fd, operation_finished
7ebc05
          * could occur before all the reads are done.  Force the read now.*/
7ebc05
+        crm_trace("%s dispatching stdout", prefix);
7ebc05
         dispatch_stdout(op);
7ebc05
+        crm_trace("%s: %p", op->stdout_data);
7ebc05
     }
7ebc05
 
7ebc05
     if (signo) {
7ebc05
@@ -495,11 +503,11 @@ services_os_action_execute(svc_action_t * op, gboolean synchronous)
7ebc05
 
7ebc05
             if (poll_rc > 0) {
7ebc05
                 if (fds[0].revents & POLLIN) {
7ebc05
-                    read_output(op->opaque->stdout_fd, op);
7ebc05
+                    svc_read_output(op->opaque->stdout_fd, op, FALSE);
7ebc05
                 }
7ebc05
 
7ebc05
                 if (fds[1].revents & POLLIN) {
7ebc05
-                    read_output(op->opaque->stderr_fd, op);
7ebc05
+                    svc_read_output(op->opaque->stderr_fd, op, TRUE);
7ebc05
                 }
7ebc05
 
7ebc05
                 if (fds[2].revents & POLLIN) {
7ebc05
@@ -578,8 +586,8 @@ services_os_action_execute(svc_action_t * op, gboolean synchronous)
7ebc05
         }
7ebc05
 #endif
7ebc05
 
7ebc05
-        read_output(op->opaque->stdout_fd, op);
7ebc05
-        read_output(op->opaque->stderr_fd, op);
7ebc05
+        svc_read_output(op->opaque->stdout_fd, op, FALSE);
7ebc05
+        svc_read_output(op->opaque->stderr_fd, op, TRUE);
7ebc05
 
7ebc05
         close(op->opaque->stdout_fd);
7ebc05
         close(op->opaque->stderr_fd);
7ebc05
diff --git a/lrmd/lrmd.c b/lrmd/lrmd.c
7ebc05
index 65421d0..517e98f 100644
7ebc05
--- a/lrmd/lrmd.c
7ebc05
+++ b/lrmd/lrmd.c
7ebc05
@@ -599,7 +599,9 @@ action_complete(svc_action_t * action)
7ebc05
     cmd->lrmd_op_status = action->status;
7ebc05
     rsc = cmd->rsc_id ? g_hash_table_lookup(rsc_list, cmd->rsc_id) : NULL;
7ebc05
 
7ebc05
-    if (action->stdout_data) {
7ebc05
+    if (action->stderr_data) {
7ebc05
+        cmd->output = strdup(action->stderr_data);
7ebc05
+    } else if (action->stdout_data) {
7ebc05
         cmd->output = strdup(action->stdout_data);
7ebc05
     }
7ebc05
 #if SUPPORT_NAGIOS