shutdowntest.c revision afeecbba0359d2b4404cdf896e6b6d0b5a8443b0
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/* liblxcapi
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes *
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes * Copyright © 2012 Canonical Ltd.
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes *
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes * This program is free software; you can redistribute it and/or modify
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes * it under the terms of the GNU General Public License version 2, as
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes * published by the Free Software Foundation.
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes *
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes * This program is distributed in the hope that it will be useful,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes * but WITHOUT ANY WARRANTY; without even the implied warranty of
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes * GNU General Public License for more details.
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes *
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes * You should have received a copy of the GNU General Public License along
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes * with this program; if not, write to the Free Software Foundation, Inc.,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes#include "../lxc/lxccontainer.h"
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
70953fb44a7140fe206c3a5f011e24209c8c5c6abnicholes#include <unistd.h>
70953fb44a7140fe206c3a5f011e24209c8c5c6abnicholes#include <signal.h>
70953fb44a7140fe206c3a5f011e24209c8c5c6abnicholes#include <stdio.h>
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes#include <sys/types.h>
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes#include <sys/wait.h>
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes#include <stdlib.h>
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes#include <errno.h>
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes#define MYNAME "lxctest1"
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesint main(int argc, char *argv[])
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes{
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes struct lxc_container *c;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes int ret = 1;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if ((c = lxc_container_new(MYNAME, NULL)) == NULL) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes ret = 1;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes goto out;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (c->is_defined(c)) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes goto out;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (!c->set_config_item(c, "lxc.network.type", "veth")) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes fprintf(stderr, "%d: failed to set network type\n", __LINE__);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes goto out;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes c->set_config_item(c, "lxc.network.link", "lxcbr0");
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes c->set_config_item(c, "lxc.network.flags", "up");
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (!c->createl(c, "ubuntu", "-r", "lucid", NULL)) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes fprintf(stderr, "%d: failed to create a lucid container\n", __LINE__);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes goto out;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (!c->is_defined(c)) {
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes goto out;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes c->load_config(c, NULL);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes c->want_daemonize(c);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (!c->startl(c, 0, NULL)) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes fprintf(stderr, "%d: failed to start %s\n", __LINE__, MYNAME);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes goto out;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes fprintf(stderr, "%d: %s started, you have 60 seconds to test a console\n", __LINE__, MYNAME);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes sleep(60); // wait a minute to let user connect to console
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (!c->shutdown(c, 60)) {
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg fprintf(stderr, "%d: failed to shut down %s\n", __LINE__, MYNAME);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes goto out;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (!c->destroy(c)) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes fprintf(stderr, "%d: error deleting %s\n", __LINE__, MYNAME);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes goto out;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (c->is_defined(c)) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes goto out;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes ret = 0;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesout:
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes lxc_container_put(c);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes exit(ret);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes}
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes