]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
lxc: introduce lxc-kill command (v4)
authorgkurz@linux.vnet.ibm.com <gkurz@linux.vnet.ibm.com>
Thu, 29 Apr 2010 08:03:59 +0000 (10:03 +0200)
committerDaniel Lezcano <dlezcano@fr.ibm.com>
Thu, 29 Apr 2010 08:03:59 +0000 (10:03 +0200)
lxc-kill send a signal to the process 1 of the container.

If this command is used on an application container ran by
lxc-execute, the lxc-init will receive the signal and will forward it to
the process 2 which is the command specified in the command line.

Signed-off-by: Greg Kurz <gkurz@fr.ibm.com>
Signed-off-by: Michel Normand <normand@fr.ibm.com>
Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
configure.ac
doc/Makefile.am
doc/lxc-execute.sgml.in
doc/lxc-kill.sgml.in [new file with mode: 0644]
doc/see_also.sgml.in
src/lxc/Makefile.am
src/lxc/lxc_init.c
src/lxc/lxc_kill.c [new file with mode: 0644]

index f82e7df756db1af6548124d9a806ed36bf329257..4c8c50f4b779d28f84f77523cdcec1f4fd08cfd8 100644 (file)
@@ -95,6 +95,7 @@ AC_CONFIG_FILES([
        doc/lxc-ls.sgml
        doc/lxc-ps.sgml
        doc/lxc-cgroup.sgml
+       doc/lxc-kill.sgml
        doc/lxc.conf.sgml
        doc/lxc.sgml
        doc/common_options.sgml
index bd96c9933a98dbd280edb7f6a0fedbe459ca0657..54e5f221726c5f853200f6d1e7a6a5e13a4b351e 100644 (file)
@@ -20,6 +20,7 @@ man_MANS = \
        lxc-ls.1 \
        lxc-ps.1 \
        lxc-cgroup.1 \
+       lxc-kill.1 \
        \
        lxc.conf.5 \
        \
index fe51b9bf34202938544684fa9974f0dd8177c80c..7bd0098079c025051bfd4591a9d6038fffab2793 100644 (file)
@@ -86,6 +86,12 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
       container, <command>lxc-init</command> has the pid 1 and the
       first process of the application has the pid 2.
      </para>
+     <para>
+      The above <command>lxc-init</command> is designed to forward received
+      signals to the started command.
+      So <command>lxc-kill</command> (1) sent signal is received
+      by the user specified command (pid 2 in the container).
+     </para>
   </refsect1>
 
   <refsect1>
diff --git a/doc/lxc-kill.sgml.in b/doc/lxc-kill.sgml.in
new file mode 100644 (file)
index 0000000..c3373c6
--- /dev/null
@@ -0,0 +1,91 @@
+<!DOCTYPE refentry PUBLIC "-//Davenport//DTD DocBook V3.0//EN" [
+
+<!ENTITY copyrights SYSTEM "@builddir@/copyrights.sgml">
+<!ENTITY commonoptions SYSTEM "@builddir@/common_options.sgml">
+<!ENTITY seealso SYSTEM "@builddir@/see_also.sgml">
+
+<!ENTITY mcr "<citerefentry>
+<refentrytitle><command>mcr</command></refentrytitle>
+<manvolnum/1/</citerefentry>">
+
+]>
+
+<refentry>
+
+  <docinfo><date>@LXC_GENERATE_DATE@</date></docinfo>
+
+  <refmeta>
+    <refentrytitle>lxc-kill</refentrytitle>
+    <manvolnum>1</manvolnum>
+    <refmiscinfo>IBM</refmiscinfo>
+  </refmeta>
+
+  <refnamediv>
+    <refname>lxc-kill</refname>
+
+    <refpurpose>
+      Send a signal to the process 1 of the container.
+    </refpurpose>
+  </refnamediv>
+
+  <refsynopsisdiv>
+    <cmdsynopsis>
+      <command>lxc-kill --name=<replaceable>NAME</replaceable> <replaceable>SIGNUM</replaceable></command>
+    </cmdsynopsis>
+  </refsynopsisdiv>
+
+  <refsect1>
+    <title>Description</title>
+
+    <para>
+      <command>lxc-kill</command> send
+      the <replaceable>SIGNUM</replaceable> signal to the first process of the container.
+    </para>
+
+    <para>
+       If this command is used on an application container ran by
+       lxc-execute, the lxc-init will receive the signal and will forward it to
+       the process 2 which is the command specified in the command line. See
+       lxc-execute (1).
+    </para>
+  </refsect1>
+
+  &commonoptions;
+
+  <refsect1>
+    <title>Examples</title>
+
+    <para>
+      To send the signal 26 to the process pi1 running in container
+      <literal>123</literal> :
+    </para>
+
+    <programlisting>
+      lxc-execute -n 123 -- pi1 -d 500000
+      lxc-kill --name=123 26
+    </programlisting>
+
+  </refsect1>
+
+  &copyrights;
+
+  &seealso;
+
+</refentry>
+
+<!-- Keep this comment at the end of the file
+Local variables:
+mode: sgml
+sgml-omittag:t
+sgml-shorttag:t
+sgml-minimize-attributes:nil
+sgml-always-quote-attributes:t
+sgml-indent-step:2
+sgml-indent-data:t
+sgml-parent-document:nil
+sgml-default-dtd-file:nil
+sgml-exposed-tags:nil
+sgml-local-catalogs:nil
+sgml-local-ecat-files:nil
+End:
+-->
index 93b9344b98ed30be7976dc5018b1d15b674e78cb..78b99b46bca8950aa8c928730eeb22d74a58cdc0 100644 (file)
@@ -57,6 +57,11 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
        <manvolnum>1</manvolnum>
       </citerefentry>,
 
+      <citerefentry>
+       <refentrytitle><command>lxc-kill</command></refentrytitle>
+       <manvolnum>1</manvolnum>
+      </citerefentry>,
+
       <citerefentry>
        <refentrytitle><command>lxc-console</command></refentrytitle>
        <manvolnum>1</manvolnum>
index 4c4783df1c607b438f78b87efbfb931635b97ab0..510bc9dcada6fc0097c3e66ca2d8d99d66a7c3d4 100644 (file)
@@ -82,7 +82,8 @@ bin_PROGRAMS = \
        lxc-cgroup \
        lxc-unfreeze \
        lxc-checkpoint \
-       lxc-restart
+       lxc-restart \
+       lxc-kill
 
 libexec_PROGRAMS = \
        lxc-init
@@ -105,6 +106,7 @@ lxc_stop_SOURCES = lxc_stop.c
 lxc_unfreeze_SOURCES = lxc_unfreeze.c
 lxc_unshare_SOURCES = lxc_unshare.c
 lxc_wait_SOURCES = lxc_wait.c
+lxc_kill_SOURCES = lxc_kill.c
 
 install-exec-local: install-soPROGRAMS
        mv $(DESTDIR)$(libdir)/liblxc.so $(DESTDIR)$(libdir)/liblxc.so.$(VERSION)
index b0b7ac7c9d01e186ad1574b44ca37b43453f27c2..a34818e5f13ecbe4952f39fe70ff3a30f3cceb12 100644 (file)
@@ -46,12 +46,23 @@ static struct option options[] = {
        { 0, 0, 0, 0 },
 };
 
+static int was_interrupted = 0;
+
 int main(int argc, char *argv[])
 {
+
+       void interrupt_handler(int sig)
+       {
+               if (!was_interrupted)
+                       was_interrupted = sig;
+       }
+
        pid_t pid;
        int nbargs = 0;
        int err = -1;
        char **aargv;
+       sigset_t mask, omask;
+       int i;
 
        while (1) {
                int ret = getopt_long_only(argc, argv, "", options, NULL);
@@ -75,6 +86,18 @@ int main(int argc, char *argv[])
        aargv = &argv[optind];
        argc -= nbargs;
 
+       sigfillset(&mask);
+       sigprocmask(SIG_SETMASK, &mask, &omask);
+
+       for (i = 1; i < NSIG; i++) {
+               struct sigaction act;
+
+               sigfillset(&act.sa_mask);
+               act.sa_flags = 0;
+               act.sa_handler = interrupt_handler;
+               sigaction(i, &act, NULL);
+       }
+
        pid = fork();
        
        if (pid < 0)
@@ -82,6 +105,10 @@ int main(int argc, char *argv[])
 
        if (!pid) {
                
+               for (i = 1; i < NSIG; i++)
+                       signal(i, SIG_DFL);
+               sigprocmask(SIG_SETMASK, &omask, NULL);
+
                if (lxc_setup_fs())
                        exit(err);
 
@@ -92,6 +119,8 @@ int main(int argc, char *argv[])
                exit(err);
        }
 
+       sigprocmask(SIG_SETMASK, &omask, NULL);
+
        /* no need of other inherited fds but stderr */
        close(fileno(stdin));
        close(fileno(stdout));
@@ -102,6 +131,11 @@ int main(int argc, char *argv[])
                int orphan = 0;
                pid_t waited_pid;
 
+               if (was_interrupted) {
+                       kill(pid, was_interrupted);
+                       was_interrupted = 0;
+               }
+
                waited_pid = wait(&status);
                if (waited_pid < 0) {
                        if (errno == ECHILD)
diff --git a/src/lxc/lxc_kill.c b/src/lxc/lxc_kill.c
new file mode 100644 (file)
index 0000000..3d996a5
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * lxc: linux Container library
+ *
+ * (C) Copyright IBM Corp. 2007, 2010
+ *
+ * Authors:
+ * Daniel Lezcano <dlezcano at fr.ibm.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <unistd.h>
+#include <errno.h>
+#include <sys/param.h>
+#include <stdlib.h>
+#include <signal.h>
+#include "commands.h"
+#include "arguments.h"
+#include "namespace.h"
+#include "log.h"
+
+lxc_log_define(lxc_kill_ui, lxc);
+
+static const struct option my_longopts[] = {
+       LXC_COMMON_OPTIONS
+};
+
+static struct lxc_arguments my_args = {
+       .progname = "lxc-kill",
+       .help     = "\
+--name=NAME SIGNUM\n\
+\n\
+Sends signal number SIGNUM to the first user process in container NAME\n\
+\n\
+Options :\n\
+  -n, --name=NAME     NAME for name of the container\n",
+       .options  = my_longopts,
+       .parser   = NULL,
+       .checker  = NULL,
+};
+
+int main(int argc, char *argv[], char *envp[])
+{
+       int ret;
+       pid_t pid;
+       int sig;
+
+       ret = lxc_arguments_parse(&my_args, argc, argv);
+       if (ret)
+               return ret;
+
+       ret = lxc_log_init(my_args.log_file, my_args.log_priority,
+                          my_args.progname, my_args.quiet);
+       if (ret)
+               return ret;
+
+       if (my_args.argc) {
+               sig = atoi(my_args.argv[0]);
+               if (!sig || sig >= NSIG) {
+                       ERROR("'%s' isn't a valid signal number",
+                             my_args.argv[0]);
+                       return -1;
+               }
+       } else
+               sig=SIGKILL;
+
+       pid = get_init_pid(my_args.name);
+       if (pid < 0) {
+               ERROR("failed to get the init pid");
+               return -1;
+       }
+
+       ret = kill(pid, sig);
+       if (ret < 0) {
+               ERROR("failed to kill the init pid");
+               return -1;
+       }
+
+       return 0;
+}