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
|
/* libguestfs
* Copyright (C) 2011 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 user cancellation.
*
* We perform the test using two threads. The main thread issues
* guestfs commands to download and upload large files. Uploads and
* downloads are done to/from a pipe which is connected back to the
* current process. The second test thread sits on the other end of
* the pipe, feeding or consuming data slowly, and injecting the user
* cancel events at a particular place in the transfer.
*
* It is important to test both download and upload separately, since
* these exercise different code paths in the library. However this
* adds complexity here because these tests are symmetric-but-opposite
* cases.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <error.h>
#include <sys/time.h>
#include <math.h>
#include <pthread.h>
#include "cloexec.h"
#include "guestfs.h"
#include "guestfs-utils.h"
static const off_t filesize = 1024*1024*1024;
static void *start_test_thread (void *) __attribute__((noreturn));
static off_t random_cancel_posn (void);
struct test_thread_data {
guestfs_h *g; /* handle */
int direction; /* direction of transfer */
#define DIRECTION_UP 1 /* upload (test thread is writing) */
#define DIRECTION_DOWN 2 /* download (test thread is reading) */
int fd; /* pipe to read/write */
off_t cancel_posn; /* position at which to cancel */
off_t transfer_size; /* how much data thread wrote/read */
};
int
main (int argc, char *argv[])
{
guestfs_h *g;
pthread_t test_thread;
struct test_thread_data data;
int fds[2], r, op_error, op_errno, errors = 0;
char dev_fd[64];
srand48 (time (NULL));
g = guestfs_create ();
if (g == NULL)
error (EXIT_FAILURE, errno, "guestfs_create");
if (guestfs_add_drive_scratch (g, filesize, -1) == -1)
exit (EXIT_FAILURE);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);
if (guestfs_part_disk (g, "/dev/sda", "mbr") == -1)
exit (EXIT_FAILURE);
if (guestfs_mkfs (g, "ext2", "/dev/sda1") == -1)
exit (EXIT_FAILURE);
if (guestfs_mount (g, "/dev/sda1", "/") == -1)
exit (EXIT_FAILURE);
/*----- Upload cancellation test -----*/
data.g = g;
data.direction = DIRECTION_UP;
if (pipe (fds) == -1)
error (EXIT_FAILURE, errno, "pipe");
/* We don't want the pipe to be passed to subprocesses. */
if (set_cloexec_flag (fds[0], 1) == -1 ||
set_cloexec_flag (fds[1], 1) == -1)
error (EXIT_FAILURE, errno, "set_cloexec_flag");
data.fd = fds[1];
snprintf (dev_fd, sizeof dev_fd, "/dev/fd/%d", fds[0]);
data.cancel_posn = random_cancel_posn ();
/* Create the test thread. */
r = pthread_create (&test_thread, NULL, start_test_thread, &data);
if (r != 0)
error (EXIT_FAILURE, r, "pthread_create");
/* Do the upload. */
op_error = guestfs_upload (g, dev_fd, "/upload");
op_errno = guestfs_last_errno (g);
/* Kill the test thread and clean up. */
r = pthread_cancel (test_thread);
if (r != 0)
error (EXIT_FAILURE, r, "pthread_cancel");
r = pthread_join (test_thread, NULL);
if (r != 0)
error (EXIT_FAILURE, r, "pthread_join");
close (fds[0]);
close (fds[1]);
/* We expect to get an error, with errno == EINTR. */
if (op_error == -1 && op_errno == EINTR)
printf ("test-user-cancel: upload cancellation test passed (%ld/%ld)\n",
(long) data.cancel_posn, (long) data.transfer_size);
else {
fprintf (stderr, "test-user-cancel: upload cancellation test FAILED\n");
fprintf (stderr, "cancel_posn %ld, upload returned %d, errno = %d (%s)\n",
(long) data.cancel_posn, op_error, op_errno, strerror (op_errno));
errors++;
}
if (guestfs_rm (g, "/upload") == -1)
exit (EXIT_FAILURE);
/*----- Download cancellation test -----*/
if (guestfs_touch (g, "/download") == -1)
exit (EXIT_FAILURE);
if (guestfs_truncate_size (g, "/download", filesize/4) == -1)
exit (EXIT_FAILURE);
data.g = g;
data.direction = DIRECTION_DOWN;
if (pipe (fds) == -1)
error (EXIT_FAILURE, errno, "pipe");
/* We don't want the pipe to be passed to subprocesses. */
if (set_cloexec_flag (fds[0], 1) == -1 ||
set_cloexec_flag (fds[1], 1) == -1)
error (EXIT_FAILURE, errno, "set_cloexec_flag");
data.fd = fds[0];
snprintf (dev_fd, sizeof dev_fd, "/dev/fd/%d", fds[1]);
data.cancel_posn = random_cancel_posn ();
/* Create the test thread. */
r = pthread_create (&test_thread, NULL, start_test_thread, &data);
if (r != 0)
error (EXIT_FAILURE, r, "pthread_create");
/* Do the download. */
op_error = guestfs_download (g, "/download", dev_fd);
op_errno = guestfs_last_errno (g);
/* Kill the test thread and clean up. */
r = pthread_cancel (test_thread);
if (r != 0)
error (EXIT_FAILURE, r, "pthread_cancel");
r = pthread_join (test_thread, NULL);
if (r != 0)
error (EXIT_FAILURE, r, "pthread_join");
close (fds[0]);
close (fds[1]);
/* We expect to get an error, with errno == EINTR. */
if (op_error == -1 && op_errno == EINTR)
printf ("test-user-cancel: download cancellation test passed (%ld/%ld)\n",
(long) data.cancel_posn, (long) data.transfer_size);
else {
fprintf (stderr, "test-user-cancel: download cancellation test FAILED\n");
fprintf (stderr, "cancel_posn %ld, upload returned %d, errno = %d (%s)\n",
(long) data.cancel_posn, op_error, op_errno, strerror (op_errno));
errors++;
}
exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
static char buffer[BUFSIZ];
static void *
start_test_thread (void *datav)
{
struct test_thread_data *data = datav;
ssize_t r;
size_t n;
data->transfer_size = 0;
if (data->direction == DIRECTION_UP) { /* thread is writing */
/* Feed data in, up to the cancellation point. */
while (data->transfer_size < data->cancel_posn) {
n = MIN (sizeof buffer,
(size_t) (data->cancel_posn - data->transfer_size));
r = write (data->fd, buffer, n);
if (r == -1)
error (EXIT_FAILURE, errno,
"test thread: write to pipe before user cancel");
data->transfer_size += r;
}
/* Keep feeding data after the cancellation point for as long as
* the main thread wants it.
*/
while (1) {
/* Repeatedly assert the cancel flag. We have to do this because
* the guestfs_upload command in the main thread may not have
* started yet.
*/
guestfs_user_cancel (data->g);
r = write (data->fd, buffer, sizeof buffer);
if (r == -1)
error (EXIT_FAILURE, errno,
"test thread: write to pipe after user cancel");
data->transfer_size += r;
}
} else { /* thread is reading */
/* Sink data, up to the cancellation point. */
while (data->transfer_size < data->cancel_posn) {
n = MIN (sizeof buffer,
(size_t) (data->cancel_posn - data->transfer_size));
r = read (data->fd, buffer, n);
if (r == -1)
error (EXIT_FAILURE, errno,
"test thread: read from pipe before user cancel");
if (r == 0)
error (EXIT_FAILURE, errno,
"test thread: unexpected end of file before user cancel");
data->transfer_size += r;
}
/* Do user cancellation. */
guestfs_user_cancel (data->g);
/* Keep sinking data as long as the main thread is writing. */
while (1) {
r = read (data->fd, buffer, sizeof buffer);
if (r == -1)
error (EXIT_FAILURE, errno,
"test thread: read from pipe after user cancel");
if (r == 0)
break;
data->transfer_size += r;
}
while (1)
pause ();
}
}
static double random_gauss (double mu, double sd);
/* Generate a random cancellation position, but skew it towards
* smaller numbers.
*/
static off_t
random_cancel_posn (void)
{
const double mu = 65536;
const double sd = 65536 * 4;
double r;
do {
r = random_gauss (mu, sd);
} while (r <= 0);
return (off_t) r;
}
/* Generate a random Gaussian distributed number using the Box-Muller
* transformation. (http://www.taygeta.com/random/gaussian.html)
*/
static double
random_gauss (double mu, double sd)
{
double x1, x2, w, y1;
do {
x1 = 2. * drand48 () - 1.;
x2 = 2. * drand48 () - 1.;
w = x1 * x1 + x2 * x2;
} while (w >= 1.);
w = sqrt ((-2. * log (w)) / w);
y1 = x1 * w;
//y2 = x2 * w;
return mu + y1 * sd;
}
|