-
Notifications
You must be signed in to change notification settings - Fork 322
/
run.c
185 lines (151 loc) · 5.44 KB
/
run.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* crun - OCI runtime written in C
*
* Copyright (C) 2017, 2018, 2019, 2020 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 "crun.h"
#include "libcrun/container.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,
OPTION_NO_PIVOT,
OPTION_KEEP,
};
static const char *bundle = NULL;
static bool keep = false;
static libcrun_context_t crun_context;
static struct argp_option options[]
= { { "bundle", 'b', "DIR", 0, "container bundle (default \".\")", 0 },
{ "config", 'f', "FILE", 0, "override the config file name", 0 },
{ "detach", 'd', 0, 0, "detach from the parent", 0 },
{ "console-socket", OPTION_CONSOLE_SOCKET, "SOCKET", 0,
"path to a socket that will receive the ptmx end of the tty", 0 },
{ "preserve-fds", OPTION_PRESERVE_FDS, "N", 0, "pass additional FDs to the container", 0 },
{ "pid-file", OPTION_PID_FILE, "FILE", 0, "where to write the PID of the container", 0 },
{ "keep", OPTION_KEEP, 0, 0, "do not delete the container after it exits", 0 },
{ "no-subreaper", OPTION_NO_SUBREAPER, 0, 0, "do not create a subreaper process (ignored)", 0 },
{ "no-new-keyring", OPTION_NO_NEW_KEYRING, 0, 0, "keep the same session key", 0 },
{ "no-pivot", OPTION_NO_PIVOT, 0, 0, "do not use pivot_root", 0 },
{
0,
} };
static char args_doc[] = "run [OPTION]... CONTAINER";
static const char *config_file = "config.json";
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case 'd':
crun_context.detach = true;
break;
case 'f':
config_file = argp_mandatory_argument (arg, state);
break;
case 'b':
bundle = crun_context.bundle = argp_mandatory_argument (arg, state);
break;
case OPTION_KEEP:
keep = true;
break;
case OPTION_CONSOLE_SOCKET:
crun_context.console_socket = argp_mandatory_argument (arg, state);
break;
case OPTION_PRESERVE_FDS:
crun_context.preserve_fds = parse_int_or_fail (argp_mandatory_argument (arg, state), "preserve-fds");
break;
case OPTION_NO_SUBREAPER:
break;
case OPTION_NO_NEW_KEYRING:
crun_context.no_new_keyring = true;
break;
case OPTION_PID_FILE:
crun_context.pid_file = argp_mandatory_argument (arg, state);
break;
case OPTION_NO_PIVOT:
crun_context.no_pivot = 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_run (struct crun_global_arguments *global_args, int argc, char **argv, libcrun_error_t *err)
{
int first_arg = 0, ret;
cleanup_container libcrun_container_t *container = NULL;
cleanup_free char *bundle_cleanup = NULL;
cleanup_free char *config_file_cleanup = NULL;
crun_context.preserve_fds = 0;
crun_context.listen_fds = 0;
argp_parse (&run_argp, argc, argv, ARGP_IN_ORDER, &first_arg, &crun_context);
crun_assert_n_args (argc - first_arg, 1, 1);
/* Make sure the config is an absolute path before changing the directory. */
if ((strcmp ("config.json", config_file) != 0))
{
if (config_file[0] != '/')
{
config_file_cleanup = realpath (config_file, NULL);
if (config_file_cleanup == NULL)
libcrun_fail_with_error (errno, "realpath `%s` failed", config_file);
config_file = config_file_cleanup;
}
}
/* Make sure the bundle is an absolute path. */
if (bundle == NULL)
bundle = bundle_cleanup = getcwd (NULL, 0);
else
{
if (bundle[0] != '/')
{
bundle_cleanup = realpath (bundle, NULL);
if (bundle_cleanup == NULL)
libcrun_fail_with_error (errno, "realpath `%s` failed", bundle);
bundle = bundle_cleanup;
}
if (chdir (bundle) < 0)
libcrun_fail_with_error (errno, "chdir `%s` failed", bundle);
}
container = libcrun_container_load_from_file (config_file, err);
if (container == NULL)
return -1;
ret = init_libcrun_context (&crun_context, argv[first_arg], global_args, err);
if (UNLIKELY (ret < 0))
return ret;
crun_context.bundle = bundle;
if (getenv ("LISTEN_FDS"))
{
crun_context.listen_fds = parse_int_or_fail (getenv ("LISTEN_FDS"), "LISTEN_FDS");
crun_context.preserve_fds += crun_context.listen_fds;
}
return libcrun_container_run (&crun_context, container, keep ? LIBCRUN_RUN_OPTIONS_KEEP : 0, err);
}