]> git.sven.stormbind.net Git - sven/jattach.git/blob - src/posix/jattach_hotspot.c
New upstream version 2.0
[sven/jattach.git] / src / posix / jattach_hotspot.c
1 /*
2  * Copyright 2021 Andrei Pangin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/un.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include "psutil.h"
28
29
30 // Check if remote JVM has already opened socket for Dynamic Attach
31 static int check_socket(int pid) {
32     char path[MAX_PATH];
33     snprintf(path, sizeof(path), "%s/.java_pid%d", tmp_path, pid);
34
35     struct stat stats;
36     return stat(path, &stats) == 0 && S_ISSOCK(stats.st_mode) ? 0 : -1;
37 }
38
39 // Check if a file is owned by current user
40 static uid_t get_file_owner(const char* path) {
41     struct stat stats;
42     return stat(path, &stats) == 0 ? stats.st_uid : (uid_t)-1;
43 }
44
45 // Force remote JVM to start Attach listener.
46 // HotSpot will start Attach listener in response to SIGQUIT if it sees .attach_pid file
47 static int start_attach_mechanism(int pid, int nspid) {
48     char path[MAX_PATH];
49     snprintf(path, sizeof(path), "/proc/%d/cwd/.attach_pid%d", nspid, nspid);
50
51     int fd = creat(path, 0660);
52     if (fd == -1 || (close(fd) == 0 && get_file_owner(path) != geteuid())) {
53         // Some mounted filesystems may change the ownership of the file.
54         // JVM will not trust such file, so it's better to remove it and try a different path
55         unlink(path);
56
57         // Failed to create attach trigger in current directory. Retry in /tmp
58         snprintf(path, sizeof(path), "%s/.attach_pid%d", tmp_path, nspid);
59         fd = creat(path, 0660);
60         if (fd == -1) {
61             return -1;
62         }
63         close(fd);
64     }
65
66     // We have to still use the host namespace pid here for the kill() call
67     kill(pid, SIGQUIT);
68
69     // Start with 20 ms sleep and increment delay each iteration. Total timeout is 6000 ms
70     struct timespec ts = {0, 20000000};
71     int result;
72     do {
73         nanosleep(&ts, NULL);
74         result = check_socket(nspid);
75     } while (result != 0 && (ts.tv_nsec += 20000000) < 500000000);
76
77     unlink(path);
78     return result;
79 }
80
81 // Connect to UNIX domain socket created by JVM for Dynamic Attach
82 static int connect_socket(int pid) {
83     int fd = socket(PF_UNIX, SOCK_STREAM, 0);
84     if (fd == -1) {
85         return -1;
86     }
87
88     struct sockaddr_un addr;
89     addr.sun_family = AF_UNIX;
90
91     int bytes = snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/.java_pid%d", tmp_path, pid);
92     if (bytes >= sizeof(addr.sun_path)) {
93         addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
94     }
95
96     if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
97         close(fd);
98         return -1;
99     }
100     return fd;
101 }
102
103 // Send command with arguments to socket
104 static int write_command(int fd, int argc, char** argv) {
105     // Protocol version
106     if (write(fd, "1", 2) <= 0) {
107         return -1;
108     }
109
110     int i;
111     for (i = 0; i < 4; i++) {
112         const char* arg = i < argc ? argv[i] : "";
113         if (write(fd, arg, strlen(arg) + 1) <= 0) {
114             return -1;
115         }
116     }
117     return 0;
118 }
119
120 // Mirror response from remote JVM to stdout
121 static int read_response(int fd, int argc, char** argv) {
122     char buf[8192];
123     ssize_t bytes = read(fd, buf, sizeof(buf) - 1);
124     if (bytes == 0) {
125         fprintf(stderr, "Unexpected EOF reading response\n");
126         return 1;
127     } else if (bytes < 0) {
128         perror("Error reading response");
129         return 1;
130     }
131
132     // First line of response is the command result code
133     buf[bytes] = 0;
134     int result = atoi(buf);
135
136     // Special treatment of 'load' command
137     if (result == 0 && argc > 0 && strcmp(argv[0], "load") == 0) {
138         size_t total = bytes;
139         while (total < sizeof(buf) - 1 && (bytes = read(fd, buf + total, sizeof(buf) - 1 - total)) > 0) {
140             total += (size_t)bytes;
141         }
142         bytes = total;
143
144         // The second line is the result of 'load' command; since JDK 9 it starts from "return code: "
145         buf[bytes] = 0;
146         result = atoi(strncmp(buf + 2, "return code: ", 13) == 0 ? buf + 15 : buf + 2);
147     }
148
149     // Mirror JVM response to stdout
150     printf("JVM response code = ");
151     do {
152         fwrite(buf, 1, bytes, stdout);
153         bytes = read(fd, buf, sizeof(buf));
154     } while (bytes > 0);
155     printf("\n");
156
157     return result;
158 }
159
160 int jattach_hotspot(int pid, int nspid, int argc, char** argv) {
161     if (check_socket(nspid) != 0 && start_attach_mechanism(pid, nspid) != 0) {
162         perror("Could not start attach mechanism");
163         return 1;
164     }
165
166     int fd = connect_socket(nspid);
167     if (fd == -1) {
168         perror("Could not connect to socket");
169         return 1;
170     }
171
172     printf("Connected to remote JVM\n");
173
174     if (write_command(fd, argc, argv) != 0) {
175         perror("Error writing to socket");
176         close(fd);
177         return 1;
178     }
179
180     int result = read_response(fd, argc, argv);
181     close(fd);
182
183     return result;
184 }