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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
/* 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.
*/
/* Test mount-local APIs in parallel. */
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <error.h>
#include <pthread.h>
#include "guestfs.h"
#include "guestfs-utils.h"
#include "estimate-max-threads.h"
#include "ignore-value.h"
#include "getprogname.h"
#define TOTAL_TIME 60 /* Seconds, excluding launch. */
#define DEBUG 1 /* Print overview debugging messages. */
#define MAX_THREADS 12
struct thread_state {
pthread_t thread; /* Thread handle. */
char *mp; /* Mount point. */
int exit_status; /* Thread exit status. */
};
static struct thread_state threads[MAX_THREADS];
static size_t nr_threads;
static void *start_thread (void *) __attribute__((noreturn));
static void test_mountpoint (const char *mp);
static void cleanup_thread_state (void);
static int guestunmount (const char *mp, unsigned flags);
#define GUESTUNMOUNT_SILENT 1
#define GUESTUNMOUNT_RMDIR 2
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[])
{
size_t i;
char *skip;
struct sigaction sa;
int r, errors = 0;
void *status;
srandom (time (NULL) + getpid ());
/* If the --test flag is given, then this is the test subprocess. */
if (argc == 3 && STREQ (argv[1], "--test")) {
test_mountpoint (argv[2]);
exit (EXIT_SUCCESS);
}
/* Allow the test to be skipped by setting an environment variable. */
skip = getenv ("SKIP_TEST_PARALLEL_MOUNT_LOCAL");
if (skip && guestfs_int_is_true (skip) > 0) {
fprintf (stderr, "%s: test skipped because environment variable set.\n",
getprogname ());
exit (77);
}
if (access ("/dev/fuse", W_OK) == -1) {
fprintf (stderr, "%s: test skipped because /dev/fuse is not writable.\n",
getprogname ());
exit (77);
}
/* Choose the number of threads based on the amount of free memory. */
nr_threads = MIN (MAX_THREADS, estimate_max_threads ());
memset (&sa, 0, sizeof sa);
sa.sa_handler = catch_sigint;
sa.sa_flags = SA_RESTART;
sigaction (SIGINT, &sa, NULL);
sigaction (SIGQUIT, &sa, NULL);
if (DEBUG)
printf ("starting test with %zu threads\n", nr_threads);
for (i = 0; i < nr_threads; ++i) {
/* Create a mount point for this thread to use. */
if (asprintf (&threads[i].mp, "mp%zu", i) == -1)
error (EXIT_FAILURE, errno, "asprintf");
rmdir (threads[i].mp);
if (mkdir (threads[i].mp, 0700) == -1) {
cleanup_thread_state ();
error (EXIT_FAILURE, errno, "mkdir: %s", threads[i].mp);
}
/* Start the thread. */
if (DEBUG) {
printf ("%-8s : starting thread\n", threads[i].mp);
fflush (stdout);
}
r = pthread_create (&threads[i].thread, NULL, start_thread,
&threads[i]);
if (r != 0) {
cleanup_thread_state ();
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) {
cleanup_thread_state ();
error (EXIT_FAILURE, r, "pthread_join");
}
if (*(int *)status != 0) {
fprintf (stderr, "%s: thread returned an error\n", threads[i].mp);
errors++;
}
}
cleanup_thread_state ();
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;
pid_t pid;
int status, r;
g = guestfs_create ();
if (g == NULL) {
perror ("guestfs_create");
state->exit_status = 1;
pthread_exit (&state->exit_status);
}
guestfs_set_identifier (g, state->mp);
if (guestfs_add_drive_scratch (g, 512*1024*1024, -1) == -1)
goto error;
if (guestfs_launch (g) == -1)
goto error;
if (guestfs_part_disk (g, "/dev/sda", "mbr") == -1)
goto error;
if (guestfs_mkfs (g, "ext2", "/dev/sda1") == -1)
goto error;
if (guestfs_mount (g, "/dev/sda1", "/") == -1)
goto error;
time (&start_t);
for (;;) {
/* Keep testing until we run out of time. */
time (&t);
if (quit || t - start_t >= TOTAL_TIME)
break;
if (DEBUG) {
printf ("%-8s < mounting filesystem\n", state->mp);
fflush (stdout);
}
if (guestfs_mount_local (g, state->mp, -1) == -1)
goto error;
/* Run the test in an exec'd subprocess. This minimizes the
* chance of shared file descriptors or other resources [ie.
* across clone] causing deadlocks in FUSE.
*/
pid = fork ();
if (pid == -1) {
perror ("fork");
goto error;
}
if (pid == 0) { /* child */
setpgid (0, 0); /* so we don't get ^C from parent */
execlp ("./test-parallel-mount-local",
"test-parallel-mount-local", "--test", state->mp, NULL);
perror ("execlp");
goto error;
}
/* Run the FUSE main loop. We don't really want to see libguestfs
* errors here since these are harmless.
*/
guestfs_push_error_handler (g, NULL, NULL);
r = guestfs_mount_local_run (g);
guestfs_pop_error_handler (g);
/* Wait for child process to exit and catch any errors from it. */
again:
if (waitpid (pid, &status, 0) == -1) {
if (errno == EINTR)
goto again;
perror ("waitpid");
goto error;
}
if (!WIFEXITED (status) || WEXITSTATUS (status) != 0) {
char status_string[80];
fprintf (stderr, "%s: %s\n", state->mp,
guestfs_int_exit_status_to_string (status, "test",
status_string,
sizeof status_string));
goto error;
}
if (r == -1) /* guestfs_mount_local_run above failed */
goto error;
}
if (DEBUG) {
printf ("%-8s : shutting down handle and thread\n", state->mp);
fflush (stdout);
}
if (guestfs_shutdown (g) == -1)
pthread_exit (&state->exit_status);
guestfs_close (g);
/* Test finished successfully. */
state->exit_status = 0;
pthread_exit (&state->exit_status);
error:
guestfs_close (g);
state->exit_status = 1;
pthread_exit (&state->exit_status);
}
/* This runs as a subprocess and must test the mountpoint at 'mp'. */
static void
test_mountpoint (const char *mp)
{
const int nr_passes = 5 + (random () & 31);
int pass;
int ret = EXIT_FAILURE;
FILE *fp;
if (!mp || STREQ (mp, ""))
error (EXIT_FAILURE, 0, "%s: invalid or empty mountpoint path", __func__);
if (DEBUG) {
printf ("%-8s | testing filesystem\n", mp);
fflush (stdout);
}
if (chdir (mp) == -1) {
perror (mp);
goto error;
}
/* Run through the same set of tests repeatedly a number of times.
* The aim of this stress test is repeated mount/unmount, not
* testing the FUSE data path, so we don't do much here.
*/
for (pass = 0; pass < nr_passes; ++pass) {
if (mkdir ("tmp.d", 0700) == -1) {
perror ("mkdir: tmp.d");
goto error;
}
fp = fopen ("file", "w");
if (fp == NULL) {
perror ("create: file");
goto error;
}
fprintf (fp, "hello world\n");
fclose (fp);
if (rename ("tmp.d", "newdir") == -1) {
perror ("rename tmp.d newdir");
goto error;
}
if (link ("file", "newfile") == -1) {
perror ("link: file newfile");
goto error;
}
if (rmdir ("newdir") == -1) {
perror ("rmdir: newdir");
goto error;
}
if (unlink ("file") == -1) {
perror ("unlink: file");
goto error;
}
if (unlink ("newfile") == -1) {
perror ("unlink: newfile");
goto error;
}
}
if (DEBUG) {
printf ("%-8s | test finished\n", mp);
fflush (stdout);
}
ret = EXIT_SUCCESS;
error:
ignore_value (chdir (".."));
if (guestunmount (mp, 0) == -1)
error (EXIT_FAILURE, 0, "guestunmount %s: failed, see earlier errors", mp);
if (DEBUG) {
printf ("%-8s > unmounted filesystem\n", mp);
fflush (stdout);
}
exit (ret);
}
static int
guestunmount (const char *mp, unsigned flags)
{
char cmd[256];
int status, r;
if (flags & GUESTUNMOUNT_RMDIR) {
r = rmdir (mp);
if (r == 0 || (r == -1 && errno != EBUSY && errno != ENOTCONN))
return 0;
}
snprintf (cmd, sizeof cmd,
"../../fuse/guestunmount%s %s",
(flags & GUESTUNMOUNT_SILENT) ? " --quiet" : "", mp);
status = system (cmd);
if (!WIFEXITED (status) ||
(WEXITSTATUS (status) != 0 && WEXITSTATUS (status) != 2)) {
fprintf (stderr, "guestunmount exited with bad status (%d)\n", status);
return -1;
}
if (flags & GUESTUNMOUNT_RMDIR) {
if (rmdir (mp) == -1) {
perror ("rmdir");
return -1;
}
}
return 0;
}
/* Cleanup thread state. */
static void
cleanup_thread_state (void)
{
size_t i;
for (i = 0; i < nr_threads; ++i) {
if (threads[i].mp) {
guestunmount (threads[i].mp, GUESTUNMOUNT_SILENT|GUESTUNMOUNT_RMDIR);
free (threads[i].mp);
}
}
}
|