From f27953c48d58c81a5a0d18f0fe93c9c039ee6b7b Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Tue, 6 Mar 2018 13:16:28 +0100 Subject: [PATCH] Standalone POSIX pipe utility Pipe between files using POSIX stdio. This facilitates raw transfers between arbitrary resources exposed by the VFS library. Fix #2708 --- repos/gems/recipes/src/pipe/content.mk | 2 + repos/gems/recipes/src/pipe/hash | 1 + repos/gems/recipes/src/pipe/used_apis | 3 ++ repos/gems/run/pipe.run | 40 +++++++++++++++++ repos/gems/src/app/pipe/README | 15 +++++++ repos/gems/src/app/pipe/main.cc | 59 ++++++++++++++++++++++++++ repos/gems/src/app/pipe/target.mk | 3 ++ 7 files changed, 123 insertions(+) create mode 100644 repos/gems/recipes/src/pipe/content.mk create mode 100644 repos/gems/recipes/src/pipe/hash create mode 100644 repos/gems/recipes/src/pipe/used_apis create mode 100644 repos/gems/run/pipe.run create mode 100644 repos/gems/src/app/pipe/README create mode 100644 repos/gems/src/app/pipe/main.cc create mode 100644 repos/gems/src/app/pipe/target.mk diff --git a/repos/gems/recipes/src/pipe/content.mk b/repos/gems/recipes/src/pipe/content.mk new file mode 100644 index 0000000000..c888567bee --- /dev/null +++ b/repos/gems/recipes/src/pipe/content.mk @@ -0,0 +1,2 @@ +SRC_DIR = src/app/pipe +include $(GENODE_DIR)/repos/base/recipes/src/content.inc diff --git a/repos/gems/recipes/src/pipe/hash b/repos/gems/recipes/src/pipe/hash new file mode 100644 index 0000000000..39cdd0ded6 --- /dev/null +++ b/repos/gems/recipes/src/pipe/hash @@ -0,0 +1 @@ +- diff --git a/repos/gems/recipes/src/pipe/used_apis b/repos/gems/recipes/src/pipe/used_apis new file mode 100644 index 0000000000..2115aa2d67 --- /dev/null +++ b/repos/gems/recipes/src/pipe/used_apis @@ -0,0 +1,3 @@ +base +libc +posix diff --git a/repos/gems/run/pipe.run b/repos/gems/run/pipe.run new file mode 100644 index 0000000000..9ed3208097 --- /dev/null +++ b/repos/gems/run/pipe.run @@ -0,0 +1,40 @@ +create_boot_directory + +install_config { + + + + + + + + + + + + + + + + + + + + + + + + + +} + +build "core init app/pipe" + +build_boot_image { + core init pipe + ld.lib.so libc.lib.so libm.lib.so posix.lib.so +} + +append qemu_args " -nographic " + +run_genode_until "child .* exited with exit value 0.*\n" 10 diff --git a/repos/gems/src/app/pipe/README b/repos/gems/src/app/pipe/README new file mode 100644 index 0000000000..7bd766efc9 --- /dev/null +++ b/repos/gems/src/app/pipe/README @@ -0,0 +1,15 @@ +The pipe utility is for piping between files using the +semantics of POSIX stdio. + +An example of piping clock jitter to a terminal session: + +! +! +! +! +! +! +! +! +! +! diff --git a/repos/gems/src/app/pipe/main.cc b/repos/gems/src/app/pipe/main.cc new file mode 100644 index 0000000000..30db196ef6 --- /dev/null +++ b/repos/gems/src/app/pipe/main.cc @@ -0,0 +1,59 @@ +/* + * \brief Standalone POSIX pipe + * \author Emery Hemingway + * \date 2018-03-06 + */ + +/* + * Copyright (C) 2018 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU Affero General Public License version 3. + */ + +/* Genode includes */ +#include + +/* Libc includes */ +#include +#include +#include + +int main() +{ + enum { SIXTEEN_K = 1 << 14 }; + static char buf[SIXTEEN_K]; + + Genode::uint64_t total = 0; + + while (true) { + + auto const nr = fread(buf, 1, sizeof(buf), stdin); + if (nr == 0 && feof(stdin)) + break; + + if (nr < 1 || nr > sizeof(buf)) { + int res = errno; + Genode::error((char const *)strerror(res)); + return res; + } + + auto remain = nr; + auto off = 0; + while (remain > 0) { + auto const nw = fwrite(buf+off, 1, remain, stdout); + if (nw < 1 || nw > remain) { + int res = errno; + Genode::error((char const *)strerror(res)); + return res; + } + + remain -= nw; + off += nw; + total += nw; + } + } + + Genode::log("piped ", total, " bytes"); + return 0; +}; diff --git a/repos/gems/src/app/pipe/target.mk b/repos/gems/src/app/pipe/target.mk new file mode 100644 index 0000000000..8d6692932b --- /dev/null +++ b/repos/gems/src/app/pipe/target.mk @@ -0,0 +1,3 @@ +TARGET = pipe +SRC_CC = main.cc +LIBS = base libc posix