Skip to content

Commit

Permalink
src: add process.ppid
Browse files Browse the repository at this point in the history
Fixes: #14957
PR-URL: #16839
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
cjihrig authored and MylesBorins committed Feb 13, 2018
1 parent a1d7469 commit 2d4fca2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,19 @@ system platform on which the Node.js process is running. For instance
console.log(`This platform is ${process.platform}`);
```

## process.ppid
<!-- YAML
added: REPLACEME
-->

* {integer}

The `process.ppid` property returns the PID of the current parent process.

```js
console.log(`The parent process is pid ${process.ppid}`);
```

## process.release
<!-- YAML
added: v3.0.0
Expand Down
9 changes: 9 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2950,6 +2950,12 @@ static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
}


static void GetParentProcessId(Local<Name> property,
const PropertyCallbackInfo<Value>& info) {
info.GetReturnValue().Set(Integer::New(info.GetIsolate(), uv_os_getppid()));
}


static Local<Object> GetFeatures(Environment* env) {
EscapableHandleScope scope(env->isolate());

Expand Down Expand Up @@ -3305,6 +3311,9 @@ void SetupProcessObject(Environment* env,
READONLY_PROPERTY(process, "pid", Integer::New(env->isolate(), getpid()));
READONLY_PROPERTY(process, "features", GetFeatures(env));

process->SetAccessor(FIXED_ONE_BYTE_STRING(env->isolate(), "ppid"),
GetParentProcessId);

auto need_immediate_callback_string =
FIXED_ONE_BYTE_STRING(env->isolate(), "_needImmediateCallback");
CHECK(process->SetAccessor(env->context(), need_immediate_callback_string,
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-process-ppid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
require('../common');
const assert = require('assert');
const cp = require('child_process');

if (process.argv[2] === 'child') {
// The following console.log() call is part of the test's functionality.
console.log(process.ppid);
} else {
const child = cp.spawnSync(process.execPath, [__filename, 'child']);

assert.strictEqual(child.status, 0);
assert.strictEqual(child.signal, null);
assert.strictEqual(+child.stdout.toString().trim(), process.pid);
assert.strictEqual(child.stderr.toString().trim(), '');
}

2 comments on commit 2d4fca2

@theIDinside
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I ask why this has not been accepted yet? This is an incredibly useful feature.

@Trott
Copy link
Member

@Trott Trott commented on 2d4fca2 Apr 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I ask why this has not been accepted yet?

This was merged 4 years ago and is in every currently-supported version of Node.js.
¯\(ツ)

Please sign in to comment.