Skip to content

Commit

Permalink
tools: lxc-destroy: share internal API symbols
Browse files Browse the repository at this point in the history
Signed-off-by: 2xsec <dh48.jeong@samsung.com>
  • Loading branch information
2xsec authored and Christian Brauner committed Jul 2, 2018
1 parent bc46bee commit a72ffe6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/lxc/Makefile.am
Expand Up @@ -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_config_SOURCES = tools/lxc_config.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_execute_SOURCES = tools/lxc_execute.c tools/arguments.c tools/tool_utils.c
lxc_freeze_SOURCES = tools/lxc_freeze.c tools/arguments.c
Expand Down
77 changes: 39 additions & 38 deletions src/lxc/tools/lxc_destroy.c
Expand Up @@ -29,10 +29,12 @@
#include <lxc/lxccontainer.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);
static bool quiet;
lxc_log_define(lxc_destroy, lxc);

static int my_parser(struct lxc_arguments *args, int c, char *arg);

static const struct option my_longopts[] = {
{"force", no_argument, 0, 'f'},
Expand Down Expand Up @@ -83,59 +85,56 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}

if (my_args.quiet)
quiet = true;

c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
if (!c) {
if (!quiet)
fprintf(stderr, "System error loading container\n");
ERROR("System error loading container");
exit(EXIT_FAILURE);
}

if (my_args.rcfile) {
c->clear_config(c);

if (!c->load_config(c, my_args.rcfile)) {
fprintf(stderr, "Failed to load rcfile\n");
ERROR("Failed to load rcfile");
lxc_container_put(c);
exit(EXIT_FAILURE);
}

c->configfile = strdup(my_args.rcfile);
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);
exit(EXIT_FAILURE);
}
}

if (!c->may_control(c)) {
if (!quiet)
fprintf(stderr, "Insufficent privileges to control %s\n", my_args.name);
ERROR("Insufficent privileges to control %s", my_args.name);
lxc_container_put(c);
exit(EXIT_FAILURE);
}

if (!c->is_defined(c)) {
if (!quiet)
fprintf(stderr, "Container is not defined\n");
ERROR("Container is not defined");
lxc_container_put(c);
exit(EXIT_FAILURE);
}

if (my_args.task == SNAP) {
bret = do_destroy_with_snapshots(c);
if (bret && !quiet)
printf("Destroyed container %s including snapshots \n", my_args.name);
if (bret)
ERROR("Destroyed container %s including snapshots", my_args.name);
} else {
bret = do_destroy(c);
if (bret && !quiet)
printf("Destroyed container %s\n", my_args.name);
if (bret)
ERROR("Destroyed container %s", my_args.name);
}

lxc_container_put(c);

if (bret)
exit(EXIT_SUCCESS);

exit(EXIT_FAILURE);
}

Expand All @@ -151,34 +150,33 @@ static int my_parser(struct lxc_arguments *args, int c, char *arg)
static bool do_destroy(struct lxc_container *c)
{
bool bret = true;
char path[TOOL_MAXPATHLEN];
char path[MAXPATHLEN];

/* 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);
if (ret < 0 || ret >= TOOL_MAXPATHLEN)
int ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path, c->name);
if (ret < 0 || ret >= MAXPATHLEN)
return false;

if (file_exists(path)) {
if (!quiet)
fprintf(stdout, "Destroying %s failed: %s has clones.\n", c->name, c->name);
ERROR("Destroying %s failed: %s has clones.", c->name, c->name);
return false;
}

ret = snprintf(path, TOOL_MAXPATHLEN, "%s/%s/snaps", c->config_path, c->name);
if (ret < 0 || ret >= TOOL_MAXPATHLEN)
ret = snprintf(path, MAXPATHLEN, "%s/%s/snaps", c->config_path, c->name);
if (ret < 0 || ret >= MAXPATHLEN)
return false;

if (rmdir(path) < 0 && errno != ENOENT) {
if (!quiet)
fprintf(stdout, "Destroying %s failed: %s has snapshots.\n", c->name, c->name);
ERROR("Destroying %s failed: %s has snapshots.", c->name, c->name);
return false;
}

if (c->is_running(c)) {
if (!my_args.force && !quiet) {
fprintf(stderr, "%s is running\n", my_args.name);
if (!my_args.force) {
ERROR("%s is running", my_args.name);
return false;
}

/* If the container was ephemeral it will be removed on shutdown. */
c->stop(c);
}
Expand All @@ -187,15 +185,15 @@ static bool do_destroy(struct lxc_container *c)
* stopped it. */
if (c->is_defined(c)) {
char buf[256];

ret = c->get_config_item(c, "lxc.ephemeral", buf, 256);
if (ret > 0 && strcmp(buf, "0") == 0) {
bret = c->destroy(c);
}
}

if (!bret) {
if (!quiet)
fprintf(stderr, "Destroying %s failed\n", my_args.name);
ERROR("Destroying %s failed", my_args.name);
return false;
}

Expand All @@ -207,7 +205,7 @@ static bool do_destroy_with_snapshots(struct lxc_container *c)
struct lxc_container *c1;
struct stat fbuf;
bool bret = false;
char path[TOOL_MAXPATHLEN];
char path[MAXPATHLEN];
char *buf = NULL;
char *lxcpath = NULL;
char *lxcname = NULL;
Expand All @@ -217,8 +215,8 @@ static bool do_destroy_with_snapshots(struct lxc_container *c)
int counter = 0;

/* Destroy clones. */
ret = snprintf(path, TOOL_MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path, c->name);
if (ret < 0 || ret >= TOOL_MAXPATHLEN)
ret = snprintf(path, MAXPATHLEN, "%s/%s/lxc_snapshots", c->config_path, c->name);
if (ret < 0 || ret >= MAXPATHLEN)
return false;

fd = open(path, O_RDONLY | O_CLOEXEC);
Expand All @@ -232,14 +230,14 @@ static bool do_destroy_with_snapshots(struct lxc_container *c)
/* Make sure that the string is \0 terminated. */
buf = calloc(fbuf.st_size + 1, sizeof(char));
if (!buf) {
fprintf(stderr, "failed to allocate memory\n");
ERROR("Failed to allocate memory");
close(fd);
return false;
}

ret = read(fd, buf, fbuf.st_size);
if (ret < 0) {
fprintf(stderr, "could not read %s\n", path);
ERROR("Could not read %s", path);
close(fd);
free(buf);
return false;
Expand All @@ -249,11 +247,13 @@ static bool do_destroy_with_snapshots(struct lxc_container *c)
while ((lxcpath = strtok_r(!counter ? buf : NULL, "\n", &scratch))) {
if (!(lxcname = strtok_r(NULL, "\n", &scratch)))
break;

c1 = lxc_container_new(lxcname, lxcpath);
if (!c1) {
counter++;
continue;
}

/* We do not destroy recursively. If a clone of a clone
* has clones or snapshots the user should remove it
* explicitly. */
Expand All @@ -262,15 +262,16 @@ static bool do_destroy_with_snapshots(struct lxc_container *c)
free(buf);
return false;
}

lxc_container_put(c1);
counter++;
}
free(buf);
}

/* Destroy snapshots located in the containers snap/ folder. */
ret = snprintf(path, TOOL_MAXPATHLEN, "%s/%s/snaps", c->config_path, c->name);
if (ret < 0 || ret >= TOOL_MAXPATHLEN)
ret = snprintf(path, MAXPATHLEN, "%s/%s/snaps", c->config_path, c->name);
if (ret < 0 || ret >= MAXPATHLEN)
return false;

if (rmdir(path) < 0 && errno != ENOENT)
Expand Down

0 comments on commit a72ffe6

Please sign in to comment.