[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tyndur-devel] [PATCH] neuer shell-Befehl: killall



+ Neuen Shell-Befehl killall implementiert, der alle Prozesse mit gegebenem Namen killt.

Signed-off-by: Stefan Linke <particleflux@xxxxxxxxx>
---
 src/modules/c/shell/cmds/killall.c | 118 +++++++++++++++++++++++++++++++++++++
 src/modules/c/shell/lang/lang_en.c |  14 +++++
 src/modules/c/shell/lang/resstr.h  |   5 ++
 src/modules/c/shell/shell.c        |   1 +
 src/modules/c/shell/shell.h        |   1 +
 5 files changed, 139 insertions(+)
 create mode 100644 src/modules/c/shell/cmds/killall.c

diff --git a/src/modules/c/shell/cmds/killall.c b/src/modules/c/shell/cmds/killall.c
new file mode 100644
index 0000000..48eec14
--- /dev/null
+++ b/src/modules/c/shell/cmds/killall.c
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2009 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Kevin Wolf
+ *
+ * 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 tyndur Project
+ *     and its contributors.
+ * 4. Neither the name of the tyndur 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 <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <syscall.h>
+#include <lost/config.h>
+
+#include <errno.h>
+#include <signal.h>
+#include <tms.h>
+
+static void killall_display_usage(void);
+
+#ifdef CONFIG_SHELL_BUILTIN_ONLY
+    int shell_command_killall(int argc, char* argv[], const char* args)
+#else
+    int main(int argc, char* argv[])
+#endif
+{
+    int ret=0,i=0,fail=0,matches=0;
+    task_info_t *task_info = NULL;
+    char *process;
+    char copied_cmdline[255];
+
+    if (argc < 2) {
+        killall_display_usage();
+        return -1;
+    }
+
+    task_info = enumerate_tasks();
+
+    if (task_info == NULL) {
+        return EXIT_FAILURE;
+    }
+
+
+    for (i = 0; i < task_info->task_count; ++i) {
+        strncpy(copied_cmdline, task_info->tasks[i].cmdline, 255);
+
+        process = strtok((char *) task_info->tasks[i].cmdline, " ");
+        process = process == NULL
+                ? strrchr(task_info->tasks[i].cmdline, '/')
+                : strrchr(process, '/');
+        ++process;
+
+        if (strcmp(argv[1], process) == 0) {
+            ++matches;
+            ret = kill(task_info->tasks[i].pid, SIGTERM);
+
+            if (ret < 0) {
+                printf(TMS(killall_cant_kill, 
+                        "Konnte pid %d - %s nicht beenden: %s (%s)\n"),
+                        task_info->tasks[i].pid, errno, strerror(errno));
+
+                if (!fail) {
+                    fail = 1;
+                }
+            } else {
+                printf(TMS(killall_killed, "pid %d beendet\n"), 
+                       task_info->tasks[i].pid);
+            }
+        }
+
+        process = NULL;
+    }
+
+    mem_free(task_info, task_info->info_size);
+
+    if (matches == 0) {
+        printf(TMS(killall_not_found, "Kein passender Prozess gefunden\n"));
+    }
+
+    if (fail) {
+        return EXIT_FAILURE;
+    }
+
+    return EXIT_SUCCESS;
+}
+
+static void killall_display_usage()
+{
+    printf(TMS(killall_usage, "Aufruf: killall <name>\n"));
+}
+
diff --git a/src/modules/c/shell/lang/lang_en.c b/src/modules/c/shell/lang/lang_en.c
index 3366190..c16abcf 100644
--- a/src/modules/c/shell/lang/lang_en.c
+++ b/src/modules/c/shell/lang/lang_en.c
@@ -250,6 +250,20 @@ static const struct tms_strings dict[] = {
 
 
 
+    &__tms_killall_usage,
+    "usage: killall <name>\n",
+
+    &__tms_killall_not_found,
+    "no matching process found\n",
+
+    &__tms_killall_killed,
+    "pid %d killed\n",
+
+    &__tms_killall_cant_kill,
+    "couldn't kill pid %d - %s: %s (%s)\n",
+
+
+
     &__tms_ln_usage,
     "\nUsage: ln <target> <link-path>\n\n"
     "Create hardlink to file/directory <target> at <link-path>.\n",
diff --git a/src/modules/c/shell/lang/resstr.h b/src/modules/c/shell/lang/resstr.h
index 5a11d64..fdff7b9 100644
--- a/src/modules/c/shell/lang/resstr.h
+++ b/src/modules/c/shell/lang/resstr.h
@@ -85,6 +85,11 @@ extern void* __tms_kill_usage;
 extern void* __tms_kill_not_found;
 extern void* __tms_kill_error;
 
+extern void* __tms_killall_usage;
+extern void* __tms_killall_not_found;
+extern void* __tms_killall_cant_kill;
+extern void* __tms_killall_killed;
+
 extern void* __tms_ln_usage;
 extern void* __tms_ln_error;
 
diff --git a/src/modules/c/shell/shell.c b/src/modules/c/shell/shell.c
index 55a3f3b..cdca2f1 100644
--- a/src/modules/c/shell/shell.c
+++ b/src/modules/c/shell/shell.c
@@ -87,6 +87,7 @@ shell_command_t shell_commands[] = {
     {"echo",        &shell_command_echo},
     {"free",        &shell_command_free},
     {"kill",        &shell_command_kill},
+    {"killall",     &shell_command_killall},
     {"ls",          &shell_command_ls},
     {"mkdir",       &shell_command_mkdir},
     {"pipe",        &shell_command_pipe},
diff --git a/src/modules/c/shell/shell.h b/src/modules/c/shell/shell.h
index 40f91c9..8e94409 100644
--- a/src/modules/c/shell/shell.h
+++ b/src/modules/c/shell/shell.h
@@ -68,6 +68,7 @@ int shell_command_clear(int argc, char* argv[], const char* args);
     int shell_command_echo(int argc, char* argv[], const char* args);
     int shell_command_free(int argc, char* argv[], const char* args);
     int shell_command_kill(int argc, char* argv[], const char* args);
+    int shell_command_killall(int argc, char* argv[], const char* args);
     int shell_command_ls(int argc, char* argv[], const char* args);
     int shell_command_mkdir(int argc, char* argv[], const char* args);
     int shell_command_pipe(int argc, char* argv[], const char* args);
-- 
1.8.1.5