Skip to contentSkip to conversation

lvm shell enhancements

PATCH New Tony Asleson 1 participant 3 messages Jan 8, 2016

lore
Share & tools
lore
#0 lore raw

cover lvm shell enhancements

In an effort to avoid forking & exec'ing the lvm command line a user can create a sub process and pipe commands to/from lvm utilizing lvm shell.

However, this approach suffers from the following:
1. lvm shell uses GNU readline which echos STDIN to STDOUT and it also appends additional characters to STDOUT when doing this which complicates the code communicating with lvm shell.
2. lvm shell ouputs messages to STDERR even when the command is successful, thus the client has no way to easily determine if the command completed successfully or not.

This patch series addresses these two issues, please review.

Thanks!

Tony Asleson (2):
lvm.c: Don't use GNU readline when no tty
lvm.c: Add return value lvm shell prompt

 tools/lvm.c |   71 +++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 60 insertions(+), 11 deletions(-)
#1 lore raw

PATCH 1/2 lvm.c: Don't use GNU readline when no tty

If you use lvm shell and write requests to STDIN and read from STDOUT over a pipe you will find that the GNU readline library echos STDIN to STDOUT. This wouldn't be so much of an issue if that's all it did, but it also injects ' \r' (seemingly random) into STDOUT when echoing longer requests.

strace snippet from lvm shell:
...

read(0, "/", 1)                         = 1
write(1, "/", 1)                        = 1
read(0, "s", 1)                         = 1
write(1, "s \r", 3)                     = 3
read(0, "d", 1)                         = 1
write(1, "d", 1)                        = 1

...

This patch removes the calls to GNU readline when STDIN is not a tty as readline functionality is not needed or wanted in this use case.

---
 tools/lvm.c |   56 +++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 47 insertions(+), 9 deletions(-)
tools/lvm.c +47 -9
index 63cc0b4..2f3b911 100644
@@ -181,22 +181,56 @@ static void _write_history(void)
 		log_very_verbose("Couldn't write history to %s.", hist_file);
 }
 
+static char *_fetchline(const char *prompt)
+{
+	char *line = NULL;
+	size_t allocated_size = 0;
+
+	fprintf(stdout, "%s", prompt);
+	fflush(stdout);
+
+	if (getline(&line, &allocated_size, stdin) == -1) {
+		free(line);
+		line = NULL;
+	}
+
+	if (line) {
+		size_t l = strlen(line);
+
+		/* Mimic same behavior as readline, remove trailing \n if present */
+		if (l) {
+			l--;
+			if (line[l] == '\n')
+				line[l] = '\0';
+		}
+	}
+	return line;
+}
+
+typedef char *(*fetch_line_fp)(const char *);
+
 int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
 {
-	int argc, ret;
+	int argc, ret = 0;
 	char *input = NULL, *args[MAX_ARGS], **argv;
+	int tty = 0;
+	fetch_line_fp get_next_line = _fetchline;
 
-	rl_readline_name = "lvm";
-	rl_attempted_completion_function = (rl_completion_func_t *) _completion;
+	tty = isatty(STDIN_FILENO);
 
-	_read_history(cmd);
+	if (tty) {
+		rl_readline_name = "lvm";
+		rl_attempted_completion_function = (rl_completion_func_t *) _completion;
+		_read_history(cmd);
+		get_next_line = readline;
+	}
 
 	_cmdline = cmdline;
 
 	_cmdline->interactive = 1;
 	while (1) {
 		free(input);
-		input = readline("lvm> ");
+		input = get_next_line("lvm> ");
 
 		/* EOF */
 		if (!input) {
@@ -209,7 +243,8 @@ int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
 		if (!*input)
 			continue;
 
-		add_history(input);
+		if (tty)
+			add_history(input);
 
 		argv = args;
 
@@ -230,7 +265,8 @@ int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
 			continue;
 
 		if (!strcmp(argv[0], "quit") || !strcmp(argv[0], "exit")) {
-			remove_history(history_length - 1);
+			if (tty)
+				remove_history(history_length - 1);
 			log_error("Exiting.");
 			break;
 		}
@@ -240,11 +276,13 @@ int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
 			log_error("No such command '%s'.  Try 'help'.",
 				  argv[0]);
 
-                if ((ret != ECMD_PROCESSED) && !error_message_produced()) {
+		if ((ret != ECMD_PROCESSED) && !error_message_produced()) {
 			log_debug(INTERNAL_ERROR "Failed command did not use log_error");
 			log_error("Command failed with status code %d.", ret);
 		}
-		_write_history();
+
+		if (tty)
+			_write_history();
 	}
 
 	free(input);
Signature · 1 line
1.7.1
#2 lore raw

PATCH 2/2 lvm.c: Add return value lvm shell prompt

In reply to Tony Asleson

lvm shell will print messages to stderr when the command completes successfully. Thus there is no simple way to know if things executed OK as you cannot use the absence of stderr as success. This patch adds a return code to the prompt when STDIN is *not* a tty so you cal tell if the previous command completed successfully.

An example:

[0] lvm> pvs

  PV         VG   Fmt  Attr PSize  PFree
  /dev/sdb        lvm2 ---  36.00g 36.00g
  /dev/sdc        lvm2 ---  36.00g 36.00g
  /dev/sdd        lvm2 ---  36.00g 36.00g
  /dev/sde        lvm2 ---  36.00g 36.00g

[0] lvm> vgs doesnotexist
Volume group "doesnotexist" not found
Cannot process volume group doesnotexist
[5] lvm>

---
 tools/lvm.c |   13 ++++++++++++-
 1 files changed, 12 insertions(+), 1 deletions(-)
tools/lvm.c +12 -1
index 2f3b911..89ef8d3 100644
@@ -215,6 +215,7 @@ int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
 	char *input = NULL, *args[MAX_ARGS], **argv;
 	int tty = 0;
 	fetch_line_fp get_next_line = _fetchline;
+	char prompt[32];
 
 	tty = isatty(STDIN_FILENO);
 
@@ -223,6 +224,7 @@ int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
 		rl_attempted_completion_function = (rl_completion_func_t *) _completion;
 		_read_history(cmd);
 		get_next_line = readline;
+		snprintf(prompt, sizeof(prompt), "lvm> ");
 	}
 
 	_cmdline = cmdline;
@@ -230,7 +232,12 @@ int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
 	_cmdline->interactive = 1;
 	while (1) {
 		free(input);
-		input = get_next_line("lvm> ");
+
+		if (!tty) {
+			snprintf(prompt, sizeof(prompt), "[%d] lvm> ", ret);
+		}
+
+		input = get_next_line(prompt);
 
 		/* EOF */
 		if (!input) {
@@ -283,6 +290,10 @@ int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
 
 		if (tty)
 			_write_history();
+
+		/* Zero ret if all was good */
+		if (ret == ECMD_PROCESSED)
+			ret = 0;
 	}
 
 	free(input);
Signature · 1 line
1.7.1