src: abstract getpid() operation
There are a few places where we paper over the fact that getpid() is called GetCurrentProcessId() on Windows. Let's move it into a function. PR-URL: https://github.com/nodejs/node/pull/17087 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
This commit is contained in:
parent
65439b4c17
commit
7dc35e937d
@ -4,12 +4,6 @@
|
||||
#include "node_buffer.h"
|
||||
#include "node_platform.h"
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define getpid GetCurrentProcessId
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <algorithm>
|
||||
|
||||
@ -184,7 +178,8 @@ void Environment::PrintSyncTrace() const {
|
||||
Local<v8::StackTrace> stack =
|
||||
StackTrace::CurrentStackTrace(isolate(), 10, StackTrace::kDetailed);
|
||||
|
||||
fprintf(stderr, "(node:%d) WARNING: Detected use of sync API\n", getpid());
|
||||
fprintf(stderr, "(node:%u) WARNING: Detected use of sync API\n",
|
||||
GetProcessId());
|
||||
|
||||
for (int i = 0; i < stack->GetFrameCount() - 1; i++) {
|
||||
Local<StackFrame> stack_frame = stack->GetFrame(i);
|
||||
|
@ -13,8 +13,8 @@
|
||||
#include <vector>
|
||||
|
||||
#ifdef __POSIX__
|
||||
#include <limits.h>
|
||||
#include <unistd.h> // setuid, getuid
|
||||
#include <limits.h> // PTHREAD_STACK_MIN
|
||||
#include <pthread.h>
|
||||
#endif // __POSIX__
|
||||
|
||||
namespace node {
|
||||
@ -108,7 +108,8 @@ static int StartDebugSignalHandler() {
|
||||
CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, nullptr));
|
||||
CHECK_EQ(0, pthread_attr_destroy(&attr));
|
||||
if (err != 0) {
|
||||
fprintf(stderr, "node[%d]: pthread_create: %s\n", getpid(), strerror(err));
|
||||
fprintf(stderr, "node[%u]: pthread_create: %s\n",
|
||||
GetProcessId(), strerror(err));
|
||||
fflush(stderr);
|
||||
// Leave SIGUSR1 blocked. We don't install a signal handler,
|
||||
// receiving the signal would terminate the process.
|
||||
|
13
src/node.cc
13
src/node.cc
@ -99,7 +99,6 @@
|
||||
#if defined(_MSC_VER)
|
||||
#include <direct.h>
|
||||
#include <io.h>
|
||||
#define getpid GetCurrentProcessId
|
||||
#define umask _umask
|
||||
typedef int mode_t;
|
||||
#else
|
||||
@ -1659,13 +1658,8 @@ NO_RETURN void Assert(const char* const (*args)[4]) {
|
||||
if (uv_exepath(exepath, &exepath_size))
|
||||
snprintf(exepath, sizeof(exepath), "node");
|
||||
|
||||
char pid[12] = {0};
|
||||
#ifndef _WIN32
|
||||
snprintf(pid, sizeof(pid), "[%u]", getpid());
|
||||
#endif
|
||||
|
||||
fprintf(stderr, "%s%s: %s:%s:%s%s Assertion `%s' failed.\n",
|
||||
exepath, pid, filename, linenum,
|
||||
fprintf(stderr, "%s[%u]: %s:%s:%s%s Assertion `%s' failed.\n",
|
||||
exepath, GetProcessId(), filename, linenum,
|
||||
function, *function ? ":" : "", message);
|
||||
fflush(stderr);
|
||||
|
||||
@ -3197,7 +3191,8 @@ void SetupProcessObject(Environment* env,
|
||||
process_env_template->NewInstance(env->context()).ToLocalChecked();
|
||||
process->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "env"), process_env);
|
||||
|
||||
READONLY_PROPERTY(process, "pid", Integer::New(env->isolate(), getpid()));
|
||||
READONLY_PROPERTY(process, "pid",
|
||||
Integer::New(env->isolate(), GetProcessId()));
|
||||
READONLY_PROPERTY(process, "features", GetFeatures(env));
|
||||
|
||||
CHECK(process->SetAccessor(env->context(),
|
||||
|
@ -248,6 +248,7 @@ void RegisterSignalHandler(int signal,
|
||||
bool reset_handler = false);
|
||||
#endif
|
||||
|
||||
uint32_t GetProcessId();
|
||||
bool SafeGetenv(const char* key, std::string* text);
|
||||
|
||||
template <typename T, size_t N>
|
||||
|
16
src/util.cc
16
src/util.cc
@ -24,6 +24,14 @@
|
||||
#include "node_internals.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef __POSIX__
|
||||
#include <unistd.h> // getpid()
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <windows.h> // GetCurrentProcessId()
|
||||
#endif
|
||||
|
||||
namespace node {
|
||||
|
||||
using v8::Isolate;
|
||||
@ -105,4 +113,12 @@ void LowMemoryNotification() {
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t GetProcessId() {
|
||||
#ifdef _WIN32
|
||||
return GetCurrentProcessId();
|
||||
#else
|
||||
return getpid();
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace node
|
||||
|
Loading…
x
Reference in New Issue
Block a user