Guest User

Untitled

a guest
Jun 24th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. From 818b33b60e94801226b2127053d4b20636da7f05 Mon Sep 17 00:00:00 2001
  2. From: Ryan Dahl <ry@tinyclouds.org>
  3. Date: Tue, 5 Oct 2010 09:51:50 -0700
  4. Subject: [PATCH] child_process: Set CLOEXEC on the parent ends of the pipe
  5.  
  6. ---
  7. src/node_child_process.cc | 6 ++++++
  8. test/fixtures/fd-leaker-child.js | 17 +++++++++++++++++
  9. test/fixtures/fd-leaker.js | 32 ++++++++++++++++++++++++++++++++
  10. test/simple/test-stdio-fd-leaking.js | 25 +++++++++++++++++++++++++
  11. 4 files changed, 80 insertions(+), 0 deletions(-)
  12. create mode 100644 test/fixtures/fd-leaker-child.js
  13. create mode 100644 test/fixtures/fd-leaker.js
  14. create mode 100644 test/simple/test-stdio-fd-leaking.js
  15.  
  16. diff --git a/src/node_child_process.cc b/src/node_child_process.cc
  17. index 9201a91..2bd29e2 100644
  18. --- a/src/node_child_process.cc
  19. +++ b/src/node_child_process.cc
  20. @@ -218,6 +218,12 @@ int ChildProcess::Spawn(const char *file,
  21. return -1;
  22. }
  23.  
  24. + // Set CLOEXEC on the parent ends of the pipe, so we don't leak them into
  25. + // another child.
  26. + if (custom_fds[0] == -1) fcntl(stdin_pipe[1], F_SETFD, FD_CLOEXEC);
  27. + if (custom_fds[1] == -1) fcntl(stdout_pipe[0], F_SETFD, FD_CLOEXEC);
  28. + if (custom_fds[2] == -1) fcntl(stderr_pipe[0], F_SETFD, FD_CLOEXEC);
  29. +
  30. // Save environ in the case that we get it clobbered
  31. // by the child process.
  32. char **save_our_env = environ;
  33. diff --git a/test/fixtures/fd-leaker-child.js b/test/fixtures/fd-leaker-child.js
  34. new file mode 100644
  35. index 0000000..2fc1268
  36. --- /dev/null
  37. +++ b/test/fixtures/fd-leaker-child.js
  38. @@ -0,0 +1,17 @@
  39. +setInterval(function () {
  40. + console.log('hello');
  41. +}, 500);
  42. +
  43. +process.stdout.on('error', function (e) {
  44. + console.error('Ignoring stdout error: %s', e.message);
  45. +});
  46. +
  47. +var stdin = process.openStdin();
  48. +
  49. +stdin.on('error', function (e) {
  50. + console.error('Ignoring stdin error: %s', e.message);
  51. +});
  52. +
  53. +stdin.on('end', function () {
  54. + process.exit(0);
  55. +});
  56. diff --git a/test/fixtures/fd-leaker.js b/test/fixtures/fd-leaker.js
  57. new file mode 100644
  58. index 0000000..fe36e61
  59. --- /dev/null
  60. +++ b/test/fixtures/fd-leaker.js
  61. @@ -0,0 +1,32 @@
  62. +var spawn = require('child_process').spawn;
  63. +var join = require('path').join;
  64. +var assert = require('assert');
  65. +
  66. +
  67. +var childPath = join(__dirname, 'fd-leaker-child.js');
  68. +
  69. +var childA = spawn(process.execPath, [childPath]);
  70. +console.log(childA.pid);
  71. +
  72. +// Start another child
  73. +childA.stdout.setEncoding('utf8');
  74. +
  75. +var childB;
  76. +
  77. +childA.stdout.on('data', function (d) {
  78. + assert.equal('hello\n', d);
  79. +
  80. + if (!childB) {
  81. + childB = spawn(process.execPath, [childPath]);
  82. + console.log(childB.pid);
  83. +
  84. + childB.stdout.setEncoding('utf8');
  85. + childB.stdout.on('data', function (d) {
  86. + assert.equal('hello\n', d);
  87. + // Now commit suicide.
  88. + process.exit(0);
  89. + // Do the children live? They shouldn't.
  90. + });
  91. + }
  92. +});
  93. +
  94. diff --git a/test/simple/test-stdio-fd-leaking.js b/test/simple/test-stdio-fd-leaking.js
  95. new file mode 100644
  96. index 0000000..6982970
  97. --- /dev/null
  98. +++ b/test/simple/test-stdio-fd-leaking.js
  99. @@ -0,0 +1,25 @@
  100. +var common = require('../common');
  101. +var assert = require('assert');
  102. +var spawn = require('child_process').spawn;
  103. +
  104. +// Example ptree:
  105. +// 21347 test-stdio-fd-leaking.js (this script)
  106. +// 21349 fd-leaker.js
  107. +// 21361 fd-leaker-child.js
  108. +// 21362 fd-leaker-child.js
  109. +
  110. +// fd-leaker starts two child processes: fd-leaker-child1, then 0.1 seconds
  111. +// later fd-leaker-child2. It ouputs the PIDs of both child processes so
  112. +// that this script knows them.
  113. +//
  114. +// fd-leaker-child prints, in an infinite loop 'hello' to stdout. It
  115. +// ignores EPIPE errors on stdout and ONLY exits when stdin is closed.
  116. +// It SHOULD exit when the parent exits, because the parent should close the
  117. +// stdin implicitly.
  118. +//
  119. +// fd-leaker start two instances of fd-leaker-child so to demonstrate a bug.
  120. +// The stdio pipes into the child processes are leaked into another so that
  121. +// stdin is never actually closed...
  122. +
  123. +
  124. +
  125. --
  126. 1.7.2
Add Comment
Please, Sign In to add comment