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 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
|
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/xattr.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
int
fd_based_fops_1(char *filename); // for fd based fops after unlink
int
fd_based_fops_2(char *filename); // for fd based fops before unlink
int
dup_fd_based_fops(char *filename); // fops based on fd after dup
int
path_based_fops(char *filename); // for fops based on path
int
dir_based_fops(char *filename); // for fops which operate on directory
int
link_based_fops(
char *filename); // for fops which operate in link files (symlinks)
int
test_open_modes(
char *filename); // to test open syscall with open modes available.
int
generic_open_read_write(
char *filename,
int flag); // generic function which does open write and read.
int
main(int argc, char *argv[])
{
int ret = -1;
char filename[255] = {
0,
};
if (argc > 1)
strcpy(filename, argv[1]);
else
strcpy(filename, "temp-xattr-test-file");
ret = fd_based_fops_1(strcat(filename, "_1"));
if (ret < 0)
fprintf(stderr, "fd based file operation 1 failed\n");
else
fprintf(stdout, "fd based file operation 1 passed\n");
ret = fd_based_fops_2(strcat(filename, "_2"));
if (ret < 0)
fprintf(stderr, "fd based file operation 2 failed\n");
else
fprintf(stdout, "fd based file operation 2 passed\n");
ret = dup_fd_based_fops(strcat(filename, "_3"));
if (ret < 0)
fprintf(stderr, "dup fd based file operation failed\n");
else
fprintf(stdout, "dup fd based file operation passed\n");
ret = path_based_fops(strcat(filename, "_4"));
if (ret < 0)
fprintf(stderr, "path based file operation failed\n");
else
fprintf(stdout, "path based file operation passed\n");
ret = dir_based_fops(strcat(filename, "_5"));
if (ret < 0)
fprintf(stderr, "directory based file operation failed\n");
else
fprintf(stdout, "directory based file operation passed\n");
ret = link_based_fops(strcat(filename, "_5"));
if (ret < 0)
fprintf(stderr, "link based file operation failed\n");
else
fprintf(stdout, "link based file operation passed\n");
ret = test_open_modes(strcat(filename, "_5"));
if (ret < 0)
fprintf(stderr, "testing modes of 'open' call failed\n");
else
fprintf(stdout, "testing modes of 'open' call passed\n");
out:
return ret;
}
int
fd_based_fops_1(char *filename)
{
int fd = 0;
int ret = -1;
struct stat stbuf = {
0,
};
char wstr[50] = {
0,
};
char rstr[50] = {
0,
};
fd = open(filename, O_RDWR | O_CREAT);
if (fd < 0) {
fd = 0;
fprintf(stderr, "open failed : %s\n", strerror(errno));
goto out;
}
ret = unlink(filename);
if (ret < 0) {
fprintf(stderr, "unlink failed : %s\n", strerror(errno));
goto out;
}
strcpy(wstr, "This is my string\n");
ret = write(fd, wstr, strlen(wstr));
if (ret <= 0) {
ret = -1;
fprintf(stderr, "write failed: %s\n", strerror(errno));
goto out;
}
ret = lseek(fd, 0, SEEK_SET);
if (ret < 0) {
fprintf(stderr, "lseek failed: %s\n", strerror(errno));
goto out;
}
ret = read(fd, rstr, strlen(wstr));
if (ret <= 0) {
ret = -1;
fprintf(stderr, "read failed: %s\n", strerror(errno));
goto out;
}
ret = memcmp(rstr, wstr, strlen(wstr));
if (ret != 0) {
ret = -1;
fprintf(stderr, "read returning junk\n");
goto out;
}
ret = ftruncate(fd, 0);
if (ret < 0) {
fprintf(stderr, "ftruncate failed : %s\n", strerror(errno));
goto out;
}
ret = fstat(fd, &stbuf);
if (ret < 0) {
fprintf(stderr, "fstat failed : %s\n", strerror(errno));
goto out;
}
ret = fchmod(fd, 0640);
if (ret < 0) {
fprintf(stderr, "fchmod failed : %s\n", strerror(errno));
goto out;
}
ret = fchown(fd, 10001, 10001);
if (ret < 0) {
fprintf(stderr, "fchown failed : %s\n", strerror(errno));
goto out;
}
ret = fsync(fd);
if (ret < 0) {
fprintf(stderr, "fsync failed : %s\n", strerror(errno));
goto out;
}
ret = fsetxattr(fd, "trusted.xattr-test", "working", 8, 0);
if (ret < 0) {
fprintf(stderr, "fsetxattr failed : %s\n", strerror(errno));
goto out;
}
ret = fdatasync(fd);
if (ret < 0) {
fprintf(stderr, "fdatasync failed : %s\n", strerror(errno));
goto out;
}
ret = flistxattr(fd, NULL, 0);
if (ret <= 0) {
ret = -1;
fprintf(stderr, "flistxattr failed : %s\n", strerror(errno));
goto out;
}
ret = fgetxattr(fd, "trusted.xattr-test", NULL, 0);
if (ret <= 0) {
ret = -1;
fprintf(stderr, "fgetxattr failed : %s\n", strerror(errno));
goto out;
}
ret = fremovexattr(fd, "trusted.xattr-test");
if (ret < 0) {
fprintf(stderr, "fremovexattr failed : %s\n", strerror(errno));
goto out;
}
ret = 0;
out:
if (fd)
close(fd);
return ret;
}
int
fd_based_fops_2(char *filename)
{
int fd = 0;
int ret = -1;
struct stat stbuf = {
0,
};
char wstr[50] = {
0,
};
char rstr[50] = {
0,
};
fd = open(filename, O_RDWR | O_CREAT);
if (fd < 0) {
fd = 0;
fprintf(stderr, "open failed : %s\n", strerror(errno));
goto out;
}
ret = ftruncate(fd, 0);
if (ret < 0) {
fprintf(stderr, "ftruncate failed : %s\n", strerror(errno));
goto out;
}
strcpy(wstr, "This is my second string\n");
ret = write(fd, wstr, strlen(wstr));
if (ret < 0) {
ret = -1;
fprintf(stderr, "write failed: %s\n", strerror(errno));
goto out;
}
lseek(fd, 0, SEEK_SET);
if (ret < 0) {
fprintf(stderr, "lseek failed: %s\n", strerror(errno));
goto out;
}
ret = read(fd, rstr, strlen(wstr));
if (ret <= 0) {
ret = -1;
fprintf(stderr, "read failed: %s\n", strerror(errno));
goto out;
}
ret = memcmp(rstr, wstr, strlen(wstr));
if (ret != 0) {
ret = -1;
fprintf(stderr, "read returning junk\n");
goto out;
}
ret = fstat(fd, &stbuf);
if (ret < 0) {
fprintf(stderr, "fstat failed : %s\n", strerror(errno));
goto out;
}
ret = fchmod(fd, 0640);
if (ret < 0) {
fprintf(stderr, "fchmod failed : %s\n", strerror(errno));
goto out;
}
ret = fchown(fd, 10001, 10001);
if (ret < 0) {
fprintf(stderr, "fchown failed : %s\n", strerror(errno));
goto out;
}
ret = fsync(fd);
if (ret < 0) {
fprintf(stderr, "fsync failed : %s\n", strerror(errno));
goto out;
}
ret = fsetxattr(fd, "trusted.xattr-test", "working", 8, 0);
if (ret < 0) {
fprintf(stderr, "fsetxattr failed : %s\n", strerror(errno));
goto out;
}
ret = fdatasync(fd);
if (ret < 0) {
fprintf(stderr, "fdatasync failed : %s\n", strerror(errno));
goto out;
}
ret = flistxattr(fd, NULL, 0);
if (ret <= 0) {
ret = -1;
fprintf(stderr, "flistxattr failed : %s\n", strerror(errno));
goto out;
}
ret = fgetxattr(fd, "trusted.xattr-test", NULL, 0);
if (ret <= 0) {
ret = -1;
fprintf(stderr, "fgetxattr failed : %s\n", strerror(errno));
goto out;
}
ret = fremovexattr(fd, "trusted.xattr-test");
if (ret < 0) {
fprintf(stderr, "fremovexattr failed : %s\n", strerror(errno));
goto out;
}
out:
if (fd)
close(fd);
unlink(filename);
return ret;
}
int
path_based_fops(char *filename)
{
int ret = -1;
int fd = 0;
struct stat stbuf = {
0,
};
char newfilename[255] = {
0,
};
fd = creat(filename, 0644);
if (fd < 0) {
fprintf(stderr, "creat failed: %s\n", strerror(errno));
goto out;
}
ret = truncate(filename, 0);
if (ret < 0) {
fprintf(stderr, "truncate failed: %s\n", strerror(errno));
goto out;
}
ret = stat(filename, &stbuf);
if (ret < 0) {
fprintf(stderr, "stat failed: %s\n", strerror(errno));
goto out;
}
ret = chmod(filename, 0640);
if (ret < 0) {
fprintf(stderr, "chmod failed: %s\n", strerror(errno));
goto out;
}
ret = chown(filename, 10001, 10001);
if (ret < 0) {
fprintf(stderr, "chown failed: %s\n", strerror(errno));
goto out;
}
ret = setxattr(filename, "trusted.xattr-test", "working", 8, 0);
if (ret < 0) {
fprintf(stderr, "setxattr failed: %s\n", strerror(errno));
goto out;
}
ret = listxattr(filename, NULL, 0);
if (ret <= 0) {
ret = -1;
fprintf(stderr, "listxattr failed: %s\n", strerror(errno));
goto out;
}
ret = getxattr(filename, "trusted.xattr-test", NULL, 0);
if (ret <= 0) {
ret = -1;
fprintf(stderr, "getxattr failed: %s\n", strerror(errno));
goto out;
}
ret = removexattr(filename, "trusted.xattr-test");
if (ret < 0) {
fprintf(stderr, "removexattr failed: %s\n", strerror(errno));
goto out;
}
ret = access(filename, R_OK | W_OK);
if (ret < 0) {
fprintf(stderr, "access failed: %s\n", strerror(errno));
goto out;
}
strcpy(newfilename, filename);
strcat(newfilename, "_new");
ret = rename(filename, newfilename);
if (ret < 0) {
fprintf(stderr, "rename failed: %s\n", strerror(errno));
goto out;
}
unlink(newfilename);
out:
if (fd)
close(fd);
unlink(filename);
return ret;
}
int
dup_fd_based_fops(char *filename)
{
int fd = 0;
int newfd = 0;
int ret = -1;
struct stat stbuf = {
0,
};
char wstr[50] = {
0,
};
char rstr[50] = {
0,
};
fd = open(filename, O_RDWR | O_CREAT);
if (fd < 0) {
fd = 0;
fprintf(stderr, "open failed : %s\n", strerror(errno));
goto out;
}
newfd = dup(fd);
if (newfd < 0) {
ret = -1;
fprintf(stderr, "dup failed: %s\n", strerror(errno));
goto out;
}
close(fd);
strcpy(wstr, "This is my string\n");
ret = write(newfd, wstr, strlen(wstr));
if (ret <= 0) {
ret = -1;
fprintf(stderr, "write failed: %s\n", strerror(errno));
goto out;
}
ret = lseek(newfd, 0, SEEK_SET);
if (ret < 0) {
fprintf(stderr, "lseek failed: %s\n", strerror(errno));
goto out;
}
ret = read(newfd, rstr, strlen(wstr));
if (ret <= 0) {
ret = -1;
fprintf(stderr, "read failed: %s\n", strerror(errno));
goto out;
}
ret = memcmp(rstr, wstr, strlen(wstr));
if (ret != 0) {
ret = -1;
fprintf(stderr, "read returning junk\n");
goto out;
}
ret = ftruncate(newfd, 0);
if (ret < 0) {
fprintf(stderr, "ftruncate failed : %s\n", strerror(errno));
goto out;
}
ret = fstat(newfd, &stbuf);
if (ret < 0) {
fprintf(stderr, "fstat failed : %s\n", strerror(errno));
goto out;
}
ret = fchmod(newfd, 0640);
if (ret < 0) {
fprintf(stderr, "fchmod failed : %s\n", strerror(errno));
goto out;
}
ret = fchown(newfd, 10001, 10001);
if (ret < 0) {
fprintf(stderr, "fchown failed : %s\n", strerror(errno));
goto out;
}
ret = fsync(newfd);
if (ret < 0) {
fprintf(stderr, "fsync failed : %s\n", strerror(errno));
goto out;
}
ret = fsetxattr(newfd, "trusted.xattr-test", "working", 8, 0);
if (ret < 0) {
fprintf(stderr, "fsetxattr failed : %s\n", strerror(errno));
goto out;
}
ret = fdatasync(newfd);
if (ret < 0) {
fprintf(stderr, "fdatasync failed : %s\n", strerror(errno));
goto out;
}
ret = flistxattr(newfd, NULL, 0);
if (ret <= 0) {
ret = -1;
fprintf(stderr, "flistxattr failed : %s\n", strerror(errno));
goto out;
}
ret = fgetxattr(newfd, "trusted.xattr-test", NULL, 0);
if (ret <= 0) {
ret = -1;
fprintf(stderr, "fgetxattr failed : %s\n", strerror(errno));
goto out;
}
ret = fremovexattr(newfd, "trusted.xattr-test");
if (ret < 0) {
fprintf(stderr, "fremovexattr failed : %s\n", strerror(errno));
goto out;
}
ret = 0;
out:
if (newfd)
close(newfd);
ret = unlink(filename);
if (ret < 0)
fprintf(stderr, "unlink failed : %s\n", strerror(errno));
return ret;
}
int
dir_based_fops(char *dirname)
{
int ret = -1;
DIR *dp = NULL;
char buff[255] = {
0,
};
struct dirent *dbuff = {
0,
};
struct stat stbuff = {
0,
};
char newdname[255] = {
0,
};
char *cwd = NULL;
ret = mkdir(dirname, 0755);
if (ret < 0) {
fprintf(stderr, "mkdir failed: %s\n", strerror(errno));
goto out;
}
dp = opendir(dirname);
if (dp == NULL) {
fprintf(stderr, "opendir failed: %s\n", strerror(errno));
goto out;
}
dbuff = readdir(dp);
if (NULL == dbuff) {
fprintf(stderr, "readdir failed: %s\n", strerror(errno));
goto out;
}
ret = closedir(dp);
if (ret < 0) {
fprintf(stderr, "closedir failed: %s\n", strerror(errno));
goto out;
}
ret = stat(dirname, &stbuff);
if (ret < 0) {
fprintf(stderr, "stat failed: %s\n", strerror(errno));
goto out;
}
ret = chmod(dirname, 0744);
if (ret < 0) {
fprintf(stderr, "chmod failed: %s\n", strerror(errno));
goto out;
}
ret = chown(dirname, 10001, 10001);
if (ret < 0) {
fprintf(stderr, "chmod failed: %s\n", strerror(errno));
goto out;
}
ret = setxattr(dirname, "trusted.xattr-test", "working", 8, 0);
if (ret < 0) {
fprintf(stderr, "setxattr failed: %s\n", strerror(errno));
goto out;
}
ret = listxattr(dirname, NULL, 0);
if (ret <= 0) {
ret = -1;
fprintf(stderr, "listxattr failed: %s\n", strerror(errno));
goto out;
}
ret = getxattr(dirname, "trusted.xattr-test", NULL, 0);
if (ret <= 0) {
ret = -1;
fprintf(stderr, "getxattr failed: %s\n", strerror(errno));
goto out;
}
ret = removexattr(dirname, "trusted.xattr-test");
if (ret < 0) {
fprintf(stderr, "removexattr failed: %s\n", strerror(errno));
goto out;
}
strcpy(newdname, dirname);
strcat(newdname, "/../");
ret = chdir(newdname);
if (ret < 0) {
fprintf(stderr, "chdir failed: %s\n", strerror(errno));
goto out;
}
cwd = getcwd(buff, 255);
if (NULL == cwd) {
fprintf(stderr, "getcwd failed: %s\n", strerror(errno));
goto out;
}
strcpy(newdname, dirname);
strcat(newdname, "new");
ret = rename(dirname, newdname);
if (ret < 0) {
fprintf(stderr, "rename failed: %s\n", strerror(errno));
goto out;
}
ret = rmdir(newdname);
if (ret < 0) {
fprintf(stderr, "rmdir failed: %s\n", strerror(errno));
return ret;
}
out:
rmdir(dirname);
return ret;
}
int
link_based_fops(char *filename)
{
int ret = -1;
int fd = 0;
char newname[255] = {
0,
};
char linkname[255] = {
0,
};
struct stat lstbuf = {
0,
};
fd = creat(filename, 0644);
if (fd < 0) {
fd = 0;
fprintf(stderr, "creat failed: %s\n", strerror(errno));
goto out;
}
strcpy(newname, filename);
strcat(newname, "_hlink");
ret = link(filename, newname);
if (ret < 0) {
fprintf(stderr, "link failed: %s\n", strerror(errno));
goto out;
}
ret = unlink(filename);
if (ret < 0) {
fprintf(stderr, "unlink failed: %s\n", strerror(errno));
goto out;
}
strcpy(linkname, filename);
strcat(linkname, "_slink");
ret = symlink(newname, linkname);
if (ret < 0) {
fprintf(stderr, "symlink failed: %s\n", strerror(errno));
goto out;
}
ret = lstat(linkname, &lstbuf);
if (ret < 0) {
fprintf(stderr, "lstbuf failed: %s\n", strerror(errno));
goto out;
}
ret = lchown(linkname, 10001, 10001);
if (ret < 0) {
fprintf(stderr, "lchown failed: %s\n", strerror(errno));
goto out;
}
ret = lsetxattr(linkname, "trusted.lxattr-test", "working", 8, 0);
if (ret < 0) {
fprintf(stderr, "lsetxattr failed: %s\n", strerror(errno));
goto out;
}
ret = llistxattr(linkname, NULL, 0);
if (ret < 0) {
ret = -1;
fprintf(stderr, "llistxattr failed: %s\n", strerror(errno));
goto out;
}
ret = lgetxattr(linkname, "trusted.lxattr-test", NULL, 0);
if (ret < 0) {
ret = -1;
fprintf(stderr, "lgetxattr failed: %s\n", strerror(errno));
goto out;
}
ret = lremovexattr(linkname, "trusted.lxattr-test");
if (ret < 0) {
fprintf(stderr, "lremovexattr failed: %s\n", strerror(errno));
goto out;
}
out:
if (fd)
close(fd);
unlink(linkname);
unlink(newname);
}
int
test_open_modes(char *filename)
{
int ret = -1;
ret = generic_open_read_write(filename, O_CREAT | O_WRONLY);
if (3 != ret) {
fprintf(stderr, "flag O_CREAT|O_WRONLY failed: \n");
goto out;
}
ret = generic_open_read_write(filename, O_CREAT | O_RDWR);
if (ret != 0) {
fprintf(stderr, "flag O_CREAT|O_RDWR failed\n");
goto out;
}
ret = generic_open_read_write(filename, O_CREAT | O_RDONLY);
if (ret != 0) {
fprintf(stderr, "flag O_CREAT|O_RDONLY failed\n");
goto out;
}
ret = creat(filename, 0644);
close(ret);
ret = generic_open_read_write(filename, O_WRONLY);
if (3 != ret) {
fprintf(stderr, "flag O_WRONLY failed\n");
goto out;
}
ret = creat(filename, 0644);
close(ret);
ret = generic_open_read_write(filename, O_RDWR);
if (0 != ret) {
fprintf(stderr, "flag O_RDWR failed\n");
goto out;
}
ret = creat(filename, 0644);
close(ret);
ret = generic_open_read_write(filename, O_RDONLY);
if (0 != ret) {
fprintf(stderr, "flag O_RDONLY failed\n");
goto out;
}
ret = creat(filename, 0644);
close(ret);
ret = generic_open_read_write(filename, O_TRUNC | O_WRONLY);
if (3 != ret) {
fprintf(stderr, "flag O_TRUNC|O_WRONLY failed\n");
goto out;
}
#if 0 /* undefined behaviour, unable to reliably test */
ret = creat (filename, 0644);
close (ret);
ret = generic_open_read_write (filename, O_TRUNC|O_RDONLY);
if (0 != ret) {
fprintf (stderr, "flag O_TRUNC|O_RDONLY failed\n");
goto out;
}
#endif
ret = generic_open_read_write(filename, O_CREAT | O_RDWR | O_SYNC);
if (0 != ret) {
fprintf(stderr, "flag O_CREAT|O_RDWR|O_SYNC failed\n");
goto out;
}
ret = creat(filename, 0644);
close(ret);
ret = generic_open_read_write(filename, O_CREAT | O_EXCL);
if (0 != ret) {
fprintf(stderr, "flag O_CREAT|O_EXCL failed\n");
goto out;
}
out:
return ret;
}
int
generic_open_read_write(char *filename, int flag)
{
int fd = 0;
int ret = -1;
char wstring[50] = {
0,
};
char rstring[50] = {
0,
};
fd = open(filename, flag);
if (fd < 0) {
if (flag == O_CREAT | O_EXCL && errno == EEXIST) {
unlink(filename);
return 0;
} else {
fd = 0;
fprintf(stderr, "open failed: %s\n", strerror(errno));
return 1;
}
}
strcpy(wstring, "My string to write\n");
ret = write(fd, wstring, strlen(wstring));
if (ret <= 0) {
if (errno != EBADF) {
fprintf(stderr, "write failed: %s\n", strerror(errno));
close(fd);
unlink(filename);
return 2;
}
}
ret = lseek(fd, 0, SEEK_SET);
if (ret < 0) {
close(fd);
unlink(filename);
return 4;
}
ret = read(fd, rstring, strlen(wstring));
if (ret < 0) {
close(fd);
unlink(filename);
return 3;
}
/* Compare the rstring with wstring. But we do not want to return
* error when the flag is either O_RDONLY, O_CREAT|O_RDONLY or
* O_TRUNC|O_RDONLY. Because in that case we are not writing
* anything to the file.*/
ret = memcmp(wstring, rstring, strlen(wstring));
if (0 != ret && !(flag == O_CREAT | O_RDONLY || flag == O_RDONLY ||
flag == O_TRUNC | O_RDONLY)) {
fprintf(stderr, "read is returning junk\n");
close(fd);
unlink(filename);
return 4;
}
close(fd);
unlink(filename);
return 0;
}
|