This repository has been archived by the owner. It is now read-only.

Why does nodejs hand down a socket instead of a character device when exec is being called? #5689

Open
FabianFrank opened this Issue Jun 14, 2013 · 2 comments

Comments

Projects
None yet
4 participants

When using a simple snippet to execute a command with nodejs, such as

var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) }
exec("./stdout_type", puts);

The stdin and stdout that are given to the process are not character devices, but sockets. For example ruby, perl and other languages hand down a character device. Full experiment:
https://github.com/FabianFrank/node-exec-socket

It seems that generally this is not problematic. However, today I stumbled upon the following piece of code in Facebook's xctool, where fdstat is the result of calling fstat(fileno(stdout), &fdstat);.

if (!S_ISFIFO(fdstat.st_mode)) {
    [_outputHandle synchronizeFile];
}

https://github.com/facebook/xctool/blob/master/xctool/xctool/Reporter.m#L212

What happens when you run the resulting program through nodejs' exec is that since stdout is not a FIFO, synchronizeFile is called, which results in the following error, as sockets don't support sychronizeFile.

2013-06-13 21:38:51.398 xctool[33336:707] *** Terminating app due to uncaught exception 'NSFileHandleOperationException', reason: '*** -[NSConcreteFileHandle synchronizeFile]: Operation not supported'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff9378db06 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff8db163f0 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff9378d8dc +[NSException raise:format:] + 204
    3   xctool                              0x000000010a0ceab0 -[Reporter close] + 469
    4   xctool                              0x000000010a0cf907 -[TextReporter close] + 67
    5   CoreFoundation                      0x00007fff93789460 -[NSArray makeObjectsPerformSelector:] + 272
    6   xctool                              0x000000010a0c9c40 -[XCTool run] + 2471
    7   xctool                              0x000000010a0c8cb0 main + 244
    8   xctool                              0x000000010a0c8bb4 start + 52
)
libc++abi.dylib: terminate called throwing an exception

Clearly, this is something that xctool could (and probably should) handle better, but it makes you wonder if it wouldn't be better if nodejs could hand down a character device. Is there a specific reason, for why handing down a socket is better?

Member

bnoordhuis commented Jun 14, 2013

Is there a specific reason, for why handing down a socket is better?

A UNIX socket lets you send and receive file descriptors, something you can't do with pipes or FIFOs.

v0.6 actually used pipes to address issues like the one you described above but that got people up in arms because it took away the aforementioned functionality. That's why v0.8 switched back to UNIX sockets.

I guess we could add an option that says 'use a pipe rather than a socket pair' but that would be strictly a workaround for broken programs. It's arguably better to fix the issue upstream, like you did with xctool.

Member

seishun commented Apr 27, 2014

Please, at least document this behavior. I just spent a significant amount of time figuring out why I can't properly run pysandbox via exec – it checks if stdin is either S_ISCHR, S_ISREG or S_ISFIFO.

You might call programs that don't check for S_ISSOCK broken, but this is the second time I'm encountering this issue. Last time it was a bash script that reads from /dev/stdin. It errors with "No such device or address" when stdin is a socket.

gibfahn pushed a commit to ibmruntimes/node that referenced this issue Apr 26, 2016

2016-04-26, Version 6.0.0 (Current) Release
The following significant (semver-major) changes have been made since the
previous Node v5.0.0 release.

* Buffer
  * New Buffer constructors have been added
    [#4682](nodejs/node#4682)
  * Previously deprecated Buffer APIs are removed
    [#5048](nodejs/node#5048),
    [#4594](nodejs/node#4594)
  * Improved error handling [#4514](nodejs/node#4514)
* Cluster
  * Worker emitted as first argument in 'message' event
    [#5361](nodejs/node#5361).
* Crypto
  * Improved error handling [#3100](nodejs/node#3100),
    [#5611](nodejs/node#5611)
  * Simplified Certificate class bindings
    [#5382](nodejs/node#5382)
  * Improved control over FIPS mode
    [#5181](nodejs/node#5181)
  * pbkdf2 digest overloading is deprecated
    [#4047](nodejs/node#4047)
* Dependencies
  * Reintroduce shared c-ares build support
    [#5775](nodejs/node#5775).
  * V8 updated to 5.0.71.31 [#6111](nodejs/node#6111).
* DNS
  * Add resolvePtr API to query plain DNS PTR records
    [#4921](nodejs/node#4921).
* Domains
  * Clear stack when no error handler
  [#4659](nodejs/node#4659).
* File System
  * The `fs.realpath()` and `fs.realpathSync()` methods have been updated
    to use a more efficient libuv implementation. This change includes the
    removal of the `cache` argument and the method can throw new errors
    [#3594](nodejs/node#3594)
  * FS apis can now accept and return paths as Buffers
    [#5616](nodejs/node#5616).
  * Error handling and type checking improvements
    [#5616](nodejs/node#5616),
    [#5590](nodejs/node#5590),
    [#4518](nodejs/node#4518),
    [#3917](nodejs/node#3917).
  * fs.read's string interface is deprecated
    [#4525](nodejs/node#4525)
* HTTP
  * 'clientError' can now be used to return custom errors from an
    HTTP server [#4557](nodejs/node#4557).
* Modules
  * Current directory is now prioritized for local lookups
    [#5689](nodejs/node#5689)
  * Symbolic links are preserved when requiring modules
    [#5950](nodejs/node#5950)
* Net
  * DNS hints no longer implicitly set
    [#6021](nodejs/node#6021).
  * Improved error handling and type checking
    [#5981](nodejs/node#5981),
    [#5733](nodejs/node#5733),
    [#2904](nodejs/node#2904)
* Path
  * Improved type checking [#5348](nodejs/node#5348).
* Process
  * Introduce process warnings API
    [#4782](nodejs/node#4782).
  * Throw exception when non-function passed to nextTick
    [#3860](nodejs/node#3860).
* Readline
  * Emit key info unconditionally
    [#6024](nodejs/node#6024)
* REPL
  * Assignment to `_` will emit a warning.
    [#5535](nodejs/node#5535)
* Timers
  * Fail early when callback is not a function
    [#4362](nodejs/node#4362)
* TLS
  * Rename 'clientError' to 'tlsClientError'
    [#4557](nodejs/node#4557)
  * SHA1 used for sessionIdContext
    [#3866](nodejs/node#3866)
* TTY
  * Previously deprecated setRawMode wrapper is removed
    [#2528](nodejs/node#2528).
* Util
  * Changes to Error object formatting
    [#4582](nodejs/node#4582).
* Windows
  * Windows XP and Vista are no longer supported
    [#5167](nodejs/node#5167),
    [#5167](nodejs/node#5167).

gibfahn pushed a commit to ibmruntimes/node that referenced this issue Apr 27, 2016

2016-04-26, Version 6.0.0 (Current) Release
The following significant (semver-major) changes have been made since the
previous Node v5.0.0 release.

* Buffer
  * New Buffer constructors have been added
    [#4682](nodejs/node#4682)
  * Previously deprecated Buffer APIs are removed
    [#5048](nodejs/node#5048),
    [#4594](nodejs/node#4594)
  * Improved error handling [#4514](nodejs/node#4514)
* Cluster
  * Worker emitted as first argument in 'message' event
    [#5361](nodejs/node#5361).
* Crypto
  * Improved error handling [#3100](nodejs/node#3100),
    [#5611](nodejs/node#5611)
  * Simplified Certificate class bindings
    [#5382](nodejs/node#5382)
  * Improved control over FIPS mode
    [#5181](nodejs/node#5181)
  * pbkdf2 digest overloading is deprecated
    [#4047](nodejs/node#4047)
* Dependencies
  * Reintroduce shared c-ares build support
    [#5775](nodejs/node#5775).
  * V8 updated to 5.0.71.31 [#6111](nodejs/node#6111).
* DNS
  * Add resolvePtr API to query plain DNS PTR records
    [#4921](nodejs/node#4921).
* Domains
  * Clear stack when no error handler
  [#4659](nodejs/node#4659).
* File System
  * The `fs.realpath()` and `fs.realpathSync()` methods have been updated
    to use a more efficient libuv implementation. This change includes the
    removal of the `cache` argument and the method can throw new errors
    [#3594](nodejs/node#3594)
  * FS apis can now accept and return paths as Buffers
    [#5616](nodejs/node#5616).
  * Error handling and type checking improvements
    [#5616](nodejs/node#5616),
    [#5590](nodejs/node#5590),
    [#4518](nodejs/node#4518),
    [#3917](nodejs/node#3917).
  * fs.read's string interface is deprecated
    [#4525](nodejs/node#4525)
* HTTP
  * 'clientError' can now be used to return custom errors from an
    HTTP server [#4557](nodejs/node#4557).
* Modules
  * Current directory is now prioritized for local lookups
    [#5689](nodejs/node#5689)
  * Symbolic links are preserved when requiring modules
    [#5950](nodejs/node#5950)
* Net
  * DNS hints no longer implicitly set
    [#6021](nodejs/node#6021).
  * Improved error handling and type checking
    [#5981](nodejs/node#5981),
    [#5733](nodejs/node#5733),
    [#2904](nodejs/node#2904)
* OS X
  * MACOSX_DEPLOYMENT_TARGET has been bumped up to 10.7
    [#6402](nodejs/node#6402).
* Path
  * Improved type checking [#5348](nodejs/node#5348).
* Process
  * Introduce process warnings API
    [#4782](nodejs/node#4782).
  * Throw exception when non-function passed to nextTick
    [#3860](nodejs/node#3860).
* Readline
  * Emit key info unconditionally
    [#6024](nodejs/node#6024)
* REPL
  * Assignment to `_` will emit a warning.
    [#5535](nodejs/node#5535)
* Timers
  * Fail early when callback is not a function
    [#4362](nodejs/node#4362)
* TLS
  * Rename 'clientError' to 'tlsClientError'
    [#4557](nodejs/node#4557)
  * SHA1 used for sessionIdContext
    [#3866](nodejs/node#3866)
* TTY
  * Previously deprecated setRawMode wrapper is removed
    [#2528](nodejs/node#2528).
* Util
  * Changes to Error object formatting
    [#4582](nodejs/node#4582).
* Windows
  * Windows XP and Vista are no longer supported
    [#5167](nodejs/node#5167),
    [#5167](nodejs/node#5167).

gibfahn pushed a commit to ibmruntimes/node that referenced this issue May 6, 2016

2016-04-26, Version 6.0.0 (Current) Release
The following significant (semver-major) changes have been made since the
previous Node v5.0.0 release.

* Buffer
  * New Buffer constructors have been added
    [#4682](nodejs/node#4682)
  * Previously deprecated Buffer APIs are removed
    [#5048](nodejs/node#5048),
    [#4594](nodejs/node#4594)
  * Improved error handling [#4514](nodejs/node#4514)
* Cluster
  * Worker emitted as first argument in 'message' event
    [#5361](nodejs/node#5361).
* Crypto
  * Improved error handling [#3100](nodejs/node#3100),
    [#5611](nodejs/node#5611)
  * Simplified Certificate class bindings
    [#5382](nodejs/node#5382)
  * Improved control over FIPS mode
    [#5181](nodejs/node#5181)
  * pbkdf2 digest overloading is deprecated
    [#4047](nodejs/node#4047)
* Dependencies
  * Reintroduce shared c-ares build support
    [#5775](nodejs/node#5775).
  * V8 updated to 5.0.71.31 [#6111](nodejs/node#6111).
* DNS
  * Add resolvePtr API to query plain DNS PTR records
    [#4921](nodejs/node#4921).
* Domains
  * Clear stack when no error handler
  [#4659](nodejs/node#4659).
* File System
  * The `fs.realpath()` and `fs.realpathSync()` methods have been updated
    to use a more efficient libuv implementation. This change includes the
    removal of the `cache` argument and the method can throw new errors
    [#3594](nodejs/node#3594)
  * FS apis can now accept and return paths as Buffers
    [#5616](nodejs/node#5616).
  * Error handling and type checking improvements
    [#5616](nodejs/node#5616),
    [#5590](nodejs/node#5590),
    [#4518](nodejs/node#4518),
    [#3917](nodejs/node#3917).
  * fs.read's string interface is deprecated
    [#4525](nodejs/node#4525)
* HTTP
  * 'clientError' can now be used to return custom errors from an
    HTTP server [#4557](nodejs/node#4557).
* Modules
  * Current directory is now prioritized for local lookups
    [#5689](nodejs/node#5689)
  * Symbolic links are preserved when requiring modules
    [#5950](nodejs/node#5950)
* Net
  * DNS hints no longer implicitly set
    [#6021](nodejs/node#6021).
  * Improved error handling and type checking
    [#5981](nodejs/node#5981),
    [#5733](nodejs/node#5733),
    [#2904](nodejs/node#2904)
* OS X
  * MACOSX_DEPLOYMENT_TARGET has been bumped up to 10.7
    [#6402](nodejs/node#6402).
* Path
  * Improved type checking [#5348](nodejs/node#5348).
* Process
  * Introduce process warnings API
    [#4782](nodejs/node#4782).
  * Throw exception when non-function passed to nextTick
    [#3860](nodejs/node#3860).
* Readline
  * Emit key info unconditionally
    [#6024](nodejs/node#6024)
* REPL
  * Assignment to `_` will emit a warning.
    [#5535](nodejs/node#5535)
* Timers
  * Fail early when callback is not a function
    [#4362](nodejs/node#4362)
* TLS
  * Rename 'clientError' to 'tlsClientError'
    [#4557](nodejs/node#4557)
  * SHA1 used for sessionIdContext
    [#3866](nodejs/node#3866)
* TTY
  * Previously deprecated setRawMode wrapper is removed
    [#2528](nodejs/node#2528).
* Util
  * Changes to Error object formatting
    [#4582](nodejs/node#4582).
* Windows
  * Windows XP and Vista are no longer supported
    [#5167](nodejs/node#5167),
    [#5167](nodejs/node#5167).
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.