[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Lost] [Patch] wait und waitpid
Mit diesem Patch wird unsere Libc um die Funktionen wait() und waitpid
bereichert. (Oder auch nicht *g*)
Index: src/modules/c/shell/commmands.c
===================================================================
--- src/modules/c/shell/commmands.c (Revision 698)
+++ src/modules/c/shell/commmands.c (Arbeitskopie)
@@ -42,6 +42,7 @@
#include "rpc.h"
#include "shell.h"
#include "dirent.h"
+#include <sys/wait.h>
#include "init.h"
@@ -160,9 +161,8 @@
// Wenn es gewuenscht wurde, wird jetzt gewartet, bis der Prozess
// beendet wird.
if (wait == TRUE) {
- while (get_parent_pid(pid) != 0) {
- yield();
- }
+ int status;
+ while (waitpid(pid, &status, 0) != pid);
}
// FIXME: s.o.
Index: src/modules/include/sys/wait.h
===================================================================
--- src/modules/include/sys/wait.h (Revision 0)
+++ src/modules/include/sys/wait.h (Revision 0)
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2007 The LOST Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the LOST Project
+ * by Antoine Kaufmann.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the LOST Project
+ * and its contributors.
+ * 4. Neither the name of the LOST Project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef _SYS_WAIT_H_
+#define _SYS_WAIT_H_
+#include <sys/types.h>
+
+// TODO
+#define WIFEXITED(staus) (1)
+#define WIFSIGNALED(status) (0)
+
+#define WEXITSTATUS(status) (status)
+
+
+pid_t wait(int* status);
+pid_t waitpid(pid_t pid, int* status, int options);
+
+#endif //ifndef _SYS_WAIT_H_
Index: src/modules/include/init.h
===================================================================
--- src/modules/include/init.h (Revision 698)
+++ src/modules/include/init.h (Arbeitskopie)
@@ -63,6 +63,7 @@
extern int main(int argc, char* argv[]);
extern void init_sync_messages();
extern void init_envvars();
+extern void init_waitpid();
extern void stdio_init();
void _start() {
//v();
@@ -71,6 +72,7 @@
init_sync_messages();
init_envvars();
+ init_waitpid();
#ifndef _NO_STDIO_
stdio_init();
#ifndef _NO_STDIO_BUFFER_
Index: src/modules/lib/stdlibc/exit.c
===================================================================
--- src/modules/lib/stdlibc/exit.c (Revision 698)
+++ src/modules/lib/stdlibc/exit.c (Arbeitskopie)
@@ -104,6 +104,12 @@
*/
void _exit(int result)
{
+ // RPC an Elternprozess schicken
+ char* msg = "CHL_EXIT ";
+ int* status = (int*) (msg + 8);
+ *status = result;
+ send_message(get_parent_pid(0), 512, 0, strlen(msg), msg);
+
init_process_exit(result);
destroy_process();
Index: src/modules/lib/posix/wait.c
===================================================================
--- src/modules/lib/posix/wait.c (Revision 0)
+++ src/modules/lib/posix/wait.c (Revision 0)
@@ -0,0 +1,196 @@
+/*
+ * Copyright (c) 2007 The LOST Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the LOST Project
+ * by Antoine Kaufmann.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the LOST Project
+ * and its contributors.
+ * 4. Neither the name of the LOST Project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <rpc.h>
+#include <assert.h>
+#include <collections.h>
+#include <syscall.h>
+#include <sys/wait.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <errno.h>
+
+struct wait_child {
+ pid_t pid;
+ bool running;
+ int status;
+};
+
+// Liste mit den Kindprozessen
+static list_t* wait_list = NULL;
+
+static void rpc_child_exit(pid_t pid, dword correlation_id, size_t data_size,
+ void* data);
+
+/**
+ * Wait-Funktionen initialisieren
+ */
+void init_waitpid()
+{
+ wait_list = list_create();
+ register_message_handler("CHL_EXIT", &rpc_child_exit);
+}
+
+/**
+ * Kindprozess aus der liste hoeln
+ */
+static struct wait_child* wait_child_get(pid_t pid)
+{
+ int i;
+ struct wait_child* wait_child;
+ for (i = 0; (wait_child = list_get_element_at(wait_list, i)); i++) {
+ if (wait_child->pid == pid) {
+ return wait_child;
+ }
+ }
+
+ return NULL;
+}
+
+/**
+ * Neuen Kindprozess registrieren
+ *
+ * Nicht static weil auch in init_execute benutzt
+ */
+void wait_child_add(pid_t pid)
+{
+ p();
+ struct wait_child* wait_child = wait_child_get(pid);
+
+ // Es kann sein, dass der Prozess schon terminiert hat, wenn diese funktion
+ // aufgerufen wird!
+ if (wait_child != NULL) {
+ return;
+ }
+
+ wait_child = malloc(sizeof(struct wait_child));
+ assert(wait_child != NULL);
+
+ wait_child->pid = pid;
+ wait_child->running = TRUE;
+ list_push(wait_list, wait_child);
+
+ v();
+}
+
+/**
+ * Handle fuer Waitpid aus der Liste entfernen
+ */
+static void wait_child_del(pid_t pid)
+{
+ int i;
+ struct wait_child* wait_child;
+ for (i = 0; (wait_child = list_get_element_at(wait_list, i)); i++) {
+ if (wait_child->pid == pid) {
+ list_remove(wait_list, i);
+ return;
+ }
+ }
+}
+
+/**
+ * RPC der von den Kindprozessen gesendet wird, um uns den Exit-Code
+ * mitzuteilen
+ */
+static void rpc_child_exit(pid_t pid, dword correlation_id, size_t data_size,
+ void* data)
+{
+ struct wait_child* wait_child;
+
+ // Pruefen ob der RPC wirklich die richtige Groesse hat
+ if (data_size != sizeof(int)) {
+ return;
+ }
+
+ p();
+ // Wenn der Prozess noch nicht als Kindprozess registriert wurde, wird das
+ // jetzt gemacht
+ wait_child = wait_child_get(pid);
+ if (wait_child == NULL) {
+ wait_child_add(pid);
+ }
+ wait_child = wait_child_get(pid);
+
+ assert(wait_child != NULL);
+ wait_child->status = *((int *) data);
+ wait_child->running = FALSE;
+ v();
+}
+
+/**
+ * Wartet bis der angegebene Prozess terminiert
+ */
+pid_t waitpid(pid_t pid, int* status, int options)
+{
+ struct wait_child* wait_child;
+ // Status eines Bestimmten Prozesses gewuenscht
+ if (pid > 0) {
+ wait_child = wait_child_get(pid);
+
+ // Nicht unser Kindprozess
+ if (wait_child == NULL) {
+ errno = ECHILD;
+ return -1;
+ }
+
+ // Warten bis der Prozess terminiert
+ while (wait_child->running && (get_parent_pid(pid) != 0)) {
+ yield();
+ }
+
+ if (status != NULL) {
+ *status = wait_child->status;
+ }
+ wait_child_del(pid);
+ return pid;
+ } else {
+ // Irgend ein Prozess (FIXME)
+ errno = ECHILD;
+ return -1;
+ }
+}
+
+
+/**
+ * Wartet bis ein Kindprozess terminiert
+ *
+ * @param status Pointer auf Variable fuer Rueckgabewert
+ *
+ * @return PID des Kindprozesses
+ */
+pid_t wait(int* status)
+{
+ return waitpid(-1, status, 0);
+}
+
Index: src/modules/lib/init.c
===================================================================
--- src/modules/lib/init.c (Revision 698)
+++ src/modules/lib/init.c (Arbeitskopie)
@@ -39,6 +39,9 @@
#include "init.h"
#include "stdlib.h"
+// Kindprozess eintragen
+extern void wait_child_add(pid_t pid);
+
/**
* Dem aktuellen Prozess beim Init-Prozess als Treiber anmelden.
* @param name Der Treibername
@@ -89,10 +92,18 @@
/**
*
*/
+
pid_t init_execute(const char* cmd)
{
+ pid_t pid;
+
// TODO: Relative Pfade
- return rpc_get_dword(1, "LOADELF ", strlen(cmd) + 1, (char*)cmd);
+ pid = rpc_get_dword(1, "LOADELF ", strlen(cmd) + 1, (char*)cmd);
+
+ // Kindprozess eintragen, falls er erfolgreich gestartet wurde
+ if (pid != 0) {
+ wait_child_add(pid);
+ }
+ return pid;
}
-