~ci-train-bot/ubuntu-app-launch/ubuntu-app-launch-ubuntu-zesty-2405

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * Copyright © 2014 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *     Ted Gould <ted.gould@canonical.com>
 */

#include "helpers.h"

int kill (pid_t pid, int signal);
pid_t getpgid (pid_t);

int
main (int argc, char * argv[])
{
	/* Break off a new process group */
	setpgid(0, 0);

	GDBusConnection * cgmanager = cgroup_manager_connection();
	g_return_val_if_fail(cgmanager != NULL, -1);

	GPid selfpid = getpid();
	GPid parentpid = getppid();

	/* We're gonna try to kill things forever, literally. It's important
	   enough that we can't consider failure an option. */
	gboolean killed = TRUE;
	while (killed) {
		GList * pidlist = pids_from_cgroup(cgmanager, NULL, NULL);
		GList * head;

		killed = FALSE;

		for (head = pidlist; head != NULL; head = g_list_next(head)) {
			GPid pid = GPOINTER_TO_INT(head->data);

			/* We don't want to kill ourselves, or if we're being executed by
			   a script, that script, either. We also don't want things in our
			   process group which we forked at the opening */
			if (pid != selfpid && pid != parentpid && getpgid(pid) != selfpid) {
				g_debug("Killing pid: %d", pid);
				kill(pid, SIGKILL);
				killed = TRUE;
			}
		}

		g_list_free(pidlist);
	}

	cgroup_manager_unref(cgmanager);

	return 0;
}