bad_dup2.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Invalid calls to dup2
  31. */
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. #include <limits.h>
  39. #include <errno.h>
  40. #include <err.h>
  41. #include "config.h"
  42. #include "test.h"
  43. static
  44. void
  45. dup2_fd2(int fd, const char *desc)
  46. {
  47. int rv;
  48. rv = dup2(STDIN_FILENO, fd);
  49. report_test(rv, errno, EBADF, desc);
  50. if (rv != -1) {
  51. close(fd); /* just in case */
  52. }
  53. }
  54. static
  55. void
  56. dup2_self(void)
  57. {
  58. struct stat sb;
  59. int rv;
  60. int testfd;
  61. /* use fd that isn't in use */
  62. testfd = CLOSED_FD;
  63. rv = dup2(STDIN_FILENO, testfd);
  64. if (rv == -1) {
  65. warn("UH-OH: couldn't copy stdin");
  66. return;
  67. }
  68. rv = dup2(testfd, testfd);
  69. if (rv == testfd) {
  70. warnx("passed: dup2 to same fd");
  71. }
  72. else if (rv<0) {
  73. warn("FAILURE: dup2 to same fd: error");
  74. }
  75. else {
  76. warnx("FAILURE: dup2 to same fd: returned %d instead", rv);
  77. }
  78. rv = fstat(testfd, &sb);
  79. if (rv==0) {
  80. warnx("passed: fstat fd after dup2 to itself");
  81. }
  82. else if (errno!=EUNIMP && errno!=ENOSYS) {
  83. warn("FAILURE: fstat fd after dup2 to itself");
  84. }
  85. else {
  86. /* no support for fstat; try lseek */
  87. rv = lseek(testfd, 0, SEEK_CUR);
  88. if (rv==0 || (rv==-1 && errno==ESPIPE)) {
  89. warnx("passed: lseek fd after dup2 to itself");
  90. }
  91. else {
  92. warn("FAILURE: lseek fd after dup2 to itself");
  93. }
  94. }
  95. close(testfd);
  96. }
  97. void
  98. test_dup2(void)
  99. {
  100. /* This does the first fd. */
  101. test_dup2_fd();
  102. /* Any interesting cases added here should also go in common_fds.c */
  103. dup2_fd2(-1, "dup2 to -1");
  104. dup2_fd2(-5, "dup2 to -5");
  105. dup2_fd2(IMPOSSIBLE_FD, "dup2 to impossible fd");
  106. #ifdef OPEN_MAX
  107. dup2_fd2(OPEN_MAX, "dup2 to OPEN_MAX");
  108. #else
  109. warnx("Warning: OPEN_MAX not defined - test skipped");
  110. #endif
  111. dup2_self();
  112. }