Advertisement
Guest User

Untitled

a guest
Feb 26th, 2012
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <unistd.h>
  4. #include <sys/ioctl.h>
  5. #include <time.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <stdlib.h>
  9. #include <sys/mman.h>
  10.  
  11. #include <linux/random.h>
  12.  
  13. int looping = 1;
  14.  
  15. int pos_write = 0;
  16. int buff_read_remaining = 0; // This many bytes left in the buffer
  17. int buff_size = 1048576;
  18. char* entbuff = 0;
  19.  
  20. void unmap_ent()
  21. {
  22. // unmap. Should be called by atexit() if we successfully mapped.
  23. munmap(entbuff, buff_size);
  24. }
  25.  
  26. FILE* fdRandom = NULL;
  27.  
  28. void close_fdRandom()
  29. {
  30. fclose(fdRandom);
  31. }
  32.  
  33. void read_from_buffer()
  34. {
  35. // If we don't have any bytes left to read, nullop.
  36. if(0 == buff_read_remaining)
  37. return;
  38.  
  39. // Where in the buffer do we read from?
  40. // pos_write is the next byte which will be written.
  41. // So pos_write - 1 will be the last valid written byte.
  42. int buff_read_pos = (pos_write - 1) - buff_read_remaining;
  43. if(buff_read_pos < 0)
  44. // Buffer wrapped.
  45. buff_read_pos += buff_size;
  46.  
  47. fwrite(entbuff + buff_read_pos, 1, 1, fdRandom);
  48. int brr_w = buff_read_remaining;
  49. --buff_read_remaining;
  50. }
  51.  
  52. void write_to_buffer()
  53. {
  54. // fprintf(stderr, "wtb: bs(%d), brr(%d), pw(%d), entbuff(0x%X)\n", buff_size, buff_read_remaining, pos_write, entbuff);
  55. // If our buffer is full, nullop.
  56. if(buff_size == buff_read_remaining)
  57. return;
  58.  
  59. fread(entbuff + pos_write, 1, 1, fdRandom);
  60. int brr_w = buff_read_remaining;
  61. int pw_w = pos_write;
  62. ++buff_read_remaining;
  63. ++pos_write;
  64. }
  65.  
  66. int check_ent()
  67. {
  68. int entcnt;
  69. int r;
  70. r = ioctl(fileno(fdRandom), RNDGETENTCNT, &entcnt);
  71. if(0 > r) {
  72. int e = errno;
  73. fprintf(stderr, "Error with ioctl call: %s\n", strerror(e));
  74. return -1;
  75. }
  76. return entcnt;
  77. }
  78.  
  79. int main(int argc, char* argv[])
  80. {
  81. int entcnt = 0;
  82. int entthresh_high = 4096 * 3 / 4;
  83. int entthresh_low = 4096 * 2 / 4;
  84. int waittime = 1;
  85.  
  86. entbuff = mmap(NULL, buff_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  87. if(MAP_FAILED == entbuff)
  88. {
  89. int e = errno;
  90. fprintf(stderr, "Error with mmap call: %s\n", strerror(e));
  91. return -1;
  92. }
  93. if(NULL == entbuff)
  94. {
  95. int e = errno;
  96. fprintf(stderr, "mmap returned NULL?!: %s\n", strerror(e));
  97. return -1;
  98. }
  99.  
  100. int at_res = atexit(unmap_ent); // We mapped, so unmap when we're done.
  101. if(0 != at_res)
  102. {
  103. fprintf(stderr, "Warning: failed to register unmap_ent with atexit()");
  104. }
  105.  
  106. fdRandom = fopen("/dev/random", "rw");
  107. if(0 == fdRandom)
  108. {
  109. int e = errno;
  110. fprintf(stderr, "Error with mmap call: %s\n", strerror(e));
  111. return -1;
  112. }
  113.  
  114. at_res = atexit(close_fdRandom); // We opened the file, remember to close it.
  115. if(0 != at_res)
  116. {
  117. fprintf(stderr, "Warning: failed to register close_fdRandom with atexit()");
  118. }
  119.  
  120. if(argc == 2) {
  121. waittime = atoi(argv[1]);
  122. if(waittime < 1) {
  123. fprintf(stderr, "specified wait time cannot be less than 1\n");
  124. return -1;
  125. }
  126. }
  127. fprintf(stderr, "wait time is %d, high threshold is %d, low threshold is %d\n", waittime, entthresh_high, entthresh_low);
  128. // loop here, calling ioctl(rfd, RNDGETENTCNT) and printing the result
  129. entcnt = check_ent();
  130. while( -1 != entcnt)
  131. {
  132. fprintf(stderr, "Sleeping %d seconds. we have %d bytes in buffer.\n", waittime, buff_read_remaining, entcnt);
  133. sleep(waittime);
  134. entcnt = check_ent();
  135. int op_bytes = 0; // how many bytes we've added or removed this cycle.
  136. while (
  137. (
  138. ((entcnt >= entthresh_high) && (buff_read_remaining != buff_size))
  139. || ((entcnt <= entthresh_low) && (buff_read_remaining != 0))
  140. )
  141. && (op_bytes++ < 64)
  142. )
  143. {
  144. if(entcnt >= entthresh_high)
  145. {
  146. // fprintf(stderr, "%d >= %d, and we have %d in buffer\n", entcnt, entthresh_high, buff_read_remaining);
  147. write_to_buffer();
  148. }
  149. else if(entcnt <= entthresh_low)
  150. {
  151. // fprintf(stderr, "%d <= %d, and we have %d in buffer\n", entcnt, entthresh_low, buff_read_remaining);
  152. read_from_buffer();
  153. }
  154. else
  155. {
  156. fprintf(stderr, "BUG: entcnt(%d), etl(%d), eth(%d), brr(%d), bs(%d)\n", entcnt, entthresh_low, entthresh_high, buff_read_remaining, buff_size);
  157. }
  158. entcnt = check_ent();
  159. }
  160. // fprintf(stderr, "Sleeping %d seconds. we have %d bytes in buffer. Last known ent was %d\n", waittime, buff_read_remaining, entcnt);
  161. }
  162.  
  163. return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement