Skip to content

Commit

Permalink
process, feat: support process.ppid and SubProcess::ppid. (#524)
Browse files Browse the repository at this point in the history
* process, feat: support `process.ppid`.

* SubProcess, feat: support `SubProcess::ppid`.
  • Loading branch information
richardo2016 authored and xicilion committed Sep 24, 2019
1 parent 0a20059 commit a5a1597
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 16 deletions.
5 changes: 4 additions & 1 deletion fibjs/include/SubProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ namespace fibjs {

class SubProcess : public SubProcess_base {
public:
SubProcess(intptr_t pid)
SubProcess(intptr_t pid, intptr_t ppid)
: m_pid(pid)
, m_ppid(ppid)
{
}

Expand Down Expand Up @@ -59,6 +60,7 @@ class SubProcess : public SubProcess_base {
virtual result_t wait(int32_t& retVal, AsyncEvent* ac);
virtual result_t findWindow(exlib::string name, v8::Local<v8::Value>& retVal);
virtual result_t get_pid(int32_t& retVal);
virtual result_t get_ppid(int32_t& retVal);
virtual result_t get_stdin(obj_ptr<BufferedStream_base>& retVal);
virtual result_t get_stdout(obj_ptr<BufferedStream_base>& retVal);

Expand Down Expand Up @@ -140,6 +142,7 @@ class SubProcess : public SubProcess_base {
obj_ptr<Timer> m_timer;

intptr_t m_pid;
intptr_t m_ppid;
};

} /* namespace fibjs */
Expand Down
16 changes: 16 additions & 0 deletions fibjs/include/ifs/SubProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SubProcess_base : public BufferedStream_base {
virtual result_t wait(int32_t& retVal, AsyncEvent* ac) = 0;
virtual result_t findWindow(exlib::string name, v8::Local<v8::Value>& retVal) = 0;
virtual result_t get_pid(int32_t& retVal) = 0;
virtual result_t get_ppid(int32_t& retVal) = 0;
virtual result_t get_stdin(obj_ptr<BufferedStream_base>& retVal) = 0;
virtual result_t get_stdout(obj_ptr<BufferedStream_base>& retVal) = 0;

Expand All @@ -47,6 +48,7 @@ class SubProcess_base : public BufferedStream_base {
static void s_wait(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_findWindow(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_get_pid(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_get_ppid(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_get_stdin(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_get_stdout(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);

Expand All @@ -67,6 +69,7 @@ inline ClassInfo& SubProcess_base::class_info()

static ClassData::ClassProperty s_property[] = {
{ "pid", s_get_pid, block_set, false },
{ "ppid", s_get_ppid, block_set, false },
{ "stdin", s_get_stdin, block_set, false },
{ "stdout", s_get_stdout, block_set, false }
};
Expand Down Expand Up @@ -145,6 +148,19 @@ inline void SubProcess_base::s_get_pid(v8::Local<v8::Name> property, const v8::P
METHOD_RETURN();
}

inline void SubProcess_base::s_get_ppid(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args)
{
int32_t vr;

METHOD_NAME("SubProcess.ppid");
METHOD_INSTANCE(SubProcess_base);
PROPERTY_ENTER();

hr = pInst->get_ppid(vr);

METHOD_RETURN();
}

inline void SubProcess_base::s_get_stdin(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args)
{
obj_ptr<BufferedStream_base> vr;
Expand Down
15 changes: 15 additions & 0 deletions fibjs/include/ifs/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class process_base : public EventEmitter_base {
static result_t get_arch(exlib::string& retVal);
static result_t get_platform(exlib::string& retVal);
static result_t get_pid(int32_t& retVal);
static result_t get_ppid(int32_t& retVal);
static result_t get_stdin(obj_ptr<File_base>& retVal);
static result_t get_stdout(obj_ptr<File_base>& retVal);
static result_t get_stderr(obj_ptr<File_base>& retVal);
Expand Down Expand Up @@ -79,6 +80,7 @@ class process_base : public EventEmitter_base {
static void s_get_arch(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_get_platform(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_get_pid(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_get_ppid(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_get_stdin(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_get_stdout(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_get_stderr(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args);
Expand Down Expand Up @@ -133,6 +135,7 @@ inline ClassInfo& process_base::class_info()
{ "arch", s_get_arch, block_set, true },
{ "platform", s_get_platform, block_set, true },
{ "pid", s_get_pid, block_set, true },
{ "ppid", s_get_ppid, block_set, true },
{ "stdin", s_get_stdin, block_set, true },
{ "stdout", s_get_stdout, block_set, true },
{ "stderr", s_get_stderr, block_set, true },
Expand Down Expand Up @@ -257,6 +260,18 @@ inline void process_base::s_get_pid(v8::Local<v8::Name> property, const v8::Prop
METHOD_RETURN();
}

inline void process_base::s_get_ppid(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args)
{
int32_t vr;

METHOD_NAME("process.ppid");
PROPERTY_ENTER();

hr = get_ppid(vr);

METHOD_RETURN();
}

inline void process_base::s_get_stdin(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& args)
{
obj_ptr<File_base> vr;
Expand Down
12 changes: 10 additions & 2 deletions fibjs/src/process/SubProcess_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ result_t SubProcess::create(exlib::string command, v8::Local<v8::Array> args, v8
util_base::clone(cur_envs, opt_envs_v);
else if (hr < 0)
return hr;

v8::Local<v8::Object> opt_envs = opt_envs_v->ToObject();
v8::Local<v8::Value> dflt_k;
bool has_k;
Expand Down Expand Up @@ -241,7 +241,9 @@ result_t SubProcess::create(exlib::string command, v8::Local<v8::Array> args, v8
return CHECK_ERROR(-err);
}

obj_ptr<SubProcess> sub = new SubProcess(pid);
int32_t _current_pid = -1;
process_base::get_pid(_current_pid);
obj_ptr<SubProcess> sub = new SubProcess(pid, _current_pid);
s_ids.insert(std::pair<pid_t, obj_ptr<SubProcess>>(pid, sub));

s_lock.unlock();
Expand All @@ -267,6 +269,12 @@ result_t SubProcess::get_pid(int32_t& retVal)
return 0;
}

result_t SubProcess::get_ppid(int32_t& retVal)
{
retVal = m_ppid;
return 0;
}

result_t SubProcess::kill(int32_t signal)
{
if (m_timer)
Expand Down
12 changes: 10 additions & 2 deletions fibjs/src/process/SubProcess_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ result_t SubProcess::create(exlib::string command, v8::Local<v8::Array> args, v8
util_base::clone(cur_envs, opt_envs_v);
else if (hr < 0)
return hr;

v8::Local<v8::Object> opt_envs = opt_envs_v->ToObject();
v8::Local<v8::Value> dflt_k;
bool has_k;
Expand Down Expand Up @@ -209,7 +209,9 @@ result_t SubProcess::create(exlib::string command, v8::Local<v8::Array> args, v8

::CloseHandle(pi.hThread);

obj_ptr<SubProcess> sub = new SubProcess((intptr_t)pi.hProcess);
int32_t _current_pid = -1;
process_base::get_pid(_current_pid);
obj_ptr<SubProcess> sub = new SubProcess((intptr_t)pi.hProcess, (intptr_t)_current_pid);
if (redirect) {
::CloseHandle(cin_pipe[1]);
::CloseHandle(cout_pipe[1]);
Expand All @@ -234,6 +236,12 @@ result_t SubProcess::get_pid(int32_t& retVal)
return 0;
}

result_t SubProcess::get_ppid(int32_t& retVal)
{
retVal = (int32_t)m_ppid;
return 0;
}

result_t SubProcess::kill(int32_t signal)
{
if (m_timer)
Expand Down
14 changes: 13 additions & 1 deletion fibjs/src/process/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
#ifdef _WIN32
#include <psapi.h>
#include "utf8.h"

#include "process_win.h"
#else

#include <unistd.h>
#include "editline/include/editline.h"

inline int32_t _umask(int32_t m)
Expand Down Expand Up @@ -233,6 +234,17 @@ result_t process_base::get_pid(int32_t& retVal)
return 0;
}

result_t process_base::get_ppid(int32_t& retVal)
{
#ifdef _WIN32
retVal = (int32_t)GetParentProcessID();
#else
retVal = getppid();
#endif

return 0;
}

result_t process_base::get_stdin(obj_ptr<File_base>& retVal)
{
Isolate* isolate = Isolate::current();
Expand Down
25 changes: 25 additions & 0 deletions fibjs/src/process/process_win.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifdef _WIN32
#include <windows.h>
#include <tlhelp32.h>

static DWORD GetParentProcessID(DWORD pid = GetCurrentProcessId())
{
int parent_pid = -1;
HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pe = { 0 };
pe.dwSize = sizeof(PROCESSENTRY32);

if (Process32First(h, &pe)) {
do {
if (pe.th32ProcessID == pid) {
parent_pid = pe.th32ParentProcessID;
break;
}
} while (Process32Next(h, &pe));
}

CloseHandle(h);

return parent_pid;
}
#endif
4 changes: 4 additions & 0 deletions idl/us-en/SubProcess.idl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ interface SubProcess : BufferedStream
*/
readonly Integer pid;

/*! @brief The readonly id of parent process of current chid process object's.
*/
readonly Integer ppid;

/*! @brief The readonly stdin object of process reffered by current child process object.
*/
readonly BufferedStream stdin;
Expand Down
6 changes: 6 additions & 0 deletions idl/us-en/process.idl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ module process
/*! @brief query current platform,possible value can be: 'darwin', 'freebsd', 'linux', or 'win32' */
static readonly String platform;

/*! @brief read id of current process */
static readonly Integer pid;

/*! @brief read id of current process's parent process */
static readonly Integer ppid;

/*! @brief change the current umask,Windows does't support this.
@param mask the mask to set
@return the previous mask
Expand Down
24 changes: 14 additions & 10 deletions idl/zh-cn/SubProcess.idl
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,34 @@
interface SubProcess : BufferedStream
{
/*! @brief 杀掉当前对象指向的进程,并传递信号
@param signal 传递的信号
*/
@param signal 传递的信号
*/
kill(Integer signal);

/*! @brief 等待当前对象指向的进程结束,并返回进程结束代码
@return 进程的结束代码
*/
@return 进程的结束代码
*/
Integer wait() async;

/*! @brief 查询当前对象所指向的进程是否存在指定名称的窗口,仅限 windows
@param name 窗口名称
@return 窗口存在则返回窗口的 rect,否则返回 undefined
*/
@param name 窗口名称
@return 窗口存在则返回窗口的 rect,否则返回 undefined
*/
Value findWindow(String name);

/*! @brief 读取当前对象指向的进程的 id
*/
*/
readonly Integer pid;

/*! @brief 读取当前对象指向的父进程的 id
*/
readonly Integer ppid;

/*! @brief 读取当前对象指向的进程的标准输入对象
*/
*/
readonly BufferedStream stdin;

/*! @brief 读取当前对象指向的进程的标准输出对象
*/
*/
readonly BufferedStream stdout;
};
3 changes: 3 additions & 0 deletions idl/zh-cn/process.idl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ module process : EventEmitter
/*! @brief 读取当前对象指向的进程的 id */
static readonly Integer pid;

/*! @brief 读取当前对象指向的父进程的 id */
static readonly Integer ppid;

/*! @brief 查询当前进程标准输入对象 */
static readonly File stdin;

Expand Down
2 changes: 2 additions & 0 deletions test/process/exec.ppid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log(process.ppid);
console.log(process.pid);
26 changes: 26 additions & 0 deletions test/process_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ describe('process', () => {
assert.isNumber(process.pid);
assert.ok(process.pid);
});

describe("ppid", () => {
it("basic", () => {
assert.property(process, 'ppid');
assert.isNumber(process.ppid);
});

it("`ppid` in subprocess equals to its parent process's `pid`", () => {
var sp = process.open(cmd, [path.join(__dirname, 'process', 'exec.ppid.js')]);
var [_ppid, _pid] = sp.readLines();

assert.strictEqual(_pid, `${sp.pid}`);
assert.strictEqual(_ppid, `${process.pid}`);
sp.close();
});

it("SubProcess::ppid equals to its parent process's `pid`", () => {
var sp = process.open(cmd, [path.join(__dirname, 'process', 'exec.ppid.js')]);
var [_ppid, _pid] = sp.readLines();

assert.strictEqual(_pid, `${sp.pid}`);
assert.strictEqual(_ppid, `${sp.ppid}`);
assert.strictEqual(sp.ppid, process.pid);
sp.close();
});
});

it("stdout", () => {
var bs = process.open(cmd, [path.join(__dirname, 'process', 'exec.js')]);
Expand Down

0 comments on commit a5a1597

Please sign in to comment.