BarryServer : Git

All the code for all my projects
// BarryServer : Git / Orion / commit / d41a53cbc7d055b1c00cf0a339dbed6925f4f02c / task / signal.c

// Related

Orion

Barry Importing existing Orion kernel d41a53c (2 years, 4 months ago)
diff --git a/task/signal.c b/task/signal.c
new file mode 100644
index 0000000..c80c279
--- /dev/null
+++ b/task/signal.c
@@ -0,0 +1,63 @@
+/*
+ * This file handles signals to tasks.  It handles blocking signals, and
+ * registering the signal handlers.  It send/dispatches signals to tasks and
+ * runs the registered signal handlers when appropriate.
+ */
+
+#include <stdint.h>
+#include <errno.h>
+#include "task.h"
+
+extern TaskQueue tasks;
+
+/* Handle the signals for a task */
+void
+handle_signals(void)
+{
+	if (!(current->sigset & ~current->blockedSignals))
+		return;
+
+	int signal;
+	for (signal = 0; signal < 32; signal++) {
+		if (!(current->sigset & (1 << (signal - 1))))
+			continue;
+		current->status = signal;
+		terminate();
+	}
+}
+
+/* Send a signal to a thread */
+int
+tgkill(pid_t tgid, pid_t tid, int sig)
+{
+	if (sig < 0 || sig > 31)
+		return -EINVAL;
+
+	Task *task = find_task(tid);
+	if (!task || task->tgid != tgid)
+		return -ESRCH;
+	if (sig)
+		task->sigset |= (1 << (sig - 1));
+
+	return 0;
+}
+
+/* Send a signal to a process */
+int
+kill(pid_t pid, int sig)
+{
+	if (sig < 0 || sig > 31)
+		return -EINVAL;
+
+	int sent = 0;
+	Task *task;
+	for (task = tasks.start; task; task = task->tnext) {
+		if (task->tgid != pid)
+			continue;
+		if (sig)
+			task->sigset |= (1 << (sig - 1));
+		sent++;
+	}
+
+	return sent ? 0 : -ESRCH;
+}