1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2015 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/prctl.h>
23 #include <curl/curl.h>
25 #include "sd-daemon.h"
29 #include "btrfs-util.h"
33 #include "import-util.h"
34 #include "curl-util.h"
35 #include "import-job.h"
36 #include "import-common.h"
37 #include "import-tar.h"
39 typedef enum TarProgress
{
53 ImportJob
*checksum_job
;
54 ImportJob
*signature_job
;
56 TarImportFinished on_finished
;
70 TarImport
* tar_import_unref(TarImport
*i
) {
75 (void) kill_and_sigcont(i
->tar_pid
, SIGKILL
);
76 (void) wait_for_terminate(i
->tar_pid
, NULL
);
79 import_job_unref(i
->tar_job
);
80 import_job_unref(i
->checksum_job
);
81 import_job_unref(i
->signature_job
);
83 curl_glue_unref(i
->glue
);
84 sd_event_unref(i
->event
);
87 (void) btrfs_subvol_remove(i
->temp_path
);
88 (void) rm_rf_dangerous(i
->temp_path
, false, true, false);
103 const char *image_root
,
104 TarImportFinished on_finished
,
107 _cleanup_(tar_import_unrefp
) TarImport
*i
= NULL
;
113 i
= new0(TarImport
, 1);
117 i
->on_finished
= on_finished
;
118 i
->userdata
= userdata
;
120 i
->image_root
= strdup(image_root
?: "/var/lib/machines");
125 i
->event
= sd_event_ref(event
);
127 r
= sd_event_default(&i
->event
);
132 r
= curl_glue_new(&i
->glue
, i
->event
);
136 i
->glue
->on_finished
= import_job_curl_on_finished
;
137 i
->glue
->userdata
= i
;
145 static void tar_import_report_progress(TarImport
*i
, TarProgress p
) {
152 case TAR_DOWNLOADING
: {
153 unsigned remain
= 85;
157 if (i
->checksum_job
) {
158 percent
+= i
->checksum_job
->progress_percent
* 5 / 100;
162 if (i
->signature_job
) {
163 percent
+= i
->signature_job
->progress_percent
* 5 / 100;
168 percent
+= i
->tar_job
->progress_percent
* remain
/ 100;
185 assert_not_reached("Unknown progress state");
188 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
189 log_debug("Combined progress %u%%", percent
);
192 static int tar_import_make_local_copy(TarImport
*i
) {
201 if (!i
->final_path
) {
202 r
= import_make_path(i
->tar_job
->url
, i
->tar_job
->etag
, i
->image_root
, ".tar-", NULL
, &i
->final_path
);
207 r
= import_make_local_copy(i
->final_path
, i
->image_root
, i
->local
, i
->force_local
);
214 static bool tar_import_is_done(TarImport
*i
) {
218 if (i
->tar_job
->state
!= IMPORT_JOB_DONE
)
220 if (i
->checksum_job
&& i
->checksum_job
->state
!= IMPORT_JOB_DONE
)
222 if (i
->signature_job
&& i
->signature_job
->state
!= IMPORT_JOB_DONE
)
228 static void tar_import_job_on_finished(ImportJob
*j
) {
237 if (j
== i
->checksum_job
)
238 log_error_errno(j
->error
, "Failed to retrieve SHA256 checksum, cannot verify. (Try --verify=no?)");
239 else if (j
== i
->signature_job
)
240 log_error_errno(j
->error
, "Failed to retrieve signature file, cannot verify. (Try --verify=no?)");
242 log_error_errno(j
->error
, "Failed to retrieve image file. (Wrong URL?)");
248 /* This is invoked if either the download completed
249 * successfully, or the download was skipped because we
250 * already have the etag. */
252 if (!tar_import_is_done(i
))
255 j
->disk_fd
= safe_close(i
->tar_job
->disk_fd
);
257 if (i
->tar_pid
> 0) {
258 r
= wait_for_terminate_and_warn("tar", i
->tar_pid
, true);
264 if (!i
->tar_job
->etag_exists
) {
265 /* This is a new download, verify it, and move it into place */
267 tar_import_report_progress(i
, TAR_VERIFYING
);
269 r
= import_verify(i
->tar_job
, i
->checksum_job
, i
->signature_job
);
273 tar_import_report_progress(i
, TAR_FINALIZING
);
275 r
= import_make_read_only(i
->temp_path
);
279 if (rename(i
->temp_path
, i
->final_path
) < 0) {
280 r
= log_error_errno(errno
, "Failed to rename to final image name: %m");
288 tar_import_report_progress(i
, TAR_COPYING
);
290 r
= tar_import_make_local_copy(i
);
298 i
->on_finished(i
, r
, i
->userdata
);
300 sd_event_exit(i
->event
, r
);
303 static int tar_import_job_on_open_disk(ImportJob
*j
) {
304 _cleanup_close_pair_
int pipefd
[2] = { -1 , -1 };
312 assert(i
->tar_job
== j
);
313 assert(!i
->final_path
);
314 assert(!i
->temp_path
);
315 assert(i
->tar_pid
<= 0);
317 r
= import_make_path(j
->url
, j
->etag
, i
->image_root
, ".tar-", NULL
, &i
->final_path
);
321 r
= tempfn_random(i
->final_path
, &i
->temp_path
);
325 mkdir_parents_label(i
->temp_path
, 0700);
327 r
= btrfs_subvol_make(i
->temp_path
);
329 if (mkdir(i
->temp_path
, 0755) < 0)
330 return log_error_errno(errno
, "Failed to create directory %s: %m", i
->temp_path
);
332 return log_error_errno(errno
, "Failed to create subvolume %s: %m", i
->temp_path
);
334 j
->disk_fd
= import_fork_tar(i
->temp_path
, &i
->tar_pid
);
341 static void tar_import_job_on_progress(ImportJob
*j
) {
349 tar_import_report_progress(i
, TAR_DOWNLOADING
);
352 int tar_import_pull(TarImport
*i
, const char *url
, const char *local
, bool force_local
, ImportVerify verify
) {
357 if (!http_url_is_valid(url
))
360 if (local
&& !machine_name_is_valid(local
))
366 r
= free_and_strdup(&i
->local
, local
);
369 i
->force_local
= force_local
;
372 r
= import_job_new(&i
->tar_job
, url
, i
->glue
, i
);
376 i
->tar_job
->on_finished
= tar_import_job_on_finished
;
377 i
->tar_job
->on_open_disk
= tar_import_job_on_open_disk
;
378 i
->tar_job
->on_progress
= tar_import_job_on_progress
;
379 i
->tar_job
->calc_checksum
= verify
!= IMPORT_VERIFY_NO
;
381 r
= import_find_old_etags(url
, i
->image_root
, DT_DIR
, ".tar-", NULL
, &i
->tar_job
->old_etags
);
385 r
= import_make_verification_jobs(&i
->checksum_job
, &i
->signature_job
, verify
, url
, i
->glue
, tar_import_job_on_finished
, i
);
389 r
= import_job_begin(i
->tar_job
);
393 if (i
->checksum_job
) {
394 i
->checksum_job
->on_progress
= tar_import_job_on_progress
;
396 r
= import_job_begin(i
->checksum_job
);
401 if (i
->signature_job
) {
402 i
->signature_job
->on_progress
= tar_import_job_on_progress
;
404 r
= import_job_begin(i
->signature_job
);