[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Lost] [Patch] Signale
Hier ein Patch der LOST die wichtigsten Funktionen für Posix-Signale
beibringt. Dabei wird der bisherige signal-Syscall entfernt. Die neue
implementierung funktioniert über send_message.
Index: src/modules/include/syscall.h
===================================================================
--- src/modules/include/syscall.h (Revision 589)
+++ src/modules/include/syscall.h (Arbeitskopie)
@@ -43,7 +43,6 @@
void add_intr_handler(dword intr);
void rpc(pid_t pid);
-void signal(pid_t pid, dword signal);
void send_message(pid_t pid, dword function, dword correlation_id, dword len, char* data);
void wait_for_rpc();
void v_and_wait_for_rpc();
Index: src/modules/lib/syscalls/rpc.c
===================================================================
--- src/modules/lib/syscalls/rpc.c (Revision 589)
+++ src/modules/lib/syscalls/rpc.c (Arbeitskopie)
@@ -16,22 +16,6 @@
} while (result != 0);
}
-void signal(dword pid, dword signal)
-{
- dword result = 0;
-
- do {
- asm(
- "pushl %2;"
- "pushl $4;"
- "pushl %1;"
- "mov $51, %%eax;"
- "int $0x30;"
- "add $0xb, %%esp;"
- : "=a" (result) : "g" (pid), "g" (signal + 256), "0" (result));
- } while (result != 0);
-}
-
void send_message(dword pid, dword function, dword correlation_id, dword len, char* data)
{
dword result = 0;
Index: src/modules/lib/rpc/messaging.c
===================================================================
--- src/modules/lib/rpc/messaging.c (Revision 590)
+++ src/modules/lib/rpc/messaging.c (Arbeitskopie)
@@ -37,6 +37,7 @@
#include "rpc.h"
#include "syscall.h"
#include "string.h"
+#include <signal.h>
// Diese Deklaration ist so eigentlich nicht richtig, der RPC-Handler nimmt
// durchaus Parameter. Allerdings folgt das ganze sowieso nicht dem
@@ -168,5 +169,8 @@
} else if (function == RPC_TIMER) {
// Ein Timer ist abgelaufen
timer_callback(correlation_id);
+ } else if (function == RPC_SIGNAL) {
+ raise(correlation_id);
}
+
}
Index: src/modules/include/signal.h
===================================================================
--- src/modules/include/signal.h (Revision 0)
+++ src/modules/include/signal.h (Revision 0)
@@ -0,0 +1,132 @@
+/*
+ * 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 _SIGNAL_H_
+#define _SIGNAL_H_
+#include <sys/types.h>
+
+// Signale
+
+/// Verbindung zu Terminal Verloren
+#define SIGHUP 1
+
+/// Unterbrechung vom Benutzer angeforder (Ctrl + C)
+#define SIGINT 2
+
+/// TODO
+#define SIGQUIT 3
+
+/// Fehlerhafte Instruktion
+#define SIGILL 4
+
+/// Debug
+#define SIGTRAP 5
+
+/// Prozess terminieren (Kann zwar abgefangen werden, aber nach abarbeiten der
+/// Routine wird der Prozess trotzdem beendet).
+#define SIGABRT 6
+
+/// TODO
+#define SIGBUS 7
+
+/// Fehler bei einer arithmetischen Operation
+#define SIGFPE 8
+
+/// Prozess terminieren (kann _nicht_ abgefangen werden)
+#define SIGKILL 9
+
+/// Benutzerdefiniertes Signal 1
+#define SIGUSR1 10
+
+/// Speicherzugriffsfehler
+#define SIGSEGV 11
+
+/// Benutzerdefiniertes Signal 2
+#define SIGUSR2 12
+
+/// Pipe-Fehler
+#define SIGPIPE 13
+
+/// TODO
+#define SIGALRM 14
+
+/// Prozess terminieren (kann abgefangen werden)
+#define SIGTERM 15
+
+/// LOST-Spezifisches Signal: Eingabedaten auf Terminal bereit
+#define SIGTERMINPUT 31
+
+/// Start des frei verwendbaren Bereichs
+#define SIGRTMIN 32
+
+/// Ende des frei verwendbaren Bereichs
+#define SIGRTMAX 63
+
+// Hier sollte noch viel mehr kommen
+
+/// Nur fuer interne Verwendung. Muss immer 1 groesser sein als die groesste
+/// Signalnummer und teilbar durch 8
+#define _SIGNO_MAX 64
+
+
+
+// Gehoert nach Spezifikation da nicht rein.
+void _signal_default_handler(int signal);
+
+/// Standard-Signalhandler
+#define SIG_DFL (&_signal_default_handler)
+
+/// Das gibt signal im Fehlerfall zurueck
+#define SIG_ERR (NULL)
+
+/// Signalhandler fuer ignonierte Signale
+#define SIG_IGN (NULL)
+
+
+
+/// Typ fuer einen Pointer auf einen Signal-Handler
+typedef void (*sighandler_t)(int);
+
+/// Einen Signal-Hander aendern
+sighandler_t signal(int signum, sighandler_t handler);
+
+/// Ein Signal zum aktuellen Prozess senden
+int raise(int signal);
+
+/// Ein Signal an einen anderen Prozess senden
+int kill(pid_t pid, int signal);
+
+#endif // ifndef _SIGNAL_H_
+
Index: src/modules/lib/posix/signal.c
===================================================================
--- src/modules/lib/posix/signal.c (Revision 0)
+++ src/modules/lib/posix/signal.c (Revision 0)
@@ -0,0 +1,177 @@
+/*
+ * 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 <signal.h>
+#include <stdio.h>
+#include <collections.h>
+#include <rpc.h>
+#include <syscall.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+
+/// Array mit Pointern auf die Signal-Handler
+static sighandler_t signal_handlers [_SIGNO_MAX];
+bool initialized = FALSE;
+
+/**
+ * Signale initialisieren
+ */
+void init_signals()
+{
+ int i;
+ for (i = 0; i < _SIGNO_MAX; i++) {
+ signal_handlers[i] = SIG_DFL;
+ }
+
+ initialized = TRUE;
+}
+
+/**
+ * Ein Signal an den aktuellen Prozess senden
+ *
+ * @param signum Signalnummer
+ *
+ * @return 0 bei Erfolg, != 0 im Fehlerfall
+ */
+int raise(int signum)
+{
+ // Falls die Liste noch nicht initialisiert ist, wird das jetzt gemacht
+ if (initialized == FALSE) {
+ init_signals();
+ }
+
+ // Wenn das Signal Groesser ist als die Groesste gueltige wird sofort
+ // abgebrochen.
+ if (signum >= _SIGNO_MAX) {
+ return -1;
+ }
+
+ sighandler_t handler = signal_handlers[signum];
+ if (handler != NULL) {
+ handler(signum);
+ }
+ return 0;
+}
+
+/**
+ * Ein Signal an einen anderen Prozess senden
+ *
+ * @param pid Prozessnummer
+ * @param signum Signalnummer
+ *
+ * @return 0 bei Erfolg, != 0 im Fehlerfall
+ */
+int kill(pid_t pid, int signum)
+{
+ send_message(pid, RPC_SIGNAL, signum, 0, NULL);
+ return 0;
+}
+
+/**
+ * Standard-Handler fuer Signale
+ *
+ * @param signum Signalnummer
+ */
+void _signal_default_handler(int signum)
+{
+ printf("Signal %d\n", signum);
+
+ switch (signum) {
+ // Signale die den Prozess mit Schnickschnack wie Core-File beenden
+ // (TODO)
+ case SIGQUIT:
+ case SIGILL:
+ case SIGTRAP:
+ case SIGABRT:
+ case SIGBUS:
+ case SIGFPE:
+ case SIGSEGV:
+ // Hier koennte man jetzt jede Menge debug-Zeug einbauen wie
+ // core-Dumps und sowas ;-)
+ // Und das break fehlt hier auch absichtlich!
+
+ // Signale die einfach nur den Prozess beenden
+ case SIGHUP:
+ case SIGINT:
+ case SIGKILL:
+ case SIGUSR1:
+ case SIGUSR2:
+ case SIGPIPE:
+ case SIGALRM:
+ case SIGTERM:
+ _exit(EXIT_FAILURE);
+ break;
+ // TODO: Dinge wie Prozess anhalten und wieder starten
+ }
+}
+
+/**
+ * Handler fuer ein ignoriertes Signal
+ *
+ * @param signum Signalnummer
+ */
+void _signnal_ignore_handler(int signum)
+{
+}
+
+/**
+ * Handler fuer ein bestimmtes Signal aendern
+ *
+ * @param signum Nummer des Signals
+ * @param handler Neuer Handler
+ *
+ * @return Vorheriger Signalhandler bei Erfolg, SIG_ERR im Fehlerfall
+ */
+sighandler_t signal(int signum, sighandler_t handler)
+{
+ // Initialisieren falls das noch nicht gemacht ist
+ if (initialized == FALSE) {
+ init_signals();
+ }
+
+ // Ungueltige Signale und SIGKILL koennen nicht gesetzt werden
+ if ((signum >= _SIGNO_MAX) || (signum == SIGKILL)) {
+ return SIG_ERR;
+ }
+
+ // Alter Handler Speichern, weil er zurueck gegeben wird
+ sighandler_t old_handler = signal_handlers[signum];
+
+ // Neuer setzen
+ signal_handlers[signum] = handler;
+
+ return old_handler;
+}