mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Aleksander Morgado <aleksander@aleksander.es>
To: barebox@lists.infradead.org
Cc: Aleksander Morgado <aleksander@aleksander.es>
Subject: [PATCH 2/2] sandbox: --stdin and --stdout allow max one bidirectional console
Date: Wed, 31 May 2017 18:12:41 +0200	[thread overview]
Message-ID: <7499fda2a3c12627c05a66d1a6a9111646beb695.1496247033.git.aleksander@aleksander.es> (raw)
In-Reply-To: <cover.1496247033.git.aleksander@aleksander.es>
In-Reply-To: <cover.1496247033.git.aleksander@aleksander.es>

Allow up to one bidirectional FIFO/file based console in addition to
the default stdin/stdout console that is always registered.

We avoid opening the FIFO files while parsing the options because the
whole logic may block if e.g. trying to open the --stdout FIFO and
there is no reader in the other end. So instead, we store the --stdout
and --stdin file paths given, and we open both sequentially once all
the options have been parsed. This also allows us to validate that at
most a single pair of --stdin and --stdout paths has been given.
e.g.:
    term1 $ mkfifo /tmp/bbstdin
    term1 $ mkfifo /tmp/bbstdout
    term1 $ ./barebox -I /tmp/bbstdin -O /tmp/bbstdout
       (blocks until a reader is available in the stdout FIFO)

    term2 $ cat /tmp/bbstdout & cat > /tmp/bbstdin
       (starts reader and writer, which triggers barebox to continue)

If only one console is activated (CONFIG_CONSOLE_ACTIVATE_ALL=n), the
default stdin/stdout console will be preferred instead of the new
FIFO/file based, which would need to be activated explicitly later on.
e.g.:
    barebox@barebox sandbox:/ cs1.active=ioe

Signed-off-by: Aleksander Morgado <aleksander@aleksander.es>
---
 arch/sandbox/os/common.c | 48 +++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 37 insertions(+), 11 deletions(-)

diff --git a/arch/sandbox/os/common.c b/arch/sandbox/os/common.c
index 192917ac2..0461abaac 100644
--- a/arch/sandbox/os/common.c
+++ b/arch/sandbox/os/common.c
@@ -39,6 +39,7 @@
 #include <signal.h>
 #include <sys/select.h>
 #include <sys/wait.h>
+#include <linux/limits.h>
 /*
  * ...except the ones needed to connect with barebox
  */
@@ -329,9 +330,11 @@ static const char optstring[] = "hm:i:e:d:O:I:x:y:";
 int main(int argc, char *argv[])
 {
 	void *ram;
-	int opt, ret, fd;
+	int opt, ret;
 	int malloc_size = CONFIG_MALLOC_SIZE;
 	int fdno = 0, envno = 0, option_index = 0;
+	char path_stdout[PATH_MAX + 1] = { 0 };
+	char path_stdin[PATH_MAX + 1] = { 0 };
 
 	while (1) {
 		option_index = 0;
@@ -360,22 +363,18 @@ int main(int argc, char *argv[])
 			}
 			break;
 		case 'O':
-			fd = open(optarg, O_WRONLY);
-			if (fd < 0) {
-				perror("open");
+			if (path_stdout[0]) {
+				printf("error: cannot specify -O,--stdout multiple times\n");
 				exit(1);
 			}
-
-			barebox_register_console(-1, fd);
+			strncpy(path_stdout, optarg, PATH_MAX);
 			break;
 		case 'I':
-			fd = open(optarg, O_RDWR);
-			if (fd < 0) {
-				perror("open");
+			if (path_stdin[0]) {
+				printf("error: cannot specify -I,--stdin multiple times\n");
 				exit(1);
 			}
-
-			barebox_register_console(fd, -1);
+			strncpy(path_stdin, optarg, PATH_MAX);
 			break;
 		case 'x':
 			sdl_xres = strtoul(optarg, NULL, 0);
@@ -426,6 +425,33 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	/* Register additional FIFO console */
+	if (path_stdout[0] || path_stdin[0]) {
+		int fdout, fdin;
+
+		/* Both must be given to build a FIFO console */
+		if (!path_stdin[0] || !path_stdout[0]) {
+			printf("error: both -I,--stdin and -O,--stdout must be"
+			       "specified to enable the FIFO console\n");
+			exit(1);
+		}
+
+		fdout = open(path_stdout, O_WRONLY);
+		if (fdout < 0) {
+			perror("open stdout");
+			exit(1);
+		}
+
+		fdin = open(path_stdin, O_RDWR);
+		if (fdin < 0) {
+			perror("open stdin");
+			exit(1);
+		}
+
+		barebox_register_console(fdin, fdout);
+	}
+
+	/* Register default stdin/stdout console */
 	barebox_register_console(fileno(stdin), fileno(stdout));
 
 	rawmode();
-- 
2.13.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2017-05-31 16:13 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-31 16:12 [PATCH 0/2] Enabling support for the FIFO based console in sandbox Aleksander Morgado
2017-05-31 16:12 ` [PATCH 1/2] sandbox: fix registering multiple consoles Aleksander Morgado
2017-05-31 16:12 ` Aleksander Morgado [this message]
2017-06-01  8:24   ` [PATCH 2/2] sandbox: --stdin and --stdout allow max one bidirectional console Sascha Hauer
2017-06-01  8:28     ` Aleksander Morgado
2017-06-01  8:41       ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7499fda2a3c12627c05a66d1a6a9111646beb695.1496247033.git.aleksander@aleksander.es \
    --to=aleksander@aleksander.es \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox