Browse Source

pty: Fix KTP_STDIN

Serj Kalichev 1 year ago
parent
commit
e8d7907c14
4 changed files with 45 additions and 17 deletions
  1. 25 1
      bin/klish/interactive.c
  2. 4 4
      klish/ksession/kexec.c
  3. 15 12
      klish/ktp/ktp_session.c
  4. 1 0
      klish/ktp_session.h

+ 25 - 1
bin/klish/interactive.c

@@ -269,8 +269,32 @@ static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
 	void *associated_data, void *udata)
 {
 	ctx_t *ctx = (ctx_t *)udata;
+	ktp_session_state_e state = KTP_SESSION_STATE_ERROR;
 
-	tinyrl_read(ctx->tinyrl);
+	if (!ctx)
+		return BOOL_FALSE;
+
+	state = ktp_session_state(ctx->ktp);
+
+	// Standard klish command line
+	if (state == KTP_SESSION_STATE_IDLE) {
+		tinyrl_read(ctx->tinyrl);
+		return BOOL_TRUE;
+	}
+
+	// Interactive command
+	if ((state == KTP_SESSION_STATE_WAIT_FOR_CMD) &&
+		KTP_STATUS_IS_INTERACTIVE(ktp_session_cmd_features(ctx->ktp))) {
+		int fd = fileno(tinyrl_istream(ctx->tinyrl));
+		char buf[1024] = {};
+		ssize_t bytes_readed = 0;
+
+		while ((bytes_readed = read(fd, buf, sizeof(buf))) > 0) {
+			ktp_session_stdin(ctx->ktp, buf, bytes_readed);
+			if (bytes_readed != sizeof(buf))
+				break;
+		}
+	}
 
 	// Happy compiler
 	eloop = eloop;

+ 4 - 4
klish/ksession/kexec.c

@@ -251,8 +251,8 @@ static bool_t kexec_prepare(kexec_t *exec)
 		kcontext_t *context = (kcontext_t *)faux_list_data(
 			faux_list_head(exec->contexts));
 
-//		ptm = open(PTMX_PATH, O_RDWR, O_NOCTTY);
-		ptm = open(PTMX_PATH, O_RDWR, 0);
+		ptm = open(PTMX_PATH, O_RDWR, O_NOCTTY);
+//		ptm = open(PTMX_PATH, O_RDWR, 0);
 		if (ptm < 0)
 			return BOOL_FALSE;
 		// Set O_NONBLOCK flag here. Because this flag is ignored while
@@ -262,8 +262,8 @@ static bool_t kexec_prepare(kexec_t *exec)
 		grantpt(ptm);
 		unlockpt(ptm);
 		pts_name = ptsname(ptm);
-//		pts = open(pts_name, O_RDWR, O_NOCTTY);
-		pts = open(pts_name, O_RDWR, 0);
+		pts = open(pts_name, O_RDWR, O_NOCTTY);
+//		pts = open(pts_name, O_RDWR, 0);
 		if (pts < 0) {
 			close(ptm);
 			return BOOL_FALSE;

+ 15 - 12
klish/ktp/ktp_session.c

@@ -596,7 +596,7 @@ static bool_t ktp_session_read_cb(faux_async_t *async,
 
 
 static bool_t ktp_session_req(ktp_session_t *ktp, ktp_cmd_e cmd,
-	const char *line, faux_error_t *error, bool_t dry_run)
+	const char *line, size_t line_len, faux_error_t *error, bool_t dry_run)
 {
 	faux_msg_t *req = NULL;
 	ktp_status_e status = KTP_STATUS_NONE;
@@ -605,19 +605,13 @@ static bool_t ktp_session_req(ktp_session_t *ktp, ktp_cmd_e cmd,
 	if (!ktp)
 		return BOOL_FALSE;
 
-	if (ktp->state != KTP_SESSION_STATE_IDLE) {
-		faux_error_sprintf(error,
-			"Can't create request. Session state is not suitable");
-		return BOOL_FALSE;
-	}
-
 	// Set dry-run flag
 	if (dry_run)
 		status |= KTP_STATUS_DRY_RUN;
 
 	req = ktp_msg_preform(cmd, status);
 	if (line)
-		faux_msg_add_param(req, KTP_PARAM_LINE, line, strlen(line));
+		faux_msg_add_param(req, KTP_PARAM_LINE, line, line_len);
 	faux_msg_send_async(req, ktp->async);
 	faux_msg_free(req);
 
@@ -636,7 +630,7 @@ static bool_t ktp_session_req(ktp_session_t *ktp, ktp_cmd_e cmd,
 bool_t ktp_session_cmd(ktp_session_t *ktp, const char *line,
 	faux_error_t *error, bool_t dry_run)
 {
-	if (!ktp_session_req(ktp, KTP_CMD, line, error, dry_run))
+	if (!ktp_session_req(ktp, KTP_CMD, line, strlen(line), error, dry_run))
 		return BOOL_FALSE;
 	ktp->state = KTP_SESSION_STATE_WAIT_FOR_CMD;
 
@@ -646,7 +640,7 @@ bool_t ktp_session_cmd(ktp_session_t *ktp, const char *line,
 
 bool_t ktp_session_auth(ktp_session_t *ktp, faux_error_t *error)
 {
-	if (!ktp_session_req(ktp, KTP_AUTH, NULL, error, BOOL_FALSE))
+	if (!ktp_session_req(ktp, KTP_AUTH, NULL, 0, error, BOOL_FALSE))
 		return BOOL_FALSE;
 	ktp->state = KTP_SESSION_STATE_UNAUTHORIZED;
 
@@ -656,7 +650,7 @@ bool_t ktp_session_auth(ktp_session_t *ktp, faux_error_t *error)
 
 bool_t ktp_session_completion(ktp_session_t *ktp, const char *line, bool_t dry_run)
 {
-	if (!ktp_session_req(ktp, KTP_COMPLETION, line, NULL, dry_run))
+	if (!ktp_session_req(ktp, KTP_COMPLETION, line, strlen(line), NULL, dry_run))
 		return BOOL_FALSE;
 	ktp->state = KTP_SESSION_STATE_WAIT_FOR_COMPLETION;
 
@@ -666,7 +660,7 @@ bool_t ktp_session_completion(ktp_session_t *ktp, const char *line, bool_t dry_r
 
 bool_t ktp_session_help(ktp_session_t *ktp, const char *line)
 {
-	if (!ktp_session_req(ktp, KTP_HELP, line, NULL, BOOL_TRUE))
+	if (!ktp_session_req(ktp, KTP_HELP, line, strlen(line), NULL, BOOL_TRUE))
 		return BOOL_FALSE;
 	ktp->state = KTP_SESSION_STATE_WAIT_FOR_HELP;
 
@@ -674,6 +668,15 @@ bool_t ktp_session_help(ktp_session_t *ktp, const char *line)
 }
 
 
+bool_t ktp_session_stdin(ktp_session_t *ktp, const char *line, size_t line_len)
+{
+	if (!ktp_session_req(ktp, KTP_STDIN, line, line_len, NULL, BOOL_TRUE))
+		return BOOL_FALSE;
+
+	return BOOL_TRUE;
+}
+
+
 bool_t ktp_session_retcode(ktp_session_t *ktp, int *retcode)
 {
 	if (!ktp)

+ 1 - 0
klish/ktp_session.h

@@ -93,6 +93,7 @@ bool_t ktp_session_auth(ktp_session_t *ktp, faux_error_t *error);
 bool_t ktp_session_completion(ktp_session_t *ktp, const char *line,
 	bool_t dry_run);
 bool_t ktp_session_help(ktp_session_t *ktp, const char *line);
+bool_t ktp_session_stdin(ktp_session_t *ktp, const char *line, size_t line_len);
 bool_t ktp_session_retcode(ktp_session_t *ktp, int *retcode);
 ktp_status_e ktp_session_cmd_features(const ktp_session_t *ktp);