Skip to content

Instantly share code, notes, and snippets.

@jandre
Last active August 16, 2016 17:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jandre/8485608 to your computer and use it in GitHub Desktop.
Save jandre/8485608 to your computer and use it in GitHub Desktop.
fork example
var fork = require('child_process').fork;
var spawn = require('child_process').spawn;
var fs = require('fs');
var path = require('path');
function forkProcess() {
var fd = fs.openSync('/tmp/sensitive_file', 'w');
var opts = {
};
fork(process.argv[1], ["--forked"], opts)
setInterval(function() {}, 60 * 1000)
};
function spawnProcess() {
var fd = fs.openSync('/tmp/sensitive_file', 'w');
var opts = {};
spawn(process.argv[0], [ process.argv[1], "--forked" ], opts);
setInterval(function() {}, 60 * 1000)
};
function forked() {
setInterval(function() {}, 60 * 1000)
}
if (process.argv[2] == '--forked') {
console.log("forked");
forked();
} else {
spawnProcess();
}
jandre@ubuntu:/tmp$ strace -f node forktest.js 2> /tmp/log1 &
[1] 39055
jandre@ubuntu:/tmp$ ps auxw | grep node
jandre 39055 0.1 0.0 4648 820 pts/11 S 19:19 0:00 strace -f node forktest.js
jandre 39056 0.4 0.8 658380 8704 pts/11 Sl 19:19 0:00 node forktest.js
jandre 39058 0.4 0.8 657352 8772 pts/11 Sl 19:19 0:00 node /tmp/forktest.js --forked
jandre@ubuntu:/tmp$ cat /tmp/log1 | grep sensitive
[pid 39056] open("/tmp/sensitive_file", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 9
#
# both parent and child have the fd /tmp/sensitive_file open
#
jandre@ubuntu:/tmp$ lsof | grep sensitive
node 39056 jandre 9w REG 8,1 0 827178 /tmp/sensitive_file
node 39058 jandre 9w REG 8,1 0 827178 /tmp/sensitive_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment