]> git.sven.stormbind.net Git - sven/jattach.git/blob - src/posix/jattach_hotspot.c
releasing package jattach version 2.2-1
[sven/jattach.git] / src / posix / jattach_hotspot.c
1 /*
2  * Copyright jattach authors
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 extern int mnt_changed;
31
32 // Check if remote JVM has already opened socket for Dynamic Attach
33 static int check_socket(int pid) {
34     char path[MAX_PATH];
35     snprintf(path, sizeof(path), "%s/.java_pid%d", tmp_path, pid);
36
37     struct stat stats;
38     return stat(path, &stats) == 0 && S_ISSOCK(stats.st_mode) ? 0 : -1;
39 }
40
41 // Check if a file is owned by current user
42 static uid_t get_file_owner(const char* path) {
43     struct stat stats;
44     return stat(path, &stats) == 0 ? stats.st_uid : (uid_t)-1;
45 }
46
47 // Force remote JVM to start Attach listener.
48 // HotSpot will start Attach listener in response to SIGQUIT if it sees .attach_pid file
49 static int start_attach_mechanism(int pid, int nspid) {
50     char path[MAX_PATH];
51     snprintf(path, sizeof(path), "/proc/%d/cwd/.attach_pid%d", mnt_changed > 0 ? nspid : pid, nspid);
52
53     int fd = creat(path, 0660);
54     if (fd == -1 || (close(fd) == 0 && get_file_owner(path) != geteuid())) {
55         // Some mounted filesystems may change the ownership of the file.
56         // JVM will not trust such file, so it's better to remove it and try a different path
57         unlink(path);
58
59         // Failed to create attach trigger in current directory. Retry in /tmp
60         snprintf(path, sizeof(path), "%s/.attach_pid%d", tmp_path, nspid);
61         fd = creat(path, 0660);
62         if (fd == -1) {
63             return -1;
64         }
65         close(fd);
66     }
67
68     // We have to still use the host namespace pid here for the kill() call
69     kill(pid, SIGQUIT);
70
71     // Start with 20 ms sleep and increment delay each iteration. Total timeout is 6000 ms
72     struct timespec ts = {0, 20000000};
73     int result;
74     do {
75         nanosleep(&ts, NULL);
76         result = check_socket(nspid);
77     } while (result != 0 && (ts.tv_nsec += 20000000) < 500000000);
78
79     unlink(path);
80     return result;
81 }
82
83 // Connect to UNIX domain socket created by JVM for Dynamic Attach
84 static int connect_socket(int pid) {
85     int fd = socket(PF_UNIX, SOCK_STREAM, 0);
86     if (fd == -1) {
87         return -1;
88     }
89
90     struct sockaddr_un addr;
91     addr.sun_family = AF_UNIX;
92
93     int bytes = snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/.java_pid%d", tmp_path, pid);
94     if (bytes >= sizeof(addr.sun_path)) {
95         addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
96     }
97
98     if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
99         close(fd);
100         return -1;
101     }
102     return fd;
103 }
104
105 // Send command with arguments to socket
106 static int write_command(int fd, int argc, char** argv) {
107     char buf[8192];
108     const char* const limit = buf + sizeof(buf);
109
110     // jcmd has 2 arguments maximum; merge excessive arguments into one
111     int cmd_args = argc >= 2 && strcmp(argv[0], "jcmd") == 0 ? 2 : argc >= 4 ? 4 : argc;
112
113     // Protocol version
114     char* p = stpncpy(buf, "1", sizeof(buf)) + 1;
115
116     int i;
117     for (i = 0; i < argc && p < limit; i++) {
118         if (i >= cmd_args) p[-1] = ' ';
119         p = stpncpy(p, argv[i], limit - p) + 1;
120     }
121     for (i = cmd_args; i < 4 && p < limit; i++) {
122         *p++ = 0;
123     }
124
125     const char* q = p < limit ? p : limit;
126     for (p = buf; p < q; ) {
127         ssize_t bytes = write(fd, p, q - p);
128         if (bytes <= 0) {
129             return -1;
130         }
131         p += (size_t)bytes;
132     }
133     return 0;
134 }
135
136 // Mirror response from remote JVM to stdout
137 static int read_response(int fd, int argc, char** argv, int print_output) {
138     char buf[8192];
139     ssize_t bytes = read(fd, buf, sizeof(buf) - 1);
140     if (bytes == 0) {
141         fprintf(stderr, "Unexpected EOF reading response\n");
142         return 1;
143     } else if (bytes < 0) {
144         perror("Error reading response");
145         return 1;
146     }
147
148     // First line of response is the command result code
149     buf[bytes] = 0;
150     int result = atoi(buf);
151
152     // Special treatment of 'load' command
153     if (result == 0 && argc > 0 && strcmp(argv[0], "load") == 0) {
154         size_t total = bytes;
155         while (total < sizeof(buf) - 1 && (bytes = read(fd, buf + total, sizeof(buf) - 1 - total)) > 0) {
156             total += (size_t)bytes;
157         }
158         bytes = total;
159
160         // The second line is the result of 'load' command; since JDK 9 it starts from "return code: "
161         buf[bytes] = 0;
162         result = atoi(strncmp(buf + 2, "return code: ", 13) == 0 ? buf + 15 : buf + 2);
163     }
164
165     if (print_output) {
166         // Mirror JVM response to stdout
167         printf("JVM response code = ");
168         do {
169             fwrite(buf, 1, bytes, stdout);
170             bytes = read(fd, buf, sizeof(buf));
171         } while (bytes > 0);
172         printf("\n");
173     }
174
175     return result;
176 }
177
178 int jattach_hotspot(int pid, int nspid, int argc, char** argv, int print_output) {
179     if (check_socket(nspid) != 0 && start_attach_mechanism(pid, nspid) != 0) {
180         perror("Could not start attach mechanism");
181         return 1;
182     }
183
184     int fd = connect_socket(nspid);
185     if (fd == -1) {
186         perror("Could not connect to socket");
187         return 1;
188     }
189
190     if (print_output) {
191         printf("Connected to remote JVM\n");
192     }
193
194     if (write_command(fd, argc, argv) != 0) {
195         perror("Error writing to socket");
196         close(fd);
197         return 1;
198     }
199
200     int result = read_response(fd, argc, argv, print_output);
201     close(fd);
202
203     return result;
204 }