Add a kill command

This sends SIGKILL to a running sandbox process.
The command has completion for running applications,
for both application IDs and instance IDs.

Closes: #2180

Closes: #2181
Approved by: alexlarsson
This commit is contained in:
Matthias Clasen
2018-10-03 22:55:41 -04:00
committed by Atomic Bot
parent 02506d0eab
commit 9c84b88e46
8 changed files with 252 additions and 0 deletions

View File

@@ -61,6 +61,7 @@ flatpak_SOURCES = \
app/flatpak-builtins-search.c \
app/flatpak-builtins-repair.c \
app/flatpak-builtins-create-usb.c \
app/flatpak-builtins-kill.c \
app/flatpak-table-printer.c \
app/flatpak-table-printer.h \
app/flatpak-complete.c \

137
app/flatpak-builtins-kill.c Normal file
View File

@@ -0,0 +1,137 @@
/*
* Copyright © 2018 Red Hat, Inc
*
* This program 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, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Matthias Clasen <mclasen@redhat.com>
*/
#include "config.h"
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <glib/gi18n.h>
#include "libglnx/libglnx.h"
#include "flatpak-builtins.h"
#include "flatpak-instance.h"
static GOptionEntry options[] = {
{ NULL }
};
static gboolean
kill_instance (const char *id,
GError **error)
{
g_autoptr(GPtrArray) instances = NULL;
int j;
int killed = 0;
instances = flatpak_instance_get_all ();
for (j = 0; j < instances->len; j++)
{
FlatpakInstance *instance = (FlatpakInstance *)g_ptr_array_index (instances, j);
if (strcmp (id, flatpak_instance_get_app (instance)) == 0 ||
strcmp (id, flatpak_instance_get_id (instance)) == 0)
{
pid_t pid = flatpak_instance_get_child_pid (instance);
kill (pid, SIGKILL);
killed++;
}
}
g_debug ("Killed %d instances", killed);
if (killed == 0)
return flatpak_fail (error, _("%s is not running."), id);
return TRUE;
}
gboolean
flatpak_builtin_kill (int argc,
char **argv,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GOptionContext) context = NULL;
const char *instance;
context = g_option_context_new (_("INSTANCE - Stop a running application"));
g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
if (!flatpak_option_context_parse (context, options, &argc, &argv, FLATPAK_BUILTIN_FLAG_NO_DIR, NULL, cancellable, error))
return FALSE;
if (argc > 2)
{
usage_error (context, _("Extra arguments given"), error);
return FALSE;
}
if (argc < 2)
{
usage_error (context, _("Must specify the app to kill"), error);
return FALSE;
}
instance = argv[1];
return kill_instance (instance, error);
}
gboolean
flatpak_complete_kill (FlatpakCompletion *completion)
{
g_autoptr(GOptionContext) context = NULL;
g_autoptr(GPtrArray) instances = NULL;
int i;
context = g_option_context_new ("");
if (!flatpak_option_context_parse (context, options, &completion->argc, &completion->argv, FLATPAK_BUILTIN_FLAG_NO_DIR, NULL, NULL, NULL))
return FALSE;
switch (completion->argc)
{
case 0:
case 1: /* NAME */
flatpak_complete_options (completion, global_entries);
flatpak_complete_options (completion, options);
instances = flatpak_instance_get_all ();
for (i = 0; i < instances->len; i++)
{
FlatpakInstance *instance = (FlatpakInstance *)g_ptr_array_index (instances, i);
flatpak_complete_word (completion, "%s ", flatpak_instance_get_app (instance));
flatpak_complete_word (completion, "%s ", flatpak_instance_get_id (instance));
}
break;
default:
break;
}
return TRUE;
}

View File

@@ -98,6 +98,7 @@ BUILTINPROTO (config)
BUILTINPROTO (search)
BUILTINPROTO (repair)
BUILTINPROTO (create_usb)
BUILTINPROTO (kill)
#undef BUILTINPROTO

View File

@@ -82,6 +82,7 @@ static FlatpakCommand commands[] = {
{ "make-current", N_("Specify default version to run"), flatpak_builtin_make_current_app, flatpak_complete_make_current_app },
{ "enter", N_("Enter the namespace of a running application"), flatpak_builtin_enter, flatpak_complete_enter },
{ "ps", N_("Enumerate running applications"), flatpak_builtin_ps, flatpak_complete_ps },
{ "kill", N_("Stop a running application"), flatpak_builtin_kill, flatpak_complete_kill },
/* translators: please keep the leading newline and space */
{ N_("\n Manage file access") },

View File

@@ -57,6 +57,7 @@ man1 = \
flatpak-search.1 \
flatpak-create-usb.1 \
flatpak-repair.1 \
flatpak-kill.1 \
$(NULL)
man5 = \

View File

@@ -39,6 +39,7 @@
<xi:include href="@srcdir@/flatpak-document-info.xml"/>
<xi:include href="@srcdir@/flatpak-document-list.xml"/>
<xi:include href="@srcdir@/flatpak-document-unexport.xml"/>
<xi:include href="@srcdir@/flatpak-kill.xml"/>
<xi:include href="@srcdir@/flatpak-permission-remove.xml"/>
<xi:include href="@srcdir@/flatpak-permission-list.xml"/>
<xi:include href="@srcdir@/flatpak-permission-show.xml"/>

103
doc/flatpak-kill.xml Normal file
View File

@@ -0,0 +1,103 @@
<?xml version='1.0'?> <!--*-nxml-*-->
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<refentry id="flatpak-kill">
<refentryinfo>
<title>flatpak kill</title>
<productname>flatpak</productname>
<authorgroup>
<author>
<contrib>Developer</contrib>
<firstname>Matthias</firstname>
<surname>Clasen</surname>
<email>mclasen@redhat.com</email>
</author>
</authorgroup>
</refentryinfo>
<refmeta>
<refentrytitle>flatpak kill</refentrytitle>
<manvolnum>1</manvolnum>
</refmeta>
<refnamediv>
<refname>flatpak-kill</refname>
<refpurpose>Stop a running application</refpurpose>
</refnamediv>
<refsynopsisdiv>
<cmdsynopsis>
<command>flatpak kill</command>
<arg choice="plain">INSTANCE</arg>
</cmdsynopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para>
Kill a running sandbox. <replaceable>INSTANCE</replaceable> can be
either the numeric instance ID or the application ID of a running
Flatpak.
</para>
</refsect1>
<refsect1>
<title>Options</title>
<para>The following options are understood:</para>
<variablelist>
<varlistentry>
<term><option>-h</option></term>
<term><option>--help</option></term>
<listitem><para>
Show help options and exit.
</para></listitem>
</varlistentry>
<varlistentry>
<term><option>-v</option></term>
<term><option>--verbose</option></term>
<listitem><para>
Print debug information during command processing.
</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--ostree-verbose</option></term>
<listitem><para>
Print OSTree debug information during command processing.
</para></listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1>
<title>Examples</title>
<para>
<command>$ flatpak kill org.gnome.Todo</command>
</para>
</refsect1>
<refsect1>
<title>See also</title>
<para>
<citerefentry><refentrytitle>flatpak</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>flatpak-run</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>flatpak-ps</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</para>
</refsect1>
</refentry>

View File

@@ -237,6 +237,13 @@
Run an application.
</para></listitem>
</varlistentry>
<varlistentry>
<term><citerefentry><refentrytitle>flatpak-kill</refentrytitle><manvolnum>1</manvolnum></citerefentry></term>
<listitem><para>
Stop a running application.
</para></listitem>
</varlistentry>
<varlistentry>
<term><citerefentry><refentrytitle>flatpak-override</refentrytitle><manvolnum>1</manvolnum></citerefentry></term>