1 /* nat/Runtime.c - java/lang/Runtime
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5 M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6 P. Tomsich, J. Wenninger
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Roman Obermaiser
29 Changes: Joseph Wenninger
32 $Id: VMRuntime.c 1433 2004-11-04 16:40:43Z motse $
40 #include <sys/utsname.h>
44 #include "exceptions.h"
50 #include "toolbox/logging.h"
51 #include "toolbox/memory.h"
52 #include "java_io_File.h"
53 #include "java_lang_String.h"
54 #include "java_lang_Process.h"
55 #include "java_util_Properties.h" /* needed for java_lang_Runtime.h */
56 #include "java_lang_VMRuntime.h"
59 #ifndef STATIC_CLASSPATH
65 /* should we run all finalizers on exit? */
66 static s4 finalizeOnExit = false;
69 static bool shouldFinalizersBeRunOnExit=false;
70 static int activeprops = 20;
72 static char *proplist[MAXPROPS][2] = {
73 { "java.class.path", NULL },
74 { "java.home", NULL },
75 { "user.home", NULL },
76 { "user.name", NULL },
81 { "os.version", NULL },
82 { "java.library.path",NULL},
84 { "java.class.version", "45.3" },
85 { "java.version", PACKAGE":"VERSION },
86 { "java.vendor", "CACAO Team" },
87 { "java.vendor.url", "http://www.complang.tuwien.ac.at/java/cacao/" },
88 { "java.vm.name", "CACAO"},
89 { "java.tmpdir", "/tmp/"},
90 { "java.io.tmpdir", "/tmp/"},
92 { "path.separator", ":" },
93 { "file.separator", "/" },
94 { "line.separator", "\n" },
95 { "java.protocol.handler.pkgs", "gnu.java.net.protocol"}
98 void attach_property(char *name, char *value)
100 if (activeprops >= MAXPROPS) panic("Too many properties defined");
101 proplist[activeprops][0] = name;
102 proplist[activeprops][1] = value;
106 * Class: java_lang_VMRuntime
107 * Method: execInternal
108 * Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/io/File;)Ljava/lang/Process;
110 JNIEXPORT java_lang_Process* JNICALL Java_java_lang_VMRuntime_execInternal(JNIEnv *env, jclass clazz, java_objectarray *cmd, java_objectarray *shellenv, java_io_File *workingdir)
112 log_text("Java_java_lang_Runtime_execInternal called");
119 * Class: java/lang/VMRuntime
120 * Method: exitInternal
123 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, s4 par1)
128 cacao_shutdown(par1);
133 * Class: java/lang/Runtime
137 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
139 return gc_get_free_bytes();
144 * Class: java/lang/Runtime
148 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
155 * Class: java/lang/Runtime
156 * Method: runFinalization
159 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
161 gc_invoke_finalizers();
166 * Class: java/lang/Runtime
167 * Method: runFinalizersOnExit
170 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, s4 par1)
175 shouldFinalizersBeRunOnExit=par1;
179 * Class: java/lang/Runtime
180 * Method: runFinalizationsForExit
183 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
185 if (shouldFinalizersBeRunOnExit) {
187 // gc_finalize_all();
189 log_text("Java_java_lang_VMRuntime_runFinalizationForExit called");
195 * Class: java/lang/Runtime
196 * Method: totalMemory
199 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
201 return gc_get_heap_size();
206 * Class: java/lang/Runtime
207 * Method: traceInstructions
210 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, s4 par1)
217 * Class: java/lang/Runtime
218 * Method: traceMethodCalls
221 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, s4 par1)
228 * Class: java_lang_Runtime
229 * Method: availableProcessors
232 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
234 #if defined(_SC_NPROC_ONLN)
235 return (s4) sysconf(_SC_NPROC_ONLN);
237 #elif defined(_SC_NPROCESSORS_ONLN)
238 return (s4) sysconf(_SC_NPROCESSORS_ONLN);
247 * Class: java_lang_Runtime
249 * Signature: (Ljava/lang/String;)I
251 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *par1)
260 log_text("Java_java_lang_VMRuntime_nativeLoad");
262 data = javastring_toutf(par1, 0);
265 log_text("nativeLoad: Error: empty string");
269 buffer_len = utf_strlen(data) + 40;
272 buffer = MNEW(char, buffer_len);
273 strcpy(buffer, "Java_java_lang_VMRuntime_nativeLoad:");
274 utf_sprint(buffer + strlen((char *) data), data);
278 MFREE(buffer, char, buffer_len);
281 #ifndef STATIC_CLASSPATH
282 /*here it could be interesting to store the references in a list eg for nicely cleaning up or for certain platforms*/
283 if (dlopen(data->text,RTLD_NOW | RTLD_GLOBAL)) {
284 log_text("LIBLOADED");
296 * Class: java_lang_VMRuntime
297 * Method: nativeGetLibname
298 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
300 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_nativeGetLibname(JNIEnv *env, jclass clazz, java_lang_String *par1, java_lang_String *par2)
305 java_lang_String *resultString;
306 data = javastring_toutf(par2, 0);
309 log_text("nativeGetLibName: Error: empty string");
313 buffer_len = utf_strlen(data) + 6 /*lib .so */ +1 /*0*/;
314 buffer = MNEW(char, buffer_len);
315 sprintf(buffer,"lib");
316 utf_sprint(buffer+3,data);
317 strcat(buffer,".so");
319 log_text("nativeGetLibName:");
323 resultString=javastring_new_char(buffer);
325 MFREE(buffer, char, buffer_len);
331 * Class: java_lang_VMRuntime
332 * Method: insertSystemProperties
333 * Signature: (Ljava/util/Properties;)V
335 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_insertSystemProperties(JNIEnv *env, jclass clazz, java_util_Properties *p)
338 #define BUFFERSIZE 200
341 char buffer[BUFFERSIZE];
342 struct utsname utsnamebuf;
344 proplist[0][1] = classpath;
345 proplist[1][1] = getenv("JAVA_HOME");
346 proplist[2][1] = getenv("HOME");
347 proplist[3][1] = getenv("USER");
348 proplist[4][1] = getcwd(buffer, BUFFERSIZE);
350 /* get properties from system */
352 proplist[5][1] = utsnamebuf.machine;
353 proplist[6][1] = utsnamebuf.sysname;
354 proplist[7][1] = utsnamebuf.release;
356 #if defined(STATIC_CLASSPATH)
357 proplist[8][1] = ".";
359 proplist[8][1] = getenv("LD_LIBRARY_PATH");
363 *exceptionptr = new_exception(string_java_lang_NullPointerException);
367 /* search for method to add properties */
368 m = class_resolvemethod(p->header.vftbl->class,
370 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;")
374 *exceptionptr = new_exception_message(string_java_lang_NoSuchMethodError,
375 "java.lang.Properties.put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;)");
379 /* add the properties */
380 for (i = 0; i < activeprops; i++) {
382 if (proplist[i][1] == NULL) proplist[i][1] = "";
384 asm_calljavafunction(m,
386 javastring_new_char(proplist[i][0]),
387 javastring_new_char(proplist[i][1]),
397 * Class: java_lang_VMRuntime
401 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
403 return gc_get_max_heap_size();
408 * These are local overrides for various environment variables in Emacs.
409 * Please do not remove this and leave it at the end of the file, where
410 * Emacs will automagically detect them.
411 * ---------------------------------------------------------------------
414 * indent-tabs-mode: t