diff --git a/src/env.cc b/src/env.cc index a0d82986c9e231..56166042903d06 100644 --- a/src/env.cc +++ b/src/env.cc @@ -2,12 +2,6 @@ #include "async-wrap.h" #include "v8-profiler.h" -#if defined(_MSC_VER) -#define getpid GetCurrentProcessId -#else -#include -#endif - #include #include @@ -130,7 +124,8 @@ void Environment::PrintSyncTrace() const { Local 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 stack_frame = stack->GetFrame(i); diff --git a/src/inspector_agent.cc b/src/inspector_agent.cc index 976899e677da09..3a3f4e0842ab52 100644 --- a/src/inspector_agent.cc +++ b/src/inspector_agent.cc @@ -13,8 +13,8 @@ #include #ifdef __POSIX__ -#include -#include // setuid, getuid +#include // PTHREAD_STACK_MIN +#include #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. diff --git a/src/node.cc b/src/node.cc index 4728798d0580a2..e9a08f8165a3bf 100644 --- a/src/node.cc +++ b/src/node.cc @@ -100,7 +100,6 @@ #if defined(_MSC_VER) #include #include -#define getpid GetCurrentProcessId #define umask _umask typedef int mode_t; #else @@ -2040,13 +2039,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); @@ -3522,7 +3516,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)); auto need_immediate_callback_string = diff --git a/src/node_internals.h b/src/node_internals.h index 85f90a60f8d278..5056ace8664f70 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -143,6 +143,7 @@ void RegisterSignalHandler(int signal, bool reset_handler = false); #endif +uint32_t GetProcessId(); bool SafeGetenv(const char* key, std::string* text); template diff --git a/src/util.cc b/src/util.cc index ef93d16968e465..0fb897bc8e891c 100644 --- a/src/util.cc +++ b/src/util.cc @@ -24,6 +24,14 @@ #include "node_internals.h" #include +#ifdef __POSIX__ +#include // getpid() +#endif + +#ifdef _MSC_VER +#include // 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