Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cbe1e5c

Browse filesBrowse files
bnoordhuisgibfahn
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 <cjihrig@gmail.com> Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
1 parent 96bf44a commit cbe1e5c
Copy full SHA for cbe1e5c

File tree

Expand file treeCollapse file tree

5 files changed

+27
-19
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+27
-19
lines changed
Open diff view settings
Collapse file

‎src/env.cc‎

Copy file name to clipboardExpand all lines: src/env.cc
+2-7Lines changed: 2 additions & 7 deletions
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

@@ -130,7 +124,8 @@ void Environment::PrintSyncTrace() const {
130124
Local<v8::StackTrace> stack =
131125
StackTrace::CurrentStackTrace(isolate(), 10, StackTrace::kDetailed);
132126

133-
fprintf(stderr, "(node:%d) WARNING: Detected use of sync API\n", getpid());
127+
fprintf(stderr, "(node:%u) WARNING: Detected use of sync API\n",
128+
GetProcessId());
134129

135130
for (int i = 0; i < stack->GetFrameCount() - 1; i++) {
136131
Local<StackFrame> stack_frame = stack->GetFrame(i);
Collapse file

‎src/inspector_agent.cc‎

Copy file name to clipboardExpand all lines: src/inspector_agent.cc
+4-3Lines 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.
Collapse file

‎src/node.cc‎

Copy file name to clipboardExpand all lines: src/node.cc
+4-9Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
#if defined(_MSC_VER)
101101
#include <direct.h>
102102
#include <io.h>
103-
#define getpid GetCurrentProcessId
104103
#define umask _umask
105104
typedef int mode_t;
106105
#else
@@ -2040,13 +2039,8 @@ NO_RETURN void Assert(const char* const (*args)[4]) {
20402039
if (uv_exepath(exepath, &exepath_size))
20412040
snprintf(exepath, sizeof(exepath), "node");
20422041

2043-
char pid[12] = {0};
2044-
#ifndef _WIN32
2045-
snprintf(pid, sizeof(pid), "[%u]", getpid());
2046-
#endif
2047-
2048-
fprintf(stderr, "%s%s: %s:%s:%s%s Assertion `%s' failed.\n",
2049-
exepath, pid, filename, linenum,
2042+
fprintf(stderr, "%s[%u]: %s:%s:%s%s Assertion `%s' failed.\n",
2043+
exepath, GetProcessId(), filename, linenum,
20502044
function, *function ? ":" : "", message);
20512045
fflush(stderr);
20522046

@@ -3522,7 +3516,8 @@ void SetupProcessObject(Environment* env,
35223516
process_env_template->NewInstance(env->context()).ToLocalChecked();
35233517
process->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "env"), process_env);
35243518

3525-
READONLY_PROPERTY(process, "pid", Integer::New(env->isolate(), getpid()));
3519+
READONLY_PROPERTY(process, "pid",
3520+
Integer::New(env->isolate(), GetProcessId()));
35263521
READONLY_PROPERTY(process, "features", GetFeatures(env));
35273522

35283523
auto need_immediate_callback_string =
Collapse file

‎src/node_internals.h‎

Copy file name to clipboardExpand all lines: src/node_internals.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ void RegisterSignalHandler(int signal,
143143
bool reset_handler = false);
144144
#endif
145145

146+
uint32_t GetProcessId();
146147
bool SafeGetenv(const char* key, std::string* text);
147148

148149
template <typename T, size_t N>
Collapse file

‎src/util.cc‎

Copy file name to clipboardExpand all lines: src/util.cc
+16Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.