Skip to content

Commit

Permalink
src: abstract getpid() operation
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bnoordhuis authored and gibfahn committed Dec 20, 2017
1 parent 017a75b commit c592073
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
9 changes: 2 additions & 7 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -130,7 +124,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);
Expand Down
7 changes: 4 additions & 3 deletions src/inspector_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 4 additions & 9 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
#if defined(_MSC_VER)
#include <direct.h>
#include <io.h>
#define getpid GetCurrentProcessId
#define umask _umask
typedef int mode_t;
#else
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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 =
Expand Down
1 change: 1 addition & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T, size_t N>
Expand Down
16 changes: 16 additions & 0 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -105,4 +113,12 @@ void LowMemoryNotification() {
}
}

uint32_t GetProcessId() {
#ifdef _WIN32
return GetCurrentProcessId();
#else
return getpid();
#endif
}

} // namespace node

0 comments on commit c592073

Please sign in to comment.