|
1 /* |
|
2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 # include "incls/_precompiled.incl" |
|
26 # include "incls/_attachListener_linux.cpp.incl" |
|
27 |
|
28 #include <unistd.h> |
|
29 #include <signal.h> |
|
30 #include <sys/types.h> |
|
31 #include <sys/socket.h> |
|
32 #include <sys/un.h> |
|
33 #include <sys/stat.h> |
|
34 |
|
35 // The attach mechanism on Linux uses a UNIX domain socket. An attach listener |
|
36 // thread is created at startup or is created on-demand via a signal from |
|
37 // the client tool. The attach listener creates a socket and binds it to a file |
|
38 // in the filesystem. The attach listener then acts as a simple (single- |
|
39 // threaded) server - tt waits for a client to connect, reads the request, |
|
40 // executes it, and returns the response to the client via the socket |
|
41 // connection. |
|
42 // |
|
43 // As the socket is a UNIX domain socket it means that only clients on the |
|
44 // local machine can connect. In addition there are two other aspects to |
|
45 // the security: |
|
46 // 1. The well known file that the socket is bound to has permission 400 |
|
47 // 2. When a client connect, the SO_PEERCRED socket option is used to |
|
48 // obtain the credentials of client. We check that the effective uid |
|
49 // of the client matches this process. |
|
50 |
|
51 // forward reference |
|
52 class LinuxAttachOperation; |
|
53 |
|
54 class LinuxAttachListener: AllStatic { |
|
55 private: |
|
56 // the path to which we bind the UNIX domain socket |
|
57 static char _path[PATH_MAX+1]; |
|
58 static bool _has_path; |
|
59 |
|
60 // the file descriptor for the listening socket |
|
61 static int _listener; |
|
62 |
|
63 static void set_path(char* path) { |
|
64 if (path == NULL) { |
|
65 _has_path = false; |
|
66 } else { |
|
67 strncpy(_path, path, PATH_MAX); |
|
68 _path[PATH_MAX] = '\0'; |
|
69 _has_path = true; |
|
70 } |
|
71 } |
|
72 |
|
73 static void set_listener(int s) { _listener = s; } |
|
74 |
|
75 // reads a request from the given connected socket |
|
76 static LinuxAttachOperation* read_request(int s); |
|
77 |
|
78 public: |
|
79 enum { |
|
80 ATTACH_PROTOCOL_VER = 1 // protocol version |
|
81 }; |
|
82 enum { |
|
83 ATTACH_ERROR_BADVERSION = 101 // error codes |
|
84 }; |
|
85 |
|
86 // initialize the listener, returns 0 if okay |
|
87 static int init(); |
|
88 |
|
89 static char* path() { return _path; } |
|
90 static bool has_path() { return _has_path; } |
|
91 static int listener() { return _listener; } |
|
92 |
|
93 // write the given buffer to a socket |
|
94 static int write_fully(int s, char* buf, int len); |
|
95 |
|
96 static LinuxAttachOperation* dequeue(); |
|
97 }; |
|
98 |
|
99 class LinuxAttachOperation: public AttachOperation { |
|
100 private: |
|
101 // the connection to the client |
|
102 int _socket; |
|
103 |
|
104 public: |
|
105 void complete(jint res, bufferedStream* st); |
|
106 |
|
107 void set_socket(int s) { _socket = s; } |
|
108 int socket() const { return _socket; } |
|
109 |
|
110 LinuxAttachOperation(char* name) : AttachOperation(name) { |
|
111 set_socket(-1); |
|
112 } |
|
113 }; |
|
114 |
|
115 // statics |
|
116 char LinuxAttachListener::_path[PATH_MAX+1]; |
|
117 bool LinuxAttachListener::_has_path; |
|
118 int LinuxAttachListener::_listener = -1; |
|
119 |
|
120 // Supporting class to help split a buffer into individual components |
|
121 class ArgumentIterator : public StackObj { |
|
122 private: |
|
123 char* _pos; |
|
124 char* _end; |
|
125 public: |
|
126 ArgumentIterator(char* arg_buffer, size_t arg_size) { |
|
127 _pos = arg_buffer; |
|
128 _end = _pos + arg_size - 1; |
|
129 } |
|
130 char* next() { |
|
131 if (*_pos == '\0') { |
|
132 return NULL; |
|
133 } |
|
134 char* res = _pos; |
|
135 char* next_pos = strchr(_pos, '\0'); |
|
136 if (next_pos < _end) { |
|
137 next_pos++; |
|
138 } |
|
139 _pos = next_pos; |
|
140 return res; |
|
141 } |
|
142 }; |
|
143 |
|
144 |
|
145 // atexit hook to stop listener and unlink the file that it is |
|
146 // bound too. |
|
147 extern "C" { |
|
148 static void listener_cleanup() { |
|
149 static int cleanup_done; |
|
150 if (!cleanup_done) { |
|
151 cleanup_done = 1; |
|
152 int s = LinuxAttachListener::listener(); |
|
153 if (s != -1) { |
|
154 ::close(s); |
|
155 } |
|
156 if (LinuxAttachListener::has_path()) { |
|
157 ::unlink(LinuxAttachListener::path()); |
|
158 } |
|
159 } |
|
160 } |
|
161 } |
|
162 |
|
163 // Initialization - create a listener socket and bind it to a file |
|
164 |
|
165 int LinuxAttachListener::init() { |
|
166 char path[PATH_MAX+1]; // socket file |
|
167 int listener; // listener socket (file descriptor) |
|
168 |
|
169 // register function to cleanup |
|
170 ::atexit(listener_cleanup); |
|
171 |
|
172 // create the listener socket |
|
173 listener = ::socket(PF_UNIX, SOCK_STREAM, 0); |
|
174 if (listener == -1) { |
|
175 return -1; |
|
176 } |
|
177 |
|
178 int res = -1; |
|
179 struct sockaddr_un addr; |
|
180 addr.sun_family = AF_UNIX; |
|
181 |
|
182 // FIXME: Prior to b39 the tool-side API expected to find the well |
|
183 // known file in the working directory. To allow this libjvm.so work with |
|
184 // a pre-b39 SDK we create it in the working directory if |
|
185 // +StartAttachListener is used is used. All unit tests for this feature |
|
186 // currently used this flag. Once b39 SDK has been promoted we can remove |
|
187 // this code. |
|
188 if (StartAttachListener) { |
|
189 sprintf(path, ".java_pid%d", os::current_process_id()); |
|
190 strcpy(addr.sun_path, path); |
|
191 ::unlink(path); |
|
192 res = ::bind(listener, (struct sockaddr*)&addr, sizeof(addr)); |
|
193 } |
|
194 if (res == -1) { |
|
195 sprintf(path, "%s/.java_pid%d", os::get_temp_directory(), os::current_process_id()); |
|
196 strcpy(addr.sun_path, path); |
|
197 ::unlink(path); |
|
198 res = ::bind(listener, (struct sockaddr*)&addr, sizeof(addr)); |
|
199 } |
|
200 if (res == -1) { |
|
201 RESTARTABLE(::close(listener), res); |
|
202 return -1; |
|
203 } |
|
204 set_path(path); |
|
205 |
|
206 // put in listen mode and set permission |
|
207 if ((::listen(listener, 5) == -1) || (::chmod(path, S_IREAD|S_IWRITE) == -1)) { |
|
208 RESTARTABLE(::close(listener), res); |
|
209 ::unlink(path); |
|
210 set_path(NULL); |
|
211 return -1; |
|
212 } |
|
213 set_listener(listener); |
|
214 |
|
215 return 0; |
|
216 } |
|
217 |
|
218 // Given a socket that is connected to a peer we read the request and |
|
219 // create an AttachOperation. As the socket is blocking there is potential |
|
220 // for a denial-of-service if the peer does not response. However this happens |
|
221 // after the peer credentials have been checked and in the worst case it just |
|
222 // means that the attach listener thread is blocked. |
|
223 // |
|
224 LinuxAttachOperation* LinuxAttachListener::read_request(int s) { |
|
225 char ver_str[8]; |
|
226 sprintf(ver_str, "%d", ATTACH_PROTOCOL_VER); |
|
227 |
|
228 // The request is a sequence of strings so we first figure out the |
|
229 // expected count and the maximum possible length of the request. |
|
230 // The request is: |
|
231 // <ver>0<cmd>0<arg>0<arg>0<arg>0 |
|
232 // where <ver> is the protocol version (1), <cmd> is the command |
|
233 // name ("load", "datadump", ...), and <arg> is an argument |
|
234 int expected_str_count = 2 + AttachOperation::arg_count_max; |
|
235 int max_len = (strlen(ver_str) + 1) + (AttachOperation::name_length_max + 1) + |
|
236 AttachOperation::arg_count_max*(AttachOperation::arg_length_max + 1); |
|
237 |
|
238 char buf[max_len]; |
|
239 int str_count = 0; |
|
240 |
|
241 // Read until all (expected) strings have been read, the buffer is |
|
242 // full, or EOF. |
|
243 |
|
244 int off = 0; |
|
245 int left = max_len; |
|
246 |
|
247 do { |
|
248 int n; |
|
249 RESTARTABLE(read(s, buf+off, left), n); |
|
250 if (n == -1) { |
|
251 return NULL; // reset by peer or other error |
|
252 } |
|
253 if (n == 0) { |
|
254 break; |
|
255 } |
|
256 for (int i=0; i<n; i++) { |
|
257 if (buf[off+i] == 0) { |
|
258 // EOS found |
|
259 str_count++; |
|
260 |
|
261 // The first string is <ver> so check it now to |
|
262 // check for protocol mis-match |
|
263 if (str_count == 1) { |
|
264 if ((strlen(buf) != strlen(ver_str)) || |
|
265 (atoi(buf) != ATTACH_PROTOCOL_VER)) { |
|
266 char msg[32]; |
|
267 sprintf(msg, "%d\n", ATTACH_ERROR_BADVERSION); |
|
268 write_fully(s, msg, strlen(msg)); |
|
269 return NULL; |
|
270 } |
|
271 } |
|
272 } |
|
273 } |
|
274 off += n; |
|
275 left -= n; |
|
276 } while (left > 0 && str_count < expected_str_count); |
|
277 |
|
278 if (str_count != expected_str_count) { |
|
279 return NULL; // incomplete request |
|
280 } |
|
281 |
|
282 // parse request |
|
283 |
|
284 ArgumentIterator args(buf, (max_len)-left); |
|
285 |
|
286 // version already checked |
|
287 char* v = args.next(); |
|
288 |
|
289 char* name = args.next(); |
|
290 if (name == NULL || strlen(name) > AttachOperation::name_length_max) { |
|
291 return NULL; |
|
292 } |
|
293 |
|
294 LinuxAttachOperation* op = new LinuxAttachOperation(name); |
|
295 |
|
296 for (int i=0; i<AttachOperation::arg_count_max; i++) { |
|
297 char* arg = args.next(); |
|
298 if (arg == NULL) { |
|
299 op->set_arg(i, NULL); |
|
300 } else { |
|
301 if (strlen(arg) > AttachOperation::arg_length_max) { |
|
302 delete op; |
|
303 return NULL; |
|
304 } |
|
305 op->set_arg(i, arg); |
|
306 } |
|
307 } |
|
308 |
|
309 op->set_socket(s); |
|
310 return op; |
|
311 } |
|
312 |
|
313 |
|
314 // Dequeue an operation |
|
315 // |
|
316 // In the Linux implementation there is only a single operation and clients |
|
317 // cannot queue commands (except at the socket level). |
|
318 // |
|
319 LinuxAttachOperation* LinuxAttachListener::dequeue() { |
|
320 for (;;) { |
|
321 int s; |
|
322 |
|
323 // wait for client to connect |
|
324 struct sockaddr addr; |
|
325 socklen_t len = sizeof(addr); |
|
326 RESTARTABLE(::accept(listener(), &addr, &len), s); |
|
327 if (s == -1) { |
|
328 return NULL; // log a warning? |
|
329 } |
|
330 |
|
331 // get the credentials of the peer and check the effective uid/guid |
|
332 // - check with jeff on this. |
|
333 struct ucred cred_info; |
|
334 socklen_t optlen = sizeof(cred_info); |
|
335 if (::getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void*)&cred_info, &optlen) == -1) { |
|
336 int res; |
|
337 RESTARTABLE(::close(s), res); |
|
338 continue; |
|
339 } |
|
340 uid_t euid = geteuid(); |
|
341 gid_t egid = getegid(); |
|
342 |
|
343 if (cred_info.uid != euid || cred_info.gid != egid) { |
|
344 int res; |
|
345 RESTARTABLE(::close(s), res); |
|
346 continue; |
|
347 } |
|
348 |
|
349 // peer credential look okay so we read the request |
|
350 LinuxAttachOperation* op = read_request(s); |
|
351 if (op == NULL) { |
|
352 int res; |
|
353 RESTARTABLE(::close(s), res); |
|
354 continue; |
|
355 } else { |
|
356 return op; |
|
357 } |
|
358 } |
|
359 } |
|
360 |
|
361 // write the given buffer to the socket |
|
362 int LinuxAttachListener::write_fully(int s, char* buf, int len) { |
|
363 do { |
|
364 int n = ::write(s, buf, len); |
|
365 if (n == -1) { |
|
366 if (errno != EINTR) return -1; |
|
367 } else { |
|
368 buf += n; |
|
369 len -= n; |
|
370 } |
|
371 } |
|
372 while (len > 0); |
|
373 return 0; |
|
374 } |
|
375 |
|
376 // Complete an operation by sending the operation result and any result |
|
377 // output to the client. At this time the socket is in blocking mode so |
|
378 // potentially we can block if there is a lot of data and the client is |
|
379 // non-responsive. For most operations this is a non-issue because the |
|
380 // default send buffer is sufficient to buffer everything. In the future |
|
381 // if there are operations that involves a very big reply then it the |
|
382 // socket could be made non-blocking and a timeout could be used. |
|
383 |
|
384 void LinuxAttachOperation::complete(jint result, bufferedStream* st) { |
|
385 JavaThread* thread = JavaThread::current(); |
|
386 ThreadBlockInVM tbivm(thread); |
|
387 |
|
388 thread->set_suspend_equivalent(); |
|
389 // cleared by handle_special_suspend_equivalent_condition() or |
|
390 // java_suspend_self() via check_and_wait_while_suspended() |
|
391 |
|
392 // write operation result |
|
393 char msg[32]; |
|
394 sprintf(msg, "%d\n", result); |
|
395 int rc = LinuxAttachListener::write_fully(this->socket(), msg, strlen(msg)); |
|
396 |
|
397 // write any result data |
|
398 if (rc == 0) { |
|
399 LinuxAttachListener::write_fully(this->socket(), (char*) st->base(), st->size()); |
|
400 ::shutdown(this->socket(), 2); |
|
401 } |
|
402 |
|
403 // done |
|
404 RESTARTABLE(::close(this->socket()), rc); |
|
405 |
|
406 // were we externally suspended while we were waiting? |
|
407 thread->check_and_wait_while_suspended(); |
|
408 |
|
409 delete this; |
|
410 } |
|
411 |
|
412 |
|
413 // AttachListener functions |
|
414 |
|
415 AttachOperation* AttachListener::dequeue() { |
|
416 JavaThread* thread = JavaThread::current(); |
|
417 ThreadBlockInVM tbivm(thread); |
|
418 |
|
419 thread->set_suspend_equivalent(); |
|
420 // cleared by handle_special_suspend_equivalent_condition() or |
|
421 // java_suspend_self() via check_and_wait_while_suspended() |
|
422 |
|
423 AttachOperation* op = LinuxAttachListener::dequeue(); |
|
424 |
|
425 // were we externally suspended while we were waiting? |
|
426 thread->check_and_wait_while_suspended(); |
|
427 |
|
428 return op; |
|
429 } |
|
430 |
|
431 int AttachListener::pd_init() { |
|
432 JavaThread* thread = JavaThread::current(); |
|
433 ThreadBlockInVM tbivm(thread); |
|
434 |
|
435 thread->set_suspend_equivalent(); |
|
436 // cleared by handle_special_suspend_equivalent_condition() or |
|
437 // java_suspend_self() via check_and_wait_while_suspended() |
|
438 |
|
439 int ret_code = LinuxAttachListener::init(); |
|
440 |
|
441 // were we externally suspended while we were waiting? |
|
442 thread->check_and_wait_while_suspended(); |
|
443 |
|
444 return ret_code; |
|
445 } |
|
446 |
|
447 // Attach Listener is started lazily except in the case when |
|
448 // +ReduseSignalUsage is used |
|
449 bool AttachListener::init_at_startup() { |
|
450 if (ReduceSignalUsage) { |
|
451 return true; |
|
452 } else { |
|
453 return false; |
|
454 } |
|
455 } |
|
456 |
|
457 // If the file .attach_pid<pid> exists in the working directory |
|
458 // or /tmp then this is the trigger to start the attach mechanism |
|
459 bool AttachListener::is_init_trigger() { |
|
460 if (init_at_startup() || is_initialized()) { |
|
461 return false; // initialized at startup or already initialized |
|
462 } |
|
463 char fn[32]; |
|
464 sprintf(fn, ".attach_pid%d", os::current_process_id()); |
|
465 int ret; |
|
466 struct stat64 st; |
|
467 RESTARTABLE(::stat64(fn, &st), ret); |
|
468 if (ret == -1) { |
|
469 sprintf(fn, "/tmp/.attach_pid%d", os::current_process_id()); |
|
470 RESTARTABLE(::stat64(fn, &st), ret); |
|
471 } |
|
472 if (ret == 0) { |
|
473 // simple check to avoid starting the attach mechanism when |
|
474 // a bogus user creates the file |
|
475 if (st.st_uid == geteuid()) { |
|
476 init(); |
|
477 return true; |
|
478 } |
|
479 } |
|
480 return false; |
|
481 } |
|
482 |
|
483 // if VM aborts then remove listener |
|
484 void AttachListener::abort() { |
|
485 listener_cleanup(); |
|
486 } |
|
487 |
|
488 void AttachListener::pd_data_dump() { |
|
489 os::signal_notify(SIGQUIT); |
|
490 } |
|
491 |
|
492 AttachOperationFunctionInfo* AttachListener::pd_find_operation(const char* n) { |
|
493 return NULL; |
|
494 } |
|
495 |
|
496 jint AttachListener::pd_set_flag(AttachOperation* op, outputStream* out) { |
|
497 out->print_cr("flag '%s' cannot be changed", op->arg(0)); |
|
498 return JNI_ERR; |
|
499 } |
|
500 |
|
501 void AttachListener::pd_detachall() { |
|
502 // do nothing for now |
|
503 } |