Browse Source

kexec: kexec_done()

Serj Kalichev 2 years ago
parent
commit
6d8159cd18
3 changed files with 30 additions and 5 deletions
  1. 1 0
      klish/kexec.h
  2. 25 1
      klish/ksession/kexec.c
  3. 4 4
      klish/ksession/ksession.c

+ 1 - 0
klish/kexec.h

@@ -32,6 +32,7 @@ bool_t kexec_set_stdout(kexec_t *exec, int stdout);
 int kexec_stderr(const kexec_t *exec);
 bool_t kexec_set_stderr(kexec_t *exec, int stderr);
 // Return code
+bool_t kexec_done(const kexec_t *exec);
 bool_t kexec_retcode(const kexec_t *exec, int *status);
 // CONTEXTs
 bool_t kexec_add_contexts(kexec_t *exec, kcontext_t *context);

+ 25 - 1
klish/ksession/kexec.c

@@ -99,8 +99,29 @@ size_t kexec_is_empty(const kexec_t *exec)
 }
 
 
+// kexec is done when all the kexec's contexts are done
+bool_t kexec_done(const kexec_t *exec)
+{
+	faux_list_node_t *iter = NULL;
+	kcontext_t *context = NULL;
+
+	assert(exec);
+	if (!exec)
+		return BOOL_FALSE;
+
+	iter = kexec_contexts_iter(exec);
+	while ((context = kexec_contexts_each(&iter))) {
+		if (!kcontext_done(context))
+			return BOOL_FALSE;
+	}
+
+	return BOOL_TRUE;
+}
+
+
 // Retcode of kexec is a retcode of its first context execution because
-// next contexts just a filters.
+// next contexts just a filters. 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)
 {
 	assert(exec);
@@ -110,6 +131,9 @@ bool_t kexec_retcode(const kexec_t *exec, int *status)
 	if (kexec_is_empty(exec))
 		return BOOL_FALSE;
 
+	if (!kexec_done(exec)) // Unfinished execution
+		return BOOL_FALSE;
+
 	if (status)
 		*status = kcontext_retcode(
 			(kcontext_t *)faux_list_data(faux_list_head(exec->contexts)));

+ 4 - 4
klish/ksession/ksession.c

@@ -83,7 +83,7 @@ void ksession_free(ksession_t *session)
 
 
 bool_t ksession_exec_locally(ksession_t *session, const char *line,
-	int status, faux_error_t *error)
+	int *retcode, faux_error_t *error)
 {
 	kexec_t *exec = NULL;
 
@@ -96,10 +96,10 @@ bool_t ksession_exec_locally(ksession_t *session, const char *line,
 	if (!exec)
 		return BOOL_FALSE;
 
-	kexec_exec(exec);
+	if (!kexec_exec(exec))
+		return BOOL_FALSE; // Something went wrong
 
-
-	status = status;
+	kexec_retcode(exec, retcode);
 
 	return BOOL_TRUE;
 }