Fri, 06 Sep 2013 20:16:09 +0200
8023038: PPC64 (part 15): Platform files for AIX/PPC64 support
Reviewed-by: kvn
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * Copyright 2012, 2013 SAP AG. All rights reserved. |
aoqi@0 | 4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 5 | * |
aoqi@0 | 6 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 7 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 8 | * published by the Free Software Foundation. |
aoqi@0 | 9 | * |
aoqi@0 | 10 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 13 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 14 | * accompanied this code). |
aoqi@0 | 15 | * |
aoqi@0 | 16 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 17 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 18 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 19 | * |
aoqi@0 | 20 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 21 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 22 | * questions. |
aoqi@0 | 23 | * |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | #include "precompiled.hpp" |
aoqi@0 | 27 | #include "runtime/interfaceSupport.hpp" |
aoqi@0 | 28 | #include "runtime/os.hpp" |
aoqi@0 | 29 | #include "services/attachListener.hpp" |
aoqi@0 | 30 | #include "services/dtraceAttacher.hpp" |
aoqi@0 | 31 | |
aoqi@0 | 32 | #include <unistd.h> |
aoqi@0 | 33 | #include <signal.h> |
aoqi@0 | 34 | #include <sys/types.h> |
aoqi@0 | 35 | #include <sys/socket.h> |
aoqi@0 | 36 | #include <sys/un.h> |
aoqi@0 | 37 | #include <sys/stat.h> |
aoqi@0 | 38 | |
aoqi@0 | 39 | #ifndef UNIX_PATH_MAX |
aoqi@0 | 40 | #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)0)->sun_path) |
aoqi@0 | 41 | #endif |
aoqi@0 | 42 | |
aoqi@0 | 43 | // The attach mechanism on Linux uses a UNIX domain socket. An attach listener |
aoqi@0 | 44 | // thread is created at startup or is created on-demand via a signal from |
aoqi@0 | 45 | // the client tool. The attach listener creates a socket and binds it to a file |
aoqi@0 | 46 | // in the filesystem. The attach listener then acts as a simple (single- |
aoqi@0 | 47 | // threaded) server - it waits for a client to connect, reads the request, |
aoqi@0 | 48 | // executes it, and returns the response to the client via the socket |
aoqi@0 | 49 | // connection. |
aoqi@0 | 50 | // |
aoqi@0 | 51 | // As the socket is a UNIX domain socket it means that only clients on the |
aoqi@0 | 52 | // local machine can connect. In addition there are two other aspects to |
aoqi@0 | 53 | // the security: |
aoqi@0 | 54 | // 1. The well known file that the socket is bound to has permission 400 |
aoqi@0 | 55 | // 2. When a client connect, the SO_PEERID socket option is used to |
aoqi@0 | 56 | // obtain the credentials of client. We check that the effective uid |
aoqi@0 | 57 | // of the client matches this process. |
aoqi@0 | 58 | |
aoqi@0 | 59 | // forward reference |
aoqi@0 | 60 | class AixAttachOperation; |
aoqi@0 | 61 | |
aoqi@0 | 62 | class AixAttachListener: AllStatic { |
aoqi@0 | 63 | private: |
aoqi@0 | 64 | // the path to which we bind the UNIX domain socket |
aoqi@0 | 65 | static char _path[UNIX_PATH_MAX]; |
aoqi@0 | 66 | static bool _has_path; |
aoqi@0 | 67 | // Shutdown marker to prevent accept blocking during clean-up. |
aoqi@0 | 68 | static bool _shutdown; |
aoqi@0 | 69 | |
aoqi@0 | 70 | // the file descriptor for the listening socket |
aoqi@0 | 71 | static int _listener; |
aoqi@0 | 72 | |
aoqi@0 | 73 | static void set_path(char* path) { |
aoqi@0 | 74 | if (path == NULL) { |
aoqi@0 | 75 | _has_path = false; |
aoqi@0 | 76 | } else { |
aoqi@0 | 77 | strncpy(_path, path, UNIX_PATH_MAX); |
aoqi@0 | 78 | _path[UNIX_PATH_MAX-1] = '\0'; |
aoqi@0 | 79 | _has_path = true; |
aoqi@0 | 80 | } |
aoqi@0 | 81 | } |
aoqi@0 | 82 | |
aoqi@0 | 83 | static void set_listener(int s) { _listener = s; } |
aoqi@0 | 84 | |
aoqi@0 | 85 | // reads a request from the given connected socket |
aoqi@0 | 86 | static AixAttachOperation* read_request(int s); |
aoqi@0 | 87 | |
aoqi@0 | 88 | public: |
aoqi@0 | 89 | enum { |
aoqi@0 | 90 | ATTACH_PROTOCOL_VER = 1 // protocol version |
aoqi@0 | 91 | }; |
aoqi@0 | 92 | enum { |
aoqi@0 | 93 | ATTACH_ERROR_BADVERSION = 101 // error codes |
aoqi@0 | 94 | }; |
aoqi@0 | 95 | |
aoqi@0 | 96 | // initialize the listener, returns 0 if okay |
aoqi@0 | 97 | static int init(); |
aoqi@0 | 98 | |
aoqi@0 | 99 | static char* path() { return _path; } |
aoqi@0 | 100 | static bool has_path() { return _has_path; } |
aoqi@0 | 101 | static int listener() { return _listener; } |
aoqi@0 | 102 | // Shutdown marker to prevent accept blocking during clean-up |
aoqi@0 | 103 | static void set_shutdown(bool shutdown) { _shutdown = shutdown; } |
aoqi@0 | 104 | static bool is_shutdown() { return _shutdown; } |
aoqi@0 | 105 | |
aoqi@0 | 106 | // write the given buffer to a socket |
aoqi@0 | 107 | static int write_fully(int s, char* buf, int len); |
aoqi@0 | 108 | |
aoqi@0 | 109 | static AixAttachOperation* dequeue(); |
aoqi@0 | 110 | }; |
aoqi@0 | 111 | |
aoqi@0 | 112 | class AixAttachOperation: public AttachOperation { |
aoqi@0 | 113 | private: |
aoqi@0 | 114 | // the connection to the client |
aoqi@0 | 115 | int _socket; |
aoqi@0 | 116 | |
aoqi@0 | 117 | public: |
aoqi@0 | 118 | void complete(jint res, bufferedStream* st); |
aoqi@0 | 119 | |
aoqi@0 | 120 | void set_socket(int s) { _socket = s; } |
aoqi@0 | 121 | int socket() const { return _socket; } |
aoqi@0 | 122 | |
aoqi@0 | 123 | AixAttachOperation(char* name) : AttachOperation(name) { |
aoqi@0 | 124 | set_socket(-1); |
aoqi@0 | 125 | } |
aoqi@0 | 126 | }; |
aoqi@0 | 127 | |
aoqi@0 | 128 | // statics |
aoqi@0 | 129 | char AixAttachListener::_path[UNIX_PATH_MAX]; |
aoqi@0 | 130 | bool AixAttachListener::_has_path; |
aoqi@0 | 131 | int AixAttachListener::_listener = -1; |
aoqi@0 | 132 | // Shutdown marker to prevent accept blocking during clean-up |
aoqi@0 | 133 | bool AixAttachListener::_shutdown = false; |
aoqi@0 | 134 | |
aoqi@0 | 135 | // Supporting class to help split a buffer into individual components |
aoqi@0 | 136 | class ArgumentIterator : public StackObj { |
aoqi@0 | 137 | private: |
aoqi@0 | 138 | char* _pos; |
aoqi@0 | 139 | char* _end; |
aoqi@0 | 140 | public: |
aoqi@0 | 141 | ArgumentIterator(char* arg_buffer, size_t arg_size) { |
aoqi@0 | 142 | _pos = arg_buffer; |
aoqi@0 | 143 | _end = _pos + arg_size - 1; |
aoqi@0 | 144 | } |
aoqi@0 | 145 | char* next() { |
aoqi@0 | 146 | if (*_pos == '\0') { |
aoqi@0 | 147 | return NULL; |
aoqi@0 | 148 | } |
aoqi@0 | 149 | char* res = _pos; |
aoqi@0 | 150 | char* next_pos = strchr(_pos, '\0'); |
aoqi@0 | 151 | if (next_pos < _end) { |
aoqi@0 | 152 | next_pos++; |
aoqi@0 | 153 | } |
aoqi@0 | 154 | _pos = next_pos; |
aoqi@0 | 155 | return res; |
aoqi@0 | 156 | } |
aoqi@0 | 157 | }; |
aoqi@0 | 158 | |
aoqi@0 | 159 | // On AIX if sockets block until all data has been transmitted |
aoqi@0 | 160 | // successfully in some communication domains a socket "close" may |
aoqi@0 | 161 | // never complete. We have to take care that after the socket shutdown |
aoqi@0 | 162 | // the listener never enters accept state. |
aoqi@0 | 163 | |
aoqi@0 | 164 | // atexit hook to stop listener and unlink the file that it is |
aoqi@0 | 165 | // bound too. |
aoqi@0 | 166 | |
aoqi@0 | 167 | // Some modifications to the listener logic to prevent deadlocks on exit. |
aoqi@0 | 168 | // 1. We Shutdown the socket here instead. AixAttachOperation::complete() is not the right place |
aoqi@0 | 169 | // since more than one agent in a sequence in JPLIS live tests wouldn't work (Listener thread |
aoqi@0 | 170 | // would be dead after the first operation completion). |
aoqi@0 | 171 | // 2. close(s) may never return if the listener thread is in socket accept(). Unlinking the file |
aoqi@0 | 172 | // should be sufficient for cleanup. |
aoqi@0 | 173 | extern "C" { |
aoqi@0 | 174 | static void listener_cleanup() { |
aoqi@0 | 175 | static int cleanup_done; |
aoqi@0 | 176 | if (!cleanup_done) { |
aoqi@0 | 177 | cleanup_done = 1; |
aoqi@0 | 178 | AixAttachListener::set_shutdown(true); |
aoqi@0 | 179 | int s = AixAttachListener::listener(); |
aoqi@0 | 180 | if (s != -1) { |
aoqi@0 | 181 | ::shutdown(s, 2); |
aoqi@0 | 182 | } |
aoqi@0 | 183 | if (AixAttachListener::has_path()) { |
aoqi@0 | 184 | ::unlink(AixAttachListener::path()); |
aoqi@0 | 185 | } |
aoqi@0 | 186 | } |
aoqi@0 | 187 | } |
aoqi@0 | 188 | } |
aoqi@0 | 189 | |
aoqi@0 | 190 | // Initialization - create a listener socket and bind it to a file |
aoqi@0 | 191 | |
aoqi@0 | 192 | int AixAttachListener::init() { |
aoqi@0 | 193 | char path[UNIX_PATH_MAX]; // socket file |
aoqi@0 | 194 | char initial_path[UNIX_PATH_MAX]; // socket file during setup |
aoqi@0 | 195 | int listener; // listener socket (file descriptor) |
aoqi@0 | 196 | |
aoqi@0 | 197 | // register function to cleanup |
aoqi@0 | 198 | ::atexit(listener_cleanup); |
aoqi@0 | 199 | |
aoqi@0 | 200 | int n = snprintf(path, UNIX_PATH_MAX, "%s/.java_pid%d", |
aoqi@0 | 201 | os::get_temp_directory(), os::current_process_id()); |
aoqi@0 | 202 | if (n < (int)UNIX_PATH_MAX) { |
aoqi@0 | 203 | n = snprintf(initial_path, UNIX_PATH_MAX, "%s.tmp", path); |
aoqi@0 | 204 | } |
aoqi@0 | 205 | if (n >= (int)UNIX_PATH_MAX) { |
aoqi@0 | 206 | return -1; |
aoqi@0 | 207 | } |
aoqi@0 | 208 | |
aoqi@0 | 209 | // create the listener socket |
aoqi@0 | 210 | listener = ::socket(PF_UNIX, SOCK_STREAM, 0); |
aoqi@0 | 211 | if (listener == -1) { |
aoqi@0 | 212 | return -1; |
aoqi@0 | 213 | } |
aoqi@0 | 214 | |
aoqi@0 | 215 | // bind socket |
aoqi@0 | 216 | struct sockaddr_un addr; |
aoqi@0 | 217 | addr.sun_family = AF_UNIX; |
aoqi@0 | 218 | strcpy(addr.sun_path, initial_path); |
aoqi@0 | 219 | ::unlink(initial_path); |
aoqi@0 | 220 | // We must call bind with the actual socketaddr length. This is obligatory for AS400. |
aoqi@0 | 221 | int res = ::bind(listener, (struct sockaddr*)&addr, SUN_LEN(&addr)); |
aoqi@0 | 222 | if (res == -1) { |
aoqi@0 | 223 | RESTARTABLE(::close(listener), res); |
aoqi@0 | 224 | return -1; |
aoqi@0 | 225 | } |
aoqi@0 | 226 | |
aoqi@0 | 227 | // put in listen mode, set permissions, and rename into place |
aoqi@0 | 228 | res = ::listen(listener, 5); |
aoqi@0 | 229 | if (res == 0) { |
aoqi@0 | 230 | RESTARTABLE(::chmod(initial_path, (S_IREAD|S_IWRITE) & ~(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)), res); |
aoqi@0 | 231 | if (res == 0) { |
aoqi@0 | 232 | res = ::rename(initial_path, path); |
aoqi@0 | 233 | } |
aoqi@0 | 234 | } |
aoqi@0 | 235 | if (res == -1) { |
aoqi@0 | 236 | RESTARTABLE(::close(listener), res); |
aoqi@0 | 237 | ::unlink(initial_path); |
aoqi@0 | 238 | return -1; |
aoqi@0 | 239 | } |
aoqi@0 | 240 | set_path(path); |
aoqi@0 | 241 | set_listener(listener); |
aoqi@0 | 242 | set_shutdown(false); |
aoqi@0 | 243 | |
aoqi@0 | 244 | return 0; |
aoqi@0 | 245 | } |
aoqi@0 | 246 | |
aoqi@0 | 247 | // Given a socket that is connected to a peer we read the request and |
aoqi@0 | 248 | // create an AttachOperation. As the socket is blocking there is potential |
aoqi@0 | 249 | // for a denial-of-service if the peer does not response. However this happens |
aoqi@0 | 250 | // after the peer credentials have been checked and in the worst case it just |
aoqi@0 | 251 | // means that the attach listener thread is blocked. |
aoqi@0 | 252 | // |
aoqi@0 | 253 | AixAttachOperation* AixAttachListener::read_request(int s) { |
aoqi@0 | 254 | char ver_str[8]; |
aoqi@0 | 255 | sprintf(ver_str, "%d", ATTACH_PROTOCOL_VER); |
aoqi@0 | 256 | |
aoqi@0 | 257 | // The request is a sequence of strings so we first figure out the |
aoqi@0 | 258 | // expected count and the maximum possible length of the request. |
aoqi@0 | 259 | // The request is: |
aoqi@0 | 260 | // <ver>0<cmd>0<arg>0<arg>0<arg>0 |
aoqi@0 | 261 | // where <ver> is the protocol version (1), <cmd> is the command |
aoqi@0 | 262 | // name ("load", "datadump", ...), and <arg> is an argument |
aoqi@0 | 263 | int expected_str_count = 2 + AttachOperation::arg_count_max; |
aoqi@0 | 264 | const int max_len = (sizeof(ver_str) + 1) + (AttachOperation::name_length_max + 1) + |
aoqi@0 | 265 | AttachOperation::arg_count_max*(AttachOperation::arg_length_max + 1); |
aoqi@0 | 266 | |
aoqi@0 | 267 | char buf[max_len]; |
aoqi@0 | 268 | int str_count = 0; |
aoqi@0 | 269 | |
aoqi@0 | 270 | // Read until all (expected) strings have been read, the buffer is |
aoqi@0 | 271 | // full, or EOF. |
aoqi@0 | 272 | |
aoqi@0 | 273 | int off = 0; |
aoqi@0 | 274 | int left = max_len; |
aoqi@0 | 275 | |
aoqi@0 | 276 | do { |
aoqi@0 | 277 | int n; |
aoqi@0 | 278 | // Don't block on interrupts because this will |
aoqi@0 | 279 | // hang in the clean-up when shutting down. |
aoqi@0 | 280 | n = read(s, buf+off, left); |
aoqi@0 | 281 | if (n == -1) { |
aoqi@0 | 282 | return NULL; // reset by peer or other error |
aoqi@0 | 283 | } |
aoqi@0 | 284 | if (n == 0) { // end of file reached |
aoqi@0 | 285 | break; |
aoqi@0 | 286 | } |
aoqi@0 | 287 | for (int i=0; i<n; i++) { |
aoqi@0 | 288 | if (buf[off+i] == 0) { |
aoqi@0 | 289 | // EOS found |
aoqi@0 | 290 | str_count++; |
aoqi@0 | 291 | |
aoqi@0 | 292 | // The first string is <ver> so check it now to |
aoqi@0 | 293 | // check for protocol mis-match |
aoqi@0 | 294 | if (str_count == 1) { |
aoqi@0 | 295 | if ((strlen(buf) != strlen(ver_str)) || |
aoqi@0 | 296 | (atoi(buf) != ATTACH_PROTOCOL_VER)) { |
aoqi@0 | 297 | char msg[32]; |
aoqi@0 | 298 | sprintf(msg, "%d\n", ATTACH_ERROR_BADVERSION); |
aoqi@0 | 299 | write_fully(s, msg, strlen(msg)); |
aoqi@0 | 300 | return NULL; |
aoqi@0 | 301 | } |
aoqi@0 | 302 | } |
aoqi@0 | 303 | } |
aoqi@0 | 304 | } |
aoqi@0 | 305 | off += n; |
aoqi@0 | 306 | left -= n; |
aoqi@0 | 307 | } while (left > 0 && str_count < expected_str_count); |
aoqi@0 | 308 | |
aoqi@0 | 309 | if (str_count != expected_str_count) { |
aoqi@0 | 310 | return NULL; // incomplete request |
aoqi@0 | 311 | } |
aoqi@0 | 312 | |
aoqi@0 | 313 | // parse request |
aoqi@0 | 314 | |
aoqi@0 | 315 | ArgumentIterator args(buf, (max_len)-left); |
aoqi@0 | 316 | |
aoqi@0 | 317 | // version already checked |
aoqi@0 | 318 | char* v = args.next(); |
aoqi@0 | 319 | |
aoqi@0 | 320 | char* name = args.next(); |
aoqi@0 | 321 | if (name == NULL || strlen(name) > AttachOperation::name_length_max) { |
aoqi@0 | 322 | return NULL; |
aoqi@0 | 323 | } |
aoqi@0 | 324 | |
aoqi@0 | 325 | AixAttachOperation* op = new AixAttachOperation(name); |
aoqi@0 | 326 | |
aoqi@0 | 327 | for (int i=0; i<AttachOperation::arg_count_max; i++) { |
aoqi@0 | 328 | char* arg = args.next(); |
aoqi@0 | 329 | if (arg == NULL) { |
aoqi@0 | 330 | op->set_arg(i, NULL); |
aoqi@0 | 331 | } else { |
aoqi@0 | 332 | if (strlen(arg) > AttachOperation::arg_length_max) { |
aoqi@0 | 333 | delete op; |
aoqi@0 | 334 | return NULL; |
aoqi@0 | 335 | } |
aoqi@0 | 336 | op->set_arg(i, arg); |
aoqi@0 | 337 | } |
aoqi@0 | 338 | } |
aoqi@0 | 339 | |
aoqi@0 | 340 | op->set_socket(s); |
aoqi@0 | 341 | return op; |
aoqi@0 | 342 | } |
aoqi@0 | 343 | |
aoqi@0 | 344 | |
aoqi@0 | 345 | // Dequeue an operation |
aoqi@0 | 346 | // |
aoqi@0 | 347 | // In the Linux implementation there is only a single operation and clients |
aoqi@0 | 348 | // cannot queue commands (except at the socket level). |
aoqi@0 | 349 | // |
aoqi@0 | 350 | AixAttachOperation* AixAttachListener::dequeue() { |
aoqi@0 | 351 | for (;;) { |
aoqi@0 | 352 | int s; |
aoqi@0 | 353 | |
aoqi@0 | 354 | // wait for client to connect |
aoqi@0 | 355 | struct sockaddr addr; |
aoqi@0 | 356 | socklen_t len = sizeof(addr); |
aoqi@0 | 357 | memset(&addr, 0, len); |
aoqi@0 | 358 | // We must prevent accept blocking on the socket if it has been shut down. |
aoqi@0 | 359 | // Therefore we allow interrups and check whether we have been shut down already. |
aoqi@0 | 360 | if (AixAttachListener::is_shutdown()) { |
aoqi@0 | 361 | return NULL; |
aoqi@0 | 362 | } |
aoqi@0 | 363 | s=::accept(listener(), &addr, &len); |
aoqi@0 | 364 | if (s == -1) { |
aoqi@0 | 365 | return NULL; // log a warning? |
aoqi@0 | 366 | } |
aoqi@0 | 367 | |
aoqi@0 | 368 | // Added timeouts for read and write. If we get no request within the |
aoqi@0 | 369 | // next AttachListenerTimeout milliseconds we just finish the connection. |
aoqi@0 | 370 | struct timeval tv; |
aoqi@0 | 371 | tv.tv_sec = 0; |
aoqi@0 | 372 | tv.tv_usec = AttachListenerTimeout * 1000; |
aoqi@0 | 373 | ::setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv)); |
aoqi@0 | 374 | ::setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char*)&tv, sizeof(tv)); |
aoqi@0 | 375 | |
aoqi@0 | 376 | // get the credentials of the peer and check the effective uid/guid |
aoqi@0 | 377 | // - check with jeff on this. |
aoqi@0 | 378 | struct peercred_struct cred_info; |
aoqi@0 | 379 | socklen_t optlen = sizeof(cred_info); |
aoqi@0 | 380 | if (::getsockopt(s, SOL_SOCKET, SO_PEERID, (void*)&cred_info, &optlen) == -1) { |
aoqi@0 | 381 | int res; |
aoqi@0 | 382 | RESTARTABLE(::close(s), res); |
aoqi@0 | 383 | continue; |
aoqi@0 | 384 | } |
aoqi@0 | 385 | uid_t euid = geteuid(); |
aoqi@0 | 386 | gid_t egid = getegid(); |
aoqi@0 | 387 | |
aoqi@0 | 388 | if (cred_info.euid != euid || cred_info.egid != egid) { |
aoqi@0 | 389 | int res; |
aoqi@0 | 390 | RESTARTABLE(::close(s), res); |
aoqi@0 | 391 | continue; |
aoqi@0 | 392 | } |
aoqi@0 | 393 | |
aoqi@0 | 394 | // peer credential look okay so we read the request |
aoqi@0 | 395 | AixAttachOperation* op = read_request(s); |
aoqi@0 | 396 | if (op == NULL) { |
aoqi@0 | 397 | int res; |
aoqi@0 | 398 | RESTARTABLE(::close(s), res); |
aoqi@0 | 399 | continue; |
aoqi@0 | 400 | } else { |
aoqi@0 | 401 | return op; |
aoqi@0 | 402 | } |
aoqi@0 | 403 | } |
aoqi@0 | 404 | } |
aoqi@0 | 405 | |
aoqi@0 | 406 | // write the given buffer to the socket |
aoqi@0 | 407 | int AixAttachListener::write_fully(int s, char* buf, int len) { |
aoqi@0 | 408 | do { |
aoqi@0 | 409 | int n = ::write(s, buf, len); |
aoqi@0 | 410 | if (n == -1) { |
aoqi@0 | 411 | if (errno != EINTR) return -1; |
aoqi@0 | 412 | } else { |
aoqi@0 | 413 | buf += n; |
aoqi@0 | 414 | len -= n; |
aoqi@0 | 415 | } |
aoqi@0 | 416 | } |
aoqi@0 | 417 | while (len > 0); |
aoqi@0 | 418 | return 0; |
aoqi@0 | 419 | } |
aoqi@0 | 420 | |
aoqi@0 | 421 | // Complete an operation by sending the operation result and any result |
aoqi@0 | 422 | // output to the client. At this time the socket is in blocking mode so |
aoqi@0 | 423 | // potentially we can block if there is a lot of data and the client is |
aoqi@0 | 424 | // non-responsive. For most operations this is a non-issue because the |
aoqi@0 | 425 | // default send buffer is sufficient to buffer everything. In the future |
aoqi@0 | 426 | // if there are operations that involves a very big reply then it the |
aoqi@0 | 427 | // socket could be made non-blocking and a timeout could be used. |
aoqi@0 | 428 | |
aoqi@0 | 429 | void AixAttachOperation::complete(jint result, bufferedStream* st) { |
aoqi@0 | 430 | JavaThread* thread = JavaThread::current(); |
aoqi@0 | 431 | ThreadBlockInVM tbivm(thread); |
aoqi@0 | 432 | |
aoqi@0 | 433 | thread->set_suspend_equivalent(); |
aoqi@0 | 434 | // cleared by handle_special_suspend_equivalent_condition() or |
aoqi@0 | 435 | // java_suspend_self() via check_and_wait_while_suspended() |
aoqi@0 | 436 | |
aoqi@0 | 437 | // write operation result |
aoqi@0 | 438 | char msg[32]; |
aoqi@0 | 439 | sprintf(msg, "%d\n", result); |
aoqi@0 | 440 | int rc = AixAttachListener::write_fully(this->socket(), msg, strlen(msg)); |
aoqi@0 | 441 | |
aoqi@0 | 442 | // write any result data |
aoqi@0 | 443 | if (rc == 0) { |
aoqi@0 | 444 | // Shutdown the socket in the cleanup function to enable more than |
aoqi@0 | 445 | // one agent attach in a sequence (see comments to listener_cleanup()). |
aoqi@0 | 446 | AixAttachListener::write_fully(this->socket(), (char*) st->base(), st->size()); |
aoqi@0 | 447 | } |
aoqi@0 | 448 | |
aoqi@0 | 449 | // done |
aoqi@0 | 450 | RESTARTABLE(::close(this->socket()), rc); |
aoqi@0 | 451 | |
aoqi@0 | 452 | // were we externally suspended while we were waiting? |
aoqi@0 | 453 | thread->check_and_wait_while_suspended(); |
aoqi@0 | 454 | |
aoqi@0 | 455 | delete this; |
aoqi@0 | 456 | } |
aoqi@0 | 457 | |
aoqi@0 | 458 | |
aoqi@0 | 459 | // AttachListener functions |
aoqi@0 | 460 | |
aoqi@0 | 461 | AttachOperation* AttachListener::dequeue() { |
aoqi@0 | 462 | JavaThread* thread = JavaThread::current(); |
aoqi@0 | 463 | ThreadBlockInVM tbivm(thread); |
aoqi@0 | 464 | |
aoqi@0 | 465 | thread->set_suspend_equivalent(); |
aoqi@0 | 466 | // cleared by handle_special_suspend_equivalent_condition() or |
aoqi@0 | 467 | // java_suspend_self() via check_and_wait_while_suspended() |
aoqi@0 | 468 | |
aoqi@0 | 469 | AttachOperation* op = AixAttachListener::dequeue(); |
aoqi@0 | 470 | |
aoqi@0 | 471 | // were we externally suspended while we were waiting? |
aoqi@0 | 472 | thread->check_and_wait_while_suspended(); |
aoqi@0 | 473 | |
aoqi@0 | 474 | return op; |
aoqi@0 | 475 | } |
aoqi@0 | 476 | |
aoqi@0 | 477 | // Performs initialization at vm startup |
aoqi@0 | 478 | // For AIX we remove any stale .java_pid file which could cause |
aoqi@0 | 479 | // an attaching process to think we are ready to receive on the |
aoqi@0 | 480 | // domain socket before we are properly initialized |
aoqi@0 | 481 | |
aoqi@0 | 482 | void AttachListener::vm_start() { |
aoqi@0 | 483 | char fn[UNIX_PATH_MAX]; |
aoqi@0 | 484 | struct stat64 st; |
aoqi@0 | 485 | int ret; |
aoqi@0 | 486 | |
aoqi@0 | 487 | int n = snprintf(fn, UNIX_PATH_MAX, "%s/.java_pid%d", |
aoqi@0 | 488 | os::get_temp_directory(), os::current_process_id()); |
aoqi@0 | 489 | assert(n < (int)UNIX_PATH_MAX, "java_pid file name buffer overflow"); |
aoqi@0 | 490 | |
aoqi@0 | 491 | RESTARTABLE(::stat64(fn, &st), ret); |
aoqi@0 | 492 | if (ret == 0) { |
aoqi@0 | 493 | ret = ::unlink(fn); |
aoqi@0 | 494 | if (ret == -1) { |
aoqi@0 | 495 | debug_only(warning("failed to remove stale attach pid file at %s", fn)); |
aoqi@0 | 496 | } |
aoqi@0 | 497 | } |
aoqi@0 | 498 | } |
aoqi@0 | 499 | |
aoqi@0 | 500 | int AttachListener::pd_init() { |
aoqi@0 | 501 | JavaThread* thread = JavaThread::current(); |
aoqi@0 | 502 | ThreadBlockInVM tbivm(thread); |
aoqi@0 | 503 | |
aoqi@0 | 504 | thread->set_suspend_equivalent(); |
aoqi@0 | 505 | // cleared by handle_special_suspend_equivalent_condition() or |
aoqi@0 | 506 | // java_suspend_self() via check_and_wait_while_suspended() |
aoqi@0 | 507 | |
aoqi@0 | 508 | int ret_code = AixAttachListener::init(); |
aoqi@0 | 509 | |
aoqi@0 | 510 | // were we externally suspended while we were waiting? |
aoqi@0 | 511 | thread->check_and_wait_while_suspended(); |
aoqi@0 | 512 | |
aoqi@0 | 513 | return ret_code; |
aoqi@0 | 514 | } |
aoqi@0 | 515 | |
aoqi@0 | 516 | // Attach Listener is started lazily except in the case when |
aoqi@0 | 517 | // +ReduseSignalUsage is used |
aoqi@0 | 518 | bool AttachListener::init_at_startup() { |
aoqi@0 | 519 | if (ReduceSignalUsage) { |
aoqi@0 | 520 | return true; |
aoqi@0 | 521 | } else { |
aoqi@0 | 522 | return false; |
aoqi@0 | 523 | } |
aoqi@0 | 524 | } |
aoqi@0 | 525 | |
aoqi@0 | 526 | // If the file .attach_pid<pid> exists in the working directory |
aoqi@0 | 527 | // or /tmp then this is the trigger to start the attach mechanism |
aoqi@0 | 528 | bool AttachListener::is_init_trigger() { |
aoqi@0 | 529 | if (init_at_startup() || is_initialized()) { |
aoqi@0 | 530 | return false; // initialized at startup or already initialized |
aoqi@0 | 531 | } |
aoqi@0 | 532 | char fn[PATH_MAX+1]; |
aoqi@0 | 533 | sprintf(fn, ".attach_pid%d", os::current_process_id()); |
aoqi@0 | 534 | int ret; |
aoqi@0 | 535 | struct stat64 st; |
aoqi@0 | 536 | RESTARTABLE(::stat64(fn, &st), ret); |
aoqi@0 | 537 | if (ret == -1) { |
aoqi@0 | 538 | snprintf(fn, sizeof(fn), "%s/.attach_pid%d", |
aoqi@0 | 539 | os::get_temp_directory(), os::current_process_id()); |
aoqi@0 | 540 | RESTARTABLE(::stat64(fn, &st), ret); |
aoqi@0 | 541 | } |
aoqi@0 | 542 | if (ret == 0) { |
aoqi@0 | 543 | // simple check to avoid starting the attach mechanism when |
aoqi@0 | 544 | // a bogus user creates the file |
aoqi@0 | 545 | if (st.st_uid == geteuid()) { |
aoqi@0 | 546 | init(); |
aoqi@0 | 547 | return true; |
aoqi@0 | 548 | } |
aoqi@0 | 549 | } |
aoqi@0 | 550 | return false; |
aoqi@0 | 551 | } |
aoqi@0 | 552 | |
aoqi@0 | 553 | // if VM aborts then remove listener |
aoqi@0 | 554 | void AttachListener::abort() { |
aoqi@0 | 555 | listener_cleanup(); |
aoqi@0 | 556 | } |
aoqi@0 | 557 | |
aoqi@0 | 558 | void AttachListener::pd_data_dump() { |
aoqi@0 | 559 | os::signal_notify(SIGQUIT); |
aoqi@0 | 560 | } |
aoqi@0 | 561 | |
aoqi@0 | 562 | AttachOperationFunctionInfo* AttachListener::pd_find_operation(const char* n) { |
aoqi@0 | 563 | return NULL; |
aoqi@0 | 564 | } |
aoqi@0 | 565 | |
aoqi@0 | 566 | jint AttachListener::pd_set_flag(AttachOperation* op, outputStream* out) { |
aoqi@0 | 567 | out->print_cr("flag '%s' cannot be changed", op->arg(0)); |
aoqi@0 | 568 | return JNI_ERR; |
aoqi@0 | 569 | } |
aoqi@0 | 570 | |
aoqi@0 | 571 | void AttachListener::pd_detachall() { |
aoqi@0 | 572 | // Cleanup server socket to detach clients. |
aoqi@0 | 573 | listener_cleanup(); |
aoqi@0 | 574 | } |