Last active
August 16, 2016 17:34
-
-
Save jandre/8485608 to your computer and use it in GitHub Desktop.
fork example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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