importd: port to PidRef
This commit is contained in:
committed by
Yu Watanabe
parent
93dea63fab
commit
625c2e10c6
@@ -12,6 +12,7 @@
|
||||
#include "format-util.h"
|
||||
#include "import-common.h"
|
||||
#include "log.h"
|
||||
#include "pidref.h"
|
||||
#include "pretty-print.h"
|
||||
#include "process-util.h"
|
||||
#include "ratelimit.h"
|
||||
@@ -45,7 +46,7 @@ typedef struct TarExport {
|
||||
uint64_t written_compressed;
|
||||
uint64_t written_uncompressed;
|
||||
|
||||
pid_t tar_pid;
|
||||
PidRef tar_pid;
|
||||
|
||||
struct stat st;
|
||||
uint64_t quota_referenced;
|
||||
@@ -63,8 +64,7 @@ TarExport *tar_export_unref(TarExport *e) {
|
||||
|
||||
sd_event_source_unref(e->output_event_source);
|
||||
|
||||
if (e->tar_pid > 1)
|
||||
sigkill_wait(e->tar_pid);
|
||||
pidref_done_sigkill_wait(&e->tar_pid);
|
||||
|
||||
if (e->temp_path) {
|
||||
(void) btrfs_subvol_remove(e->temp_path, BTRFS_REMOVE_QUOTA);
|
||||
@@ -105,6 +105,7 @@ int tar_export_new(
|
||||
.quota_referenced = UINT64_MAX,
|
||||
.last_percent = UINT_MAX,
|
||||
.progress_ratelimit = { 100 * USEC_PER_MSEC, 1 },
|
||||
.tar_pid = PIDREF_NULL,
|
||||
};
|
||||
|
||||
if (event)
|
||||
@@ -160,10 +161,13 @@ static int tar_export_finish(TarExport *e) {
|
||||
assert(e);
|
||||
assert(e->tar_fd >= 0);
|
||||
|
||||
if (e->tar_pid > 0) {
|
||||
r = wait_for_terminate_and_check("tar", TAKE_PID(e->tar_pid), WAIT_LOG);
|
||||
if (pidref_is_set(&e->tar_pid)) {
|
||||
r = pidref_wait_for_terminate_and_check("tar", &e->tar_pid, WAIT_LOG);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
pidref_done(&e->tar_pid);
|
||||
|
||||
if (r != EXIT_SUCCESS)
|
||||
return -EPROTO;
|
||||
}
|
||||
|
||||
+19
-14
@@ -15,15 +15,16 @@
|
||||
#include "import-common.h"
|
||||
#include "log.h"
|
||||
#include "os-util.h"
|
||||
#include "pidref.h"
|
||||
#include "process-util.h"
|
||||
#include "selinux-util.h"
|
||||
#include "stat-util.h"
|
||||
#include "tmpfile-util.h"
|
||||
|
||||
int import_fork_tar_x(const char *path, pid_t *ret) {
|
||||
int import_fork_tar_x(const char *path, PidRef *ret) {
|
||||
_cleanup_(pidref_done) PidRef pid = PIDREF_NULL;
|
||||
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
|
||||
bool use_selinux;
|
||||
pid_t pid;
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
@@ -34,10 +35,12 @@ int import_fork_tar_x(const char *path, pid_t *ret) {
|
||||
|
||||
use_selinux = mac_selinux_use();
|
||||
|
||||
r = safe_fork_full("(tar)",
|
||||
(int[]) { pipefd[0], -EBADF, STDERR_FILENO },
|
||||
NULL, 0,
|
||||
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REARRANGE_STDIO|FORK_LOG, &pid);
|
||||
r = pidref_safe_fork_full(
|
||||
"(tar)",
|
||||
(int[]) { pipefd[0], -EBADF, STDERR_FILENO },
|
||||
NULL, 0,
|
||||
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REARRANGE_STDIO|FORK_LOG,
|
||||
&pid);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0) {
|
||||
@@ -84,15 +87,15 @@ int import_fork_tar_x(const char *path, pid_t *ret) {
|
||||
_exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
*ret = pid;
|
||||
*ret = TAKE_PIDREF(pid);
|
||||
|
||||
return TAKE_FD(pipefd[1]);
|
||||
}
|
||||
|
||||
int import_fork_tar_c(const char *path, pid_t *ret) {
|
||||
int import_fork_tar_c(const char *path, PidRef *ret) {
|
||||
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
|
||||
_cleanup_(pidref_done) PidRef pid = PIDREF_NULL;
|
||||
bool use_selinux;
|
||||
pid_t pid;
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
@@ -103,10 +106,12 @@ int import_fork_tar_c(const char *path, pid_t *ret) {
|
||||
|
||||
use_selinux = mac_selinux_use();
|
||||
|
||||
r = safe_fork_full("(tar)",
|
||||
(int[]) { -EBADF, pipefd[1], STDERR_FILENO },
|
||||
NULL, 0,
|
||||
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REARRANGE_STDIO|FORK_LOG, &pid);
|
||||
r = pidref_safe_fork_full(
|
||||
"(tar)",
|
||||
(int[]) { -EBADF, pipefd[1], STDERR_FILENO },
|
||||
NULL, 0,
|
||||
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REARRANGE_STDIO|FORK_LOG,
|
||||
&pid);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0) {
|
||||
@@ -139,7 +144,7 @@ int import_fork_tar_c(const char *path, pid_t *ret) {
|
||||
_exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
*ret = pid;
|
||||
*ret = TAKE_PIDREF(pid);
|
||||
|
||||
return TAKE_FD(pipefd[0]);
|
||||
}
|
||||
|
||||
@@ -33,8 +33,8 @@ typedef enum ImportFlags {
|
||||
_IMPORT_FLAGS_INVALID = -EINVAL,
|
||||
} ImportFlags;
|
||||
|
||||
int import_fork_tar_c(const char *path, pid_t *ret);
|
||||
int import_fork_tar_x(const char *path, pid_t *ret);
|
||||
int import_fork_tar_c(const char *path, PidRef *ret);
|
||||
int import_fork_tar_x(const char *path, PidRef *ret);
|
||||
|
||||
int import_mangle_os_tree(const char *path);
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "log.h"
|
||||
#include "mkdir-label.h"
|
||||
#include "path-util.h"
|
||||
#include "pidref.h"
|
||||
#include "pretty-print.h"
|
||||
#include "process-util.h"
|
||||
#include "ratelimit.h"
|
||||
@@ -57,7 +58,7 @@ typedef struct TarImport {
|
||||
|
||||
struct stat input_stat;
|
||||
|
||||
pid_t tar_pid;
|
||||
PidRef tar_pid;
|
||||
|
||||
unsigned last_percent;
|
||||
RateLimit progress_ratelimit;
|
||||
@@ -69,8 +70,7 @@ TarImport* tar_import_unref(TarImport *i) {
|
||||
|
||||
sd_event_source_unref(i->input_event_source);
|
||||
|
||||
if (i->tar_pid > 1)
|
||||
sigkill_wait(i->tar_pid);
|
||||
pidref_done_sigkill_wait(&i->tar_pid);
|
||||
|
||||
rm_rf_subvolume_and_free(i->temp_path);
|
||||
|
||||
@@ -116,6 +116,7 @@ int tar_import_new(
|
||||
.last_percent = UINT_MAX,
|
||||
.image_root = TAKE_PTR(root),
|
||||
.progress_ratelimit = { 100 * USEC_PER_MSEC, 1 },
|
||||
.tar_pid = PIDREF_NULL,
|
||||
};
|
||||
|
||||
if (event)
|
||||
@@ -174,10 +175,13 @@ static int tar_import_finish(TarImport *i) {
|
||||
|
||||
i->tar_fd = safe_close(i->tar_fd);
|
||||
|
||||
if (i->tar_pid > 0) {
|
||||
r = wait_for_terminate_and_check("tar", TAKE_PID(i->tar_pid), WAIT_LOG);
|
||||
if (pidref_is_set(&i->tar_pid)) {
|
||||
r = pidref_wait_for_terminate_and_check("tar", &i->tar_pid, WAIT_LOG);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
pidref_done(&i->tar_pid);
|
||||
|
||||
if (r != EXIT_SUCCESS)
|
||||
return -EPROTO;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "log.h"
|
||||
#include "mkdir-label.h"
|
||||
#include "path-util.h"
|
||||
#include "pidref.h"
|
||||
#include "process-util.h"
|
||||
#include "pull-common.h"
|
||||
#include "pull-job.h"
|
||||
@@ -51,7 +52,7 @@ typedef struct TarPull {
|
||||
|
||||
char *local;
|
||||
|
||||
pid_t tar_pid;
|
||||
PidRef tar_pid;
|
||||
|
||||
char *final_path;
|
||||
char *temp_path;
|
||||
@@ -66,8 +67,7 @@ TarPull* tar_pull_unref(TarPull *i) {
|
||||
if (!i)
|
||||
return NULL;
|
||||
|
||||
if (i->tar_pid > 1)
|
||||
sigkill_wait(i->tar_pid);
|
||||
pidref_done_sigkill_wait(&i->tar_pid);
|
||||
|
||||
pull_job_unref(i->tar_job);
|
||||
pull_job_unref(i->checksum_job);
|
||||
@@ -131,6 +131,7 @@ int tar_pull_new(
|
||||
.image_root = TAKE_PTR(root),
|
||||
.event = TAKE_PTR(e),
|
||||
.glue = TAKE_PTR(g),
|
||||
.tar_pid = PIDREF_NULL,
|
||||
};
|
||||
|
||||
i->glue->on_finished = pull_job_curl_on_finished;
|
||||
@@ -377,10 +378,11 @@ static void tar_pull_job_on_finished(PullJob *j) {
|
||||
pull_job_close_disk_fd(i->tar_job);
|
||||
pull_job_close_disk_fd(i->settings_job);
|
||||
|
||||
if (i->tar_pid > 0) {
|
||||
r = wait_for_terminate_and_check("tar", TAKE_PID(i->tar_pid), WAIT_LOG);
|
||||
if (pidref_is_set(&i->tar_pid)) {
|
||||
r = pidref_wait_for_terminate_and_check("tar", &i->tar_pid, WAIT_LOG);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
pidref_done(&i->tar_pid);
|
||||
if (r != EXIT_SUCCESS) {
|
||||
r = -EIO;
|
||||
goto finish;
|
||||
@@ -509,7 +511,7 @@ static int tar_pull_job_on_open_disk_tar(PullJob *j) {
|
||||
|
||||
i = j->userdata;
|
||||
assert(i->tar_job == j);
|
||||
assert(i->tar_pid <= 0);
|
||||
assert(!pidref_is_set(&i->tar_pid));
|
||||
|
||||
if (i->flags & IMPORT_DIRECT)
|
||||
where = i->local;
|
||||
|
||||
Reference in New Issue
Block a user