Skip to content

Commit 7ecec67

Browse files
bnoordhuisMylesBorins
authored andcommitted
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: #17087 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Eugene Ostroukhov <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
1 parent 4c23e6a commit 7ecec67

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

src/env.cc

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
#include "async_wrap.h"
33
#include "v8-profiler.h"
44

5-
#if defined(_MSC_VER)
6-
#define getpid GetCurrentProcessId
7-
#else
8-
#include <unistd.h>
9-
#endif
10-
115
#include <stdio.h>
126
#include <algorithm>
137

@@ -182,7 +176,8 @@ void Environment::PrintSyncTrace() const {
182176
Local<v8::StackTrace> stack =
183177
StackTrace::CurrentStackTrace(isolate(), 10, StackTrace::kDetailed);
184178

185-
fprintf(stderr, "(node:%d) WARNING: Detected use of sync API\n", getpid());
179+
fprintf(stderr, "(node:%u) WARNING: Detected use of sync API\n",
180+
GetProcessId());
186181

187182
for (int i = 0; i < stack->GetFrameCount() - 1; i++) {
188183
Local<StackFrame> stack_frame = stack->GetFrame(i);

src/inspector_agent.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#include <vector>
1414

1515
#ifdef __POSIX__
16-
#include <limits.h>
17-
#include <unistd.h> // setuid, getuid
16+
#include <limits.h> // PTHREAD_STACK_MIN
17+
#include <pthread.h>
1818
#endif // __POSIX__
1919

2020
namespace node {
@@ -108,7 +108,8 @@ static int StartDebugSignalHandler() {
108108
CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, nullptr));
109109
CHECK_EQ(0, pthread_attr_destroy(&attr));
110110
if (err != 0) {
111-
fprintf(stderr, "node[%d]: pthread_create: %s\n", getpid(), strerror(err));
111+
fprintf(stderr, "node[%u]: pthread_create: %s\n",
112+
GetProcessId(), strerror(err));
112113
fflush(stderr);
113114
// Leave SIGUSR1 blocked. We don't install a signal handler,
114115
// receiving the signal would terminate the process.

src/node.cc

+4-9
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@
9999
#if defined(_MSC_VER)
100100
#include <direct.h>
101101
#include <io.h>
102-
#define getpid GetCurrentProcessId
103102
#define umask _umask
104103
typedef int mode_t;
105104
#else
@@ -1995,13 +1994,8 @@ NO_RETURN void Assert(const char* const (*args)[4]) {
19951994
if (uv_exepath(exepath, &exepath_size))
19961995
snprintf(exepath, sizeof(exepath), "node");
19971996

1998-
char pid[12] = {0};
1999-
#ifndef _WIN32
2000-
snprintf(pid, sizeof(pid), "[%u]", getpid());
2001-
#endif
2002-
2003-
fprintf(stderr, "%s%s: %s:%s:%s%s Assertion `%s' failed.\n",
2004-
exepath, pid, filename, linenum,
1997+
fprintf(stderr, "%s[%u]: %s:%s:%s%s Assertion `%s' failed.\n",
1998+
exepath, GetProcessId(), filename, linenum,
20051999
function, *function ? ":" : "", message);
20062000
fflush(stderr);
20072001

@@ -3532,7 +3526,8 @@ void SetupProcessObject(Environment* env,
35323526
process_env_template->NewInstance(env->context()).ToLocalChecked();
35333527
process->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "env"), process_env);
35343528

3535-
READONLY_PROPERTY(process, "pid", Integer::New(env->isolate(), getpid()));
3529+
READONLY_PROPERTY(process, "pid",
3530+
Integer::New(env->isolate(), GetProcessId()));
35363531
READONLY_PROPERTY(process, "features", GetFeatures(env));
35373532

35383533
CHECK(process->SetAccessor(env->context(),

src/node_internals.h

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ void RegisterSignalHandler(int signal,
166166
bool reset_handler = false);
167167
#endif
168168

169+
uint32_t GetProcessId();
169170
bool SafeGetenv(const char* key, std::string* text);
170171

171172
template <typename T, size_t N>

src/util.cc

+16
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
#include "node_internals.h"
2525
#include <stdio.h>
2626

27+
#ifdef __POSIX__
28+
#include <unistd.h> // getpid()
29+
#endif
30+
31+
#ifdef _MSC_VER
32+
#include <windows.h> // GetCurrentProcessId()
33+
#endif
34+
2735
namespace node {
2836

2937
using v8::Isolate;
@@ -105,4 +113,12 @@ void LowMemoryNotification() {
105113
}
106114
}
107115

116+
uint32_t GetProcessId() {
117+
#ifdef _WIN32
118+
return GetCurrentProcessId();
119+
#else
120+
return getpid();
121+
#endif
122+
}
123+
108124
} // namespace node

0 commit comments

Comments
 (0)