common_fds.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. * Calls with invalid fds
  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. int
  45. read_badfd(int fd)
  46. {
  47. char buf[128];
  48. return read(fd, buf, sizeof(buf));
  49. }
  50. static
  51. int
  52. write_badfd(int fd)
  53. {
  54. char buf[128];
  55. memset(buf, 'a', sizeof(buf));
  56. return write(fd, buf, sizeof(buf));
  57. }
  58. static
  59. int
  60. close_badfd(int fd)
  61. {
  62. return close(fd);
  63. }
  64. static
  65. int
  66. ioctl_badfd(int fd)
  67. {
  68. return ioctl(fd, 0, NULL);
  69. }
  70. static
  71. int
  72. lseek_badfd(int fd)
  73. {
  74. return lseek(fd, 0, SEEK_SET);
  75. }
  76. static
  77. int
  78. fsync_badfd(int fd)
  79. {
  80. return fsync(fd);
  81. }
  82. static
  83. int
  84. ftruncate_badfd(int fd)
  85. {
  86. return ftruncate(fd, 60);
  87. }
  88. static
  89. int
  90. fstat_badfd(int fd)
  91. {
  92. struct stat sb;
  93. return fstat(fd, &sb);
  94. }
  95. static
  96. int
  97. getdirentry_badfd(int fd)
  98. {
  99. char buf[32];
  100. return getdirentry(fd, buf, sizeof(buf));
  101. }
  102. static
  103. int
  104. dup2_badfd(int fd)
  105. {
  106. /* use the +1 to avoid doing dup2(CLOSED_FD, CLOSED_FD) */
  107. return dup2(fd, CLOSED_FD+1);
  108. }
  109. static
  110. void
  111. dup2_cleanup(void)
  112. {
  113. close(CLOSED_FD+1);
  114. }
  115. ////////////////////////////////////////////////////////////
  116. static
  117. void
  118. any_badfd(int (*func)(int fd), void (*cleanup)(void), const char *callname,
  119. int fd, const char *fddesc)
  120. {
  121. char fulldesc[128];
  122. int rv;
  123. snprintf(fulldesc, sizeof(fulldesc), "%s using %s", callname, fddesc);
  124. rv = func(fd);
  125. report_test(rv, errno, EBADF, fulldesc);
  126. if (cleanup) {
  127. cleanup();
  128. }
  129. }
  130. static
  131. void
  132. runtest(int (*func)(int fd), void (*cleanup)(void), const char *callname)
  133. {
  134. /*
  135. * If adding cases, also see bad_dup2.c
  136. */
  137. /* basic invalid case: fd -1 */
  138. any_badfd(func, cleanup, callname, -1, "fd -1");
  139. /* also try -5 in case -1 is special somehow */
  140. any_badfd(func, cleanup, callname, -5, "fd -5");
  141. /* try a fd we know is closed */
  142. any_badfd(func, cleanup, callname, CLOSED_FD, "closed fd");
  143. /* try a positive fd we know is out of range */
  144. any_badfd(func, cleanup, callname, IMPOSSIBLE_FD, "impossible fd");
  145. /* test for off-by-one errors */
  146. #ifdef OPEN_MAX
  147. any_badfd(func, cleanup, callname, OPEN_MAX, "fd OPEN_MAX");
  148. #else
  149. warnx("Warning: OPEN_MAX not defined, test skipped");
  150. #endif
  151. }
  152. ////////////////////////////////////////////////////////////
  153. #define T(call) \
  154. void \
  155. test_##call##_fd(void) \
  156. { \
  157. runtest(call##_badfd, NULL, #call); \
  158. }
  159. #define TC(call) \
  160. void \
  161. test_##call##_fd(void) \
  162. { \
  163. runtest(call##_badfd, call##_cleanup, #call);\
  164. }
  165. T(read);
  166. T(write);
  167. T(close);
  168. T(ioctl);
  169. T(lseek);
  170. T(fsync);
  171. T(ftruncate);
  172. T(fstat);
  173. T(getdirentry);
  174. TC(dup2);