]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/device_add_remove.c
tests: use busybox in lxc-test-usernic.in
[mirror_lxc.git] / src / tests / device_add_remove.c
1 /* DEVICE_add_remove.c
2 *
3 * Copyright © 2014 S.Çağlar Onur <caglar@10ur.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <lxc/lxccontainer.h>
22
23 #include "lxctest.h"
24 #include "memory_utils.h"
25 #include "utils.h"
26
27 #ifndef HAVE_STRLCPY
28 #include "include/strlcpy.h"
29 #endif
30
31 #define NAME "device_add_remove_test"
32 #define DEVICE "/dev/loop-control"
33
34 int main(int argc, char *argv[])
35 {
36 __do_close int fd_log = -EBADF;
37 int ret = 1;
38 struct lxc_log log = {};
39 struct lxc_container *c = NULL;
40 char template[sizeof(P_tmpdir"/attach_XXXXXX")];
41
42 (void)strlcpy(template, P_tmpdir"/attach_XXXXXX", sizeof(template));
43
44 fd_log = lxc_make_tmpfile(template, false);
45 if (fd_log < 0) {
46 lxc_error("Failed to create temporary log file for container %s\n", NAME);
47 exit(EXIT_FAILURE);
48 }
49 log.name = NAME;
50 log.file = template;
51 log.level = "TRACE";
52 log.prefix = "device_add_remove";
53 log.quiet = false;
54 log.lxcpath = NULL;
55 if (lxc_log_init(&log))
56 goto out;
57
58 c = lxc_container_new(NAME, NULL);
59 if (!c) {
60 fprintf(stderr, "Unable to instantiate container (%s)...\n", NAME);
61 goto out;
62 }
63
64 if (!c->create(c, "busybox", NULL, NULL, 1, NULL)) {
65 fprintf(stderr, "Creating the container (%s) failed...\n", NAME);
66 goto out;
67 }
68
69 c->want_daemonize(c, true);
70
71 if (!c->start(c, false, NULL)) {
72 fprintf(stderr, "Starting the container (%s) failed...\n", NAME);
73 goto out;
74 }
75
76 if (!c->add_device_node(c, DEVICE, DEVICE)) {
77 fprintf(stderr, "Adding %s to the container (%s) failed...\n", DEVICE, NAME);
78 goto out;
79 }
80
81 if (!c->remove_device_node(c, DEVICE, DEVICE)) {
82 fprintf(stderr, "Removing %s from the container (%s) failed...\n", DEVICE, NAME);
83 goto out;
84 }
85
86 if (!c->stop(c)) {
87 fprintf(stderr, "Stopping the container (%s) failed...\n", NAME);
88 goto out;
89 }
90
91 if (!c->destroy(c)) {
92 fprintf(stderr, "Destroying the container (%s) failed...\n", NAME);
93 goto out;
94 }
95
96 ret = 0;
97
98 out:
99 if (ret != 0) {
100 char buf[4096];
101 ssize_t buflen;
102 while ((buflen = read(fd_log, buf, 1024)) > 0) {
103 buflen = write(STDERR_FILENO, buf, buflen);
104 if (buflen <= 0)
105 break;
106 }
107 }
108 (void)unlink(template);
109
110 if (c)
111 lxc_container_put(c);
112 return ret;
113 }