destroytest.c revision afeecbba0359d2b4404cdf896e6b6d0b5a8443b0
6bdda696b3ea703c47e87fea61017ec655f91d92nd/* liblxcapi
6bdda696b3ea703c47e87fea61017ec655f91d92nd *
6bdda696b3ea703c47e87fea61017ec655f91d92nd * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
6bdda696b3ea703c47e87fea61017ec655f91d92nd * Copyright © 2012 Canonical Ltd.
6bdda696b3ea703c47e87fea61017ec655f91d92nd *
6bdda696b3ea703c47e87fea61017ec655f91d92nd * This program is free software; you can redistribute it and/or modify
6bdda696b3ea703c47e87fea61017ec655f91d92nd * it under the terms of the GNU General Public License version 2, as
6bdda696b3ea703c47e87fea61017ec655f91d92nd * published by the Free Software Foundation.
6bdda696b3ea703c47e87fea61017ec655f91d92nd *
6bdda696b3ea703c47e87fea61017ec655f91d92nd * This program is distributed in the hope that it will be useful,
0662ed52e814f8f08ef0e09956413a792584eddffuankg * but WITHOUT ANY WARRANTY; without even the implied warranty of
6bdda696b3ea703c47e87fea61017ec655f91d92nd * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6bdda696b3ea703c47e87fea61017ec655f91d92nd * GNU General Public License for more details.
6bdda696b3ea703c47e87fea61017ec655f91d92nd *
6bdda696b3ea703c47e87fea61017ec655f91d92nd * You should have received a copy of the GNU General Public License along
6bdda696b3ea703c47e87fea61017ec655f91d92nd * with this program; if not, write to the Free Software Foundation, Inc.,
6bdda696b3ea703c47e87fea61017ec655f91d92nd * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
6bdda696b3ea703c47e87fea61017ec655f91d92nd */
6bdda696b3ea703c47e87fea61017ec655f91d92nd#include "../lxc/lxccontainer.h"
6bdda696b3ea703c47e87fea61017ec655f91d92nd
16b55a35cff91315d261d1baa776138af465c4e4fuankg#include <unistd.h>
16b55a35cff91315d261d1baa776138af465c4e4fuankg#include <signal.h>
16b55a35cff91315d261d1baa776138af465c4e4fuankg#include <stdio.h>
6bdda696b3ea703c47e87fea61017ec655f91d92nd#include <sys/types.h>
6bdda696b3ea703c47e87fea61017ec655f91d92nd#include <sys/wait.h>
6bdda696b3ea703c47e87fea61017ec655f91d92nd#include <stdlib.h>
6bdda696b3ea703c47e87fea61017ec655f91d92nd#include <errno.h>
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd#define MYNAME "lxctest1"
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92ndstatic int create_ubuntu(void)
6bdda696b3ea703c47e87fea61017ec655f91d92nd{
6bdda696b3ea703c47e87fea61017ec655f91d92nd int status, ret;
6bdda696b3ea703c47e87fea61017ec655f91d92nd pid_t pid = fork();
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd if (pid < 0) {
6bdda696b3ea703c47e87fea61017ec655f91d92nd perror("fork");
6bdda696b3ea703c47e87fea61017ec655f91d92nd return -1;
6bdda696b3ea703c47e87fea61017ec655f91d92nd }
6bdda696b3ea703c47e87fea61017ec655f91d92nd if (pid == 0) {
6bdda696b3ea703c47e87fea61017ec655f91d92nd ret = execlp("lxc-create", "lxc-create", "-t", "ubuntu", "-f", "/etc/lxc/default.conf", "-n", MYNAME, NULL);
6bdda696b3ea703c47e87fea61017ec655f91d92nd // Should not return
6bdda696b3ea703c47e87fea61017ec655f91d92nd perror("execl");
6bdda696b3ea703c47e87fea61017ec655f91d92nd exit(1);
6bdda696b3ea703c47e87fea61017ec655f91d92nd }
6bdda696b3ea703c47e87fea61017ec655f91d92ndagain:
6bdda696b3ea703c47e87fea61017ec655f91d92nd ret = waitpid(pid, &status, 0);
6bdda696b3ea703c47e87fea61017ec655f91d92nd if (ret == -1) {
6bdda696b3ea703c47e87fea61017ec655f91d92nd if (errno == -EINTR)
6bdda696b3ea703c47e87fea61017ec655f91d92nd goto again;
6bdda696b3ea703c47e87fea61017ec655f91d92nd perror("waitpid");
6bdda696b3ea703c47e87fea61017ec655f91d92nd return -1;
6bdda696b3ea703c47e87fea61017ec655f91d92nd }
6bdda696b3ea703c47e87fea61017ec655f91d92nd if (ret != pid)
6bdda696b3ea703c47e87fea61017ec655f91d92nd goto again;
6bdda696b3ea703c47e87fea61017ec655f91d92nd if (!WIFEXITED(status)) { // did not exit normally
6bdda696b3ea703c47e87fea61017ec655f91d92nd fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
6bdda696b3ea703c47e87fea61017ec655f91d92nd return -1;
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg }
6bdda696b3ea703c47e87fea61017ec655f91d92nd return WEXITSTATUS(status);
6bdda696b3ea703c47e87fea61017ec655f91d92nd}
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92ndint main(int argc, char *argv[])
6bdda696b3ea703c47e87fea61017ec655f91d92nd{
6bdda696b3ea703c47e87fea61017ec655f91d92nd struct lxc_container *c;
6bdda696b3ea703c47e87fea61017ec655f91d92nd int ret = 1;
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd if ((c = lxc_container_new(MYNAME, NULL)) == NULL) {
6bdda696b3ea703c47e87fea61017ec655f91d92nd fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME);
6bdda696b3ea703c47e87fea61017ec655f91d92nd ret = 1;
6bdda696b3ea703c47e87fea61017ec655f91d92nd goto out;
6bdda696b3ea703c47e87fea61017ec655f91d92nd }
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg
6bdda696b3ea703c47e87fea61017ec655f91d92nd if (c->is_defined(c)) {
6bdda696b3ea703c47e87fea61017ec655f91d92nd fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
6bdda696b3ea703c47e87fea61017ec655f91d92nd goto out;
6bdda696b3ea703c47e87fea61017ec655f91d92nd }
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd if (create_ubuntu()) {
6bdda696b3ea703c47e87fea61017ec655f91d92nd fprintf(stderr, "%d: failed to create a ubuntu container\n", __LINE__);
6bdda696b3ea703c47e87fea61017ec655f91d92nd goto out;
6bdda696b3ea703c47e87fea61017ec655f91d92nd }
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd if (!c->is_defined(c)) {
6bdda696b3ea703c47e87fea61017ec655f91d92nd fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME);
6bdda696b3ea703c47e87fea61017ec655f91d92nd goto out;
6bdda696b3ea703c47e87fea61017ec655f91d92nd }
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd if (!c->destroy(c)) {
6bdda696b3ea703c47e87fea61017ec655f91d92nd fprintf(stderr, "%d: error deleting %s\n", __LINE__, MYNAME);
6bdda696b3ea703c47e87fea61017ec655f91d92nd goto out;
6bdda696b3ea703c47e87fea61017ec655f91d92nd }
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd if (c->is_defined(c)) {
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
6bdda696b3ea703c47e87fea61017ec655f91d92nd goto out;
6bdda696b3ea703c47e87fea61017ec655f91d92nd }
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg
6bdda696b3ea703c47e87fea61017ec655f91d92nd fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
6bdda696b3ea703c47e87fea61017ec655f91d92nd ret = 0;
6bdda696b3ea703c47e87fea61017ec655f91d92ndout:
6bdda696b3ea703c47e87fea61017ec655f91d92nd lxc_container_put(c);
6bdda696b3ea703c47e87fea61017ec655f91d92nd exit(ret);
6bdda696b3ea703c47e87fea61017ec655f91d92nd}
6bdda696b3ea703c47e87fea61017ec655f91d92nd