1 /*
2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
50 // As the socket is a UNIX domain socket it means that only clients on the
51 // local machine can connect. In addition there are two other aspects to
52 // the security:
53 // 1. The well known file that the socket is bound to has permission 400
54 // 2. When a client connect, the SO_PEERCRED socket option is used to
55 // obtain the credentials of client. We check that the effective uid
56 // of the client matches this process.
57
58 // forward reference
59 class LinuxAttachOperation;
60
61 class LinuxAttachListener: AllStatic {
62 private:
63 // the path to which we bind the UNIX domain socket
64 static char _path[UNIX_PATH_MAX];
65 static bool _has_path;
66
67 // the file descriptor for the listening socket
68 static int _listener;
69
70 static void set_path(char* path) {
71 if (path == NULL) {
72 _has_path = false;
73 } else {
74 strncpy(_path, path, UNIX_PATH_MAX);
75 _path[UNIX_PATH_MAX-1] = '\0';
76 _has_path = true;
77 }
78 }
79
80 static void set_listener(int s) { _listener = s; }
81
82 // reads a request from the given connected socket
83 static LinuxAttachOperation* read_request(int s);
84
85 public:
86 enum {
87 ATTACH_PROTOCOL_VER = 1 // protocol version
88 };
89 enum {
90 ATTACH_ERROR_BADVERSION = 101 // error codes
91 };
92
93 // initialize the listener, returns 0 if okay
94 static int init();
95
96 static char* path() { return _path; }
97 static bool has_path() { return _has_path; }
98 static int listener() { return _listener; }
99
100 // write the given buffer to a socket
101 static int write_fully(int s, char* buf, int len);
102
103 static LinuxAttachOperation* dequeue();
104 };
105
106 class LinuxAttachOperation: public AttachOperation {
107 private:
108 // the connection to the client
109 int _socket;
110
111 public:
112 void complete(jint res, bufferedStream* st);
113
114 void set_socket(int s) { _socket = s; }
115 int socket() const { return _socket; }
116
117 LinuxAttachOperation(char* name) : AttachOperation(name) {
118 set_socket(-1);
119 }
120 };
121
122 // statics
123 char LinuxAttachListener::_path[UNIX_PATH_MAX];
124 bool LinuxAttachListener::_has_path;
125 int LinuxAttachListener::_listener = -1;
126
127 // Supporting class to help split a buffer into individual components
128 class ArgumentIterator : public StackObj {
129 private:
130 char* _pos;
131 char* _end;
132 public:
133 ArgumentIterator(char* arg_buffer, size_t arg_size) {
134 _pos = arg_buffer;
135 _end = _pos + arg_size - 1;
136 }
137 char* next() {
138 if (*_pos == '\0') {
139 return NULL;
140 }
141 char* res = _pos;
142 char* next_pos = strchr(_pos, '\0');
143 if (next_pos < _end) {
144 next_pos++;
145 }
146 _pos = next_pos;
147 return res;
148 }
149 };
150
151
152 // atexit hook to stop listener and unlink the file that it is
153 // bound too.
154 extern "C" {
155 static void listener_cleanup() {
156 static int cleanup_done;
157 if (!cleanup_done) {
158 cleanup_done = 1;
159 int s = LinuxAttachListener::listener();
160 if (s != -1) {
161 ::close(s);
162 }
163 if (LinuxAttachListener::has_path()) {
164 ::unlink(LinuxAttachListener::path());
165 }
166 }
167 }
168 }
169
170 // Initialization - create a listener socket and bind it to a file
171
172 int LinuxAttachListener::init() {
173 char path[UNIX_PATH_MAX]; // socket file
174 char initial_path[UNIX_PATH_MAX]; // socket file during setup
175 int listener; // listener socket (file descriptor)
176
177 // register function to cleanup
178 ::atexit(listener_cleanup);
179
180 int n = snprintf(path, UNIX_PATH_MAX, "%s/.java_pid%d",
181 os::get_temp_directory(), os::current_process_id());
182 if (n < (int)UNIX_PATH_MAX) {
183 n = snprintf(initial_path, UNIX_PATH_MAX, "%s.tmp", path);
184 }
185 if (n >= (int)UNIX_PATH_MAX) {
186 return -1;
187 }
188
189 // create the listener socket
190 listener = ::socket(PF_UNIX, SOCK_STREAM, 0);
191 if (listener == -1) {
192 return -1;
193 }
194
195 // bind socket
196 struct sockaddr_un addr;
197 addr.sun_family = AF_UNIX;
198 strcpy(addr.sun_path, initial_path);
453 if (ret == -1) {
454 debug_only(warning("failed to remove stale attach pid file at %s", fn));
455 }
456 }
457 }
458
459 int AttachListener::pd_init() {
460 JavaThread* thread = JavaThread::current();
461 ThreadBlockInVM tbivm(thread);
462
463 thread->set_suspend_equivalent();
464 // cleared by handle_special_suspend_equivalent_condition() or
465 // java_suspend_self() via check_and_wait_while_suspended()
466
467 int ret_code = LinuxAttachListener::init();
468
469 // were we externally suspended while we were waiting?
470 thread->check_and_wait_while_suspended();
471
472 return ret_code;
473 }
474
475 // Attach Listener is started lazily except in the case when
476 // +ReduseSignalUsage is used
477 bool AttachListener::init_at_startup() {
478 if (ReduceSignalUsage) {
479 return true;
480 } else {
481 return false;
482 }
483 }
484
485 // If the file .attach_pid<pid> exists in the working directory
486 // or /tmp then this is the trigger to start the attach mechanism
487 bool AttachListener::is_init_trigger() {
488 if (init_at_startup() || is_initialized()) {
489 return false; // initialized at startup or already initialized
490 }
491 char fn[PATH_MAX+1];
492 sprintf(fn, ".attach_pid%d", os::current_process_id());
|
1 /*
2 * Copyright (c) 2005, 2019, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
50 // As the socket is a UNIX domain socket it means that only clients on the
51 // local machine can connect. In addition there are two other aspects to
52 // the security:
53 // 1. The well known file that the socket is bound to has permission 400
54 // 2. When a client connect, the SO_PEERCRED socket option is used to
55 // obtain the credentials of client. We check that the effective uid
56 // of the client matches this process.
57
58 // forward reference
59 class LinuxAttachOperation;
60
61 class LinuxAttachListener: AllStatic {
62 private:
63 // the path to which we bind the UNIX domain socket
64 static char _path[UNIX_PATH_MAX];
65 static bool _has_path;
66
67 // the file descriptor for the listening socket
68 static int _listener;
69
70 static bool _atexit_registered;
71
72 // reads a request from the given connected socket
73 static LinuxAttachOperation* read_request(int s);
74
75 public:
76 enum {
77 ATTACH_PROTOCOL_VER = 1 // protocol version
78 };
79 enum {
80 ATTACH_ERROR_BADVERSION = 101 // error codes
81 };
82
83 static void set_path(char* path) {
84 if (path == NULL) {
85 _path[0] = '\0';
86 _has_path = false;
87 } else {
88 strncpy(_path, path, UNIX_PATH_MAX);
89 _path[UNIX_PATH_MAX-1] = '\0';
90 _has_path = true;
91 }
92 }
93
94 static void set_listener(int s) { _listener = s; }
95
96 // initialize the listener, returns 0 if okay
97 static int init();
98
99 static char* path() { return _path; }
100 static bool has_path() { return _has_path; }
101 static int listener() { return _listener; }
102
103 // write the given buffer to a socket
104 static int write_fully(int s, char* buf, int len);
105
106 static LinuxAttachOperation* dequeue();
107 };
108
109 class LinuxAttachOperation: public AttachOperation {
110 private:
111 // the connection to the client
112 int _socket;
113
114 public:
115 void complete(jint res, bufferedStream* st);
116
117 void set_socket(int s) { _socket = s; }
118 int socket() const { return _socket; }
119
120 LinuxAttachOperation(char* name) : AttachOperation(name) {
121 set_socket(-1);
122 }
123 };
124
125 // statics
126 char LinuxAttachListener::_path[UNIX_PATH_MAX];
127 bool LinuxAttachListener::_has_path;
128 int LinuxAttachListener::_listener = -1;
129 bool LinuxAttachListener::_atexit_registered = false;
130
131 // Supporting class to help split a buffer into individual components
132 class ArgumentIterator : public StackObj {
133 private:
134 char* _pos;
135 char* _end;
136 public:
137 ArgumentIterator(char* arg_buffer, size_t arg_size) {
138 _pos = arg_buffer;
139 _end = _pos + arg_size - 1;
140 }
141 char* next() {
142 if (*_pos == '\0') {
143 return NULL;
144 }
145 char* res = _pos;
146 char* next_pos = strchr(_pos, '\0');
147 if (next_pos < _end) {
148 next_pos++;
149 }
150 _pos = next_pos;
151 return res;
152 }
153 };
154
155
156 // atexit hook to stop listener and unlink the file that it is
157 // bound too.
158 extern "C" {
159 static void listener_cleanup() {
160 int s = LinuxAttachListener::listener();
161 if (s != -1) {
162 LinuxAttachListener::set_listener(-1);
163 ::shutdown(s, SHUT_RDWR);
164 ::close(s);
165 }
166 if (LinuxAttachListener::has_path()) {
167 ::unlink(LinuxAttachListener::path());
168 LinuxAttachListener::set_path(NULL);
169 }
170 }
171 }
172
173 // Initialization - create a listener socket and bind it to a file
174
175 int LinuxAttachListener::init() {
176 char path[UNIX_PATH_MAX]; // socket file
177 char initial_path[UNIX_PATH_MAX]; // socket file during setup
178 int listener; // listener socket (file descriptor)
179
180 // register function to cleanup
181 if (!_atexit_registered) {
182 _atexit_registered = true;
183 ::atexit(listener_cleanup);
184 }
185
186 int n = snprintf(path, UNIX_PATH_MAX, "%s/.java_pid%d",
187 os::get_temp_directory(), os::current_process_id());
188 if (n < (int)UNIX_PATH_MAX) {
189 n = snprintf(initial_path, UNIX_PATH_MAX, "%s.tmp", path);
190 }
191 if (n >= (int)UNIX_PATH_MAX) {
192 return -1;
193 }
194
195 // create the listener socket
196 listener = ::socket(PF_UNIX, SOCK_STREAM, 0);
197 if (listener == -1) {
198 return -1;
199 }
200
201 // bind socket
202 struct sockaddr_un addr;
203 addr.sun_family = AF_UNIX;
204 strcpy(addr.sun_path, initial_path);
459 if (ret == -1) {
460 debug_only(warning("failed to remove stale attach pid file at %s", fn));
461 }
462 }
463 }
464
465 int AttachListener::pd_init() {
466 JavaThread* thread = JavaThread::current();
467 ThreadBlockInVM tbivm(thread);
468
469 thread->set_suspend_equivalent();
470 // cleared by handle_special_suspend_equivalent_condition() or
471 // java_suspend_self() via check_and_wait_while_suspended()
472
473 int ret_code = LinuxAttachListener::init();
474
475 // were we externally suspended while we were waiting?
476 thread->check_and_wait_while_suspended();
477
478 return ret_code;
479 }
480
481 bool AttachListener::check_socket_file() {
482 int ret;
483 struct stat64 st;
484 ret = stat64(LinuxAttachListener::path(), &st);
485 if (ret == -1) { // need to restart attach listener.
486 debug_only(warning("Socket file %s does not exist - Restart Attach Listener",
487 LinuxAttachListener::path()));
488
489 listener_cleanup();
490
491 // wait to terminate current attach listener instance...
492 while (AttachListener::transit_state(AL_INITIALIZING,
493 AL_NOT_INITIALIZED) != AL_NOT_INITIALIZED) {
494 os::yield();
495 }
496 return is_init_trigger();
497 }
498 return false;
499 }
500
501 // Attach Listener is started lazily except in the case when
502 // +ReduseSignalUsage is used
503 bool AttachListener::init_at_startup() {
504 if (ReduceSignalUsage) {
505 return true;
506 } else {
507 return false;
508 }
509 }
510
511 // If the file .attach_pid<pid> exists in the working directory
512 // or /tmp then this is the trigger to start the attach mechanism
513 bool AttachListener::is_init_trigger() {
514 if (init_at_startup() || is_initialized()) {
515 return false; // initialized at startup or already initialized
516 }
517 char fn[PATH_MAX+1];
518 sprintf(fn, ".attach_pid%d", os::current_process_id());
|