Skip to content

Commit 7dc35e9

Browse files
committed
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 65439b4 commit 7dc35e9

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

src/env.cc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
#include "node_buffer.h"
55
#include "node_platform.h"
66

7-
#if defined(_MSC_VER)
8-
#define getpid GetCurrentProcessId
9-
#else
10-
#include <unistd.h>
11-
#endif
12-
137
#include <stdio.h>
148
#include <algorithm>
159

@@ -184,7 +178,8 @@ void Environment::PrintSyncTrace() const {
184178
Local<v8::StackTrace> stack =
185179
StackTrace::CurrentStackTrace(isolate(), 10, StackTrace::kDetailed);
186180

187-
fprintf(stderr, "(node:%d) WARNING: Detected use of sync API\n", getpid());
181+
fprintf(stderr, "(node:%u) WARNING: Detected use of sync API\n",
182+
GetProcessId());
188183

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

src/inspector_agent.cc

Lines changed: 4 additions & 3 deletions
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

Lines changed: 4 additions & 9 deletions
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
@@ -1659,13 +1658,8 @@ NO_RETURN void Assert(const char* const (*args)[4]) {
16591658
if (uv_exepath(exepath, &exepath_size))
16601659
snprintf(exepath, sizeof(exepath), "node");
16611660

1662-
char pid[12] = {0};
1663-
#ifndef _WIN32
1664-
snprintf(pid, sizeof(pid), "[%u]", getpid());
1665-
#endif
1666-
1667-
fprintf(stderr, "%s%s: %s:%s:%s%s Assertion `%s' failed.\n",
1668-
exepath, pid, filename, linenum,
1661+
fprintf(stderr, "%s[%u]: %s:%s:%s%s Assertion `%s' failed.\n",
1662+
exepath, GetProcessId(), filename, linenum,
16691663
function, *function ? ":" : "", message);
16701664
fflush(stderr);
16711665

@@ -3197,7 +3191,8 @@ void SetupProcessObject(Environment* env,
31973191
process_env_template->NewInstance(env->context()).ToLocalChecked();
31983192
process->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "env"), process_env);
31993193

3200-
READONLY_PROPERTY(process, "pid", Integer::New(env->isolate(), getpid()));
3194+
READONLY_PROPERTY(process, "pid",
3195+
Integer::New(env->isolate(), GetProcessId()));
32013196
READONLY_PROPERTY(process, "features", GetFeatures(env));
32023197

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

src/node_internals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ void RegisterSignalHandler(int signal,
248248
bool reset_handler = false);
249249
#endif
250250

251+
uint32_t GetProcessId();
251252
bool SafeGetenv(const char* key, std::string* text);
252253

253254
template <typename T, size_t N>

src/util.cc

Lines changed: 16 additions & 0 deletions
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)