1 /*
2 * Copyright (c) 2006, 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 *
210 print_debug("created attach file %s\n", path);
211 }
212 return fd;
213 }
214
215 /* delete attach file for given process */
216 static void delete_attach_file(pid_t pid) {
217 char path[PATH_MAX + 1];
218 fill_attach_file_name(path, sizeof(path), pid);
219 int res = unlink(path);
220 if (res) {
221 print_debug("cannot delete attach file %s\n", path);
222 } else {
223 print_debug("deleted attach file %s\n", path);
224 }
225 }
226
227 /* attach to given JVM */
228 jvm_t* jvm_attach(pid_t pid) {
229 jvm_t* jvm;
230 int door_fd, attach_fd, i;
231
232 jvm = (jvm_t*) calloc(1, sizeof(jvm_t));
233 if (jvm == NULL) {
234 set_jvm_error(JVM_ERR_OUT_OF_MEMORY);
235 print_debug("calloc failed in %s at %d\n", __FILE__, __LINE__);
236 return NULL;
237 }
238 jvm->pid = pid;
239 attach_fd = -1;
240
241 door_fd = open_door(pid);
242 if (door_fd < 0) {
243 print_debug("trying to create attach file\n");
244 if ((attach_fd = create_attach_file(pid)) < 0) {
245 goto quit;
246 }
247
248 /* send QUIT signal to the target so that it will
249 * check for the attach file.
250 */
275 if (door_fd >= 0) {
276 jvm->door_fd = door_fd;
277 clear_jvm_error();
278 } else {
279 free(jvm);
280 jvm = NULL;
281 }
282 return jvm;
283 }
284
285 /* return the last thread local error message */
286 const char* jvm_get_last_error() {
287 const char* res = NULL;
288 thr_getspecific(jvm_error_key, (void**)&res);
289 return res;
290 }
291
292 /* detach the givenb JVM */
293 int jvm_detach(jvm_t* jvm) {
294 if (jvm) {
295 int res;
296 if (jvm->door_fd != -1) {
297 if (file_close(jvm->door_fd) != 0) {
298 set_jvm_error(JVM_ERR_CANT_CLOSE_DOOR);
299 res = -1;
300 } else {
301 clear_jvm_error();
302 res = 0;
303 }
304 }
305 free(jvm);
306 return res;
307 } else {
308 set_jvm_error(JVM_ERR_NULL_PARAM);
309 print_debug("jvm_t* is NULL\n");
310 return -1;
311 }
312 }
313
314 /*
315 * A simple table to translate some known errors into reasonable
316 * error messages
317 */
318 static struct {
319 int err;
320 const char* msg;
321 } const error_messages[] = {
322 { 100, "Bad request" },
|
1 /*
2 * Copyright (c) 2006, 2015, 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 *
210 print_debug("created attach file %s\n", path);
211 }
212 return fd;
213 }
214
215 /* delete attach file for given process */
216 static void delete_attach_file(pid_t pid) {
217 char path[PATH_MAX + 1];
218 fill_attach_file_name(path, sizeof(path), pid);
219 int res = unlink(path);
220 if (res) {
221 print_debug("cannot delete attach file %s\n", path);
222 } else {
223 print_debug("deleted attach file %s\n", path);
224 }
225 }
226
227 /* attach to given JVM */
228 jvm_t* jvm_attach(pid_t pid) {
229 jvm_t* jvm;
230 int door_fd, attach_fd, i = 0;
231
232 jvm = (jvm_t*) calloc(1, sizeof(jvm_t));
233 if (jvm == NULL) {
234 set_jvm_error(JVM_ERR_OUT_OF_MEMORY);
235 print_debug("calloc failed in %s at %d\n", __FILE__, __LINE__);
236 return NULL;
237 }
238 jvm->pid = pid;
239 attach_fd = -1;
240
241 door_fd = open_door(pid);
242 if (door_fd < 0) {
243 print_debug("trying to create attach file\n");
244 if ((attach_fd = create_attach_file(pid)) < 0) {
245 goto quit;
246 }
247
248 /* send QUIT signal to the target so that it will
249 * check for the attach file.
250 */
275 if (door_fd >= 0) {
276 jvm->door_fd = door_fd;
277 clear_jvm_error();
278 } else {
279 free(jvm);
280 jvm = NULL;
281 }
282 return jvm;
283 }
284
285 /* return the last thread local error message */
286 const char* jvm_get_last_error() {
287 const char* res = NULL;
288 thr_getspecific(jvm_error_key, (void**)&res);
289 return res;
290 }
291
292 /* detach the givenb JVM */
293 int jvm_detach(jvm_t* jvm) {
294 if (jvm) {
295 int res = 0;
296 if (jvm->door_fd != -1) {
297 if (file_close(jvm->door_fd) != 0) {
298 set_jvm_error(JVM_ERR_CANT_CLOSE_DOOR);
299 res = -1;
300 } else {
301 clear_jvm_error();
302 }
303 }
304 free(jvm);
305 return res;
306 } else {
307 set_jvm_error(JVM_ERR_NULL_PARAM);
308 print_debug("jvm_t* is NULL\n");
309 return -1;
310 }
311 }
312
313 /*
314 * A simple table to translate some known errors into reasonable
315 * error messages
316 */
317 static struct {
318 int err;
319 const char* msg;
320 } const error_messages[] = {
321 { 100, "Bad request" },
|