Commit 75e607ba by 2xsec

tools: lxc-destroy: share internal API symbols

Signed-off-by: 's avatar2xsec <dh48.jeong@samsung.com>
parent 138eda99
...@@ -270,7 +270,7 @@ lxc_autostart_SOURCES = tools/lxc_autostart.c tools/arguments.c ...@@ -270,7 +270,7 @@ lxc_autostart_SOURCES = tools/lxc_autostart.c tools/arguments.c
lxc_cgroup_SOURCES = tools/lxc_cgroup.c tools/arguments.c lxc_cgroup_SOURCES = tools/lxc_cgroup.c tools/arguments.c
lxc_config_SOURCES = tools/lxc_config.c tools/arguments.c lxc_config_SOURCES = tools/lxc_config.c tools/arguments.c
lxc_console_SOURCES = tools/lxc_console.c tools/arguments.c lxc_console_SOURCES = tools/lxc_console.c tools/arguments.c
lxc_destroy_SOURCES = tools/lxc_destroy.c tools/arguments.c tools/tool_utils.c lxc_destroy_SOURCES = tools/lxc_destroy.c tools/arguments.c
lxc_device_SOURCES = tools/lxc_device.c tools/arguments.c tools/tool_utils.c lxc_device_SOURCES = tools/lxc_device.c tools/arguments.c tools/tool_utils.c
lxc_execute_SOURCES = tools/lxc_execute.c tools/arguments.c tools/tool_utils.c lxc_execute_SOURCES = tools/lxc_execute.c tools/arguments.c tools/tool_utils.c
lxc_freeze_SOURCES = tools/lxc_freeze.c tools/arguments.c lxc_freeze_SOURCES = tools/lxc_freeze.c tools/arguments.c
......
...@@ -29,10 +29,12 @@ ...@@ -29,10 +29,12 @@
#include <lxc/lxccontainer.h> #include <lxc/lxccontainer.h>
#include "arguments.h" #include "arguments.h"
#include "tool_utils.h" #include "log.h"
#include "utils.h"
static int my_parser(struct lxc_arguments* args, int c, char* arg); lxc_log_define(lxc_destroy, lxc);
static bool quiet;
static int my_parser(struct lxc_arguments *args, int c, char *arg);
static const struct option my_longopts[] = { static const struct option my_longopts[] = {
{"force", no_argument, 0, 'f'}, {"force", no_argument, 0, 'f'},
...@@ -83,59 +85,56 @@ int main(int argc, char *argv[]) ...@@ -83,59 +85,56 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (my_args.quiet)
quiet = true;
c = lxc_container_new(my_args.name, my_args.lxcpath[0]); c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
if (!c) { if (!c) {
if (!quiet) ERROR("System error loading container");
fprintf(stderr, "System error loading container\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (my_args.rcfile) { if (my_args.rcfile) {
c->clear_config(c); c->clear_config(c);
if (!c->load_config(c, my_args.rcfile)) { if (!c->load_config(c, my_args.rcfile)) {
fprintf(stderr, "Failed to load rcfile\n"); ERROR("Failed to load rcfile");
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
c->configfile = strdup(my_args.rcfile); c->configfile = strdup(my_args.rcfile);
if (!c->configfile) { if (!c->configfile) {
fprintf(stderr, "Out of memory setting new config filename\n"); ERROR("Out of memory setting new config filename");
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
if (!c->may_control(c)) { if (!c->may_control(c)) {
if (!quiet) ERROR("Insufficent privileges to control %s", my_args.name);
fprintf(stderr, "Insufficent privileges to control %s\n", my_args.name);
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (!c->is_defined(c)) { if (!c->is_defined(c)) {
if (!quiet) ERROR("Container is not defined");
fprintf(stderr, "Container is not defined\n");
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (my_args.task == SNAP) { if (my_args.task == SNAP) {
bret = do_destroy_with_snapshots(c); bret = do_destroy_with_snapshots(c);
if (bret && !quiet) if (bret)
printf("Destroyed container %s including snapshots \n", my_args.name); ERROR("Destroyed container %s including snapshots", my_args.name);
} else { } else {
bret = do_destroy(c); bret = do_destroy(c);
if (bret && !quiet) if (bret)
printf("Destroyed container %s\n", my_args.name); ERROR("Destroyed container %s", my_args.name);
} }
lxc_container_put(c); lxc_container_put(c);
if (bret) if (bret)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
...@@ -151,34 +150,33 @@ static int my_parser(struct lxc_arguments *args, int c, char *arg) ...@@ -151,34 +150,33 @@ static int my_parser(struct lxc_arguments *args, int c, char *arg)
static bool do_destroy(struct lxc_container *c) static bool do_destroy(struct lxc_container *c)
{ {
bool bret = true; bool bret = true;
char path[TOOL_MAXPATHLEN]; char path[MAXPATHLEN];
/* First check whether the container has dependent clones or snapshots. */ /* First check whether the container has dependent clones or snapshots. */
int ret = snprintf(path, TOOL_MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path, c->name); int ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path, c->name);
if (ret < 0 || ret >= TOOL_MAXPATHLEN) if (ret < 0 || ret >= MAXPATHLEN)
return false; return false;
if (file_exists(path)) { if (file_exists(path)) {
if (!quiet) ERROR("Destroying %s failed: %s has clones.", c->name, c->name);
fprintf(stdout, "Destroying %s failed: %s has clones.\n", c->name, c->name);
return false; return false;
} }
ret = snprintf(path, TOOL_MAXPATHLEN, "%s/%s/snaps", c->config_path, c->name); ret = snprintf(path, MAXPATHLEN, "%s/%s/snaps", c->config_path, c->name);
if (ret < 0 || ret >= TOOL_MAXPATHLEN) if (ret < 0 || ret >= MAXPATHLEN)
return false; return false;
if (rmdir(path) < 0 && errno != ENOENT) { if (rmdir(path) < 0 && errno != ENOENT) {
if (!quiet) ERROR("Destroying %s failed: %s has snapshots.", c->name, c->name);
fprintf(stdout, "Destroying %s failed: %s has snapshots.\n", c->name, c->name);
return false; return false;
} }
if (c->is_running(c)) { if (c->is_running(c)) {
if (!my_args.force && !quiet) { if (!my_args.force) {
fprintf(stderr, "%s is running\n", my_args.name); ERROR("%s is running", my_args.name);
return false; return false;
} }
/* If the container was ephemeral it will be removed on shutdown. */ /* If the container was ephemeral it will be removed on shutdown. */
c->stop(c); c->stop(c);
} }
...@@ -187,6 +185,7 @@ static bool do_destroy(struct lxc_container *c) ...@@ -187,6 +185,7 @@ static bool do_destroy(struct lxc_container *c)
* stopped it. */ * stopped it. */
if (c->is_defined(c)) { if (c->is_defined(c)) {
char buf[256]; char buf[256];
ret = c->get_config_item(c, "lxc.ephemeral", buf, 256); ret = c->get_config_item(c, "lxc.ephemeral", buf, 256);
if (ret > 0 && strcmp(buf, "0") == 0) { if (ret > 0 && strcmp(buf, "0") == 0) {
bret = c->destroy(c); bret = c->destroy(c);
...@@ -194,8 +193,7 @@ static bool do_destroy(struct lxc_container *c) ...@@ -194,8 +193,7 @@ static bool do_destroy(struct lxc_container *c)
} }
if (!bret) { if (!bret) {
if (!quiet) ERROR("Destroying %s failed", my_args.name);
fprintf(stderr, "Destroying %s failed\n", my_args.name);
return false; return false;
} }
...@@ -207,7 +205,7 @@ static bool do_destroy_with_snapshots(struct lxc_container *c) ...@@ -207,7 +205,7 @@ static bool do_destroy_with_snapshots(struct lxc_container *c)
struct lxc_container *c1; struct lxc_container *c1;
struct stat fbuf; struct stat fbuf;
bool bret = false; bool bret = false;
char path[TOOL_MAXPATHLEN]; char path[MAXPATHLEN];
char *buf = NULL; char *buf = NULL;
char *lxcpath = NULL; char *lxcpath = NULL;
char *lxcname = NULL; char *lxcname = NULL;
...@@ -217,8 +215,8 @@ static bool do_destroy_with_snapshots(struct lxc_container *c) ...@@ -217,8 +215,8 @@ static bool do_destroy_with_snapshots(struct lxc_container *c)
int counter = 0; int counter = 0;
/* Destroy clones. */ /* Destroy clones. */
ret = snprintf(path, TOOL_MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path, c->name); ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path, c->name);
if (ret < 0 || ret >= TOOL_MAXPATHLEN) if (ret < 0 || ret >= MAXPATHLEN)
return false; return false;
fd = open(path, O_RDONLY | O_CLOEXEC); fd = open(path, O_RDONLY | O_CLOEXEC);
...@@ -232,14 +230,14 @@ static bool do_destroy_with_snapshots(struct lxc_container *c) ...@@ -232,14 +230,14 @@ static bool do_destroy_with_snapshots(struct lxc_container *c)
/* Make sure that the string is \0 terminated. */ /* Make sure that the string is \0 terminated. */
buf = calloc(fbuf.st_size + 1, sizeof(char)); buf = calloc(fbuf.st_size + 1, sizeof(char));
if (!buf) { if (!buf) {
fprintf(stderr, "failed to allocate memory\n"); ERROR("Failed to allocate memory");
close(fd); close(fd);
return false; return false;
} }
ret = read(fd, buf, fbuf.st_size); ret = read(fd, buf, fbuf.st_size);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "could not read %s\n", path); ERROR("Could not read %s", path);
close(fd); close(fd);
free(buf); free(buf);
return false; return false;
...@@ -249,11 +247,13 @@ static bool do_destroy_with_snapshots(struct lxc_container *c) ...@@ -249,11 +247,13 @@ static bool do_destroy_with_snapshots(struct lxc_container *c)
while ((lxcpath = strtok_r(!counter ? buf : NULL, "\n", &scratch))) { while ((lxcpath = strtok_r(!counter ? buf : NULL, "\n", &scratch))) {
if (!(lxcname = strtok_r(NULL, "\n", &scratch))) if (!(lxcname = strtok_r(NULL, "\n", &scratch)))
break; break;
c1 = lxc_container_new(lxcname, lxcpath); c1 = lxc_container_new(lxcname, lxcpath);
if (!c1) { if (!c1) {
counter++; counter++;
continue; continue;
} }
/* We do not destroy recursively. If a clone of a clone /* We do not destroy recursively. If a clone of a clone
* has clones or snapshots the user should remove it * has clones or snapshots the user should remove it
* explicitly. */ * explicitly. */
...@@ -262,6 +262,7 @@ static bool do_destroy_with_snapshots(struct lxc_container *c) ...@@ -262,6 +262,7 @@ static bool do_destroy_with_snapshots(struct lxc_container *c)
free(buf); free(buf);
return false; return false;
} }
lxc_container_put(c1); lxc_container_put(c1);
counter++; counter++;
} }
...@@ -269,8 +270,8 @@ static bool do_destroy_with_snapshots(struct lxc_container *c) ...@@ -269,8 +270,8 @@ static bool do_destroy_with_snapshots(struct lxc_container *c)
} }
/* Destroy snapshots located in the containers snap/ folder. */ /* Destroy snapshots located in the containers snap/ folder. */
ret = snprintf(path, TOOL_MAXPATHLEN, "%s/%s/snaps", c->config_path, c->name); ret = snprintf(path, MAXPATHLEN, "%s/%s/snaps", c->config_path, c->name);
if (ret < 0 || ret >= TOOL_MAXPATHLEN) if (ret < 0 || ret >= MAXPATHLEN)
return false; return false;
if (rmdir(path) < 0 && errno != ENOENT) if (rmdir(path) < 0 && errno != ENOENT)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment