/usr/src/blcr-0.8.5/tests/named_fifo.c is in blcr-dkms 0.8.5-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 | /*
* Berkeley Lab Checkpoint/Restart (BLCR) for Linux is Copyright (c)
* 2003, The Regents of the University of California, through Lawrence
* Berkeley National Laboratory (subject to receipt of any required
* approvals from the U.S. Dept. of Energy). All rights reserved.
*
* Portions may be copyrighted by others, as may be noted in specific
* copyright notices within specific files.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: named_fifo.c,v 1.24 2008/08/29 21:36:53 phargrov Exp $
*
* tests of named fifos
*/
/*
* NOTE: We rely on the Linux-specific (POSIX undefined) behavior for O_RDWR
* pipes. Linux does not block while opening a named FIFO with O_RDWR
*/
#define _LARGEFILE64_SOURCE 1 /* For O_LARGEFILE */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include "crut.h"
#define MAX_FILENAME_LENGTH 255
#define FIFO_FILENAME "tstfifo"
#define BUFLEN 256
#define PIPEDATA "Hello, world!\n"
#define FIFO_MODE 0644
/*
* 64-bit kernels are some what "free" with O_LARGEFILE since it is just a
* no-op for sizeof(long) == 8
*/
#if O_LARGEFILE
#define FLAGS_TO_IGNORE O_LARGEFILE
#else
#define FLAGS_TO_IGNORE 0
#endif
/*
* name is somewhat of a misnomer, since this is really a pair...
*/
struct test_fifo_struct {
char *filename;
int rdwr_fd; /* open first to avoid blocking */
int client_fd; /* test most IO ops to this */
int flags; /* rd, wr, or rdwr */
struct stat stat;
};
static int sigpiped = 0;
void
sigpipe_handler(int num)
{
sigpiped = 1;
}
struct test_fifo_struct *
get_fifo_struct(const char *filename, int flags)
{
int ret=-1;
struct test_fifo_struct *fifo;
int tmp_fd;
fifo = malloc(sizeof(*fifo));
if (fifo == NULL) {
perror("malloc");
goto out;
}
fifo->filename = strdup(filename);
if (fifo->filename == NULL) {
perror("strdup");
goto out_free;
}
(void)unlink(fifo->filename);
ret = mknod(fifo->filename, FIFO_MODE | S_IFIFO, 0);
if (ret < 0) {
perror("mknod");
goto out_free;
}
fifo->rdwr_fd = open(fifo->filename, O_RDWR);
if (fifo->rdwr_fd < 0) {
perror("open(rdwr_fd)");
goto out_unlink;
}
fifo->client_fd = open(fifo->filename, flags);
if (fifo->client_fd < 0) {
perror("open(client_fd)");
goto out_unlink;
}
ret = fcntl(fifo->client_fd, F_GETFL);
if (ret < 0) {
perror("fcntl");
goto out_unlink;
}
fifo->flags = ret & ~FLAGS_TO_IGNORE;
/* Swap order to test the restart code */
tmp_fd = fifo->rdwr_fd;
fifo->rdwr_fd = dup(fifo->rdwr_fd);
if (fifo->rdwr_fd < 0) {
perror("dup(rdwr_fd)");
goto out_unlink;
}
ret = close(tmp_fd);
if (ret < 0) {
perror("close(tmp_fd)");
goto out_unlink;
}
out:
return fifo;
out_unlink:
(void)unlink(fifo->filename);
out_free:
free(fifo->filename); /* Note: NULL is OK by spec. */
free(fifo);
return NULL;
}
/*
* This should write a atomic amount of data. Small means that
* the pipe does not block.
*
* Returns number of bytes written.
*/
static int
write_atomic(int fd)
{
char buf[BUFLEN];
int retval;
(void)strncpy(buf, PIPEDATA, sizeof(PIPEDATA));
CRUT_DEBUG("Writing to pipe");
retval = write(fd, buf, BUFLEN);
if (sigpiped) {
retval = -1;
CRUT_FAIL("SIGPIPE on write");
}
if (retval < 0) {
perror("write_atomic");
} else if (retval != BUFLEN) {
CRUT_FAIL("Wrote wrong length to pipe %d", fd);
} else if(retval == BUFLEN) {
retval = 0;
}
return retval;
}
/*
* Logical complement of read atomic.
* Checks to make sure that the buffer read back is the same as the one
* written.
*
* Returns number of bytes read.
* Returns < 0 if buffers don't compare, or other error.
*/
static int
read_atomic(int fd)
{
int retval;
int bytes_read;
char buf[BUFLEN];
memset(buf, 0, BUFLEN);
CRUT_DEBUG("Reading from pipe.");
retval = read(fd, buf, BUFLEN);
if (retval < 0) {
CRUT_FAIL("Couldn't read from pipe.");
CRUT_FAIL("buf='%s'", buf);
goto out;
}
bytes_read = retval;
CRUT_DEBUG("Comparing pipe data.");
retval = strncmp(PIPEDATA, buf, sizeof(PIPEDATA));
if (retval < 0) {
CRUT_FAIL("strncmp(%s, %s) failure.", PIPEDATA, buf);
goto out;
}
out:
return retval;
}
static int
flags_check_fifo(struct test_fifo_struct *f)
{
int retval;
retval = fcntl(f->client_fd, F_GETFL);
if (retval < 0) {
perror("fnctl(F_GETFL)");
} else if ((retval & ~FLAGS_TO_IGNORE) != f->flags) {
CRUT_FAIL("Post-restart flags %d don't match original %d\n", retval, f->flags);
retval = -1;
}
return retval;
}
static int
stat_check_fifo(struct test_fifo_struct *f)
{
int retval;
struct stat new_stat;
retval = fstat(f->rdwr_fd, &new_stat);
if (retval < 0) {
perror("fstat");
goto out;
}
retval = statcmp(&new_stat, &(f->stat), ST_SIZE | ST_MODE | ST_NLINK);
if (retval) {
CRUT_FAIL("File attributes changed. %d mismatches", retval);
CRUT_DEBUG("--- Old stats ---");
dump_stat(&(f->stat));
CRUT_DEBUG("--- Current stats ---");
dump_stat(&new_stat);
retval = -1;
}
out:
return retval;
}
static int
ring_test_fifo(struct test_fifo_struct *f)
{
int retval;
/*
* loops some data through the pipes to make sure they still work
*/
switch((f->flags) & O_ACCMODE) {
case O_RDONLY:
retval = write_atomic(f->rdwr_fd);
if (!retval) retval = read_atomic(f->client_fd);
break;
case O_WRONLY:
case O_RDWR:
retval = write_atomic(f->client_fd);
if (!retval) read_atomic(f->rdwr_fd);
break;
default:
retval = -1;
}
return retval;
}
static int
named_fifo_generic_setup(void **testdata, int flags)
{
int retval=-1;
struct test_fifo_struct *fifo;
signal(SIGPIPE, sigpipe_handler);
fifo = get_fifo_struct(FIFO_FILENAME, flags);
if (fifo == NULL) {
goto out;
}
*testdata = fifo;
retval = 0;
out:
return retval;
}
static int
named_fifo_read_setup(void **testdata)
{
int retval;
retval = named_fifo_generic_setup(testdata, O_RDONLY);
return retval;
}
static int
named_fifo_write_setup(void **testdata)
{
int retval;
retval = named_fifo_generic_setup(testdata, O_WRONLY);
return retval;
}
static int
named_fifo_rdwr_setup(void **testdata)
{
int retval;
retval = named_fifo_generic_setup(testdata, O_RDWR);
return retval;
}
static int
named_fifo_read_nonblock_setup(void **testdata)
{
int retval;
retval = named_fifo_generic_setup(testdata, O_RDONLY | O_NONBLOCK);
return retval;
}
static int
named_fifo_write_nonblock_setup(void **testdata)
{
int retval;
retval = named_fifo_generic_setup(testdata, O_WRONLY | O_NONBLOCK);
return retval;
}
static int
named_fifo_rdwr_nonblock_setup(void **testdata)
{
int retval;
retval = named_fifo_generic_setup(testdata, O_RDWR | O_NONBLOCK);
return retval;
}
static int
named_fifo_buffered_precheckpoint(void *p)
{
int retval;
struct test_fifo_struct *f = p;
switch((f->flags) & O_ACCMODE) {
case O_RDONLY:
/* reads from client_fd, so write to rdwr_fd */
retval = write_atomic(f->rdwr_fd);
break;
case O_WRONLY:
case O_RDWR:
/* writes to client_fd */
retval = write_atomic(f->client_fd);
break;
default:
retval = -1;
}
if (retval < 0) goto out;
retval = fstat(f->rdwr_fd, &f->stat);
if (retval < 0) {
perror("fstat");
}
out:
return retval;
}
static int
named_fifo_precheckpoint(void *p)
{
struct test_fifo_struct *fifo = (struct test_fifo_struct *)p;
int retval;
retval = fstat(fifo->rdwr_fd, &fifo->stat);
if (retval < 0) {
perror("fstat");
}
return retval;
}
static int
named_fifo_precheckpoint_unlink(void *p)
{
struct test_fifo_struct *fifo = (struct test_fifo_struct *)p;
int retval;
retval = unlink(fifo->filename);
if (retval) perror("unlink pre");
if (!retval) retval = named_fifo_precheckpoint(p);
return retval;
}
/*
* Unlinks the pipe after a checkpoint, to see if we can mkfifo in the
* kernel correctly on restart.
*/
static int
named_fifo_continue_unlink(void *p)
{
struct test_fifo_struct *fifo = (struct test_fifo_struct *)p;
int retval;
retval = unlink(fifo->filename);
if (retval) {
perror("unlink continue");
}
return retval;
}
static int
named_fifo_continue(void *p)
{
int retval;
retval = 0;
return retval;
}
/*
* This should take care of everything...
*/
static int
named_fifo_buffered_restart(void *p)
{
int retval;
struct test_fifo_struct *f = p;
/* Check that correct flags have been restored */
retval = flags_check_fifo(f);
if (retval < 0)
return retval;
/* Check that stat()s are OK */
retval = stat_check_fifo(f);
if (retval < 0)
return retval;
switch((f->flags) & O_ACCMODE) {
case O_RDONLY:
/* read from client_fd */
retval = read_atomic(f->client_fd);
break;
case O_WRONLY:
case O_RDWR:
/* wrote to client_fd, read from rdwr_fd */
retval = read_atomic(f->rdwr_fd);
break;
default:
retval = -1;
}
/* if that worked, ring the pipe once just to check sanity. */
if (!retval) {
retval = ring_test_fifo(f);
}
return retval;
}
static int
named_fifo_restart(void *p)
{
struct test_fifo_struct *f = p;
int retval;
/* Check that correct flags have been restored */
retval = flags_check_fifo(f);
if (retval < 0) {
return retval;
}
/* Check that stat()s are OK */
retval = stat_check_fifo(f);
if (retval < 0)
return retval;
/* if that worked, ring the pipe once just to check sanity. */
return ring_test_fifo(f);
}
static int
named_fifo_teardown(void *p)
{
struct test_fifo_struct *fifo = (struct test_fifo_struct *)p;
int retval;
retval = unlink(fifo->filename);
if (retval) perror("unlink teardown");
free(fifo->filename);
free(fifo);
return retval;
}
static int
named_fifo_teardown_preunlink(void *p)
{
struct test_fifo_struct *fifo = (struct test_fifo_struct *)p;
int retval;
/* Unlink must fail, since fifo should have been renamed */
retval = !unlink(fifo->filename);
if (retval) fprintf(stderr, "unlink succeeded unexpectedly\n");
free(fifo->filename);
free(fifo);
return retval;
}
int
main(int argc, char *argv[])
{
int ret;
/*
* cases for named fifos...
*
* rd, wr, rdwr - normal
* rd, wr, rdwr - unlinked after checkpoint
* rd, wr, rdwr - unlinked before checkpoint
* rd, wr, rdwr - unlinked
* rd, wr, rdwr - data in the pipe
* rd, wr, rdwr - nonblocking
*
* Only care about the above, this tests most code paths, could form
* arbitrarily silly combinations, though...
*/
struct crut_operations named_fifo_rw_ops = {
test_scope:CR_SCOPE_PROC,
test_name:"named_fifo_rw",
test_description:"Tests basic IO on fifos",
test_setup:named_fifo_rdwr_setup,
test_precheckpoint:named_fifo_precheckpoint,
test_continue:named_fifo_continue,
test_restart:named_fifo_restart,
test_teardown:named_fifo_teardown,
};
struct crut_operations named_fifo_rw_unlink_ops = {
test_scope:CR_SCOPE_PROC,
test_name:"named_fifo_rw_unlink",
test_description:"Tests whether we mknod FIFOs on restart",
test_setup:named_fifo_rdwr_setup,
test_precheckpoint:named_fifo_precheckpoint,
test_continue:named_fifo_continue_unlink,
test_restart:named_fifo_restart,
test_teardown:named_fifo_teardown,
};
struct crut_operations named_fifo_rw_preunlink_ops = {
test_scope:CR_SCOPE_PROC,
test_name:"named_fifo_rw_preunlink",
test_description:"Tests whether we checkpoint unlinked FIFOs",
test_setup:named_fifo_rdwr_setup,
test_precheckpoint:named_fifo_precheckpoint_unlink,
test_continue:named_fifo_continue,
test_restart:named_fifo_restart,
test_teardown:named_fifo_teardown_preunlink,
};
struct crut_operations named_fifo_rw_buffered_ops = {
test_scope:CR_SCOPE_PROC,
test_name:"named_fifo_rw_buffered",
test_description:"Makes sure that data in the FIFO is recovered after a checkpoint.",
test_setup:named_fifo_rdwr_setup,
test_precheckpoint:named_fifo_buffered_precheckpoint,
test_continue:named_fifo_continue,
test_restart:named_fifo_buffered_restart,
test_teardown:named_fifo_teardown,
};
struct crut_operations named_fifo_rw_nonblock_ops = {
test_scope:CR_SCOPE_PROC,
test_name:"named_fifo_rw_nonblock",
test_description:"Makes sure that data in the FIFO is recovered after a checkpoint.",
test_setup:named_fifo_rdwr_nonblock_setup,
test_precheckpoint:named_fifo_buffered_precheckpoint,
test_continue:named_fifo_continue,
test_restart:named_fifo_buffered_restart,
test_teardown:named_fifo_teardown,
};
struct crut_operations named_fifo_read_ops = {
test_scope:CR_SCOPE_PROC,
test_name:"named_fifo_read",
test_description:"Tests basic IO on fifos",
test_setup:named_fifo_read_setup,
test_precheckpoint:named_fifo_precheckpoint,
test_continue:named_fifo_continue,
test_restart:named_fifo_restart,
test_teardown:named_fifo_teardown,
};
struct crut_operations named_fifo_read_unlink_ops = {
test_scope:CR_SCOPE_PROC,
test_name:"named_fifo_read_unlink",
test_description:"Tests whether we mknod FIFOs on restart",
test_setup:named_fifo_read_setup,
test_precheckpoint:named_fifo_precheckpoint,
test_continue:named_fifo_continue_unlink,
test_restart:named_fifo_restart,
test_teardown:named_fifo_teardown,
};
struct crut_operations named_fifo_read_preunlink_ops = {
test_scope:CR_SCOPE_PROC,
test_name:"named_fifo_read_preunlink",
test_description:"Tests whether we checkpoint unlinked FIFOs",
test_setup:named_fifo_rdwr_setup,
test_precheckpoint:named_fifo_precheckpoint_unlink,
test_continue:named_fifo_continue,
test_restart:named_fifo_restart,
test_teardown:named_fifo_teardown_preunlink,
};
struct crut_operations named_fifo_read_buffered_ops = {
test_scope:CR_SCOPE_PROC,
test_name:"named_fifo_read_buffered",
test_description:"Makes sure that data in the FIFO is recovered after a checkpoint.",
test_setup:named_fifo_read_setup,
test_precheckpoint:named_fifo_buffered_precheckpoint,
test_continue:named_fifo_continue,
test_restart:named_fifo_buffered_restart,
test_teardown:named_fifo_teardown,
};
struct crut_operations named_fifo_read_nonblock_ops = {
test_scope:CR_SCOPE_PROC,
test_name:"named_fifo_read_nonblock",
test_description:"Makes sure that data in the FIFO is recovered after a checkpoint.",
test_setup:named_fifo_read_nonblock_setup,
test_precheckpoint:named_fifo_buffered_precheckpoint,
test_continue:named_fifo_continue,
test_restart:named_fifo_buffered_restart,
test_teardown:named_fifo_teardown,
};
struct crut_operations named_fifo_write_ops = {
test_scope:CR_SCOPE_PROC,
test_name:"named_fifo_write",
test_description:"Tests basic IO on fifos",
test_setup:named_fifo_write_setup,
test_precheckpoint:named_fifo_precheckpoint,
test_continue:named_fifo_continue,
test_restart:named_fifo_restart,
test_teardown:named_fifo_teardown,
};
struct crut_operations named_fifo_write_unlink_ops = {
test_scope:CR_SCOPE_PROC,
test_name:"named_fifo_write_unlink",
test_description:"Tests whether we mknod FIFOs on restart",
test_setup:named_fifo_write_setup,
test_precheckpoint:named_fifo_precheckpoint,
test_continue:named_fifo_continue_unlink,
test_restart:named_fifo_restart,
test_teardown:named_fifo_teardown,
};
struct crut_operations named_fifo_write_preunlink_ops = {
test_scope:CR_SCOPE_PROC,
test_name:"named_fifo_write_preunlink",
test_description:"Tests whether we checkpoint unlinked FIFOs",
test_setup:named_fifo_rdwr_setup,
test_precheckpoint:named_fifo_precheckpoint_unlink,
test_continue:named_fifo_continue,
test_restart:named_fifo_restart,
test_teardown:named_fifo_teardown_preunlink,
};
struct crut_operations named_fifo_write_buffered_ops = {
test_scope:CR_SCOPE_PROC,
test_name:"named_fifo_write_buffered",
test_description:"Makes sure that data in the FIFO is recovered after a checkpoint.",
test_setup:named_fifo_write_setup,
test_precheckpoint:named_fifo_buffered_precheckpoint,
test_continue:named_fifo_continue,
test_restart:named_fifo_buffered_restart,
test_teardown:named_fifo_teardown,
};
struct crut_operations named_fifo_write_nonblock_ops = {
test_scope:CR_SCOPE_PROC,
test_name:"named_fifo_write_nonblock",
test_description:"Makes sure that data in the FIFO is recovered after a checkpoint.",
test_setup:named_fifo_write_nonblock_setup,
test_precheckpoint:named_fifo_buffered_precheckpoint,
test_continue:named_fifo_continue,
test_restart:named_fifo_buffered_restart,
test_teardown:named_fifo_teardown,
};
/* add the basic tests */
crut_add_test(&named_fifo_read_ops);
crut_add_test(&named_fifo_write_ops);
crut_add_test(&named_fifo_rw_ops);
crut_add_test(&named_fifo_read_unlink_ops);
crut_add_test(&named_fifo_write_unlink_ops);
crut_add_test(&named_fifo_rw_unlink_ops);
crut_add_test(&named_fifo_read_preunlink_ops);
crut_add_test(&named_fifo_write_preunlink_ops);
crut_add_test(&named_fifo_rw_preunlink_ops);
crut_add_test(&named_fifo_read_buffered_ops);
crut_add_test(&named_fifo_write_buffered_ops);
crut_add_test(&named_fifo_rw_buffered_ops);
crut_add_test(&named_fifo_read_nonblock_ops);
crut_add_test(&named_fifo_write_nonblock_ops);
crut_add_test(&named_fifo_rw_nonblock_ops);
ret = crut_main(argc, argv);
exit(ret);
}
|