[lxc-devel] [lxc/master] storage: exit() => _exit()
2xsec on Github
lxc-bot at linuxcontainers.org
Mon Aug 20 01:46:35 UTC 2018
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 483 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180820/c0a0867a/attachment.bin>
-------------- next part --------------
From 2c70300ad8a77c560c615c676912ff2d0e613393 Mon Sep 17 00:00:00 2001
From: 2xsec <dh48.jeong at samsung.com>
Date: Mon, 20 Aug 2018 10:41:50 +0900
Subject: [PATCH 1/2] storage: exit() => _exit(). when exec is failed, child
process needs to use _exit()
Signed-off-by: 2xsec <dh48.jeong at samsung.com>
---
src/lxc/storage/nbd.c | 29 ++++++++++++++++++++++++-----
1 file changed, 24 insertions(+), 5 deletions(-)
diff --git a/src/lxc/storage/nbd.c b/src/lxc/storage/nbd.c
index 771bd9e3e..4c3ff51ef 100644
--- a/src/lxc/storage/nbd.c
+++ b/src/lxc/storage/nbd.c
@@ -48,8 +48,8 @@ struct nbd_attach_data {
const char *path;
};
-static bool clone_attach_nbd(const char *nbd, const char *path);
static int do_attach_nbd(void *d);
+static bool clone_attach_nbd(const char *nbd, const char *path);
static bool nbd_busy(int idx);
static void nbd_detach(const char *path);
static int nbd_get_partition(const char *src);
@@ -69,16 +69,21 @@ bool attach_nbd(char *src, struct lxc_conf *conf)
p = strchr(orig, ':');
if (p)
*p = '\0';
+
while (1) {
sprintf(path, "/dev/nbd%d", i);
+
if (!file_exists(path))
return false;
+
if (nbd_busy(i)) {
i++;
continue;
}
+
if (!clone_attach_nbd(path, orig))
return false;
+
conf->nbd_idx = i;
return true;
}
@@ -152,10 +157,10 @@ int nbd_mount(struct lxc_storage *bdev)
}
/* It might take awhile for the partition files to show up */
- if (partition) {
+ if (partition)
if (!wait_for_partition(path))
return -2;
- }
+
ret = mount_unknown_fs(path, bdev->dest, bdev->mntopts);
if (ret < 0)
ERROR("Error mounting %s", bdev->src);
@@ -178,6 +183,7 @@ bool requires_nbd(const char *path)
{
if (strncmp(path, "nbd:", 4) == 0)
return true;
+
return false;
}
@@ -226,6 +232,7 @@ static int do_attach_nbd(void *d)
exit(0);
} else if (fdsi.ssi_signo == SIGCHLD) {
int status;
+
/* If qemu-nbd fails, or is killed by a signal,
* then exit */
while (waitpid(-1, &status, WNOHANG) > 0) {
@@ -240,12 +247,13 @@ static int do_attach_nbd(void *d)
}
close(sfd);
+
if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
WARN("Warning: unblocking signals for nbd watcher");
execlp("qemu-nbd", "qemu-nbd", "-c", nbd, path, (char *)NULL);
SYSERROR("Error executing qemu-nbd");
- exit(1);
+ _exit(1);
}
static bool clone_attach_nbd(const char *nbd, const char *path)
@@ -259,6 +267,7 @@ static bool clone_attach_nbd(const char *nbd, const char *path)
pid = lxc_clone(do_attach_nbd, &data, CLONE_NEWPID);
if (pid < 0)
return false;
+
return true;
}
@@ -270,6 +279,7 @@ static bool nbd_busy(int idx)
ret = snprintf(path, 100, "/sys/block/nbd%d/pid", idx);
if (ret < 0 || ret >= 100)
return true;
+
return file_exists(path);
}
@@ -282,15 +292,17 @@ static void nbd_detach(const char *path)
SYSERROR("Error forking to detach nbd");
return;
}
+
if (pid) {
ret = wait_for_pid(pid);
if (ret < 0)
ERROR("nbd disconnect returned an error");
return;
}
+
execlp("qemu-nbd", "qemu-nbd", "-d", path, (char *)NULL);
SYSERROR("Error executing qemu-nbd");
- exit(1);
+ _exit(1);
}
/*
@@ -303,24 +315,31 @@ static int nbd_get_partition(const char *src)
char *p = strchr(src, ':');
if (!p)
return 0;
+
p = strchr(p+1, ':');
if (!p)
return 0;
+
p++;
+
if (*p < '1' || *p > '9')
return 0;
+
return *p - '0';
}
static bool wait_for_partition(const char *path)
{
int count = 0;
+
while (count < 5) {
if (file_exists(path))
return true;
+
sleep(1);
count++;
}
+
ERROR("Device %s did not show up after 5 seconds", path);
return false;
}
From 75457df5dcead628135fa033cc3478fa2b8b8e24 Mon Sep 17 00:00:00 2001
From: 2xsec <dh48.jeong at samsung.com>
Date: Mon, 20 Aug 2018 10:43:22 +0900
Subject: [PATCH 2/2] tools: lxc-wait: add default log priority & cleanups
Signed-off-by: 2xsec <dh48.jeong at samsung.com>
---
src/lxc/tools/lxc_wait.c | 78 +++++++++++++++++++++-------------------
1 file changed, 42 insertions(+), 36 deletions(-)
diff --git a/src/lxc/tools/lxc_wait.c b/src/lxc/tools/lxc_wait.c
index deecc7734..6c3c07085 100644
--- a/src/lxc/tools/lxc_wait.c
+++ b/src/lxc/tools/lxc_wait.c
@@ -37,25 +37,8 @@
lxc_log_define(lxc_wait, lxc);
-static int my_checker(const struct lxc_arguments *args)
-{
- if (!args->states) {
- ERROR("Missing state option to wait for");
- return -1;
- }
-
- return 0;
-}
-
-static int my_parser(struct lxc_arguments *args, int c, char *arg)
-{
- switch (c) {
- case 's': args->states = optarg; break;
- case 't': args->timeout = atol(optarg); break;
- }
-
- return 0;
-}
+static int my_parser(struct lxc_arguments *args, int c, char *arg);
+static int my_checker(const struct lxc_arguments *args);
static const struct option my_longopts[] = {
{"state", required_argument, 0, 's'},
@@ -64,8 +47,8 @@ static const struct option my_longopts[] = {
};
static struct lxc_arguments my_args = {
- .progname = "lxc-wait",
- .help = "\
+ .progname = "lxc-wait",
+ .help = "\
--name=NAME --state=STATE\n\
\n\
lxc-wait waits for NAME container state to reach STATE\n\
@@ -77,12 +60,38 @@ Options :\n\
ABORTING, FREEZING, FROZEN, THAWED\n\
-t, --timeout=TMO Seconds to wait for state changes\n\
--rcfile=FILE Load configuration file FILE\n",
- .options = my_longopts,
- .parser = my_parser,
- .checker = my_checker,
- .timeout = -1,
+ .options = my_longopts,
+ .parser = my_parser,
+ .checker = my_checker,
+ .log_priority = "ERROR",
+ .log_file = "none",
+ .timeout = -1,
};
+static int my_parser(struct lxc_arguments *args, int c, char *arg)
+{
+ switch (c) {
+ case 's':
+ args->states = optarg;
+ break;
+ case 't':
+ args->timeout = atol(optarg);
+ break;
+ }
+
+ return 0;
+}
+
+static int my_checker(const struct lxc_arguments *args)
+{
+ if (!args->states) {
+ ERROR("Missing state option to wait for");
+ return -1;
+ }
+
+ return 0;
+}
+
int main(int argc, char *argv[])
{
struct lxc_container *c;
@@ -91,18 +100,15 @@ int main(int argc, char *argv[])
if (lxc_arguments_parse(&my_args, argc, argv))
exit(EXIT_FAILURE);
- /* Only create log if explicitly instructed */
- if (my_args.log_file || my_args.log_priority) {
- log.name = my_args.name;
- log.file = my_args.log_file;
- log.level = my_args.log_priority;
- log.prefix = my_args.progname;
- log.quiet = my_args.quiet;
- log.lxcpath = my_args.lxcpath[0];
+ log.name = my_args.name;
+ log.file = my_args.log_file;
+ log.level = my_args.log_priority;
+ log.prefix = my_args.progname;
+ log.quiet = my_args.quiet;
+ log.lxcpath = my_args.lxcpath[0];
- if (lxc_log_init(&log))
- exit(EXIT_FAILURE);
- }
+ if (lxc_log_init(&log))
+ exit(EXIT_FAILURE);
c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
if (!c)
More information about the lxc-devel
mailing list