mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
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:
1 parent
4c23e6a8c7
commit
7ecec6704f
5 files changed
+27
-19
No files matched your search
+2
-7
@@ -2,12 +2,6 @@
|
||||
#include "async_wrap.h"
|
||||
#include "v8-profiler.h"
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define getpid GetCurrentProcessId
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <algorithm>
|
||||
|
||||
@@ -182,7 +176,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.
|
||||
|
||||
+4
-9
@@ -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
|
||||
@@ -1995,13 +1994,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);
|
||||
|
||||
@@ -3532,7 +3526,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(),
|
||||
|
||||
@@ -166,6 +166,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
@@ -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
|
||||
Reference in new issue
Block a user