Skip to content

Commit

Permalink
child_process: control argv0 for spawned processes
Browse files Browse the repository at this point in the history
In some cases it useful to control the value of `argv[0]`, c.f.
 - https://github.com/andrewffff/child_process_with_argv0
 - https://github.com/andrep/argv0

This patch adds explicit support for setting the value of `argv[0]`
when spawning a process.

PR-URL: #7696
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
ppannuto authored and cjihrig committed Aug 10, 2016
1 parent 2f32191 commit e561895
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
10 changes: 10 additions & 0 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ added: v0.1.90
* `options` {Object}
* `cwd` {String} Current working directory of the child process
* `env` {Object} Environment key-value pairs
* `argv0` {String} Explicitly set the value of `argv[0]` sent to the child
process. This will be set to `command` if not specified.
* `stdio` {Array|String} Child's stdio configuration. (See
[`options.stdio`][`stdio`])
* `detached` {Boolean} Prepare child to run independently of its parent
Expand Down Expand Up @@ -397,6 +399,14 @@ child.on('error', (err) => {
});
```

*Note: Certain platforms (OS X, Linux) will use the value of `argv[0]` for the
process title while others (Windows, SunOS) will use `command`.*

*Note: Node.js currently overwrites `argv[0]` with `process.execPath` on
startup, so `process.argv[0]` in a Node.js child process will not match the
`argv0` parameter passed to `spawn` from the parent, retrieve it with the
`process.argv0` property instead.*

#### options.detached
<!-- YAML
added: v0.7.10
Expand Down
6 changes: 5 additions & 1 deletion lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ function normalizeSpawnArguments(file /*, args, options*/) {
}
}

args.unshift(file);
if (typeof options.argv0 === 'string') {
args.unshift(options.argv0);
} else {
args.unshift(file);
}

var env = options.env || process.env;
var envPairs = [];
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-child-process-spawn-argv0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
require('../common');
const assert = require('assert');
const cp = require('child_process');

// This test spawns itself with an argument to indicate when it is a child to
// easily and portably print the value of argv[0]
if (process.argv[2] === 'child') {
console.log(process.argv0);
return;
}

const noArgv0 = cp.spawnSync(process.execPath, [__filename, 'child']);
assert.strictEqual(noArgv0.stdout.toString().trim(), process.execPath);

const withArgv0 = cp.spawnSync(process.execPath, [__filename, 'child'],
{argv0: 'withArgv0'});
assert.strictEqual(withArgv0.stdout.toString().trim(), 'withArgv0');

0 comments on commit e561895

Please sign in to comment.