From kevin at tyndur.org Sat Sep 3 16:52:55 2016 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 3 Sep 2016 16:52:55 +0200 Subject: [PATCH 0/7] shell: Unterstützung für Ein-/Ausgabeumleitung Message-ID: <1472914382-8896-1-git-send-email-kevin@tyndur.org> Mit dieser Serie werden die stdio-Streams explizit mit lio_dup()/lio_pass_fd() und entsprechender Information im PPB an Kindprozesse weitergegeben. Die Shell nutzt das, um Ein- und Ausgabeumleitung zu implementieren. Kevin Wolf (7): kernel2: syscall_lio_pass_fd() gefixt kernel2: Unterstützung von lio_dup() libc: file_get_stream() kernel2: lio_stream_origin() kernel2: syscall_shm_size libc/Pascal-RTL: stdio mit lio_dup() und PPB übergeben shell: Unterstützung für Ein-/Ausgabeumleitung src/include/syscallno.h | 3 + src/kernel2/include/lostio/core.h | 3 + src/kernel2/include/lostio/userspace.h | 4 + src/kernel2/include/syscall.h | 9 +++ src/kernel2/src/lostio/userspace.c | 14 +++- src/kernel2/src/syscall.c | 3 + src/kernel2/src/syscalls/lostio.c | 58 +++++++++++++- src/kernel2/src/syscalls/shm.c | 5 ++ src/modules/c/getterm/main.c | 1 + src/modules/c/shell/shell.c | 107 +++++++++++++++++++++++++- src/modules/include/init.h | 5 ++ src/modules/include/lostio.h | 6 +- src/modules/include/syscall.h | 28 +++++++ src/modules/init/loader.c | 11 +++ src/modules/lib/crt0.c | 10 +++ src/modules/lib/param.c | 128 +++++++++++++++++++++++++++++++ src/modules/lib/stdlibc/file.c | 20 +++++ src/modules/lib/stdlibc/stdio.c | 103 +++++++++++++------------ src/modules/lib/syscalls/lostio.c | 58 ++++++++++++++ src/modules/lib/syscalls/shared_memory.c | 16 ++++ src/modules/pas/lib/rtl/system.pas | 41 +++++----- 21 files changed, 556 insertions(+), 77 deletions(-) -- 2.1.4 From kevin at tyndur.org Sat Sep 3 16:52:56 2016 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 3 Sep 2016 16:52:56 +0200 Subject: [tyndur-devel] [PATCH 1/7] kernel2: syscall_lio_pass_fd() gefixt In-Reply-To: <1472914382-8896-1-git-send-email-kevin@tyndur.org> References: <1472914382-8896-1-git-send-email-kevin@tyndur.org> Message-ID: <1472914382-8896-2-git-send-email-kevin@tyndur.org> ! kernel2: syscall_lio_pass_fd() hat den neuen Stream statt den alten geunreft. Commit dc426b1c hat das vermutlich kaputtgemacht. Signed-off-by: Kevin Wolf --- src/kernel2/src/syscalls/lostio.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/kernel2/src/syscalls/lostio.c b/src/kernel2/src/syscalls/lostio.c index 9fde44d..eecffac 100644 --- a/src/kernel2/src/syscalls/lostio.c +++ b/src/kernel2/src/syscalls/lostio.c @@ -122,6 +122,7 @@ int syscall_lio_close(lio_usp_stream_t* stream_id) int syscall_lio_pass_fd(lio_usp_stream_t* stream_id, pid_t pid) { struct lio_usp_stream* fd; + struct lio_usp_stream* new_fd; struct lio_stream* stream; pm_process_t* process; int ret; @@ -138,8 +139,8 @@ int syscall_lio_pass_fd(lio_usp_stream_t* stream_id, pid_t pid) } stream = fd->stream; - fd = lio_usp_add_stream(process, stream); - *stream_id = fd->usp_id; + new_fd = lio_usp_add_stream(process, stream); + *stream_id = new_fd->usp_id; // Wir haben gerade eine zweite Referenz hinzugefügt, also kann kein // lio_close() aufgerufen, das fehlschlagen könnte. -- 2.1.4 From kevin at tyndur.org Sat Sep 3 16:52:57 2016 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 3 Sep 2016 16:52:57 +0200 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_2/7=5D_kernel2=3A_Unterst=C3=BC?= =?utf-8?q?tzung_von_lio=5Fdup=28=29?= In-Reply-To: <1472914382-8896-1-git-send-email-kevin@tyndur.org> References: <1472914382-8896-1-git-send-email-kevin@tyndur.org> Message-ID: <1472914382-8896-3-git-send-email-kevin@tyndur.org> + kernel2: Neuer Syscall lio_dup(), um zusätzliche Stream-IDs für geöffnete Streams zu bekommen (entweder um eine bestimmte ID zu überschreiben oder um eine Kopie weiterzureichen) Signed-off-by: Kevin Wolf --- src/include/syscallno.h | 1 + src/kernel2/include/lostio/userspace.h | 4 ++++ src/kernel2/include/syscall.h | 3 +++ src/kernel2/src/lostio/userspace.c | 13 +++++++++--- src/kernel2/src/syscall.c | 1 + src/kernel2/src/syscalls/lostio.c | 37 ++++++++++++++++++++++++++++++++++ src/modules/include/syscall.h | 14 +++++++++++++ src/modules/lib/syscalls/lostio.c | 33 ++++++++++++++++++++++++++++++ 8 files changed, 103 insertions(+), 3 deletions(-) diff --git a/src/include/syscallno.h b/src/include/syscallno.h index 5fa5cf1..3938718 100644 --- a/src/include/syscallno.h +++ b/src/include/syscallno.h @@ -109,6 +109,7 @@ #define SYSCALL_LIO_SYNC_ALL 99 #define SYSCALL_LIO_PASS_FD 100 #define SYSCALL_LIO_PROBE_SERVICE 101 +#define SYSCALL_LIO_DUP 102 #define SYSCALL_LIO_SRV_SERVICE_REGISTER 110 #define SYSCALL_LIO_SRV_TREE_SET_RING 111 diff --git a/src/kernel2/include/lostio/userspace.h b/src/kernel2/include/lostio/userspace.h index cde9778..8e53501 100644 --- a/src/kernel2/include/lostio/userspace.h +++ b/src/kernel2/include/lostio/userspace.h @@ -46,6 +46,10 @@ struct lio_usp_stream* lio_usp_add_stream(pm_process_t* proc, struct lio_stream* stream); +/** Weist einem Stream eine gegebene Userspace-ID zu */ +struct lio_usp_stream* lio_usp_do_add_stream(pm_process_t* proc, + struct lio_stream* stream, lio_usp_stream_t id); + /** * Gibt den zur Userspace-ID gehörenden Stream zurück, oder NULL wenn es * keinen passenden Stream gibt. diff --git a/src/kernel2/include/syscall.h b/src/kernel2/include/syscall.h index d7fce53..c979373 100644 --- a/src/kernel2/include/syscall.h +++ b/src/kernel2/include/syscall.h @@ -211,6 +211,9 @@ void syscall_lio_open(lio_usp_resource_t* resid, int flags, /// Stream schließen int syscall_lio_close(lio_usp_stream_t* stream_id); +/// Zusätzliche ID für Stream erstellen +int syscall_lio_dup(lio_usp_stream_t* source, lio_usp_stream_t* dest); + /// Stream an anderen Prozess weitergeben int syscall_lio_pass_fd(lio_usp_stream_t* stream_id, pid_t pid); diff --git a/src/kernel2/src/lostio/userspace.c b/src/kernel2/src/lostio/userspace.c index 76874fc..6fd5638 100644 --- a/src/kernel2/src/lostio/userspace.c +++ b/src/kernel2/src/lostio/userspace.c @@ -96,13 +96,13 @@ void lio_usp_remove_resource(struct lio_resource* res) } /** Weist einem Stream eine Userspace-ID zu */ -struct lio_usp_stream* lio_usp_add_stream(pm_process_t* proc, - struct lio_stream* stream) +struct lio_usp_stream* lio_usp_do_add_stream(pm_process_t* proc, + struct lio_stream* stream, lio_usp_stream_t id) { struct lio_usp_stream* entry = malloc(sizeof(*entry)); entry->stream = stream; - entry->usp_id = new_streamid(); + entry->usp_id = id; tree_insert(proc->lio_streams, entry); stream->usp_refcount++; @@ -110,6 +110,13 @@ struct lio_usp_stream* lio_usp_add_stream(pm_process_t* proc, return entry; } +/** Weist einem Stream eine Userspace-ID zu */ +struct lio_usp_stream* lio_usp_add_stream(pm_process_t* proc, + struct lio_stream* stream) +{ + return lio_usp_do_add_stream(proc, stream, new_streamid()); +} + /** * Gibt den zur Userspace-ID gehörenden Stream zurück, oder NULL wenn es * keinen passenden Stream gibt. diff --git a/src/kernel2/src/syscall.c b/src/kernel2/src/syscall.c index d1628ba..557ab12 100644 --- a/src/kernel2/src/syscall.c +++ b/src/kernel2/src/syscall.c @@ -131,6 +131,7 @@ void syscall_init() syscall_register(SYSCALL_LIO_SYNC_ALL, &syscall_lio_sync_all, 0); syscall_register(SYSCALL_LIO_PASS_FD, &syscall_lio_pass_fd, 2); syscall_register(SYSCALL_LIO_PROBE_SERVICE, &syscall_lio_probe_service, 2); + syscall_register(SYSCALL_LIO_DUP, &syscall_lio_dup, 2); syscall_register(SYSCALL_LIO_SRV_SERVICE_REGISTER, &syscall_lio_srv_service_register, 4); diff --git a/src/kernel2/src/syscalls/lostio.c b/src/kernel2/src/syscalls/lostio.c index eecffac..73aa98e 100644 --- a/src/kernel2/src/syscalls/lostio.c +++ b/src/kernel2/src/syscalls/lostio.c @@ -118,6 +118,43 @@ int syscall_lio_close(lio_usp_stream_t* stream_id) return lio_usp_unref_stream(current_process, fd); } +/// Zusätzliche ID für Stream erstellen +int syscall_lio_dup(lio_usp_stream_t* source, lio_usp_stream_t* dest) +{ + struct lio_usp_stream* old_fd; + struct lio_usp_stream* new_fd; + int ret; + + // TODO: is_userspace (source und dest) + if (*dest < -1) { + return -EBADF; + } + + old_fd = lio_usp_get_stream(current_process, *source); + if (old_fd == NULL) { + return -EBADF; + } + + new_fd = lio_usp_get_stream(current_process, *dest); + if (old_fd == new_fd) { + return 0; + } else if (new_fd != NULL) { + ret = lio_usp_unref_stream(current_process, new_fd); + if (ret < 0) { + return ret; + } + } + + if (*dest != -1) { + new_fd = lio_usp_do_add_stream(current_process, old_fd->stream, *dest); + } else { + new_fd = lio_usp_add_stream(current_process, old_fd->stream); + } + + *dest = new_fd->usp_id; + return 0; +} + /// Stream an anderen Prozess weitergeben int syscall_lio_pass_fd(lio_usp_stream_t* stream_id, pid_t pid) { diff --git a/src/modules/include/syscall.h b/src/modules/include/syscall.h index 7ab42a4..2fac1f4 100644 --- a/src/modules/include/syscall.h +++ b/src/modules/include/syscall.h @@ -149,6 +149,20 @@ lio_resource_t lio_resource(const char* path, bool follow_symlink); lio_stream_t lio_open(lio_resource_t resource, int flags); /** + * Erstellt eine zusätzliche Stream-ID für einen Stream. + * + * @param source Der zu duplizierende Stream + * @param dest Neue ID, die der Stream erhalten soll, oder -1 für eine + * beliebige freie ID. Wenn diese ID bereits einen Stream bezeichnet, wird + * dieser geschlossen. + * + * @return Neue Stream-ID bei Erfolg, negativ im Fehlerfall. Insbesondere: + * + * -EBADF Ein der Stream-IDs ist ungültig + */ +lio_stream_t lio_dup(lio_stream_t source, lio_stream_t dest); + +/** * Gibt Informationen zu einer Ressource zurück. * * @param resource ID der Ressource diff --git a/src/modules/lib/syscalls/lostio.c b/src/modules/lib/syscalls/lostio.c index 81658b7..b898e7a 100644 --- a/src/modules/lib/syscalls/lostio.c +++ b/src/modules/lib/syscalls/lostio.c @@ -116,6 +116,39 @@ int lio_stat(lio_resource_t resource, struct lio_stat* sbuf) } /** + * Erstellt eine zusätzliche Stream-ID für einen Stream. + * + * @param source Der zu duplizierende Stream + * @param dest Neue ID, die der Stream erhalten soll, oder -1 für eine + * beliebige freie ID. Wenn diese ID bereits einen Stream bezeichnet, wird + * dieser geschlossen. + * + * @return Neue Stream-ID bei Erfolg, negativ im Fehlerfall. Insbesondere: + * + * -EBADF Ein der Stream-IDs ist ungültig + */ +lio_stream_t lio_dup(lio_stream_t source, lio_stream_t dest) +{ + volatile lio_stream_t d = dest; + int result; + + asm( + "pushl %3;" + "pushl %2;" + "mov %1, %%eax;" + "int $0x30;" + "add $0x8, %%esp;" + : "=a" (result) : "i" (SYSCALL_LIO_DUP), "r" (&source), "r" (&d) + : "memory"); + + if (result < 0) { + return result; + } else { + return d; + } +} + +/** * Schließt einen Stream. * * @param s Der zu schließende Stream -- 2.1.4 From kevin at tyndur.org Sat Sep 3 16:52:58 2016 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 3 Sep 2016 16:52:58 +0200 Subject: [tyndur-devel] [PATCH 3/7] libc: file_get_stream() In-Reply-To: <1472914382-8896-1-git-send-email-kevin@tyndur.org> References: <1472914382-8896-1-git-send-email-kevin@tyndur.org> Message-ID: <1472914382-8896-4-git-send-email-kevin@tyndur.org> + libc: file_get_stream() gibt den LIOv2-Stream zu einem FILE* zurück Signed-off-by: Kevin Wolf --- src/modules/include/lostio.h | 5 ++++- src/modules/lib/stdlibc/file.c | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/modules/include/lostio.h b/src/modules/include/lostio.h index 9ad8c33..b1c56d5 100644 --- a/src/modules/include/lostio.h +++ b/src/modules/include/lostio.h @@ -82,7 +82,8 @@ typedef struct vfstree_node_t ///Handle fuer eine geoeffnete Datei -struct lostio_internal_file; +typedef struct lostio_internal_file FILE; + typedef struct { uint32_t id; @@ -152,6 +153,8 @@ struct lostio_internal_file { }; #define IS_LIO2(h) ((h)->lio2_res > 0) +lio_stream_t file_get_stream(FILE *io_res); + ///LostIO-Schnittstelle initialisieren void lostio_init(void); diff --git a/src/modules/lib/stdlibc/file.c b/src/modules/lib/stdlibc/file.c index 60c9e83..b0935bc 100644 --- a/src/modules/lib/stdlibc/file.c +++ b/src/modules/lib/stdlibc/file.c @@ -181,6 +181,15 @@ int fclose (FILE* io_res) return result ? EOF : 0; } +lio_stream_t file_get_stream(FILE *io_res) +{ + if (!is_valid_res(io_res) || !IS_LIO2_FILE(io_res)) { + return -EBADF; + } + + return io_res->res->lio2_stream; +} + static void prepare_buffer(FILE* io_res, bool is_write) { if (io_res->buffer_writes == is_write) { -- 2.1.4 From kevin at tyndur.org Sat Sep 3 16:52:59 2016 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 3 Sep 2016 16:52:59 +0200 Subject: [tyndur-devel] [PATCH 4/7] kernel2: lio_stream_origin() In-Reply-To: <1472914382-8896-1-git-send-email-kevin@tyndur.org> References: <1472914382-8896-1-git-send-email-kevin@tyndur.org> Message-ID: <1472914382-8896-5-git-send-email-kevin@tyndur.org> + kernel2: Neuer Syscall lio_stream_origin(), mit dem sich feststellen lässt, ob ein Stream tatsächlich existiert und von dem Prozess kommt, der das (z.B. in einem RPC-Aufruf) behauptet. Signed-off-by: Kevin Wolf --- src/include/syscallno.h | 3 ++- src/kernel2/include/lostio/core.h | 3 +++ src/kernel2/include/syscall.h | 3 +++ src/kernel2/src/lostio/userspace.c | 1 + src/kernel2/src/syscall.c | 1 + src/kernel2/src/syscalls/lostio.c | 16 ++++++++++++++++ src/modules/include/syscall.h | 13 +++++++++++++ src/modules/lib/syscalls/lostio.c | 25 +++++++++++++++++++++++++ 8 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/include/syscallno.h b/src/include/syscallno.h index 3938718..d4794f5 100644 --- a/src/include/syscallno.h +++ b/src/include/syscallno.h @@ -109,7 +109,8 @@ #define SYSCALL_LIO_SYNC_ALL 99 #define SYSCALL_LIO_PASS_FD 100 #define SYSCALL_LIO_PROBE_SERVICE 101 -#define SYSCALL_LIO_DUP 102 +#define SYSCALL_LIO_DUP 102 +#define SYSCALL_LIO_STREAM_ORIGIN 103 #define SYSCALL_LIO_SRV_SERVICE_REGISTER 110 #define SYSCALL_LIO_SRV_TREE_SET_RING 111 diff --git a/src/kernel2/include/lostio/core.h b/src/kernel2/include/lostio/core.h index 3e71dc3..d8ad945 100644 --- a/src/kernel2/include/lostio/core.h +++ b/src/kernel2/include/lostio/core.h @@ -66,6 +66,9 @@ struct lio_usp_stream { /** Sortierung nach usp_id im Baum */ struct tree_item usp_item; + + /** PID des Prozesses, der den Stream dem aktuellen übergeben hat */ + pid_t origin; }; struct lio_node { diff --git a/src/kernel2/include/syscall.h b/src/kernel2/include/syscall.h index c979373..e2b9148 100644 --- a/src/kernel2/include/syscall.h +++ b/src/kernel2/include/syscall.h @@ -217,6 +217,9 @@ int syscall_lio_dup(lio_usp_stream_t* source, lio_usp_stream_t* dest); /// Stream an anderen Prozess weitergeben int syscall_lio_pass_fd(lio_usp_stream_t* stream_id, pid_t pid); +/// PID des Prozesses, der den Stream dem aktuellen übergeben hat, auslesen +int syscall_lio_stream_origin(lio_usp_stream_t* stream_id); + /// Aus Stream lesen void syscall_lio_read(lio_usp_stream_t* stream_id, uint64_t* offset, size_t bytes, void* buffer, int updatepos, ssize_t* result); diff --git a/src/kernel2/src/lostio/userspace.c b/src/kernel2/src/lostio/userspace.c index 6fd5638..e814149 100644 --- a/src/kernel2/src/lostio/userspace.c +++ b/src/kernel2/src/lostio/userspace.c @@ -103,6 +103,7 @@ struct lio_usp_stream* lio_usp_do_add_stream(pm_process_t* proc, entry->stream = stream; entry->usp_id = id; + entry->origin = proc->pid; tree_insert(proc->lio_streams, entry); stream->usp_refcount++; diff --git a/src/kernel2/src/syscall.c b/src/kernel2/src/syscall.c index 557ab12..0cee76b 100644 --- a/src/kernel2/src/syscall.c +++ b/src/kernel2/src/syscall.c @@ -132,6 +132,7 @@ void syscall_init() syscall_register(SYSCALL_LIO_PASS_FD, &syscall_lio_pass_fd, 2); syscall_register(SYSCALL_LIO_PROBE_SERVICE, &syscall_lio_probe_service, 2); syscall_register(SYSCALL_LIO_DUP, &syscall_lio_dup, 2); + syscall_register(SYSCALL_LIO_STREAM_ORIGIN, &syscall_lio_stream_origin, 1); syscall_register(SYSCALL_LIO_SRV_SERVICE_REGISTER, &syscall_lio_srv_service_register, 4); diff --git a/src/kernel2/src/syscalls/lostio.c b/src/kernel2/src/syscalls/lostio.c index 73aa98e..47c2f8c 100644 --- a/src/kernel2/src/syscalls/lostio.c +++ b/src/kernel2/src/syscalls/lostio.c @@ -177,6 +177,7 @@ int syscall_lio_pass_fd(lio_usp_stream_t* stream_id, pid_t pid) stream = fd->stream; new_fd = lio_usp_add_stream(process, stream); + new_fd->origin = current_process->pid; *stream_id = new_fd->usp_id; // Wir haben gerade eine zweite Referenz hinzugefügt, also kann kein @@ -187,6 +188,21 @@ int syscall_lio_pass_fd(lio_usp_stream_t* stream_id, pid_t pid) return 0; } +/// PID des Prozesses, der den Stream dem aktuellen übergeben hat, auslesen +int syscall_lio_stream_origin(lio_usp_stream_t* stream_id) +{ + struct lio_usp_stream* fd; + + // TODO: is_userspace + fd = lio_usp_get_stream(current_process, *stream_id); + if (fd == NULL) { + return -EBADF; + } + + return fd->origin; +} + + /// Aus Stream lesen void syscall_lio_read(lio_usp_stream_t* stream_id, uint64_t* offset, size_t bytes, void* buffer, int flags, ssize_t* result) diff --git a/src/modules/include/syscall.h b/src/modules/include/syscall.h index 2fac1f4..5a493bb 100644 --- a/src/modules/include/syscall.h +++ b/src/modules/include/syscall.h @@ -198,6 +198,19 @@ int lio_close(lio_stream_t s); lio_stream_t lio_pass_fd(lio_stream_t s, pid_t pid); /** + * Gibt den Prozess zurück, der den Stream an den aktuellen Prozess übergeben + * hat. + * + * @param s Der zu prüfende Stream + * @return Falls der Stream von einem anderen Prozess übergeben wurde, dessen + * PID. Falls er von diesem Prozess geöffnet wurde, die eigene PID. Negativ + * im Fehlerfall, insbesondere: + * + * -EBADF Die Stream-ID ist ungültig + */ +int lio_stream_origin(lio_stream_t s); + +/** * Liest aus dem Stream. * * @param s Auszulesender Stream diff --git a/src/modules/lib/syscalls/lostio.c b/src/modules/lib/syscalls/lostio.c index b898e7a..eb14658 100644 --- a/src/modules/lib/syscalls/lostio.c +++ b/src/modules/lib/syscalls/lostio.c @@ -202,6 +202,31 @@ lio_stream_t lio_pass_fd(lio_stream_t s, pid_t pid) } } +/** + * Gibt den Prozess zurück, der den Stream an den aktuellen Prozess übergeben + * hat. + * + * @param s Der zu prüfende Stream + * @return Falls der Stream von einem anderen Prozess übergeben wurde, dessen + * PID. Falls er von diesem Prozess geöffnet wurde, die eigene PID. Negativ + * im Fehlerfall, insbesondere: + * + * -EBADF Die Stream-ID ist ungültig + */ +int lio_stream_origin(lio_stream_t s) +{ + int result; + + asm( + "pushl %2;" + "mov %1, %%eax;" + "int $0x30;" + "add $0x4, %%esp;" + : "=a" (result): "i" (SYSCALL_LIO_STREAM_ORIGIN), "r" (&s) : "memory"); + + return result; +} + ssize_t lio_readf(lio_stream_t s, uint64_t offset, size_t bytes, void* buf, int flags) { -- 2.1.4 From kevin at tyndur.org Sat Sep 3 16:53:00 2016 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 3 Sep 2016 16:53:00 +0200 Subject: [tyndur-devel] [PATCH 5/7] kernel2: syscall_shm_size In-Reply-To: <1472914382-8896-1-git-send-email-kevin@tyndur.org> References: <1472914382-8896-1-git-send-email-kevin@tyndur.org> Message-ID: <1472914382-8896-6-git-send-email-kevin@tyndur.org> + kernel2: Ein Syscall, um herauszufinden, wie groß ein SHM-Puffer überhaupt ist (wäre eventuell ganz geschickt bevor man ihn benutzt) Signed-off-by: Kevin Wolf --- src/include/syscallno.h | 1 + src/kernel2/include/syscall.h | 3 +++ src/kernel2/src/syscall.c | 1 + src/kernel2/src/syscalls/shm.c | 5 +++++ src/modules/lib/syscalls/shared_memory.c | 16 ++++++++++++++++ 5 files changed, 26 insertions(+) diff --git a/src/include/syscallno.h b/src/include/syscallno.h index d4794f5..4ab0e72 100644 --- a/src/include/syscallno.h +++ b/src/include/syscallno.h @@ -41,6 +41,7 @@ #define SYSCALL_SHM_CREATE 63 #define SYSCALL_SHM_ATTACH 64 #define SYSCALL_SHM_DETACH 65 +#define SYSCALL_SHM_SIZE 67 #define SYSCALL_PM_CREATE_PROCESS 3 #define SYSCALL_PM_INIT_PAGE 13 diff --git a/src/kernel2/include/syscall.h b/src/kernel2/include/syscall.h index e2b9148..ef60996 100644 --- a/src/kernel2/include/syscall.h +++ b/src/kernel2/include/syscall.h @@ -178,6 +178,9 @@ void* syscall_shm_attach(uint32_t id); /// Shared Memory schliessen void syscall_shm_detach(uint32_t id); +/// Größe eines Shared Memory zurückgeben +int32_t syscall_shm_size(uint32_t id); + /// Einen Timer anlegen void syscall_add_timer(uint32_t timer_id, uint32_t usec); diff --git a/src/kernel2/src/syscall.c b/src/kernel2/src/syscall.c index 0cee76b..2102bc2 100644 --- a/src/kernel2/src/syscall.c +++ b/src/kernel2/src/syscall.c @@ -108,6 +108,7 @@ void syscall_init() syscall_register(SYSCALL_SHM_CREATE, &syscall_shm_create, 1); syscall_register(SYSCALL_SHM_ATTACH, &syscall_shm_attach, 1); syscall_register(SYSCALL_SHM_DETACH, &syscall_shm_detach, 1); + syscall_register(SYSCALL_SHM_SIZE, &syscall_shm_size, 1); #if CONFIG_ARCH == ARCH_I386 syscall_register(SYSCALL_VM86, &syscall_vm86_old, 2); diff --git a/src/kernel2/src/syscalls/shm.c b/src/kernel2/src/syscalls/shm.c index 0892622..c86cf46 100644 --- a/src/kernel2/src/syscalls/shm.c +++ b/src/kernel2/src/syscalls/shm.c @@ -70,3 +70,8 @@ void syscall_shm_detach(uint32_t id) shm_detach(current_process, id); } +/// Größe eines Shared Memory zurückgeben +int32_t syscall_shm_size(uint32_t id) +{ + return shm_size(id); +} diff --git a/src/modules/lib/syscalls/shared_memory.c b/src/modules/lib/syscalls/shared_memory.c index dcf99f4..6c1609e 100644 --- a/src/modules/lib/syscalls/shared_memory.c +++ b/src/modules/lib/syscalls/shared_memory.c @@ -14,6 +14,7 @@ uint32_t create_shared_memory(uint32_t size) return id; } + void *open_shared_memory(uint32_t id) { vaddr_t addr; @@ -27,6 +28,21 @@ void *open_shared_memory(uint32_t id) return addr; } + +ssize_t shared_memory_size(uint32_t id) +{ + int32_t size; + + asm( + "pushl %2;" + "mov %1, %%eax;" + "int $0x30;" + "add $0x4, %%esp;" + : "=a" (size) : "i" (SYSCALL_SHM_SIZE), "r" (id)); + + return size; +} + void close_shared_memory(uint32_t id) { asm( -- 2.1.4 From kevin at tyndur.org Sat Sep 3 16:53:01 2016 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 3 Sep 2016 16:53:01 +0200 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_6/7=5D_libc/Pascal-RTL=3A_stdio?= =?utf-8?q?_mit_lio=5Fdup=28=29_und_PPB_=C3=BCbergeben?= In-Reply-To: <1472914382-8896-1-git-send-email-kevin@tyndur.org> References: <1472914382-8896-1-git-send-email-kevin@tyndur.org> Message-ID: <1472914382-8896-7-git-send-email-kevin@tyndur.org> * libc/Pascal-RTL: Anstatt beim console-Service nach dem Dateinamen für die Standardein- und -ausgabe zu fragen und diese dann neu zu öffnen, werden jetzt LIO-Streams übergeben und im PPB eingetragen. Signed-off-by: Kevin Wolf --- src/modules/c/getterm/main.c | 1 + src/modules/include/init.h | 5 ++ src/modules/include/lostio.h | 1 + src/modules/include/syscall.h | 1 + src/modules/init/loader.c | 11 ++++ src/modules/lib/crt0.c | 10 +++ src/modules/lib/param.c | 128 +++++++++++++++++++++++++++++++++++++ src/modules/lib/stdlibc/file.c | 11 ++++ src/modules/lib/stdlibc/stdio.c | 103 +++++++++++++++-------------- src/modules/pas/lib/rtl/system.pas | 41 ++++++------ 10 files changed, 243 insertions(+), 69 deletions(-) diff --git a/src/modules/c/getterm/main.c b/src/modules/c/getterm/main.c index 92d755b..ade145c 100644 --- a/src/modules/c/getterm/main.c +++ b/src/modules/c/getterm/main.c @@ -134,6 +134,7 @@ int main(int argc, char* argv[]) console_set_handle(STDERR, argv[3]); // Lokale Ein- und Ausgabehandles auch anpassen + stdin = stdout = stderr = NULL; stdio_init(); do { diff --git a/src/modules/include/init.h b/src/modules/include/init.h index 2de8092..07d7972 100644 --- a/src/modules/include/init.h +++ b/src/modules/include/init.h @@ -31,6 +31,7 @@ #include #include #include +#include #define INIT_PID 1 @@ -55,6 +56,10 @@ int ppb_get_argc(struct proc_param_block* ppb, size_t ppb_size); void ppb_copy_argv(struct proc_param_block* ppb, size_t ppb_size, char** argv, int argc); int ppb_extract_env(struct proc_param_block* ppb, size_t ppb_size); +int ppb_forward_streams(struct proc_param_block* ppb, size_t ppb_size, + pid_t from, pid_t to); +int ppb_get_streams(lio_stream_t* streams, struct proc_param_block* ppb, + size_t ppb_size, pid_t from, int num); #ifdef _USE_START_ diff --git a/src/modules/include/lostio.h b/src/modules/include/lostio.h index b1c56d5..3b79469 100644 --- a/src/modules/include/lostio.h +++ b/src/modules/include/lostio.h @@ -409,6 +409,7 @@ int lio_request_translated_blocks(struct lio_resource* res, uint64_t offset, /* Interne Funktionen */ int __iores_fileno(io_resource_t* io_res); struct lostio_internal_file* __create_file_from_iores(io_resource_t* io_res); +struct lostio_internal_file* __create_file_from_lio_stream(lio_stream_t s); #endif diff --git a/src/modules/include/syscall.h b/src/modules/include/syscall.h index 5a493bb..017d157 100644 --- a/src/modules/include/syscall.h +++ b/src/modules/include/syscall.h @@ -61,6 +61,7 @@ void* get_phys_addr(void* address); uint32_t create_shared_memory(uint32_t size); void *open_shared_memory(uint32_t id); void close_shared_memory(uint32_t id); +ssize_t shared_memory_size(uint32_t id); bool request_ports(uint32_t port, uint32_t length); bool release_ports(uint32_t port, uint32_t length); diff --git a/src/modules/init/loader.c b/src/modules/init/loader.c index 3f440ca..58649e2 100644 --- a/src/modules/init/loader.c +++ b/src/modules/init/loader.c @@ -37,6 +37,7 @@ #include #include #include +#include static struct { pid_t pid; @@ -112,6 +113,16 @@ pid_t load_single_module(void* image, size_t image_size, char* args, } if (ppb_shm_id) { + void *ppb = open_shared_memory(ppb_shm_id); + ssize_t ppb_size = shared_memory_size(ppb_shm_id); + + if (!ppb || ppb_size < 0) { + goto fail; + } + + ppb_forward_streams(ppb, ppb_size, parent_pid, loader_state.pid); + close_shared_memory(ppb_shm_id); + init_child_ppb(loader_state.pid, ppb_shm_id); } diff --git a/src/modules/lib/crt0.c b/src/modules/lib/crt0.c index fa4e2da..94cfa5e 100644 --- a/src/modules/lib/crt0.c +++ b/src/modules/lib/crt0.c @@ -32,6 +32,7 @@ #include #include #include +#include //Sollte anders geloest werden extern int main(int argc, char* argv[]); @@ -79,6 +80,15 @@ static void __attribute__((used)) init_envvars(ppb, ppb_size); init_waitpid(); #ifndef _NO_STDIO_ + if (ppb_shm_id) { + lio_stream_t streams[3]; + int ret = ppb_get_streams(streams, ppb, ppb_size, 1, 3); + if (ret == 3) { + stdin = streams[0] ? __create_file_from_lio_stream(streams[0]) : NULL; + stdout = streams[1] ? __create_file_from_lio_stream(streams[1]) : NULL; + stderr = streams[2] ? __create_file_from_lio_stream(streams[2]) : NULL; + } + } stdio_init(); #endif diff --git a/src/modules/lib/param.c b/src/modules/lib/param.c index a79be5b..f03dc53 100644 --- a/src/modules/lib/param.c +++ b/src/modules/lib/param.c @@ -28,15 +28,19 @@ #include #include +#include #include +#include #include +#include #include #include enum ppb_entry_type { PPB_CMDLINE_ARG = 1, PPB_ENV_VAR, + PPB_STREAM, }; struct ppb_entry { @@ -171,6 +175,42 @@ static int ppb_builder_finish(struct ppb_builder* b) return shm; } +static int ppb_add_cloned_stdio(struct ppb_builder* b, + lio_stream_t *remote, int num) +{ + FILE* stdio[] = { stdin, stdout, stderr }; + int i; + + assert(num <= 3); + for (i = 0; i < num; i++) { + lio_stream_t orig, copy; + int ret; + + orig = file_get_stream(stdio[i]); + if (orig < 0) { + continue; + } + + copy = lio_dup(orig, -1); + if (copy < 0) { + return copy; + } + + remote[i] = lio_pass_fd(copy, 1); + if (remote[i] < 0) { + lio_close(copy); + return remote[i]; + } + + ret = ppb_add_entry(b, PPB_STREAM, &remote[i], sizeof(remote[i])); + if (ret < 0) { + return ret; + } + } + + return 0; +} + static int ppb_add_cloned_env(struct ppb_builder* b) { int i, cnt; @@ -222,6 +262,11 @@ int ppb_from_argv(const char* const argv[]) const char* const* p; int ret; + /* Muss hier deklariert werden statt in ppb_add_cloned_stdio(), sonst ist + * der Pointer nicht mehr gültig, wenn er in ppb_builder_finish() benutzt + * wird. */ + lio_stream_t remote_streams[3]; + b = ppb_builder_init(); if (b == NULL) { errno = ENOMEM; @@ -240,6 +285,12 @@ int ppb_from_argv(const char* const argv[]) return -1; } + ret = ppb_add_cloned_stdio(b, remote_streams, 3); + if (ret < 0) { + errno = -ret; + return -1; + } + shm = ppb_builder_finish(b); if (shm < 0) { errno = -shm; @@ -363,3 +414,80 @@ int ppb_extract_env(struct proc_param_block* ppb, size_t ppb_size) return 0; } + +int ppb_forward_streams(struct proc_param_block* ppb, size_t ppb_size, + pid_t from, pid_t to) +{ + int i; + + if (!ppb_is_valid(ppb, ppb_size)) { + return -EINVAL; + } + + for (i = 0; i < ppb->num; i++) { + lio_stream_t s, remote; + + if (ppb->entries[i].type != PPB_STREAM) { + continue; + } + + if (ppb->entries[i].len != sizeof(lio_stream_t)) { + return -EFAULT; + } + + s = *(lio_stream_t*)(((char*) ppb) + ppb->entries[i].value); + + if (lio_stream_origin(s) != from) { + return -EBADF; + } + + remote = lio_pass_fd(s, to); + if (remote < 0) { + return remote; + } + + *(lio_stream_t*)(((char*) ppb) + ppb->entries[i].value) = remote; + } + + return 0; +} + +int ppb_get_streams(lio_stream_t* streams, struct proc_param_block* ppb, + size_t ppb_size, pid_t from, int num) +{ + int i; + int found = 0; + + if (!ppb_is_valid(ppb, ppb_size)) { + return -EINVAL; + } + + for (i = 0; i < ppb->num; i++) { + lio_stream_t s; + + if (ppb->entries[i].type != PPB_STREAM) { + continue; + } + + if (ppb->entries[i].len != sizeof(lio_stream_t)) { + return -EFAULT; + } + + s = *(lio_stream_t*)(((char*) ppb) + ppb->entries[i].value); + + if (lio_stream_origin(s) != from) { + return -EBADF; + } + + if (found < num) { + streams[found++] = s; + } else { + lio_close(s); + } + } + while (found < num) { + streams[found++] = 0; + } + + return found; +} diff --git a/src/modules/lib/stdlibc/file.c b/src/modules/lib/stdlibc/file.c index b0935bc..513f75b 100644 --- a/src/modules/lib/stdlibc/file.c +++ b/src/modules/lib/stdlibc/file.c @@ -73,6 +73,17 @@ FILE* __create_file_from_iores(io_resource_t* io_res) return int_res; } +FILE* __create_file_from_lio_stream(lio_stream_t s) +{ + io_resource_t* iores = malloc(sizeof(*iores)); + *iores = (io_resource_t) { + .lio2_stream = s, + .lio2_res = 1, /* XXX Hässlich, aber wird nur auf > 0 geprüft */ + .flags = IO_RES_FLAG_READAHEAD, + }; + return __create_file_from_iores(iores); +} + /** * Datei oeffnen * diff --git a/src/modules/lib/stdlibc/stdio.c b/src/modules/lib/stdlibc/stdio.c index e6a1955..7e0766f 100644 --- a/src/modules/lib/stdlibc/stdio.c +++ b/src/modules/lib/stdlibc/stdio.c @@ -51,71 +51,80 @@ void stdio_init(void) // stdin aktualisieren - path_file = fopen("console:/stdin", "r"); - if (path_file == NULL) { - // Kein stdin - stdin = NULL; - } else { - // Pfad einlesen - size = fread(path, 1, 128, path_file); - path[size] = 0; - - // Falls stdin schon geoeffnet wurde, wird freopen benutzt, damit siche - // nichts durcheinander kommt - if (stdin) { - freopen(path, "rs", stdin); + if (!stdin) { + path_file = fopen("console:/stdin", "r"); + if (path_file == NULL) { + // Kein stdin + stdin = NULL; } else { - stdin = fopen(path, "rs"); + // Pfad einlesen + size = fread(path, 1, 128, path_file); + path[size] = 0; + + // Falls stdin schon geoeffnet wurde, wird freopen benutzt, damit siche + // nichts durcheinander kommt + if (stdin) { + freopen(path, "rs", stdin); + } else { + stdin = fopen(path, "rs"); + } + fclose(path_file); } - fclose(path_file); - + } + if (stdin) { // stdin muss nicht gepuffert sein setvbuf(stdin, NULL, _IONBF, 0); } // stdout aktualisieren - path_file = fopen("console:/stdout", "r"); - if (path_file == NULL) { - // Kein stdin - stdout = NULL; - } else { - // Pfad einlesen - size = fread(path, 1, 128, path_file); - path[size] = 0; - - // Siehe oben bei stdin - if (stdout) { - freopen(path, "a", stdout); + if (!stdout) { + path_file = fopen("console:/stdout", "r"); + if (path_file == NULL) { + // Kein stdin + stdout = NULL; } else { - stdout = fopen(path, "a"); + // Pfad einlesen + size = fread(path, 1, 128, path_file); + path[size] = 0; + + // Siehe oben bei stdin + if (stdout) { + freopen(path, "a", stdout); + } else { + stdout = fopen(path, "a"); + } + fclose(path_file); } - fclose(path_file); - + } + if (stdout) { // Zeilenpuffer fuer stdout aktivieren setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf)); } // stderr - path_file = fopen("console:/stderr", "r"); - if (path_file == NULL) { - // Kein stdin - stderr = NULL; - } else { - // Pfad einlesen - size = fread(path, 1, 128, path_file); - path[size] = 0; - - // Siehe oben bei stdin - if (stderr) { - freopen(path, "a", stderr); + if (!stderr) { + path_file = fopen("console:/stderr", "r"); + if (path_file == NULL) { + // Kein stdin + stderr = NULL; } else { - stderr = fopen(path, "a"); - } + // Pfad einlesen + size = fread(path, 1, 128, path_file); + path[size] = 0; + + // Siehe oben bei stdin + if (stderr) { + freopen(path, "a", stderr); + } else { + stderr = fopen(path, "a"); + } + fclose(path_file); + } + } + if (stderr) { // stderr soll ungepuffert sein setvbuf(stderr, NULL, _IONBF, 0); - - fclose(path_file); } } diff --git a/src/modules/pas/lib/rtl/system.pas b/src/modules/pas/lib/rtl/system.pas index c9501c5..378dbd8 100644 --- a/src/modules/pas/lib/rtl/system.pas +++ b/src/modules/pas/lib/rtl/system.pas @@ -164,6 +164,11 @@ begin end; *) +function ppb_get_streams(streams: PInt64; ppb: Pointer; + size, pid, num: integer): integer; + cdecl; external name 'ppb_get_streams'; +function file_from_lio(stream: Int64): PDWord; + cdecl; external name '__create_file_from_lio_stream'; procedure c_stdio_init(); cdecl; external name 'stdio_init'; function c_setvbuf(f: Pointer; buf: Pointer; mode: integer; size: TSize): integer; cdecl; external name 'setvbuf'; @@ -174,30 +179,22 @@ var c_stderr: ^dword; external name 'stderr'; procedure SysInitStdIO; -(* var - filename: String; - c_stdin, c_stdout, c_stderr: text; -*) + streams: Array [0..2] of Int64; + ret: integer; begin -(* - TextRec(c_stdin).mode := fmClosed; - TextRec(c_stdout).mode := fmClosed; - TextRec(c_stderr).mode := fmClosed; - - filename := GetRealStdIO('console:/stdin') + #0; - Do_Open(c_stdin, @filename[1], 0); - - filename := GetRealStdIO('console:/stdout') + #0; - Do_Open(c_stdout, @filename[1], 0); - - filename := GetRealStdIO('console:/stderr') + #0; - Do_Open(c_stderr, @filename[1], 0); - - StdInputHandle := TextRec(c_stdin).handle; - StdOutputHandle := TextRec(c_stdout).handle; - StdErrorHandle := TextRec(c_stderr).handle; -*) + ret := ppb_get_streams(@streams[0], ppb_ptr, ppb_size, 1, 3); + if ret = 3 then begin + if streams[0] <> 0 then begin + c_stdin := file_from_lio(streams[0]); + end; + if streams[1] <> 0 then begin + c_stdout := file_from_lio(streams[1]); + end; + if streams[2] <> 0 then begin + c_stderr := file_from_lio(streams[2]); + end; + end; c_stdio_init(); -- 2.1.4 From kevin at tyndur.org Sat Sep 3 16:53:02 2016 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 3 Sep 2016 16:53:02 +0200 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_7/7=5D_shell=3A_Unterst=C3=BCtz?= =?utf-8?q?ung_f=C3=BCr_Ein-/Ausgabeumleitung?= In-Reply-To: <1472914382-8896-1-git-send-email-kevin@tyndur.org> References: <1472914382-8896-1-git-send-email-kevin@tyndur.org> Message-ID: <1472914382-8896-8-git-send-email-kevin@tyndur.org> Signed-off-by: Kevin Wolf --- src/modules/c/shell/shell.c | 107 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 2 deletions(-) diff --git a/src/modules/c/shell/shell.c b/src/modules/c/shell/shell.c index 8dfd2c0..acca66c 100644 --- a/src/modules/c/shell/shell.c +++ b/src/modules/c/shell/shell.c @@ -187,6 +187,45 @@ void shell_read_command(void) free(cwd); } +static bool is_redirection(const char* word, int* stdio_idx, int* flags) +{ + *stdio_idx = -1; + + if (!strcmp(word, "<")) { + *stdio_idx = 0; + *flags = LIO_READ; + } else if (!strcmp(word, ">")) { + *stdio_idx = 1; + *flags = LIO_WRITE | LIO_TRUNC; + } else if (!strcmp(word, ">>")) { + *stdio_idx = 1; + *flags = LIO_WRITE; /* TODO append */ + } else if (!strcmp(word, "2>")) { + *stdio_idx = 2; + *flags = LIO_WRITE | LIO_TRUNC; + } else if (!strcmp(word, "2>>")) { + *stdio_idx = 2; + *flags = LIO_WRITE; /* TODO append */ + } + + return (*stdio_idx != -1); +} + +static int get_stdio_fd(int index) +{ + switch (index) { + case 0: + return file_get_stream(stdin); + case 1: + return file_get_stream(stdout); + case 2: + return file_get_stream(stderr); + } + + abort(); + return -1; +} + int handle_command(char* buf) { char* args; @@ -195,6 +234,7 @@ int handle_command(char* buf) int i; int in_idx, out_idx; struct token* tokens; + int stdio_orig[3] = { 0 }; int ret; num_tokens = tokenize_cmdline(buf, NULL, NULL); @@ -217,8 +257,56 @@ int handle_command(char* buf) switch (tokens[i].type) { case TT_OPERATOR: { - ret = -1; - errno = ENOSYS; + int stdio_idx, flags; + const char* path; + lio_resource_t r; + lio_stream_t fd; + int stdio_fd; + + if (!is_redirection(tokens[i].value, &stdio_idx, &flags)) { + fprintf(stderr, "Unbekannter Operator '%s'\n", + tokens[i].value); + goto found; + } + + if (++i >= num_tokens || tokens[i].type != TT_WORD) { + fprintf(stderr, "Umleitungsziel erwartet\n"); + goto found; + } + + path = tokens[i].value; + r = lio_resource(path, 1); + if (r < 0 && (flags & LIO_WRITE)) { + lio_resource_t parent; + char* dirname = io_split_dirname(path); + char* filename = io_split_filename(path); + + if ((parent = lio_resource(dirname, 1)) > 0) { + r = lio_mkfile(parent, filename); + } + + free(dirname); + free(filename); + } + if (r < 0) { + fprintf(stderr, "Kann '%s' nicht öffnen: %s\n", + path, strerror(-r)); + } + + fd = lio_open(r, flags); + if (fd < 0) { + fprintf(stderr, "Kann '%s' nicht öffnen: %s\n", + path, strerror(-fd)); + goto found; + } + + stdio_fd = get_stdio_fd(stdio_idx); + if (!stdio_orig[stdio_idx]) { + stdio_orig[stdio_idx] = lio_dup(stdio_fd, -1); + } + fflush(stdout); + ret = lio_dup(fd, stdio_fd); + lio_close(fd); if (ret < 0) { fprintf(stderr, TMS(fail_dup, "lio_dup() fehlgeschlagen: %s\n"), @@ -290,6 +378,21 @@ found: free(args); free(argv); + /* Ausgabeumleitung rückgängig machen */ + for (i = 0; i < 3; i++) { + if (stdio_orig[i]) { + int stdio_fd = get_stdio_fd(i); + + fflush(stdout); + + ret = lio_dup(stdio_orig[i], stdio_fd); + if (ret < 0) { + fprintf(stderr, "lio_dup() fehlgeschlagen: %s\n", + strerror(-ret)); + } + } + } + return 0; } -- 2.1.4