diff --git a/src/jattach.c b/src/jattach.c index 7ee5993df..4709cc643 100755 --- a/src/jattach.c +++ b/src/jattach.c @@ -21,24 +21,25 @@ #include #include #include +#include #include #include #include #include #include -#define PATH_MAX 1024 +#define MAX_PATH 1024 #ifdef __APPLE__ // macOS has a secure per-user temporary directory const char* get_temp_directory() { - static char temp_path_storage[PATH_MAX] = {0}; + static char temp_path_storage[MAX_PATH] = {0}; if (temp_path_storage[0] == 0) { - int path_size = confstr(_CS_DARWIN_USER_TEMP_DIR, temp_path_storage, PATH_MAX); - if (path_size == 0 || path_size > PATH_MAX) { + int path_size = confstr(_CS_DARWIN_USER_TEMP_DIR, temp_path_storage, MAX_PATH); + if (path_size == 0 || path_size > MAX_PATH) { strcpy(temp_path_storage, "/tmp"); } } @@ -51,14 +52,12 @@ const char* get_temp_directory() { return "/tmp"; } -int setns(int fd, int nstype); - #endif // __APPLE__ // Check if remote JVM has already opened socket for Dynamic Attach static int check_socket(int pid) { - char path[PATH_MAX]; - snprintf(path, PATH_MAX, "%s/.java_pid%d", get_temp_directory(), pid); + char path[MAX_PATH]; + snprintf(path, MAX_PATH, "%s/.java_pid%d", get_temp_directory(), pid); struct stat stats; return stat(path, &stats) == 0 && S_ISSOCK(stats.st_mode); @@ -67,12 +66,12 @@ static int check_socket(int pid) { // Force remote JVM to start Attach listener. // HotSpot will start Attach listener in response to SIGQUIT if it sees .attach_pid file static int start_attach_mechanism(int pid, int nspid) { - char path[PATH_MAX]; - snprintf(path, PATH_MAX, "/proc/%d/cwd/.attach_pid%d", nspid, nspid); + char path[MAX_PATH]; + snprintf(path, MAX_PATH, "/proc/%d/cwd/.attach_pid%d", nspid, nspid); int fd = creat(path, 0660); if (fd == -1) { - snprintf(path, PATH_MAX, "%s/.attach_pid%d", get_temp_directory(), nspid); + snprintf(path, MAX_PATH, "%s/.attach_pid%d", get_temp_directory(), nspid); fd = creat(path, 0660); if (fd == -1) { return 0; @@ -192,7 +191,8 @@ static int enter_mount_ns(int pid) { return 1; } - return setns(newns, 0) < 0 ? 0 : 1; + // Some ancient Linux distributions do not have setns() function + return syscall(__NR_setns, newns, 0) < 0 ? 0 : 1; #else return 1; #endif