annotate src/os/solaris/vm/attachListener_solaris.cpp @ 6024:973046802b6f

7162955: Attach api on Solaris, too many open files Summary: Release server-side socket after client receives it. Reviewed-by: sla, dsamersoff, dcubed, acorn Contributed-by: dean.long@oracle.com
author dlong
date Thu, 26 Apr 2012 16:24:15 -0400
parents f95d63e2154a
children b9a9ed0f8eeb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1675
diff changeset
2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1497
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1497
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1497
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1675
diff changeset
25 #include "precompiled.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1675
diff changeset
26 #include "runtime/interfaceSupport.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1675
diff changeset
27 #include "runtime/os.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1675
diff changeset
28 #include "services/attachListener.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1675
diff changeset
29 #include "services/dtraceAttacher.hpp"
0
a61af66fc99e Initial load
duke
parents:
diff changeset
30
a61af66fc99e Initial load
duke
parents:
diff changeset
31 #include <door.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
32 #include <string.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
33 #include <signal.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
34 #include <sys/types.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
35 #include <sys/socket.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
36 #include <sys/stat.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
37
a61af66fc99e Initial load
duke
parents:
diff changeset
38 // stropts.h uses STR in stream ioctl defines
a61af66fc99e Initial load
duke
parents:
diff changeset
39 #undef STR
a61af66fc99e Initial load
duke
parents:
diff changeset
40 #include <stropts.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
41 #undef STR
a61af66fc99e Initial load
duke
parents:
diff changeset
42 #define STR(a) #a
a61af66fc99e Initial load
duke
parents:
diff changeset
43
a61af66fc99e Initial load
duke
parents:
diff changeset
44 // The attach mechanism on Solaris is implemented using the Doors IPC
a61af66fc99e Initial load
duke
parents:
diff changeset
45 // mechanism. The first tool to attempt to attach causes the attach
a61af66fc99e Initial load
duke
parents:
diff changeset
46 // listener thread to startup. This thread creats a door that is
a61af66fc99e Initial load
duke
parents:
diff changeset
47 // associated with a function that enqueues an operation to the attach
a61af66fc99e Initial load
duke
parents:
diff changeset
48 // listener. The door is attached to a file in the file system so that
a61af66fc99e Initial load
duke
parents:
diff changeset
49 // client (tools) can locate it. To enqueue an operation to the VM the
a61af66fc99e Initial load
duke
parents:
diff changeset
50 // client calls through the door which invokes the enqueue function in
a61af66fc99e Initial load
duke
parents:
diff changeset
51 // this process. The credentials of the client are checked and if the
a61af66fc99e Initial load
duke
parents:
diff changeset
52 // effective uid matches this process then the operation is enqueued.
a61af66fc99e Initial load
duke
parents:
diff changeset
53 // When an operation completes the attach listener is required to send the
a61af66fc99e Initial load
duke
parents:
diff changeset
54 // operation result and any result data to the client. In this implementation
a61af66fc99e Initial load
duke
parents:
diff changeset
55 // the result is returned via a UNIX domain socket. A pair of connected
a61af66fc99e Initial load
duke
parents:
diff changeset
56 // sockets (socketpair) is created in the enqueue function and the file
a61af66fc99e Initial load
duke
parents:
diff changeset
57 // descriptor for one of the sockets is returned to the client as the
a61af66fc99e Initial load
duke
parents:
diff changeset
58 // return from the door call. The other end is retained in this process.
a61af66fc99e Initial load
duke
parents:
diff changeset
59 // When the operation completes the result is sent to the client and
a61af66fc99e Initial load
duke
parents:
diff changeset
60 // the socket is closed.
a61af66fc99e Initial load
duke
parents:
diff changeset
61
a61af66fc99e Initial load
duke
parents:
diff changeset
62 // forward reference
a61af66fc99e Initial load
duke
parents:
diff changeset
63 class SolarisAttachOperation;
a61af66fc99e Initial load
duke
parents:
diff changeset
64
a61af66fc99e Initial load
duke
parents:
diff changeset
65 class SolarisAttachListener: AllStatic {
a61af66fc99e Initial load
duke
parents:
diff changeset
66 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
67
a61af66fc99e Initial load
duke
parents:
diff changeset
68 // the path to which we attach the door file descriptor
a61af66fc99e Initial load
duke
parents:
diff changeset
69 static char _door_path[PATH_MAX+1];
a61af66fc99e Initial load
duke
parents:
diff changeset
70 static volatile bool _has_door_path;
a61af66fc99e Initial load
duke
parents:
diff changeset
71
a61af66fc99e Initial load
duke
parents:
diff changeset
72 // door descriptor returned by door_create
a61af66fc99e Initial load
duke
parents:
diff changeset
73 static int _door_descriptor;
a61af66fc99e Initial load
duke
parents:
diff changeset
74
a61af66fc99e Initial load
duke
parents:
diff changeset
75 static void set_door_path(char* path) {
a61af66fc99e Initial load
duke
parents:
diff changeset
76 if (path == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
77 _has_door_path = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
78 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
79 strncpy(_door_path, path, PATH_MAX);
a61af66fc99e Initial load
duke
parents:
diff changeset
80 _door_path[PATH_MAX] = '\0'; // ensure it's nul terminated
a61af66fc99e Initial load
duke
parents:
diff changeset
81 _has_door_path = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
82 }
a61af66fc99e Initial load
duke
parents:
diff changeset
83 }
a61af66fc99e Initial load
duke
parents:
diff changeset
84
a61af66fc99e Initial load
duke
parents:
diff changeset
85 static void set_door_descriptor(int dd) { _door_descriptor = dd; }
a61af66fc99e Initial load
duke
parents:
diff changeset
86
a61af66fc99e Initial load
duke
parents:
diff changeset
87 // mutex to protect operation list
a61af66fc99e Initial load
duke
parents:
diff changeset
88 static mutex_t _mutex;
a61af66fc99e Initial load
duke
parents:
diff changeset
89
a61af66fc99e Initial load
duke
parents:
diff changeset
90 // semaphore to wakeup listener thread
a61af66fc99e Initial load
duke
parents:
diff changeset
91 static sema_t _wakeup;
a61af66fc99e Initial load
duke
parents:
diff changeset
92
a61af66fc99e Initial load
duke
parents:
diff changeset
93 static mutex_t* mutex() { return &_mutex; }
a61af66fc99e Initial load
duke
parents:
diff changeset
94 static sema_t* wakeup() { return &_wakeup; }
a61af66fc99e Initial load
duke
parents:
diff changeset
95
a61af66fc99e Initial load
duke
parents:
diff changeset
96 // enqueued operation list
a61af66fc99e Initial load
duke
parents:
diff changeset
97 static SolarisAttachOperation* _head;
a61af66fc99e Initial load
duke
parents:
diff changeset
98 static SolarisAttachOperation* _tail;
a61af66fc99e Initial load
duke
parents:
diff changeset
99
a61af66fc99e Initial load
duke
parents:
diff changeset
100 static SolarisAttachOperation* head() { return _head; }
a61af66fc99e Initial load
duke
parents:
diff changeset
101 static void set_head(SolarisAttachOperation* head) { _head = head; }
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103 static SolarisAttachOperation* tail() { return _tail; }
a61af66fc99e Initial load
duke
parents:
diff changeset
104 static void set_tail(SolarisAttachOperation* tail) { _tail = tail; }
a61af66fc99e Initial load
duke
parents:
diff changeset
105
a61af66fc99e Initial load
duke
parents:
diff changeset
106 // create the door
a61af66fc99e Initial load
duke
parents:
diff changeset
107 static int create_door();
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
110 enum {
a61af66fc99e Initial load
duke
parents:
diff changeset
111 ATTACH_PROTOCOL_VER = 1 // protocol version
a61af66fc99e Initial load
duke
parents:
diff changeset
112 };
a61af66fc99e Initial load
duke
parents:
diff changeset
113 enum {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 ATTACH_ERROR_BADREQUEST = 100, // error code returned by
a61af66fc99e Initial load
duke
parents:
diff changeset
115 ATTACH_ERROR_BADVERSION = 101, // the door call
a61af66fc99e Initial load
duke
parents:
diff changeset
116 ATTACH_ERROR_RESOURCE = 102,
a61af66fc99e Initial load
duke
parents:
diff changeset
117 ATTACH_ERROR_INTERNAL = 103,
a61af66fc99e Initial load
duke
parents:
diff changeset
118 ATTACH_ERROR_DENIED = 104
a61af66fc99e Initial load
duke
parents:
diff changeset
119 };
a61af66fc99e Initial load
duke
parents:
diff changeset
120
a61af66fc99e Initial load
duke
parents:
diff changeset
121 // initialize the listener
a61af66fc99e Initial load
duke
parents:
diff changeset
122 static int init();
a61af66fc99e Initial load
duke
parents:
diff changeset
123
a61af66fc99e Initial load
duke
parents:
diff changeset
124 static bool has_door_path() { return _has_door_path; }
a61af66fc99e Initial load
duke
parents:
diff changeset
125 static char* door_path() { return _door_path; }
a61af66fc99e Initial load
duke
parents:
diff changeset
126 static int door_descriptor() { return _door_descriptor; }
a61af66fc99e Initial load
duke
parents:
diff changeset
127
a61af66fc99e Initial load
duke
parents:
diff changeset
128 // enqueue an operation
a61af66fc99e Initial load
duke
parents:
diff changeset
129 static void enqueue(SolarisAttachOperation* op);
a61af66fc99e Initial load
duke
parents:
diff changeset
130
a61af66fc99e Initial load
duke
parents:
diff changeset
131 // dequeue an operation
a61af66fc99e Initial load
duke
parents:
diff changeset
132 static SolarisAttachOperation* dequeue();
a61af66fc99e Initial load
duke
parents:
diff changeset
133 };
a61af66fc99e Initial load
duke
parents:
diff changeset
134
a61af66fc99e Initial load
duke
parents:
diff changeset
135
a61af66fc99e Initial load
duke
parents:
diff changeset
136 // SolarisAttachOperation is an AttachOperation that additionally encapsulates
a61af66fc99e Initial load
duke
parents:
diff changeset
137 // a socket connection to the requesting client/tool. SolarisAttachOperation
a61af66fc99e Initial load
duke
parents:
diff changeset
138 // can additionally be held in a linked list.
a61af66fc99e Initial load
duke
parents:
diff changeset
139
a61af66fc99e Initial load
duke
parents:
diff changeset
140 class SolarisAttachOperation: public AttachOperation {
a61af66fc99e Initial load
duke
parents:
diff changeset
141 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
142 friend class SolarisAttachListener;
a61af66fc99e Initial load
duke
parents:
diff changeset
143
a61af66fc99e Initial load
duke
parents:
diff changeset
144 // connection to client
a61af66fc99e Initial load
duke
parents:
diff changeset
145 int _socket;
a61af66fc99e Initial load
duke
parents:
diff changeset
146
a61af66fc99e Initial load
duke
parents:
diff changeset
147 // linked list support
a61af66fc99e Initial load
duke
parents:
diff changeset
148 SolarisAttachOperation* _next;
a61af66fc99e Initial load
duke
parents:
diff changeset
149
a61af66fc99e Initial load
duke
parents:
diff changeset
150 SolarisAttachOperation* next() { return _next; }
a61af66fc99e Initial load
duke
parents:
diff changeset
151 void set_next(SolarisAttachOperation* next) { _next = next; }
a61af66fc99e Initial load
duke
parents:
diff changeset
152
a61af66fc99e Initial load
duke
parents:
diff changeset
153 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
154 void complete(jint res, bufferedStream* st);
a61af66fc99e Initial load
duke
parents:
diff changeset
155
a61af66fc99e Initial load
duke
parents:
diff changeset
156 int socket() const { return _socket; }
a61af66fc99e Initial load
duke
parents:
diff changeset
157 void set_socket(int s) { _socket = s; }
a61af66fc99e Initial load
duke
parents:
diff changeset
158
a61af66fc99e Initial load
duke
parents:
diff changeset
159 SolarisAttachOperation(char* name) : AttachOperation(name) {
a61af66fc99e Initial load
duke
parents:
diff changeset
160 set_socket(-1);
a61af66fc99e Initial load
duke
parents:
diff changeset
161 set_next(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
162 }
a61af66fc99e Initial load
duke
parents:
diff changeset
163 };
a61af66fc99e Initial load
duke
parents:
diff changeset
164
a61af66fc99e Initial load
duke
parents:
diff changeset
165 // statics
a61af66fc99e Initial load
duke
parents:
diff changeset
166 char SolarisAttachListener::_door_path[PATH_MAX+1];
a61af66fc99e Initial load
duke
parents:
diff changeset
167 volatile bool SolarisAttachListener::_has_door_path;
a61af66fc99e Initial load
duke
parents:
diff changeset
168 int SolarisAttachListener::_door_descriptor = -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
169 mutex_t SolarisAttachListener::_mutex;
a61af66fc99e Initial load
duke
parents:
diff changeset
170 sema_t SolarisAttachListener::_wakeup;
a61af66fc99e Initial load
duke
parents:
diff changeset
171 SolarisAttachOperation* SolarisAttachListener::_head = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
172 SolarisAttachOperation* SolarisAttachListener::_tail = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
173
a61af66fc99e Initial load
duke
parents:
diff changeset
174 // Supporting class to help split a buffer into individual components
a61af66fc99e Initial load
duke
parents:
diff changeset
175 class ArgumentIterator : public StackObj {
a61af66fc99e Initial load
duke
parents:
diff changeset
176 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
177 char* _pos;
a61af66fc99e Initial load
duke
parents:
diff changeset
178 char* _end;
a61af66fc99e Initial load
duke
parents:
diff changeset
179 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
180 ArgumentIterator(char* arg_buffer, size_t arg_size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
181 _pos = arg_buffer;
a61af66fc99e Initial load
duke
parents:
diff changeset
182 _end = _pos + arg_size - 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
183 }
a61af66fc99e Initial load
duke
parents:
diff changeset
184 char* next() {
a61af66fc99e Initial load
duke
parents:
diff changeset
185 if (*_pos == '\0') {
a61af66fc99e Initial load
duke
parents:
diff changeset
186 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
187 }
a61af66fc99e Initial load
duke
parents:
diff changeset
188 char* res = _pos;
a61af66fc99e Initial load
duke
parents:
diff changeset
189 char* next_pos = strchr(_pos, '\0');
a61af66fc99e Initial load
duke
parents:
diff changeset
190 if (next_pos < _end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
191 next_pos++;
a61af66fc99e Initial load
duke
parents:
diff changeset
192 }
a61af66fc99e Initial load
duke
parents:
diff changeset
193 _pos = next_pos;
a61af66fc99e Initial load
duke
parents:
diff changeset
194 return res;
a61af66fc99e Initial load
duke
parents:
diff changeset
195 }
a61af66fc99e Initial load
duke
parents:
diff changeset
196 };
a61af66fc99e Initial load
duke
parents:
diff changeset
197
a61af66fc99e Initial load
duke
parents:
diff changeset
198 // Calls from the door function to check that the client credentials
a61af66fc99e Initial load
duke
parents:
diff changeset
199 // match this process. Returns 0 if credentials okay, otherwise -1.
a61af66fc99e Initial load
duke
parents:
diff changeset
200 static int check_credentials() {
a61af66fc99e Initial load
duke
parents:
diff changeset
201 door_cred_t cred_info;
a61af66fc99e Initial load
duke
parents:
diff changeset
202
a61af66fc99e Initial load
duke
parents:
diff changeset
203 // get client credentials
a61af66fc99e Initial load
duke
parents:
diff changeset
204 if (door_cred(&cred_info) == -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
205 return -1; // unable to get them
a61af66fc99e Initial load
duke
parents:
diff changeset
206 }
a61af66fc99e Initial load
duke
parents:
diff changeset
207
a61af66fc99e Initial load
duke
parents:
diff changeset
208 // get our euid/eguid (probably could cache these)
a61af66fc99e Initial load
duke
parents:
diff changeset
209 uid_t euid = geteuid();
a61af66fc99e Initial load
duke
parents:
diff changeset
210 gid_t egid = getegid();
a61af66fc99e Initial load
duke
parents:
diff changeset
211
a61af66fc99e Initial load
duke
parents:
diff changeset
212 // check that the effective uid/gid matches - discuss this with Jeff.
a61af66fc99e Initial load
duke
parents:
diff changeset
213 if (cred_info.dc_euid == euid && cred_info.dc_egid == egid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
214 return 0; // okay
a61af66fc99e Initial load
duke
parents:
diff changeset
215 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
216 return -1; // denied
a61af66fc99e Initial load
duke
parents:
diff changeset
217 }
a61af66fc99e Initial load
duke
parents:
diff changeset
218 }
a61af66fc99e Initial load
duke
parents:
diff changeset
219
a61af66fc99e Initial load
duke
parents:
diff changeset
220
a61af66fc99e Initial load
duke
parents:
diff changeset
221 // Parses the argument buffer to create an AttachOperation that we should
a61af66fc99e Initial load
duke
parents:
diff changeset
222 // enqueue to the attach listener.
a61af66fc99e Initial load
duke
parents:
diff changeset
223 // The buffer is expected to be formatted as follows:
a61af66fc99e Initial load
duke
parents:
diff changeset
224 // <ver>0<cmd>0<arg>0<arg>0<arg>0
a61af66fc99e Initial load
duke
parents:
diff changeset
225 // where <ver> is the version number (must be "1"), <cmd> is the command
a61af66fc99e Initial load
duke
parents:
diff changeset
226 // name ("load, "datadump", ...) and <arg> is an argument.
a61af66fc99e Initial load
duke
parents:
diff changeset
227 //
a61af66fc99e Initial load
duke
parents:
diff changeset
228 static SolarisAttachOperation* create_operation(char* argp, size_t arg_size, int* err) {
a61af66fc99e Initial load
duke
parents:
diff changeset
229 // assume bad request until parsed
a61af66fc99e Initial load
duke
parents:
diff changeset
230 *err = SolarisAttachListener::ATTACH_ERROR_BADREQUEST;
a61af66fc99e Initial load
duke
parents:
diff changeset
231
a61af66fc99e Initial load
duke
parents:
diff changeset
232 if (arg_size < 2 || argp[arg_size-1] != '\0') {
a61af66fc99e Initial load
duke
parents:
diff changeset
233 return NULL; // no ver or not null terminated
a61af66fc99e Initial load
duke
parents:
diff changeset
234 }
a61af66fc99e Initial load
duke
parents:
diff changeset
235
a61af66fc99e Initial load
duke
parents:
diff changeset
236 // Use supporting class to iterate over the buffer
a61af66fc99e Initial load
duke
parents:
diff changeset
237 ArgumentIterator args(argp, arg_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
238
a61af66fc99e Initial load
duke
parents:
diff changeset
239 // First check the protocol version
a61af66fc99e Initial load
duke
parents:
diff changeset
240 char* ver = args.next();
a61af66fc99e Initial load
duke
parents:
diff changeset
241 if (ver == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
242 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
243 }
a61af66fc99e Initial load
duke
parents:
diff changeset
244 if (atoi(ver) != SolarisAttachListener::ATTACH_PROTOCOL_VER) {
a61af66fc99e Initial load
duke
parents:
diff changeset
245 *err = SolarisAttachListener::ATTACH_ERROR_BADVERSION;
a61af66fc99e Initial load
duke
parents:
diff changeset
246 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
247 }
a61af66fc99e Initial load
duke
parents:
diff changeset
248
a61af66fc99e Initial load
duke
parents:
diff changeset
249 // Get command name and create the operation
a61af66fc99e Initial load
duke
parents:
diff changeset
250 char* name = args.next();
a61af66fc99e Initial load
duke
parents:
diff changeset
251 if (name == NULL || strlen(name) > AttachOperation::name_length_max) {
a61af66fc99e Initial load
duke
parents:
diff changeset
252 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
253 }
a61af66fc99e Initial load
duke
parents:
diff changeset
254 SolarisAttachOperation* op = new SolarisAttachOperation(name);
a61af66fc99e Initial load
duke
parents:
diff changeset
255
a61af66fc99e Initial load
duke
parents:
diff changeset
256 // Iterate over the arguments
a61af66fc99e Initial load
duke
parents:
diff changeset
257 for (int i=0; i<AttachOperation::arg_count_max; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
258 char* arg = args.next();
a61af66fc99e Initial load
duke
parents:
diff changeset
259 if (arg == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
260 op->set_arg(i, NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
261 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
262 if (strlen(arg) > AttachOperation::arg_length_max) {
a61af66fc99e Initial load
duke
parents:
diff changeset
263 delete op;
a61af66fc99e Initial load
duke
parents:
diff changeset
264 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
265 }
a61af66fc99e Initial load
duke
parents:
diff changeset
266 op->set_arg(i, arg);
a61af66fc99e Initial load
duke
parents:
diff changeset
267 }
a61af66fc99e Initial load
duke
parents:
diff changeset
268 }
a61af66fc99e Initial load
duke
parents:
diff changeset
269
a61af66fc99e Initial load
duke
parents:
diff changeset
270 // return operation
a61af66fc99e Initial load
duke
parents:
diff changeset
271 *err = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
272 return op;
a61af66fc99e Initial load
duke
parents:
diff changeset
273 }
a61af66fc99e Initial load
duke
parents:
diff changeset
274
a61af66fc99e Initial load
duke
parents:
diff changeset
275 // create special operation to indicate all clients have detached
a61af66fc99e Initial load
duke
parents:
diff changeset
276 static SolarisAttachOperation* create_detachall_operation() {
a61af66fc99e Initial load
duke
parents:
diff changeset
277 return new SolarisAttachOperation(AttachOperation::detachall_operation_name());
a61af66fc99e Initial load
duke
parents:
diff changeset
278 }
a61af66fc99e Initial load
duke
parents:
diff changeset
279
a61af66fc99e Initial load
duke
parents:
diff changeset
280 // This is door function which the client executes via a door_call.
a61af66fc99e Initial load
duke
parents:
diff changeset
281 extern "C" {
a61af66fc99e Initial load
duke
parents:
diff changeset
282 static void enqueue_proc(void* cookie, char* argp, size_t arg_size,
a61af66fc99e Initial load
duke
parents:
diff changeset
283 door_desc_t* dt, uint_t n_desc)
a61af66fc99e Initial load
duke
parents:
diff changeset
284 {
a61af66fc99e Initial load
duke
parents:
diff changeset
285 int return_fd = -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
286 SolarisAttachOperation* op = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
287
a61af66fc99e Initial load
duke
parents:
diff changeset
288 // no listener
a61af66fc99e Initial load
duke
parents:
diff changeset
289 jint res = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
290 if (!AttachListener::is_initialized()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
291 // how did we get here?
a61af66fc99e Initial load
duke
parents:
diff changeset
292 debug_only(warning("door_call when not enabled"));
a61af66fc99e Initial load
duke
parents:
diff changeset
293 res = (jint)SolarisAttachListener::ATTACH_ERROR_INTERNAL;
a61af66fc99e Initial load
duke
parents:
diff changeset
294 }
a61af66fc99e Initial load
duke
parents:
diff changeset
295
a61af66fc99e Initial load
duke
parents:
diff changeset
296 // check client credentials
a61af66fc99e Initial load
duke
parents:
diff changeset
297 if (res == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
298 if (check_credentials() != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
299 res = (jint)SolarisAttachListener::ATTACH_ERROR_DENIED;
a61af66fc99e Initial load
duke
parents:
diff changeset
300 }
a61af66fc99e Initial load
duke
parents:
diff changeset
301 }
a61af66fc99e Initial load
duke
parents:
diff changeset
302
a61af66fc99e Initial load
duke
parents:
diff changeset
303 // if we are stopped at ShowMessageBoxOnError then maybe we can
a61af66fc99e Initial load
duke
parents:
diff changeset
304 // load a diagnostic library
a61af66fc99e Initial load
duke
parents:
diff changeset
305 if (res == 0 && is_error_reported()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
306 if (ShowMessageBoxOnError) {
a61af66fc99e Initial load
duke
parents:
diff changeset
307 // TBD - support loading of diagnostic library here
a61af66fc99e Initial load
duke
parents:
diff changeset
308 }
a61af66fc99e Initial load
duke
parents:
diff changeset
309
a61af66fc99e Initial load
duke
parents:
diff changeset
310 // can't enqueue operation after fatal error
a61af66fc99e Initial load
duke
parents:
diff changeset
311 res = (jint)SolarisAttachListener::ATTACH_ERROR_RESOURCE;
a61af66fc99e Initial load
duke
parents:
diff changeset
312 }
a61af66fc99e Initial load
duke
parents:
diff changeset
313
a61af66fc99e Initial load
duke
parents:
diff changeset
314 // create the operation
a61af66fc99e Initial load
duke
parents:
diff changeset
315 if (res == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
316 int err;
a61af66fc99e Initial load
duke
parents:
diff changeset
317 op = create_operation(argp, arg_size, &err);
a61af66fc99e Initial load
duke
parents:
diff changeset
318 res = (op == NULL) ? (jint)err : 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
319 }
a61af66fc99e Initial load
duke
parents:
diff changeset
320
a61af66fc99e Initial load
duke
parents:
diff changeset
321 // create a pair of connected sockets. Store the file descriptor
a61af66fc99e Initial load
duke
parents:
diff changeset
322 // for one end in the operation and enqueue the operation. The
a61af66fc99e Initial load
duke
parents:
diff changeset
323 // file descriptor for the other end wil be returned to the client.
a61af66fc99e Initial load
duke
parents:
diff changeset
324 if (res == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
325 int s[2];
a61af66fc99e Initial load
duke
parents:
diff changeset
326 if (socketpair(PF_UNIX, SOCK_STREAM, 0, s) < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
327 delete op;
a61af66fc99e Initial load
duke
parents:
diff changeset
328 res = (jint)SolarisAttachListener::ATTACH_ERROR_RESOURCE;
a61af66fc99e Initial load
duke
parents:
diff changeset
329 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
330 op->set_socket(s[0]);
a61af66fc99e Initial load
duke
parents:
diff changeset
331 return_fd = s[1];
a61af66fc99e Initial load
duke
parents:
diff changeset
332 SolarisAttachListener::enqueue(op);
a61af66fc99e Initial load
duke
parents:
diff changeset
333 }
a61af66fc99e Initial load
duke
parents:
diff changeset
334 }
a61af66fc99e Initial load
duke
parents:
diff changeset
335
a61af66fc99e Initial load
duke
parents:
diff changeset
336 // Return 0 (success) + file descriptor, or non-0 (error)
a61af66fc99e Initial load
duke
parents:
diff changeset
337 if (res == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
338 door_desc_t desc;
6024
973046802b6f 7162955: Attach api on Solaris, too many open files
dlong
parents: 1972
diff changeset
339 // DOOR_RELEASE flag makes sure fd is closed after passing it to
973046802b6f 7162955: Attach api on Solaris, too many open files
dlong
parents: 1972
diff changeset
340 // the client. See door_return(3DOOR) man page.
973046802b6f 7162955: Attach api on Solaris, too many open files
dlong
parents: 1972
diff changeset
341 desc.d_attributes = DOOR_DESCRIPTOR | DOOR_RELEASE;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
342 desc.d_data.d_desc.d_descriptor = return_fd;
a61af66fc99e Initial load
duke
parents:
diff changeset
343 door_return((char*)&res, sizeof(res), &desc, 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
344 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
345 door_return((char*)&res, sizeof(res), NULL, 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
346 }
a61af66fc99e Initial load
duke
parents:
diff changeset
347 }
a61af66fc99e Initial load
duke
parents:
diff changeset
348 }
a61af66fc99e Initial load
duke
parents:
diff changeset
349
a61af66fc99e Initial load
duke
parents:
diff changeset
350 // atexit hook to detach the door and remove the file
a61af66fc99e Initial load
duke
parents:
diff changeset
351 extern "C" {
a61af66fc99e Initial load
duke
parents:
diff changeset
352 static void listener_cleanup() {
a61af66fc99e Initial load
duke
parents:
diff changeset
353 static int cleanup_done;
a61af66fc99e Initial load
duke
parents:
diff changeset
354 if (!cleanup_done) {
a61af66fc99e Initial load
duke
parents:
diff changeset
355 cleanup_done = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
356 int dd = SolarisAttachListener::door_descriptor();
a61af66fc99e Initial load
duke
parents:
diff changeset
357 if (dd >= 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
358 ::close(dd);
a61af66fc99e Initial load
duke
parents:
diff changeset
359 }
a61af66fc99e Initial load
duke
parents:
diff changeset
360 if (SolarisAttachListener::has_door_path()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
361 char* path = SolarisAttachListener::door_path();
a61af66fc99e Initial load
duke
parents:
diff changeset
362 ::fdetach(path);
a61af66fc99e Initial load
duke
parents:
diff changeset
363 ::unlink(path);
a61af66fc99e Initial load
duke
parents:
diff changeset
364 }
a61af66fc99e Initial load
duke
parents:
diff changeset
365 }
a61af66fc99e Initial load
duke
parents:
diff changeset
366 }
a61af66fc99e Initial load
duke
parents:
diff changeset
367 }
a61af66fc99e Initial load
duke
parents:
diff changeset
368
a61af66fc99e Initial load
duke
parents:
diff changeset
369 // Create the door
a61af66fc99e Initial load
duke
parents:
diff changeset
370 int SolarisAttachListener::create_door() {
a61af66fc99e Initial load
duke
parents:
diff changeset
371 char door_path[PATH_MAX+1];
1675
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
372 char initial_path[PATH_MAX+1];
0
a61af66fc99e Initial load
duke
parents:
diff changeset
373 int fd, res;
a61af66fc99e Initial load
duke
parents:
diff changeset
374
a61af66fc99e Initial load
duke
parents:
diff changeset
375 // register exit function
a61af66fc99e Initial load
duke
parents:
diff changeset
376 ::atexit(listener_cleanup);
a61af66fc99e Initial load
duke
parents:
diff changeset
377
a61af66fc99e Initial load
duke
parents:
diff changeset
378 // create the door descriptor
a61af66fc99e Initial load
duke
parents:
diff changeset
379 int dd = ::door_create(enqueue_proc, NULL, 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
380 if (dd < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
381 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
382 }
a61af66fc99e Initial load
duke
parents:
diff changeset
383
1675
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
384 // create initial file to attach door descriptor
1353
a2ea687fdc7c 6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents: 1324
diff changeset
385 snprintf(door_path, sizeof(door_path), "%s/.java_pid%d",
a2ea687fdc7c 6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents: 1324
diff changeset
386 os::get_temp_directory(), os::current_process_id());
1675
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
387 snprintf(initial_path, sizeof(initial_path), "%s.tmp", door_path);
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
388 RESTARTABLE(::creat(initial_path, S_IRUSR | S_IWUSR), fd);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
389 if (fd == -1) {
1675
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
390 debug_only(warning("attempt to create %s failed", initial_path));
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
391 ::door_revoke(dd);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
392 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
393 }
a61af66fc99e Initial load
duke
parents:
diff changeset
394 assert(fd >= 0, "bad file descriptor");
a61af66fc99e Initial load
duke
parents:
diff changeset
395 RESTARTABLE(::close(fd), res);
a61af66fc99e Initial load
duke
parents:
diff changeset
396
a61af66fc99e Initial load
duke
parents:
diff changeset
397 // attach the door descriptor to the file
1675
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
398 if ((res = ::fattach(dd, initial_path)) == -1) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
399 // if busy then detach and try again
a61af66fc99e Initial load
duke
parents:
diff changeset
400 if (errno == EBUSY) {
1675
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
401 ::fdetach(initial_path);
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
402 res = ::fattach(dd, initial_path);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
403 }
a61af66fc99e Initial load
duke
parents:
diff changeset
404 if (res == -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
405 ::door_revoke(dd);
a61af66fc99e Initial load
duke
parents:
diff changeset
406 dd = -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
407 }
a61af66fc99e Initial load
duke
parents:
diff changeset
408 }
1675
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
409
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
410 // rename file so that clients can attach
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
411 if (dd >= 0) {
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
412 if (::rename(initial_path, door_path) == -1) {
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
413 RESTARTABLE(::close(dd), res);
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
414 ::fdetach(initial_path);
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
415 dd = -1;
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
416 }
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
417 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
418 if (dd >= 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
419 set_door_descriptor(dd);
1675
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
420 set_door_path(door_path);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
421 } else {
1675
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
422 // unable to create door, attach it to file, or rename file into place
a81afd9c293c 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 1552
diff changeset
423 ::unlink(initial_path);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
424 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
425 }
a61af66fc99e Initial load
duke
parents:
diff changeset
426
a61af66fc99e Initial load
duke
parents:
diff changeset
427 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
428 }
a61af66fc99e Initial load
duke
parents:
diff changeset
429
a61af66fc99e Initial load
duke
parents:
diff changeset
430 // Initialization - create the door, locks, and other initialization
a61af66fc99e Initial load
duke
parents:
diff changeset
431 int SolarisAttachListener::init() {
a61af66fc99e Initial load
duke
parents:
diff changeset
432 if (create_door()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
433 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
434 }
a61af66fc99e Initial load
duke
parents:
diff changeset
435
a61af66fc99e Initial load
duke
parents:
diff changeset
436 int status = os::Solaris::mutex_init(&_mutex);
a61af66fc99e Initial load
duke
parents:
diff changeset
437 assert_status(status==0, status, "mutex_init");
a61af66fc99e Initial load
duke
parents:
diff changeset
438
a61af66fc99e Initial load
duke
parents:
diff changeset
439 status = ::sema_init(&_wakeup, 0, NULL, NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
440 assert_status(status==0, status, "sema_init");
a61af66fc99e Initial load
duke
parents:
diff changeset
441
a61af66fc99e Initial load
duke
parents:
diff changeset
442 set_head(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
443 set_tail(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
444
a61af66fc99e Initial load
duke
parents:
diff changeset
445 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
446 }
a61af66fc99e Initial load
duke
parents:
diff changeset
447
a61af66fc99e Initial load
duke
parents:
diff changeset
448 // Dequeue an operation
a61af66fc99e Initial load
duke
parents:
diff changeset
449 SolarisAttachOperation* SolarisAttachListener::dequeue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
450 for (;;) {
a61af66fc99e Initial load
duke
parents:
diff changeset
451 int res;
a61af66fc99e Initial load
duke
parents:
diff changeset
452
a61af66fc99e Initial load
duke
parents:
diff changeset
453 // wait for somebody to enqueue something
a61af66fc99e Initial load
duke
parents:
diff changeset
454 while ((res = ::sema_wait(wakeup())) == EINTR)
a61af66fc99e Initial load
duke
parents:
diff changeset
455 ;
a61af66fc99e Initial load
duke
parents:
diff changeset
456 if (res) {
a61af66fc99e Initial load
duke
parents:
diff changeset
457 warning("sema_wait failed: %s", strerror(res));
a61af66fc99e Initial load
duke
parents:
diff changeset
458 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
459 }
a61af66fc99e Initial load
duke
parents:
diff changeset
460
a61af66fc99e Initial load
duke
parents:
diff changeset
461 // lock the list
a61af66fc99e Initial load
duke
parents:
diff changeset
462 res = os::Solaris::mutex_lock(mutex());
a61af66fc99e Initial load
duke
parents:
diff changeset
463 assert(res == 0, "mutex_lock failed");
a61af66fc99e Initial load
duke
parents:
diff changeset
464
a61af66fc99e Initial load
duke
parents:
diff changeset
465 // remove the head of the list
a61af66fc99e Initial load
duke
parents:
diff changeset
466 SolarisAttachOperation* op = head();
a61af66fc99e Initial load
duke
parents:
diff changeset
467 if (op != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
468 set_head(op->next());
a61af66fc99e Initial load
duke
parents:
diff changeset
469 if (head() == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
470 set_tail(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
471 }
a61af66fc99e Initial load
duke
parents:
diff changeset
472 }
a61af66fc99e Initial load
duke
parents:
diff changeset
473
a61af66fc99e Initial load
duke
parents:
diff changeset
474 // unlock
a61af66fc99e Initial load
duke
parents:
diff changeset
475 os::Solaris::mutex_unlock(mutex());
a61af66fc99e Initial load
duke
parents:
diff changeset
476
a61af66fc99e Initial load
duke
parents:
diff changeset
477 // if we got an operation when return it.
a61af66fc99e Initial load
duke
parents:
diff changeset
478 if (op != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
479 return op;
a61af66fc99e Initial load
duke
parents:
diff changeset
480 }
a61af66fc99e Initial load
duke
parents:
diff changeset
481 }
a61af66fc99e Initial load
duke
parents:
diff changeset
482 }
a61af66fc99e Initial load
duke
parents:
diff changeset
483
a61af66fc99e Initial load
duke
parents:
diff changeset
484 // Enqueue an operation
a61af66fc99e Initial load
duke
parents:
diff changeset
485 void SolarisAttachListener::enqueue(SolarisAttachOperation* op) {
a61af66fc99e Initial load
duke
parents:
diff changeset
486 // lock list
a61af66fc99e Initial load
duke
parents:
diff changeset
487 int res = os::Solaris::mutex_lock(mutex());
a61af66fc99e Initial load
duke
parents:
diff changeset
488 assert(res == 0, "mutex_lock failed");
a61af66fc99e Initial load
duke
parents:
diff changeset
489
a61af66fc99e Initial load
duke
parents:
diff changeset
490 // enqueue at tail
a61af66fc99e Initial load
duke
parents:
diff changeset
491 op->set_next(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
492 if (head() == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
493 set_head(op);
a61af66fc99e Initial load
duke
parents:
diff changeset
494 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
495 tail()->set_next(op);
a61af66fc99e Initial load
duke
parents:
diff changeset
496 }
a61af66fc99e Initial load
duke
parents:
diff changeset
497 set_tail(op);
a61af66fc99e Initial load
duke
parents:
diff changeset
498
a61af66fc99e Initial load
duke
parents:
diff changeset
499 // wakeup the attach listener
a61af66fc99e Initial load
duke
parents:
diff changeset
500 RESTARTABLE(::sema_post(wakeup()), res);
a61af66fc99e Initial load
duke
parents:
diff changeset
501 assert(res == 0, "sema_post failed");
a61af66fc99e Initial load
duke
parents:
diff changeset
502
a61af66fc99e Initial load
duke
parents:
diff changeset
503 // unlock
a61af66fc99e Initial load
duke
parents:
diff changeset
504 os::Solaris::mutex_unlock(mutex());
a61af66fc99e Initial load
duke
parents:
diff changeset
505 }
a61af66fc99e Initial load
duke
parents:
diff changeset
506
a61af66fc99e Initial load
duke
parents:
diff changeset
507
a61af66fc99e Initial load
duke
parents:
diff changeset
508 // support function - writes the (entire) buffer to a socket
a61af66fc99e Initial load
duke
parents:
diff changeset
509 static int write_fully(int s, char* buf, int len) {
a61af66fc99e Initial load
duke
parents:
diff changeset
510 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
511 int n = ::write(s, buf, len);
a61af66fc99e Initial load
duke
parents:
diff changeset
512 if (n == -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
513 if (errno != EINTR) return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
514 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
515 buf += n;
a61af66fc99e Initial load
duke
parents:
diff changeset
516 len -= n;
a61af66fc99e Initial load
duke
parents:
diff changeset
517 }
a61af66fc99e Initial load
duke
parents:
diff changeset
518 }
a61af66fc99e Initial load
duke
parents:
diff changeset
519 while (len > 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
520 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
521 }
a61af66fc99e Initial load
duke
parents:
diff changeset
522
a61af66fc99e Initial load
duke
parents:
diff changeset
523 // Complete an operation by sending the operation result and any result
a61af66fc99e Initial load
duke
parents:
diff changeset
524 // output to the client. At this time the socket is in blocking mode so
a61af66fc99e Initial load
duke
parents:
diff changeset
525 // potentially we can block if there is a lot of data and the client is
a61af66fc99e Initial load
duke
parents:
diff changeset
526 // non-responsive. For most operations this is a non-issue because the
a61af66fc99e Initial load
duke
parents:
diff changeset
527 // default send buffer is sufficient to buffer everything. In the future
a61af66fc99e Initial load
duke
parents:
diff changeset
528 // if there are operations that involves a very big reply then it the
a61af66fc99e Initial load
duke
parents:
diff changeset
529 // socket could be made non-blocking and a timeout could be used.
a61af66fc99e Initial load
duke
parents:
diff changeset
530
a61af66fc99e Initial load
duke
parents:
diff changeset
531 void SolarisAttachOperation::complete(jint res, bufferedStream* st) {
a61af66fc99e Initial load
duke
parents:
diff changeset
532 if (this->socket() >= 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
533 JavaThread* thread = JavaThread::current();
a61af66fc99e Initial load
duke
parents:
diff changeset
534 ThreadBlockInVM tbivm(thread);
a61af66fc99e Initial load
duke
parents:
diff changeset
535
a61af66fc99e Initial load
duke
parents:
diff changeset
536 thread->set_suspend_equivalent();
a61af66fc99e Initial load
duke
parents:
diff changeset
537 // cleared by handle_special_suspend_equivalent_condition() or
a61af66fc99e Initial load
duke
parents:
diff changeset
538 // java_suspend_self() via check_and_wait_while_suspended()
a61af66fc99e Initial load
duke
parents:
diff changeset
539
a61af66fc99e Initial load
duke
parents:
diff changeset
540 // write operation result
a61af66fc99e Initial load
duke
parents:
diff changeset
541 char msg[32];
a61af66fc99e Initial load
duke
parents:
diff changeset
542 sprintf(msg, "%d\n", res);
a61af66fc99e Initial load
duke
parents:
diff changeset
543 int rc = write_fully(this->socket(), msg, strlen(msg));
a61af66fc99e Initial load
duke
parents:
diff changeset
544
a61af66fc99e Initial load
duke
parents:
diff changeset
545 // write any result data
a61af66fc99e Initial load
duke
parents:
diff changeset
546 if (rc == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
547 write_fully(this->socket(), (char*) st->base(), st->size());
a61af66fc99e Initial load
duke
parents:
diff changeset
548 ::shutdown(this->socket(), 2);
a61af66fc99e Initial load
duke
parents:
diff changeset
549 }
a61af66fc99e Initial load
duke
parents:
diff changeset
550
a61af66fc99e Initial load
duke
parents:
diff changeset
551 // close socket and we're done
a61af66fc99e Initial load
duke
parents:
diff changeset
552 RESTARTABLE(::close(this->socket()), rc);
a61af66fc99e Initial load
duke
parents:
diff changeset
553
a61af66fc99e Initial load
duke
parents:
diff changeset
554 // were we externally suspended while we were waiting?
a61af66fc99e Initial load
duke
parents:
diff changeset
555 thread->check_and_wait_while_suspended();
a61af66fc99e Initial load
duke
parents:
diff changeset
556 }
a61af66fc99e Initial load
duke
parents:
diff changeset
557 delete this;
a61af66fc99e Initial load
duke
parents:
diff changeset
558 }
a61af66fc99e Initial load
duke
parents:
diff changeset
559
a61af66fc99e Initial load
duke
parents:
diff changeset
560
a61af66fc99e Initial load
duke
parents:
diff changeset
561 // AttachListener functions
a61af66fc99e Initial load
duke
parents:
diff changeset
562
a61af66fc99e Initial load
duke
parents:
diff changeset
563 AttachOperation* AttachListener::dequeue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
564 JavaThread* thread = JavaThread::current();
a61af66fc99e Initial load
duke
parents:
diff changeset
565 ThreadBlockInVM tbivm(thread);
a61af66fc99e Initial load
duke
parents:
diff changeset
566
a61af66fc99e Initial load
duke
parents:
diff changeset
567 thread->set_suspend_equivalent();
a61af66fc99e Initial load
duke
parents:
diff changeset
568 // cleared by handle_special_suspend_equivalent_condition() or
a61af66fc99e Initial load
duke
parents:
diff changeset
569 // java_suspend_self() via check_and_wait_while_suspended()
a61af66fc99e Initial load
duke
parents:
diff changeset
570
a61af66fc99e Initial load
duke
parents:
diff changeset
571 AttachOperation* op = SolarisAttachListener::dequeue();
a61af66fc99e Initial load
duke
parents:
diff changeset
572
a61af66fc99e Initial load
duke
parents:
diff changeset
573 // were we externally suspended while we were waiting?
a61af66fc99e Initial load
duke
parents:
diff changeset
574 thread->check_and_wait_while_suspended();
a61af66fc99e Initial load
duke
parents:
diff changeset
575
a61af66fc99e Initial load
duke
parents:
diff changeset
576 return op;
a61af66fc99e Initial load
duke
parents:
diff changeset
577 }
a61af66fc99e Initial load
duke
parents:
diff changeset
578
a61af66fc99e Initial load
duke
parents:
diff changeset
579 int AttachListener::pd_init() {
a61af66fc99e Initial load
duke
parents:
diff changeset
580 JavaThread* thread = JavaThread::current();
a61af66fc99e Initial load
duke
parents:
diff changeset
581 ThreadBlockInVM tbivm(thread);
a61af66fc99e Initial load
duke
parents:
diff changeset
582
a61af66fc99e Initial load
duke
parents:
diff changeset
583 thread->set_suspend_equivalent();
a61af66fc99e Initial load
duke
parents:
diff changeset
584 // cleared by handle_special_suspend_equivalent_condition() or
a61af66fc99e Initial load
duke
parents:
diff changeset
585 // java_suspend_self()
a61af66fc99e Initial load
duke
parents:
diff changeset
586
a61af66fc99e Initial load
duke
parents:
diff changeset
587 int ret_code = SolarisAttachListener::init();
a61af66fc99e Initial load
duke
parents:
diff changeset
588
a61af66fc99e Initial load
duke
parents:
diff changeset
589 // were we externally suspended while we were waiting?
a61af66fc99e Initial load
duke
parents:
diff changeset
590 thread->check_and_wait_while_suspended();
a61af66fc99e Initial load
duke
parents:
diff changeset
591
a61af66fc99e Initial load
duke
parents:
diff changeset
592 return ret_code;
a61af66fc99e Initial load
duke
parents:
diff changeset
593 }
a61af66fc99e Initial load
duke
parents:
diff changeset
594
a61af66fc99e Initial load
duke
parents:
diff changeset
595 // Attach Listener is started lazily except in the case when
a61af66fc99e Initial load
duke
parents:
diff changeset
596 // +ReduseSignalUsage is used
a61af66fc99e Initial load
duke
parents:
diff changeset
597 bool AttachListener::init_at_startup() {
a61af66fc99e Initial load
duke
parents:
diff changeset
598 if (ReduceSignalUsage) {
a61af66fc99e Initial load
duke
parents:
diff changeset
599 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
600 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
601 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
602 }
a61af66fc99e Initial load
duke
parents:
diff changeset
603 }
a61af66fc99e Initial load
duke
parents:
diff changeset
604
a61af66fc99e Initial load
duke
parents:
diff changeset
605 // If the file .attach_pid<pid> exists in the working directory
a61af66fc99e Initial load
duke
parents:
diff changeset
606 // or /tmp then this is the trigger to start the attach mechanism
a61af66fc99e Initial load
duke
parents:
diff changeset
607 bool AttachListener::is_init_trigger() {
a61af66fc99e Initial load
duke
parents:
diff changeset
608 if (init_at_startup() || is_initialized()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
609 return false; // initialized at startup or already initialized
a61af66fc99e Initial load
duke
parents:
diff changeset
610 }
1497
96d554193f72 6944822: Fix for 6938627 exposes problem with hard-coded buffer sizes
coleenp
parents: 1353
diff changeset
611 char fn[PATH_MAX+1];
0
a61af66fc99e Initial load
duke
parents:
diff changeset
612 sprintf(fn, ".attach_pid%d", os::current_process_id());
a61af66fc99e Initial load
duke
parents:
diff changeset
613 int ret;
a61af66fc99e Initial load
duke
parents:
diff changeset
614 struct stat64 st;
a61af66fc99e Initial load
duke
parents:
diff changeset
615 RESTARTABLE(::stat64(fn, &st), ret);
a61af66fc99e Initial load
duke
parents:
diff changeset
616 if (ret == -1) {
1353
a2ea687fdc7c 6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents: 1324
diff changeset
617 snprintf(fn, sizeof(fn), "%s/.attach_pid%d",
a2ea687fdc7c 6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents: 1324
diff changeset
618 os::get_temp_directory(), os::current_process_id());
0
a61af66fc99e Initial load
duke
parents:
diff changeset
619 RESTARTABLE(::stat64(fn, &st), ret);
a61af66fc99e Initial load
duke
parents:
diff changeset
620 }
a61af66fc99e Initial load
duke
parents:
diff changeset
621 if (ret == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
622 // simple check to avoid starting the attach mechanism when
a61af66fc99e Initial load
duke
parents:
diff changeset
623 // a bogus user creates the file
a61af66fc99e Initial load
duke
parents:
diff changeset
624 if (st.st_uid == geteuid()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
625 init();
a61af66fc99e Initial load
duke
parents:
diff changeset
626 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
627 }
a61af66fc99e Initial load
duke
parents:
diff changeset
628 }
a61af66fc99e Initial load
duke
parents:
diff changeset
629 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
630 }
a61af66fc99e Initial load
duke
parents:
diff changeset
631
a61af66fc99e Initial load
duke
parents:
diff changeset
632 // if VM aborts then detach/cleanup
a61af66fc99e Initial load
duke
parents:
diff changeset
633 void AttachListener::abort() {
a61af66fc99e Initial load
duke
parents:
diff changeset
634 listener_cleanup();
a61af66fc99e Initial load
duke
parents:
diff changeset
635 }
a61af66fc99e Initial load
duke
parents:
diff changeset
636
a61af66fc99e Initial load
duke
parents:
diff changeset
637 void AttachListener::pd_data_dump() {
a61af66fc99e Initial load
duke
parents:
diff changeset
638 os::signal_notify(SIGQUIT);
a61af66fc99e Initial load
duke
parents:
diff changeset
639 }
a61af66fc99e Initial load
duke
parents:
diff changeset
640
a61af66fc99e Initial load
duke
parents:
diff changeset
641 static jint enable_dprobes(AttachOperation* op, outputStream* out) {
a61af66fc99e Initial load
duke
parents:
diff changeset
642 const char* probe = op->arg(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
643 if (probe == NULL || probe[0] == '\0') {
a61af66fc99e Initial load
duke
parents:
diff changeset
644 out->print_cr("No probe specified");
a61af66fc99e Initial load
duke
parents:
diff changeset
645 return JNI_ERR;
a61af66fc99e Initial load
duke
parents:
diff changeset
646 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
647 int probe_typess = atoi(probe);
a61af66fc99e Initial load
duke
parents:
diff changeset
648 if (errno) {
a61af66fc99e Initial load
duke
parents:
diff changeset
649 out->print_cr("invalid probe type");
a61af66fc99e Initial load
duke
parents:
diff changeset
650 return JNI_ERR;
a61af66fc99e Initial load
duke
parents:
diff changeset
651 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
652 DTrace::enable_dprobes(probe_typess);
a61af66fc99e Initial load
duke
parents:
diff changeset
653 return JNI_OK;
a61af66fc99e Initial load
duke
parents:
diff changeset
654 }
a61af66fc99e Initial load
duke
parents:
diff changeset
655 }
a61af66fc99e Initial load
duke
parents:
diff changeset
656 }
a61af66fc99e Initial load
duke
parents:
diff changeset
657
a61af66fc99e Initial load
duke
parents:
diff changeset
658 // platform specific operations table
a61af66fc99e Initial load
duke
parents:
diff changeset
659 static AttachOperationFunctionInfo funcs[] = {
a61af66fc99e Initial load
duke
parents:
diff changeset
660 { "enabledprobes", enable_dprobes },
a61af66fc99e Initial load
duke
parents:
diff changeset
661 { NULL, NULL }
a61af66fc99e Initial load
duke
parents:
diff changeset
662 };
a61af66fc99e Initial load
duke
parents:
diff changeset
663
a61af66fc99e Initial load
duke
parents:
diff changeset
664 AttachOperationFunctionInfo* AttachListener::pd_find_operation(const char* name) {
a61af66fc99e Initial load
duke
parents:
diff changeset
665 int i;
a61af66fc99e Initial load
duke
parents:
diff changeset
666 for (i = 0; funcs[i].name != NULL; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
667 if (strcmp(funcs[i].name, name) == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
668 return &funcs[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
669 }
a61af66fc99e Initial load
duke
parents:
diff changeset
670 }
a61af66fc99e Initial load
duke
parents:
diff changeset
671 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
672 }
a61af66fc99e Initial load
duke
parents:
diff changeset
673
a61af66fc99e Initial load
duke
parents:
diff changeset
674 // Solaris specific global flag set. Currently, we support only
a61af66fc99e Initial load
duke
parents:
diff changeset
675 // changing ExtendedDTraceProbes flag.
a61af66fc99e Initial load
duke
parents:
diff changeset
676 jint AttachListener::pd_set_flag(AttachOperation* op, outputStream* out) {
a61af66fc99e Initial load
duke
parents:
diff changeset
677 const char* name = op->arg(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
678 assert(name != NULL, "flag name should not be null");
a61af66fc99e Initial load
duke
parents:
diff changeset
679 bool flag = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
680 const char* arg1;
a61af66fc99e Initial load
duke
parents:
diff changeset
681 if ((arg1 = op->arg(1)) != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
682 flag = (atoi(arg1) != 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
683 if (errno) {
a61af66fc99e Initial load
duke
parents:
diff changeset
684 out->print_cr("flag value has to be an integer");
a61af66fc99e Initial load
duke
parents:
diff changeset
685 return JNI_ERR;
a61af66fc99e Initial load
duke
parents:
diff changeset
686 }
a61af66fc99e Initial load
duke
parents:
diff changeset
687 }
a61af66fc99e Initial load
duke
parents:
diff changeset
688
1324
e392695de029 6935224: Adding new DTrace probes to work with Palantir
fparain
parents: 0
diff changeset
689 if (strcmp(name, "ExtendedDTraceProbes") == 0) {
e392695de029 6935224: Adding new DTrace probes to work with Palantir
fparain
parents: 0
diff changeset
690 DTrace::set_extended_dprobes(flag);
e392695de029 6935224: Adding new DTrace probes to work with Palantir
fparain
parents: 0
diff changeset
691 return JNI_OK;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
692 }
a61af66fc99e Initial load
duke
parents:
diff changeset
693
1324
e392695de029 6935224: Adding new DTrace probes to work with Palantir
fparain
parents: 0
diff changeset
694 if (strcmp(name, "DTraceMonitorProbes") == 0) {
e392695de029 6935224: Adding new DTrace probes to work with Palantir
fparain
parents: 0
diff changeset
695 DTrace::set_monitor_dprobes(flag);
e392695de029 6935224: Adding new DTrace probes to work with Palantir
fparain
parents: 0
diff changeset
696 return JNI_OK;
e392695de029 6935224: Adding new DTrace probes to work with Palantir
fparain
parents: 0
diff changeset
697 }
e392695de029 6935224: Adding new DTrace probes to work with Palantir
fparain
parents: 0
diff changeset
698
e392695de029 6935224: Adding new DTrace probes to work with Palantir
fparain
parents: 0
diff changeset
699 out->print_cr("flag '%s' cannot be changed", name);
e392695de029 6935224: Adding new DTrace probes to work with Palantir
fparain
parents: 0
diff changeset
700 return JNI_ERR;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
701 }
a61af66fc99e Initial load
duke
parents:
diff changeset
702
a61af66fc99e Initial load
duke
parents:
diff changeset
703 void AttachListener::pd_detachall() {
a61af66fc99e Initial load
duke
parents:
diff changeset
704 DTrace::detach_all_clients();
a61af66fc99e Initial load
duke
parents:
diff changeset
705 }