[3/4] support/shell-container.c: Add builtin kill

Message ID 20200324185043.2073568-3-adhemerval.zanella@linaro.org
State Dropped
Headers
Series [1/4] support/shell-container.c: Return 127 if execve fails |

Commit Message

Adhemerval Zanella March 24, 2020, 6:50 p.m. UTC
  No options supported.
---
 support/shell-container.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
  

Patch

diff --git a/support/shell-container.c b/support/shell-container.c
index d405eda60e..e926e74abe 100644
--- a/support/shell-container.c
+++ b/support/shell-container.c
@@ -147,6 +147,25 @@  exit_func (char **argv)
   return 0;
 }
 
+/* Emulate the "/bin/kill" command.  Options are ignored.  */
+static int
+kill_func (char **argv)
+{
+  int signum = SIGTERM;
+  int i;
+
+  for (i = 0; argv[i]; i++)
+    {
+      pid_t pid;
+      if (strcmp (argv[i], "$$") == 0)
+	pid = getpid ();
+      else
+	pid = atoi (argv[i]);
+      kill (pid, signum);
+    }
+  return 0;
+}
+
 /* This is a list of all the built-in commands we understand.  */
 static struct {
   const char *name;
@@ -156,6 +175,7 @@  static struct {
   { "echo", echo_func },
   { "cp", copy_func },
   { "exit", exit_func },
+  { "kill", kill_func },
   { NULL, NULL }
 };
 
@@ -264,6 +284,11 @@  run_command_array (char **argv)
       if (rv)
 	exit (rv);
     }
+  else if (WIFSIGNALED (status))
+    {
+      int sig = WTERMSIG (status);
+      raise (sig);
+    }
   else
     exit (1);
 }