/
test-parallel.c
157 lines (130 loc) · 4.07 KB
/
test-parallel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* libguestfs
* Copyright (C) 2012 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* This test is mainly aimed at libvirt: There appear to be a lot of
* cases where libvirt is racy when creating transient guests.
* Therefore this test simply launches lots of handles in parallel for
* many minutes, hoping to reveal problems in libvirt this way.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <error.h>
#include <pthread.h>
#include "guestfs.h"
#include "guestfs-internal-frontend.h"
#include "ignore-value.h"
#define TOTAL_TIME 600 /* Seconds, excluding launch. */
#define NR_THREADS 5
struct thread_state {
pthread_t thread; /* Thread handle. */
int exit_status; /* Thread exit status. */
};
static struct thread_state threads[NR_THREADS];
static void *start_thread (void *) __attribute__((noreturn));
static volatile sig_atomic_t quit = 0;
static void
catch_sigint (int signal)
{
static char cleaning_up[] = "\ngot signal, cleaning up ...\n";
if (quit == 0) {
quit = 1;
ignore_value (write (2, cleaning_up, sizeof cleaning_up));
}
}
int
main (int argc, char *argv[])
{
char *skip;
struct sigaction sa;
int r;
size_t i, errors = 0;
void *status;
srandom (time (NULL));
/* Allow the test to be skipped by setting an environment variable. */
skip = getenv ("SKIP_TEST_PARALLEL");
if (skip && guestfs_int_is_true (skip) > 0) {
fprintf (stderr, "%s: test skipped because environment variable set.\n",
guestfs_int_program_name);
exit (77);
}
memset (&sa, 0, sizeof sa);
sa.sa_handler = catch_sigint;
sa.sa_flags = SA_RESTART;
sigaction (SIGINT, &sa, NULL);
sigaction (SIGQUIT, &sa, NULL);
for (i = 0; i < NR_THREADS; ++i) {
/* Start the thread. */
r = pthread_create (&threads[i].thread, NULL, start_thread,
&threads[i]);
if (r != 0)
error (EXIT_FAILURE, r, "pthread_create");
}
/* Wait for the threads to exit. */
for (i = 0; i < NR_THREADS; ++i) {
r = pthread_join (threads[i].thread, &status);
if (r != 0)
error (EXIT_FAILURE, r, "pthread_join");
if (*(int *)status != 0) {
fprintf (stderr, "%zu: thread returned an error\n", i);
errors++;
}
}
exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
/* Run the test in a single thread. */
static void *
start_thread (void *statevp)
{
struct thread_state *state = statevp;
guestfs_h *g;
time_t start_t, t;
time (&start_t);
for (;;) {
/* Keep testing until we run out of time. */
time (&t);
if (quit || t - start_t >= TOTAL_TIME)
break;
g = guestfs_create ();
if (g == NULL) {
perror ("guestfs_create");
state->exit_status = 1;
pthread_exit (&state->exit_status);
}
if (guestfs_add_drive_opts (g, "/dev/null",
GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
-1) == -1) {
error:
guestfs_close (g);
state->exit_status = 1;
pthread_exit (&state->exit_status);
}
if (guestfs_launch (g) == -1)
goto error;
if (guestfs_shutdown (g) == -1)
goto error;
guestfs_close (g);
}
/* Test finished successfully. */
state->exit_status = 0;
pthread_exit (&state->exit_status);
}