710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur/* DEVICE_add_remove.c
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur *
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur * Copyright © 2014 S.Çağlar Onur <caglar@10ur.org>
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur *
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur * This program is free software; you can redistribute it and/or modify
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur * it under the terms of the GNU General Public License version 2, as
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur * published by the Free Software Foundation.
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur *
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur * This program is distributed in the hope that it will be useful,
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur * but WITHOUT ANY WARRANTY; without even the implied warranty of
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur * GNU General Public License for more details.
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur *
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur * You should have received a copy of the GNU General Public License along
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur * with this program; if not, write to the Free Software Foundation, Inc.,
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur */
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur#include <stdio.h>
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur#include <stdlib.h>
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur#include <lxc/lxccontainer.h>
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur#define NAME "device_add_remove_test"
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur#define DEVICE "/dev/network_latency"
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onurint main(int argc, char *argv[])
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur{
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur int ret = 1;
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur struct lxc_container *c;
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur c = lxc_container_new(NAME, NULL);
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur if (!c) {
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur fprintf(stderr, "Unable to instantiate container (%s)...\n", NAME);
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur goto out;
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur }
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur if (!c->create(c, "busybox", NULL, NULL, 1, NULL)) {
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur fprintf(stderr, "Creating the container (%s) failed...\n", NAME);
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur goto out;
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur }
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur c->want_daemonize(c, true);
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur if (!c->start(c, false, NULL)) {
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur fprintf(stderr, "Starting the container (%s) failed...\n", NAME);
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur goto out;
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur }
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur if (!c->add_device_node(c, DEVICE, DEVICE)) {
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur fprintf(stderr, "Adding %s to the container (%s) failed...\n", DEVICE, NAME);
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur goto out;
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur }
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur if (!c->remove_device_node(c, DEVICE, DEVICE)) {
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur fprintf(stderr, "Removing %s from the container (%s) failed...\n", DEVICE, NAME);
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur goto out;
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur }
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur if (!c->stop(c)) {
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur fprintf(stderr, "Stopping the container (%s) failed...\n", NAME);
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur goto out;
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur }
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur if (!c->destroy(c)) {
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur fprintf(stderr, "Destroying the container (%s) failed...\n", NAME);
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur goto out;
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur }
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur ret = 0;
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onurout:
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur lxc_container_put(c);
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur return ret;
710bde5e52ec3f1260f0ed7d50b7c46a8d7bbda3S.Çağlar Onur}