]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/pull.c
Merge pull request #3742 from msoltyspl/vconfix2
[thirdparty/systemd.git] / src / import / pull.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <getopt.h>
21
22 #include "sd-event.h"
23
24 #include "alloc-util.h"
25 #include "hostname-util.h"
26 #include "import-util.h"
27 #include "machine-image.h"
28 #include "parse-util.h"
29 #include "pull-raw.h"
30 #include "pull-tar.h"
31 #include "signal-util.h"
32 #include "string-util.h"
33 #include "verbs.h"
34 #include "web-util.h"
35
36 static bool arg_force = false;
37 static const char *arg_image_root = "/var/lib/machines";
38 static ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE;
39 static bool arg_settings = true;
40
41 static int interrupt_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
42 log_notice("Transfer aborted.");
43 sd_event_exit(sd_event_source_get_event(s), EINTR);
44 return 0;
45 }
46
47 static void on_tar_finished(TarPull *pull, int error, void *userdata) {
48 sd_event *event = userdata;
49 assert(pull);
50
51 if (error == 0)
52 log_info("Operation completed successfully.");
53
54 sd_event_exit(event, abs(error));
55 }
56
57 static int pull_tar(int argc, char *argv[], void *userdata) {
58 _cleanup_(tar_pull_unrefp) TarPull *pull = NULL;
59 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
60 const char *url, *local;
61 _cleanup_free_ char *l = NULL, *ll = NULL;
62 int r;
63
64 url = argv[1];
65 if (!http_url_is_valid(url)) {
66 log_error("URL '%s' is not valid.", url);
67 return -EINVAL;
68 }
69
70 if (argc >= 3)
71 local = argv[2];
72 else {
73 r = import_url_last_component(url, &l);
74 if (r < 0)
75 return log_error_errno(r, "Failed get final component of URL: %m");
76
77 local = l;
78 }
79
80 if (isempty(local) || streq(local, "-"))
81 local = NULL;
82
83 if (local) {
84 r = tar_strip_suffixes(local, &ll);
85 if (r < 0)
86 return log_oom();
87
88 local = ll;
89
90 if (!machine_name_is_valid(local)) {
91 log_error("Local image name '%s' is not valid.", local);
92 return -EINVAL;
93 }
94
95 if (!arg_force) {
96 r = image_find(local, NULL);
97 if (r < 0)
98 return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
99 else if (r > 0) {
100 log_error("Image '%s' already exists.", local);
101 return -EEXIST;
102 }
103 }
104
105 log_info("Pulling '%s', saving as '%s'.", url, local);
106 } else
107 log_info("Pulling '%s'.", url);
108
109 r = sd_event_default(&event);
110 if (r < 0)
111 return log_error_errno(r, "Failed to allocate event loop: %m");
112
113 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
114 (void) sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL);
115 (void) sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL);
116
117 r = tar_pull_new(&pull, event, arg_image_root, on_tar_finished, event);
118 if (r < 0)
119 return log_error_errno(r, "Failed to allocate puller: %m");
120
121 r = tar_pull_start(pull, url, local, arg_force, arg_verify, arg_settings);
122 if (r < 0)
123 return log_error_errno(r, "Failed to pull image: %m");
124
125 r = sd_event_loop(event);
126 if (r < 0)
127 return log_error_errno(r, "Failed to run event loop: %m");
128
129 log_info("Exiting.");
130 return -r;
131 }
132
133 static void on_raw_finished(RawPull *pull, int error, void *userdata) {
134 sd_event *event = userdata;
135 assert(pull);
136
137 if (error == 0)
138 log_info("Operation completed successfully.");
139
140 sd_event_exit(event, abs(error));
141 }
142
143 static int pull_raw(int argc, char *argv[], void *userdata) {
144 _cleanup_(raw_pull_unrefp) RawPull *pull = NULL;
145 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
146 const char *url, *local;
147 _cleanup_free_ char *l = NULL, *ll = NULL;
148 int r;
149
150 url = argv[1];
151 if (!http_url_is_valid(url)) {
152 log_error("URL '%s' is not valid.", url);
153 return -EINVAL;
154 }
155
156 if (argc >= 3)
157 local = argv[2];
158 else {
159 r = import_url_last_component(url, &l);
160 if (r < 0)
161 return log_error_errno(r, "Failed get final component of URL: %m");
162
163 local = l;
164 }
165
166 if (isempty(local) || streq(local, "-"))
167 local = NULL;
168
169 if (local) {
170 r = raw_strip_suffixes(local, &ll);
171 if (r < 0)
172 return log_oom();
173
174 local = ll;
175
176 if (!machine_name_is_valid(local)) {
177 log_error("Local image name '%s' is not valid.", local);
178 return -EINVAL;
179 }
180
181 if (!arg_force) {
182 r = image_find(local, NULL);
183 if (r < 0)
184 return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
185 else if (r > 0) {
186 log_error("Image '%s' already exists.", local);
187 return -EEXIST;
188 }
189 }
190
191 log_info("Pulling '%s', saving as '%s'.", url, local);
192 } else
193 log_info("Pulling '%s'.", url);
194
195 r = sd_event_default(&event);
196 if (r < 0)
197 return log_error_errno(r, "Failed to allocate event loop: %m");
198
199 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
200 (void) sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL);
201 (void) sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL);
202
203 r = raw_pull_new(&pull, event, arg_image_root, on_raw_finished, event);
204 if (r < 0)
205 return log_error_errno(r, "Failed to allocate puller: %m");
206
207 r = raw_pull_start(pull, url, local, arg_force, arg_verify, arg_settings);
208 if (r < 0)
209 return log_error_errno(r, "Failed to pull image: %m");
210
211 r = sd_event_loop(event);
212 if (r < 0)
213 return log_error_errno(r, "Failed to run event loop: %m");
214
215 log_info("Exiting.");
216 return -r;
217 }
218
219 static int help(int argc, char *argv[], void *userdata) {
220
221 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
222 "Download container or virtual machine images.\n\n"
223 " -h --help Show this help\n"
224 " --version Show package version\n"
225 " --force Force creation of image\n"
226 " --verify=MODE Verify downloaded image, one of: 'no',\n"
227 " 'checksum', 'signature'\n"
228 " --settings=BOOL Download settings file with image\n"
229 " --image-root=PATH Image root directory\n\n"
230 "Commands:\n"
231 " tar URL [NAME] Download a TAR image\n"
232 " raw URL [NAME] Download a RAW image\n",
233 program_invocation_short_name);
234
235 return 0;
236 }
237
238 static int parse_argv(int argc, char *argv[]) {
239
240 enum {
241 ARG_VERSION = 0x100,
242 ARG_FORCE,
243 ARG_IMAGE_ROOT,
244 ARG_VERIFY,
245 ARG_SETTINGS,
246 };
247
248 static const struct option options[] = {
249 { "help", no_argument, NULL, 'h' },
250 { "version", no_argument, NULL, ARG_VERSION },
251 { "force", no_argument, NULL, ARG_FORCE },
252 { "image-root", required_argument, NULL, ARG_IMAGE_ROOT },
253 { "verify", required_argument, NULL, ARG_VERIFY },
254 { "settings", required_argument, NULL, ARG_SETTINGS },
255 {}
256 };
257
258 int c, r;
259
260 assert(argc >= 0);
261 assert(argv);
262
263 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
264
265 switch (c) {
266
267 case 'h':
268 return help(0, NULL, NULL);
269
270 case ARG_VERSION:
271 return version();
272
273 case ARG_FORCE:
274 arg_force = true;
275 break;
276
277 case ARG_IMAGE_ROOT:
278 arg_image_root = optarg;
279 break;
280
281 case ARG_VERIFY:
282 arg_verify = import_verify_from_string(optarg);
283 if (arg_verify < 0) {
284 log_error("Invalid verification setting '%s'", optarg);
285 return -EINVAL;
286 }
287
288 break;
289
290 case ARG_SETTINGS:
291 r = parse_boolean(optarg);
292 if (r < 0)
293 return log_error_errno(r, "Failed to parse --settings= parameter '%s'", optarg);
294
295 arg_settings = r;
296 break;
297
298 case '?':
299 return -EINVAL;
300
301 default:
302 assert_not_reached("Unhandled option");
303 }
304
305 return 1;
306 }
307
308 static int pull_main(int argc, char *argv[]) {
309
310 static const Verb verbs[] = {
311 { "help", VERB_ANY, VERB_ANY, 0, help },
312 { "tar", 2, 3, 0, pull_tar },
313 { "raw", 2, 3, 0, pull_raw },
314 {}
315 };
316
317 return dispatch_verb(argc, argv, verbs, NULL);
318 }
319
320 int main(int argc, char *argv[]) {
321 int r;
322
323 setlocale(LC_ALL, "");
324 log_parse_environment();
325 log_open();
326
327 r = parse_argv(argc, argv);
328 if (r <= 0)
329 goto finish;
330
331 (void) ignore_signals(SIGPIPE, -1);
332
333 r = pull_main(argc, argv);
334
335 finish:
336 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
337 }