]>
Commit | Line | Data |
---|---|---|
fc788340 CB |
1 | /* liblxcapi |
2 | * | |
3 | * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>. | |
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 <alloca.h> | |
20 | #include <errno.h> | |
21 | #include <pthread.h> | |
22 | #include <sched.h> | |
23 | #include <signal.h> | |
24 | #include <stdio.h> | |
25 | #include <string.h> | |
26 | #include <unistd.h> | |
27 | #include <sys/reboot.h> | |
28 | #include <sys/types.h> | |
29 | #include <sys/wait.h> | |
30 | ||
31 | #include "lxc/lxccontainer.h" | |
32 | #include "lxctest.h" | |
33 | ||
34 | struct thread_args { | |
35 | int thread_id; | |
36 | int timeout; | |
37 | bool success; | |
38 | struct lxc_container *c; | |
39 | }; | |
40 | ||
41 | void *state_wrapper(void *data) | |
42 | { | |
43 | struct thread_args *args = data; | |
44 | ||
45 | lxc_debug("Starting state server thread %d\n", args->thread_id); | |
46 | ||
47 | args->success = args->c->shutdown(args->c, args->timeout); | |
48 | ||
49 | lxc_debug("State server thread %d with shutdown timeout %d returned \"%s\"\n", | |
50 | args->thread_id, args->timeout, args->success ? "SUCCESS" : "FAILED"); | |
51 | ||
52 | pthread_exit(NULL); | |
53 | return NULL; | |
54 | } | |
55 | ||
56 | int main(int argc, char *argv[]) | |
57 | { | |
58 | int i, j; | |
59 | pthread_attr_t attr; | |
60 | pthread_t threads[10]; | |
61 | struct thread_args args[10]; | |
62 | struct lxc_container *c; | |
63 | int ret = EXIT_FAILURE; | |
64 | ||
65 | c = lxc_container_new("state-server", NULL); | |
66 | if (!c) { | |
67 | lxc_error("%s", "Failed to create container \"state-server\""); | |
68 | exit(ret); | |
69 | } | |
70 | ||
71 | if (c->is_defined(c)) { | |
72 | lxc_error("%s\n", "Container \"state-server\" is defined"); | |
73 | goto on_error_put; | |
74 | } | |
75 | ||
76 | if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) { | |
77 | lxc_error("%s\n", "Failed to create busybox container \"state-server\""); | |
78 | goto on_error_put; | |
79 | } | |
80 | ||
81 | if (!c->is_defined(c)) { | |
82 | lxc_error("%s\n", "Container \"state-server\" is not defined"); | |
83 | goto on_error_put; | |
84 | } | |
85 | ||
86 | c->clear_config(c); | |
87 | ||
88 | if (!c->load_config(c, NULL)) { | |
89 | lxc_error("%s\n", "Failed to load config for container \"state-server\""); | |
90 | goto on_error_stop; | |
91 | } | |
92 | ||
93 | if (!c->want_daemonize(c, true)) { | |
94 | lxc_error("%s\n", "Failed to mark container \"state-server\" daemonized"); | |
95 | goto on_error_stop; | |
96 | } | |
97 | ||
98 | pthread_attr_init(&attr); | |
99 | ||
100 | for (j = 0; j < 10; j++) { | |
101 | lxc_debug("Starting state server test iteration %d\n", j); | |
102 | ||
103 | if (!c->startl(c, 0, NULL)) { | |
104 | lxc_error("%s\n", "Failed to start container \"state-server\" daemonized"); | |
105 | goto on_error_stop; | |
106 | } | |
107 | ||
108 | sleep(5); | |
109 | ||
110 | for (i = 0; i < 10; i++) { | |
111 | int ret; | |
112 | ||
113 | args[i].thread_id = i; | |
114 | args[i].c = c; | |
115 | args[i].timeout = -1; | |
116 | /* test non-blocking shutdown request */ | |
117 | if (i == 0) | |
118 | args[i].timeout = 0; | |
119 | ||
120 | ret = pthread_create(&threads[i], &attr, state_wrapper, (void *) &args[i]); | |
121 | if (ret != 0) | |
122 | goto on_error_stop; | |
123 | } | |
124 | ||
125 | for (i = 0; i < 10; i++) { | |
126 | int ret; | |
127 | ||
128 | ret = pthread_join(threads[i], NULL); | |
129 | if (ret != 0) | |
130 | goto on_error_stop; | |
131 | ||
132 | if (!args[i].success) { | |
133 | lxc_error("State server thread %d failed\n", args[i].thread_id); | |
134 | goto on_error_stop; | |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | ret = EXIT_SUCCESS; | |
140 | ||
141 | on_error_stop: | |
142 | if (c->is_running(c) && !c->stop(c)) | |
143 | lxc_error("%s\n", "Failed to stop container \"state-server\""); | |
144 | ||
145 | if (!c->destroy(c)) | |
146 | lxc_error("%s\n", "Failed to destroy container \"state-server\""); | |
147 | ||
148 | on_error_put: | |
149 | lxc_container_put(c); | |
150 | if (ret == EXIT_SUCCESS) | |
151 | lxc_debug("%s\n", "All state server tests passed"); | |
6ea8a436 | 152 | |
fc788340 CB |
153 | exit(ret); |
154 | } |