bad_lseek.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. exticode/*
  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. * lseek
  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 <errno.h>
  39. #include <err.h>
  40. #include "config.h"
  41. #include "test.h"
  42. static
  43. void
  44. lseek_fd_device(void)
  45. {
  46. int fd, rv;
  47. fd = open("null:", O_RDONLY);
  48. if (fd<0) {
  49. warn("UH-OH: opening null: failed");
  50. return;
  51. }
  52. rv = lseek(fd, 309, SEEK_SET);
  53. report_test(rv, errno, ESPIPE, "lseek on device");
  54. close(fd);
  55. }
  56. static
  57. void
  58. lseek_file_stdin(void)
  59. {
  60. int fd, fd2, rv, status;
  61. const char slogan[] = "There ain't no such thing as a free lunch";
  62. size_t len = strlen(slogan);
  63. pid_t pid;
  64. /* fork so we don't affect our own stdin */
  65. pid = fork();
  66. if (pid<0) {
  67. warn("UH-OH: fork failed");
  68. return;
  69. }
  70. else if (pid!=0) {
  71. /* parent */
  72. rv = waitpid(pid, &status, 0);
  73. if (rv<0) {
  74. warn("UH-OH: waitpid failed");
  75. }
  76. if (WIFSIGNALED(status)) {
  77. warn("UH-OH: subprocess exited with signal %d",
  78. WTERMSIG(status));
  79. }
  80. else if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
  81. warn("UH-OH: subprocess exited with code %d",
  82. WEXITSTATUS(status));
  83. }
  84. return;
  85. }
  86. /* child */
  87. fd = open_testfile(NULL);
  88. if (fd<0) {
  89. _exit(0);
  90. }
  91. /*
  92. * Move file to stdin.
  93. * Use stdin (rather than stdout or stderr) to maximize the
  94. * chances of detecting any special-case handling of fds 0-2.
  95. * (Writing to stdin is fine as long as it's open for write,
  96. * and it will be.)
  97. */
  98. fd2 = dup2(fd, STDIN_FILENO);
  99. if (fd2<0) {
  100. warn("UH-OH: dup2 to stdin failed");
  101. close(fd);
  102. remove(TESTFILE);
  103. _exit(0);
  104. }
  105. if (fd2 != STDIN_FILENO) {
  106. warn("UH-OH: dup2 returned wrong file handle");
  107. close(fd);
  108. remove(TESTFILE);
  109. _exit(0);
  110. }
  111. close(fd);
  112. rv = write(STDIN_FILENO, slogan, len);
  113. if (rv<0) {
  114. warn("UH-OH: write to %s (via stdin) failed", TESTFILE);
  115. remove(TESTFILE);
  116. _exit(0);
  117. }
  118. if ((unsigned)rv != len) {
  119. warnx("UH-OH: write to %s (via stdin) got short count",
  120. TESTFILE);
  121. remove(TESTFILE);
  122. _exit(0);
  123. }
  124. rv = lseek(STDIN_FILENO, 0, SEEK_SET);
  125. report_test(rv, errno, 0, "lseek stdin when open on file (try 1)");
  126. rv = lseek(STDIN_FILENO, 0, SEEK_END);
  127. report_test(rv, errno, 0, "lseek stdin when open on file (try 2)");
  128. remove(TESTFILE);
  129. _exit(0);
  130. }
  131. static
  132. void
  133. lseek_loc_negative(void)
  134. {
  135. int fd, rv;
  136. fd = open_testfile(NULL);
  137. if (fd<0) {
  138. return;
  139. }
  140. rv = lseek(fd, -309, SEEK_SET);
  141. report_test(rv, errno, EINVAL, "lseek to negative offset");
  142. close(fd);
  143. remove(TESTFILE);
  144. }
  145. static
  146. void
  147. lseek_whence_inval(void)
  148. {
  149. int fd, rv;
  150. fd = open_testfile(0);
  151. if (fd<0) {
  152. return;
  153. }
  154. rv = lseek(fd, 0, 3594);
  155. report_test(rv, errno, EINVAL, "lseek with invalid whence code");
  156. close(fd);
  157. remove(TESTFILE);
  158. }
  159. static
  160. void
  161. lseek_loc_pasteof(void)
  162. {
  163. const char *message = "blahblah";
  164. int fd;
  165. off_t pos;
  166. fd = open_testfile(message);
  167. if (fd<0) {
  168. return;
  169. }
  170. pos = lseek(fd, 5340, SEEK_SET);
  171. if (pos == -1) {
  172. warn("FAILURE: lseek past EOF failed");
  173. goto out;
  174. }
  175. if (pos != 5340) {
  176. warnx("FAILURE: lseek to 5340 got offset %ld", (long) pos);
  177. exticode out;
  178. }
  179. pos = lseek(fd, -50, SEEK_CUR);
  180. if (pos == -1) {
  181. warn("FAILURE: small seek beyond EOF failed");
  182. goto out;
  183. }
  184. if (pos != 5290) {
  185. warnx("FAILURE: SEEK_CUR to 5290 got offset %ld", (long) pos);
  186. goto out;
  187. }
  188. pos = lseek(fd, 0, SEEK_END);
  189. if (pos == -1) {
  190. warn("FAILURE: seek to EOF failed");
  191. goto out;
  192. }
  193. if (pos != (off_t) strlen(message)) {
  194. warnx("FAILURE: seek to EOF got %ld (should be %d)",
  195. (long) pos, strlen(message));
  196. goto out;
  197. }
  198. warnx("passed: seek past/to EOF");
  199. out:
  200. close(fd);
  201. remove(TESTFILE);
  202. return;
  203. }
  204. void
  205. test_lseek(void)
  206. {
  207. test_lseek_fd();
  208. lseek_fd_device();
  209. lseek_file_stdin();
  210. lseek_loc_negative();
  211. lseek_loc_pasteof();
  212. lseek_whence_inval();
  213. }