PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

test/t40b.c

http://www.minix3.org/
C | 148 lines | 97 code | 23 blank | 28 comment | 28 complexity | 98ebf09e0e6109d4fbd9ab9cdd385514 MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
  1. /* t40b.c
  2. *
  3. * Test regular files
  4. *
  5. * Select works on regular files, (pseudo) terminal devices, streams-based
  6. * files, FIFOs, pipes, and sockets. This test verifies selecting for regular
  7. * file descriptors. "File descriptors associated with regular files shall
  8. * always select true for ready to read, ready to write, and error conditions"
  9. * - Open Group. Although we set a timeout, the select should return
  10. * immediately.
  11. *
  12. * This test is part of a bigger select test. It expects as argument which sub-
  13. * test it is.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. #include <sys/select.h>
  22. #include <errno.h>
  23. #include <time.h>
  24. #define FILE1 "selecttestb-1"
  25. #define FILES 2
  26. #define TIME 3
  27. #define MAX_ERROR 10
  28. int errct = 0, subtest = -1;
  29. char errorbuf[1000];
  30. void e(int n, char *s) {
  31. printf("Subtest %d, error %d, %s\n", subtest, n, s);
  32. if (errct++ > MAX_ERROR) {
  33. printf("Too many errors; test aborted\n");
  34. exit(errct);
  35. }
  36. }
  37. int main(int argc, char **argv) {
  38. int fd1, fd2, retval;
  39. fd_set fds_read, fds_write, fds_error;
  40. struct timeval tv;
  41. time_t start, end;
  42. /* Get subtest number */
  43. if(argc != 2) {
  44. printf("Usage: %s subtest_no\n", argv[0]);
  45. exit(-1);
  46. } else if(sscanf(argv[1], "%d", &subtest) != 1) {
  47. printf("Usage: %s subtest_no\n", argv[0]);
  48. exit(-1);
  49. }
  50. /* Set timeout */
  51. tv.tv_sec = TIME;
  52. tv.tv_usec = 0;
  53. /* Open a file for writing */
  54. if((fd1 = open(FILE1, O_WRONLY|O_CREAT, 0644)) == -1) {
  55. snprintf(errorbuf, sizeof(errorbuf), "failed to open file %s for writing",
  56. FILE1);
  57. e(1, errorbuf);
  58. perror(NULL);
  59. exit(1);
  60. }
  61. /* Open the same file for reading */
  62. if((fd2 = open(FILE1, O_RDONLY)) == -1) {
  63. snprintf(errorbuf, sizeof(errorbuf), "failed to open file %s for reading",
  64. FILE1);
  65. e(2, errorbuf);
  66. perror(NULL);
  67. exit(1);
  68. }
  69. /* Clear file descriptor bit masks */
  70. FD_ZERO(&fds_read); FD_ZERO(&fds_write); FD_ZERO(&fds_error);
  71. /* Fill bit mask */
  72. FD_SET(fd1, &fds_write);
  73. FD_SET(fd2, &fds_read);
  74. FD_SET(fd1, &fds_error);
  75. FD_SET(fd2, &fds_error);
  76. /* Do the select and time how long it takes */
  77. start = time(NULL);
  78. retval = select(fd2+1, &fds_read, &fds_write, &fds_error, &tv);
  79. end = time(NULL);
  80. /* Correct amount of ready file descriptors? 1 read + 1 write + 2 errors */
  81. if(retval != 4) {
  82. e(3, "four fds should be set");
  83. }
  84. /* Test resulting bit masks */
  85. if(!FD_ISSET(fd1, &fds_write)) e(4, "write should be set");
  86. if(!FD_ISSET(fd2, &fds_read)) e(5, "read should be set");
  87. if(!FD_ISSET(fd1, &fds_error)) e(6, "error should be set");
  88. if(!FD_ISSET(fd2, &fds_error)) e(7, "error should be set");
  89. /* Was it instantaneous? */
  90. if(end-start != TIME - TIME) {
  91. snprintf(errorbuf,sizeof(errorbuf),"time spent blocking is not %d, but %ld",
  92. TIME - TIME, (long int) (end-start));
  93. e(8, errorbuf);
  94. }
  95. /* Wait for read to become ready on O_WRONLY. This should fail immediately. */
  96. FD_ZERO(&fds_read); FD_ZERO(&fds_write); FD_ZERO(&fds_error);
  97. FD_SET(fd1, &fds_read);
  98. FD_SET(fd1, &fds_error);
  99. FD_SET(fd2, &fds_error);
  100. tv.tv_sec = TIME;
  101. tv.tv_usec = 0;
  102. retval = select(fd2+1, &fds_read, NULL, &fds_error, &tv);
  103. /* Correct amount of ready file descriptors? 1 read + 2 error */
  104. if(retval != 3) e(9, "incorrect amount of ready file descriptors");
  105. if(!FD_ISSET(fd1, &fds_read)) e(10, "read should be set");
  106. if(!FD_ISSET(fd1, &fds_error)) e(11, "error should be set");
  107. if(!FD_ISSET(fd2, &fds_error)) e(12, "error should be set");
  108. /* Try again as above, bit this time with O_RDONLY in the write set */
  109. FD_ZERO(&fds_error);
  110. FD_SET(fd2, &fds_write);
  111. FD_SET(fd1, &fds_error);
  112. FD_SET(fd2, &fds_error);
  113. tv.tv_sec = TIME;
  114. tv.tv_usec = 0;
  115. retval = select(fd2+1, NULL, &fds_write, &fds_error, &tv);
  116. /* Correct amount of ready file descriptors? 1 write + 2 errors */
  117. if(retval != 3) e(13, "incorrect amount of ready file descriptors");
  118. if(!FD_ISSET(fd2, &fds_write)) e(14, "write should be set");
  119. if(!FD_ISSET(fd1, &fds_error)) e(15, "error should be set");
  120. if(!FD_ISSET(fd2, &fds_error)) e(16, "error should be set");
  121. close(fd1);
  122. close(fd2);
  123. unlink(FILE1);
  124. exit(errct);
  125. }