Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/hotspot/os/aix/os_aix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,9 @@ int os::current_process_id() {
return getpid();
}

void os::reset_cached_process_id() {
}

// DLL functions

// This must be hard coded because it's the system's temporary
Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/os/bsd/os_bsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,9 @@ int os::current_process_id() {
return (int)(getpid());
}

void os::reset_cached_process_id() {
}

// DLL functions
static int local_dladdr(const void* addr, Dl_info* info) {
#ifdef __APPLE__
Expand Down
13 changes: 12 additions & 1 deletion src/hotspot/os/linux/os_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ typedef int (*malloc_info_func_t)(int options, FILE *stream);
static malloc_info_func_t g_malloc_info = nullptr;
#endif // __GLIBC__

static int cached_pid = 0;

static int clock_tics_per_sec = 100;

// If the VM might have been created on the primordial thread, we need to resolve the
Expand Down Expand Up @@ -1516,8 +1518,16 @@ void os::Linux::fast_thread_clock_init() {

// thread_id is kernel thread id (similar to Solaris LWP id)
intx os::current_thread_id() { return os::Linux::gettid(); }

int os::current_process_id() {
return ::getpid();
// GLIBC < 2.25 caches pid in ::getpid(); we need to be able to reset this
// on CRaC restore, therefore we will use our own caching.
return cached_pid ? cached_pid : syscall(SYS_getpid);
}

void os::reset_cached_process_id() {
cached_pid = 0;
cached_pid = current_process_id();
}

// DLL functions
Expand Down Expand Up @@ -4415,6 +4425,7 @@ static void check_pax(void) {
// this is called _before_ most of the global arguments have been parsed
void os::init(void) {
char dummy; // used to get a guess on initial stack address
cached_pid = current_process_id();

clock_tics_per_sec = checked_cast<int>(sysconf(_SC_CLK_TCK));
int sys_pg_size = checked_cast<int>(sysconf(_SC_PAGESIZE));
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/os/posix/signals_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ void os::print_siginfo(outputStream* os, const void* si0) {
const pid_t pid = si->si_pid;
os->print(", si_pid: %ld", (long) pid);
if (IS_VALID_PID(pid)) {
const pid_t me = getpid();
const pid_t me = os::current_process_id();
if (me == pid) {
os->print(" (current process)");
}
Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/os/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3951,6 +3951,9 @@ int os::current_process_id() {
return (_initial_pid ? _initial_pid : _getpid());
}

void os::reset_cached_process_id() {
}

int os::win32::_processor_type = 0;
// Processor level is not available on non-NT systems, use vm_version instead
int os::win32::_processor_level = 0;
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/runtime/crac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ void VM_Crac::doit() {
}

Arguments::reset_for_crac_restore();
os::reset_cached_process_id();

if (shmid == 0) { // E.g. engine does not support restore data
log_debug(crac)("Restore parameters (JVM flags, env vars, system properties, arguments...) not provided");
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/runtime/os.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ class os: AllStatic {
// thread id on Linux/64bit is 64bit, on Windows it's 32bit
static intx current_thread_id();
static int current_process_id();
static void reset_cached_process_id();

// Short standalone OS sleep routines suitable for slow path spin loop.
// Ignores safepoints/suspension/Thread.interrupt() (so keep it short).
Expand Down
18 changes: 17 additions & 1 deletion src/java.base/share/classes/java/lang/ProcessHandleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
*/
package java.lang;

import jdk.internal.crac.Core;
import jdk.internal.crac.JDKResource;
import jdk.internal.crac.mirror.Context;
import jdk.internal.crac.mirror.Resource;
import jdk.internal.misc.InnocuousThread;

import java.lang.annotation.Native;
Expand Down Expand Up @@ -73,10 +77,22 @@ final class ProcessHandleImpl implements ProcessHandle {
private static final ConcurrentMap<Long, ExitCompletion>
completions = new ConcurrentHashMap<>();

private static JDKResource cracResource = new JDKResource() {
@Override
public void beforeCheckpoint(Context<? extends Resource> context) {
}

@Override
public void afterRestore(Context<? extends Resource> context) {
current.pid = getCurrentPid0();
}
};

static {
initNative();
long pid = getCurrentPid0();
current = new ProcessHandleImpl(pid, isAlive0(pid));
Core.Priority.NORMAL.getContext().register(cracResource);
}

private static native void initNative();
Expand Down Expand Up @@ -206,7 +222,7 @@ public CompletableFuture<ProcessHandle> onExit() {
/**
* The pid of this ProcessHandle.
*/
private final long pid;
private long pid;

/**
* The start time of this process.
Expand Down
15 changes: 14 additions & 1 deletion src/java.base/unix/native/libjava/ProcessHandleImpl_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
#include <sys/stat.h>
#include <sys/wait.h>

#ifdef LINUX
#include <sys/syscall.h>
#endif

#include <pwd.h>

#if defined(_AIX)
Expand Down Expand Up @@ -274,7 +278,11 @@ Java_java_lang_ProcessHandleImpl_waitForProcessExit0(JNIEnv* env,
*/
JNIEXPORT jlong JNICALL
Java_java_lang_ProcessHandleImpl_getCurrentPid0(JNIEnv *env, jclass clazz) {
#ifdef LINUX
pid_t pid = syscall(SYS_getpid);
#else
pid_t pid = getpid();
#endif
return (jlong) pid;
}

Expand Down Expand Up @@ -383,7 +391,12 @@ Java_java_lang_ProcessHandleImpl_parent0(JNIEnv *env,
pid_t pid = (pid_t) jpid;
pid_t ppid;

if (pid == getpid()) {
#ifdef LINUX
pid_t mypid = syscall(SYS_getpid);
#else
pid_t mypid = getpid();
#endif
if (pid == mypid) {
ppid = getppid();
} else {
jlong start = 0L;
Expand Down
7 changes: 7 additions & 0 deletions src/java.base/unix/native/libnet/Inet4AddressImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include <netinet/ip_icmp.h>
#include <stdlib.h>
#include <string.h>
#ifdef LINUX
#include <sys/syscall.h>
#endif
#include <sys/time.h>

#include "net_util.h"
Expand Down Expand Up @@ -379,7 +382,11 @@ ping4(JNIEnv *env, jint fd, SOCKETADDRESS *sa, SOCKETADDRESS *netif,
}

// icmp_id is a 16 bit data type, therefore down cast the pid
#ifdef LINUX
pid = (jchar)syscall(SYS_getpid);
#else
pid = (jchar)getpid();
#endif // !LINUX

// Make the socket non blocking so we can use select
SET_NONBLOCKING(fd);
Expand Down
7 changes: 7 additions & 0 deletions src/java.base/unix/native/libnet/Inet6AddressImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef LINUX
#include <sys/syscall.h>
#endif
#include <sys/time.h>
#include <sys/types.h>
#include <netinet/in.h>
Expand Down Expand Up @@ -587,7 +590,11 @@ ping6(JNIEnv *env, jint fd, SOCKETADDRESS *sa, SOCKETADDRESS *netif,
}

// icmp_id is a 16 bit data type, therefore down cast the pid
#ifdef LINUX
pid = (jchar)syscall(SYS_getpid);
#else
pid = (jchar)getpid();
#endif // !LINUX

// Make the socket non blocking so we can use select
SET_NONBLOCKING(fd);
Expand Down
7 changes: 7 additions & 0 deletions src/java.desktop/unix/native/libawt_xawt/xawt/XToolkit.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
#include "java_awt_TrayIcon.h"
#include <X11/extensions/XTest.h>

#ifdef LINUX
#include <sys/syscall.h>
#endif
#include <unistd.h>

uint32_t awt_NumLockMask = 0;
Expand Down Expand Up @@ -862,7 +865,11 @@ JNIEXPORT jint JNICALL Java_sun_awt_X11_XWindowPeer_getJvmPID
(JNIEnv *env, jclass cls)
{
/* Return the JVM's PID. */
#ifdef LINUX
return syscall(SYS_getpid);
#else
return getpid();
#endif // !LINUX
}

#ifndef HOST_NAME_MAX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@

public abstract class HotSpotVirtualMachine extends VirtualMachine {

private static final long CURRENT_PID = pid();

@SuppressWarnings("removal")
private static long pid() {
return ProcessHandle.current().pid();
Expand All @@ -76,7 +74,7 @@ private static long pid() {

// The tool should be a different VM to the target. This check will
// eventually be enforced by the target VM.
if (!ALLOW_ATTACH_SELF && (pid == 0 || pid == CURRENT_PID)) {
if (!ALLOW_ATTACH_SELF && (pid == 0 || pid == pid())) {
throw new IOException("Can not attach to current VM");
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/jdk.jdwp.agent/unix/native/libjdwp/exec_md.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#ifdef LINUX
#include <sys/syscall.h>
#endif
#include <sys/resource.h>
#include <unistd.h>
#include <string.h>
Expand Down Expand Up @@ -91,9 +94,14 @@ closeDescriptors(void)
#endif

if ((dp = opendir(FD_DIR)) == NULL) {
#ifdef LINUX
pid_t pid = syscall(SYS_getpid);
#else
pid_t pid = getpid();
#endif // !LINUX
ERROR_MESSAGE(("failed to open dir %s while determining"
" file descriptors to close for process %d",
FD_DIR, getpid()));
FD_DIR, pid));
return 0; // failure
}

Expand Down
9 changes: 8 additions & 1 deletion src/jdk.jdwp.agent/unix/native/libjdwp/proc_md.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
/* Posix threads */

#include <unistd.h>
#ifdef LINUX
#include <sys/syscall.h>
#endif
#include <sys/wait.h>
#include <time.h>
#include <sys/time.h>
Expand All @@ -38,7 +41,11 @@
#define GET_THREAD_ID() pthread_self()
#define THREAD_T pthread_t
#define PID_T pid_t
#define GETPID() getpid()
#ifdef LINUX
# define GETPID() syscall(SYS_getpid)
#else
# define GETPID() getpid()
#endif // !LINUX
#define GETMILLSECS(millisecs) \
{ \
struct timeval tval; \
Expand Down