Advertisement
Guest User

Untitled

a guest
May 29th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. commit 93c82634c1218feb191efcf64565582e97dd0fc5
  2. Author: Keith Johnson <kj@ubergeek42.com>
  3. Date: Sun May 29 23:00:08 2016 -0400
  4.  
  5. Use splice() instead of read()/write() in runguard
  6.  
  7. diff --git a/judge/runguard.c b/judge/runguard.c
  8. index d0154c4..4ca6a9b 100644
  9. --- a/judge/runguard.c
  10. +++ b/judge/runguard.c
  11. @@ -763,7 +763,7 @@ int main(int argc, char **argv)
  12. double tmpd;
  13. size_t data_read[3];
  14. size_t data_passed[3];
  15. - ssize_t nread, nwritten;
  16. + ssize_t nread;
  17. char str[256];
  18.  
  19. struct itimerval itimer;
  20. @@ -1115,30 +1115,50 @@ int main(int argc, char **argv)
  21. for(i=1; i<=2; i++) {
  22. if ( child_pipefd[i][PIPE_OUT] != -1 &&
  23. FD_ISSET(child_pipefd[i][PIPE_OUT],&readfds) ) {
  24. - nread = read(child_pipefd[i][PIPE_OUT], buf, BUF_SIZE);
  25. - if ( nread==-1 ) error(errno,"reading child fd %d",i);
  26. +
  27. + if (data_passed[i] == streamsize) {
  28. + /* Throw away data if we're done */
  29. + nread = read(child_pipefd[i][PIPE_OUT], buf, BUF_SIZE);
  30. + } else {
  31. + /* Otherwise copy it to the file */
  32. + nread = splice(child_pipefd[i][PIPE_OUT], NULL,
  33. + child_redirfd[i], NULL,
  34. + BUF_SIZE, SPLICE_F_MOVE);
  35. + data_passed[i] += nread;
  36. + }
  37. + if ( nread==-1 ) {
  38. + if (errno == EINTR ) continue;
  39. + error(errno,"copying data fd %d",i);
  40. + }
  41. if ( nread==0 ) {
  42. - /* EOF detected: close fd and indicate this with -1 */
  43. - if ( close(child_pipefd[i][PIPE_OUT])!=0 ) {
  44. - error(errno,"closing pipe for fd %d",i);
  45. - }
  46. - child_pipefd[i][PIPE_OUT] = -1;
  47. - continue;
  48. + if (errno == EAGAIN || errno == EWOULDBLOCK) continue;
  49. + // EOF detected: close fd and indicate this with -1
  50. + if ( close(child_pipefd[i][PIPE_OUT])!=0 ) {
  51. + error(errno,"closing pipe for fd %d",i);
  52. + }
  53. + child_pipefd[i][PIPE_OUT] = -1;
  54. + continue;
  55. }
  56. data_read[i] += nread;
  57. - if ( limit_streamsize && data_passed[i]+nread>=streamsize ) {
  58. - if ( data_passed[i]<streamsize ) {
  59. - verbose("child fd %d limit reached",i);
  60. - }
  61. - nread = streamsize - data_passed[i];
  62. +
  63. + /* Truncate to the output limit */
  64. + if (limit_streamsize && data_passed[i] > streamsize) {
  65. + verbose("child fd %i limit reached",i);
  66. + data_passed[i] = streamsize;
  67. +
  68. + ret = ftruncate(child_redirfd[i], streamsize);
  69. + if( ret!=0 ) error(errno,"truncating output fd %d", i);
  70. }
  71. - nwritten = write(child_redirfd[i], buf, nread);
  72. - if ( nwritten==-1 ) error(errno,"writing child fd %d",i);
  73. - data_passed[i] += nwritten;
  74. }
  75. }
  76. }
  77.  
  78. + /* Close the output files */
  79. + for(i=1; i<=2; i++) {
  80. + ret = close(child_redirfd[i]);
  81. + if( ret!=0 ) error(errno,"closing output fd %d", i);
  82. + }
  83. +
  84. if ( times(&endticks)==(clock_t) -1 ) {
  85. error(errno,"getting end clock ticks");
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement