Skip to content

Commit

Permalink
Fixed compiler errors on ancient Linux distributions
Browse files Browse the repository at this point in the history
  • Loading branch information
apangin committed Oct 8, 2017
1 parent 48c836c commit f47c152
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/jattach.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/syscall.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>

#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");
}
}
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f47c152

Please sign in to comment.