forked from containers/crun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkill.c
138 lines (112 loc) · 3.59 KB
/
kill.c
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* crun - OCI runtime written in C
*
* Copyright (C) 2017, 2018, 2019 Giuseppe Scrivano <[email protected]>
* crun is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* crun 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with crun. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <argp.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <regex.h>
#include "crun.h"
#include "libcrun/container.h"
#include "libcrun/status.h"
#include "libcrun/utils.h"
static char doc[] = "OCI runtime";
enum
{
OPTION_CONSOLE_SOCKET = 1000,
OPTION_PID_FILE,
OPTION_NO_SUBREAPER,
OPTION_NO_NEW_KEYRING,
OPTION_PRESERVE_FDS
};
struct kill_options_s
{
bool all;
bool regex;
};
static struct kill_options_s kill_options;
static struct argp_option options[]
= { { "all", 'a', 0, 0, "kill all the processes", 0 },
{ "regex", 'r', 0, 0, "the specified CONTAINER is a regular expression (kill multiple containers)", 0 },
{
0,
} };
static char args_doc[] = "kill CONTAINER [SIGNAL]";
static error_t
parse_opt (int key, char *arg arg_unused, struct argp_state *state arg_unused)
{
switch (key)
{
case 'a':
kill_options.all = true;
break;
case 'r':
kill_options.regex = true;
break;
case ARGP_KEY_NO_ARGS:
libcrun_fail_with_error (0, "please specify a ID for the container");
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp run_argp = { options, parse_opt, args_doc, doc, NULL, NULL, NULL };
int
crun_command_kill (struct crun_global_arguments *global_args, int argc, char **argv, libcrun_error_t *err)
{
int first_arg = 0, ret;
const char *signal;
libcrun_context_t crun_context = {
0,
};
argp_parse (&run_argp, argc, argv, ARGP_IN_ORDER, &first_arg, &kill_options);
crun_assert_n_args (argc - first_arg, 1, 2);
ret = init_libcrun_context (&crun_context, argv[first_arg], global_args, err);
if (UNLIKELY (ret < 0))
return ret;
signal = "SIGTERM";
if (argc - first_arg > 1)
signal = argv[first_arg + 1];
if (kill_options.regex)
{
regex_t re;
libcrun_container_list_t *list, *it;
ret = regcomp (&re, argv[first_arg], REG_EXTENDED | REG_NOSUB);
if (UNLIKELY (ret < 0))
libcrun_fail_with_error (0, "invalid regular expression %s", argv[first_arg]);
ret = libcrun_get_containers_list (&list, crun_context.state_root, err);
if (UNLIKELY (ret < 0))
libcrun_fail_with_error (0, "cannot read containers list");
for (it = list; it; it = it->next)
if (regexec (&re, it->name, 0, NULL, 0) == 0)
{
ret = libcrun_container_kill (&crun_context, it->name, signal, err);
if (UNLIKELY (ret < 0))
libcrun_error_write_warning_and_release (stderr, &err);
}
libcrun_free_containers_list (list);
regfree (&re);
return 0;
}
if (kill_options.all)
return libcrun_container_killall (&crun_context, argv[first_arg], signal, err);
return libcrun_container_kill (&crun_context, argv[first_arg], signal, err);
}