Browse Source

Retcode for client is 0 or first non-null context_t retcode within pipeline

Serj Kalichev 5 months ago
parent
commit
c1f638f711
2 changed files with 19 additions and 6 deletions
  1. 1 1
      bin/klish/klish.c
  2. 18 5
      klish/ksession/kexec.c

+ 1 - 1
bin/klish/klish.c

@@ -511,7 +511,7 @@ bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
 	if (!ktp_session_retcode(ktp, &rc))
 		rc = -1;
 	error = ktp_session_error(ktp);
-	if (rc < 0) {
+	if (rc != 0) {
 		if (faux_error_len(error) > 0) {
 			faux_error_node_t *err_iter = faux_error_iter(error);
 			const char *err = NULL;

+ 18 - 5
klish/ksession/kexec.c

@@ -204,11 +204,15 @@ bool_t kexec_done(const kexec_t *exec)
 }
 
 
-// Retcode of kexec is a retcode of its first context execution because
-// next contexts just a filters. Retcode valid if kexec is done. Else current
+// Retcode of kexec is a 0 if all pipelined stages have retcode=0 or first
+// non-null retcode else.
+// Retcode valid if kexec is done. Else current
 // retcode is non-valid and will not be returned at all.
 bool_t kexec_retcode(const kexec_t *exec, int *status)
 {
+	kexec_contexts_node_t *iter = NULL;
+	kcontext_t *context = NULL;
+
 	assert(exec);
 	if (!exec)
 		return BOOL_FALSE;
@@ -219,9 +223,18 @@ bool_t kexec_retcode(const kexec_t *exec, int *status)
 	if (!kexec_done(exec)) // Unfinished execution
 		return BOOL_FALSE;
 
-	if (status)
-		*status = kcontext_retcode(
-			(kcontext_t *)faux_list_data(faux_list_head(exec->contexts)));
+	if (!status) // User don't want to see retcode value
+		return BOOL_TRUE;
+
+	*status = 0;
+	iter = kexec_contexts_iter(exec);
+	while ((context = kexec_contexts_each(&iter))) {
+		int retcode = kcontext_retcode(context);
+		if (retcode != 0) {
+			*status = retcode;
+			break;
+		}
+	}
 
 	return BOOL_TRUE;
 }