OSDN Git Service

* java/lang/natRuntime.cc (insertSystemProperties): Set
[pf3gnuchains/gcc-fork.git] / libjava / java / lang / natRuntime.cc
1 // natRuntime.cc - Implementation of native side of Runtime class.
2
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 #include <config.h>
12 #include <platform.h>
13
14 #include <stdlib.h>
15
16 #include <gcj/cni.h>
17 #include <jvm.h>
18 #include <java-props.h>
19 #include <java/lang/Long.h>
20 #include <java/lang/Runtime.h>
21 #include <java/lang/UnknownError.h>
22 #include <java/lang/UnsatisfiedLinkError.h>
23 #include <gnu/gcj/runtime/FileDeleter.h>
24 #include <gnu/gcj/runtime/FinalizerThread.h>
25 #include <java/io/File.h>
26 #include <java/util/Properties.h>
27 #include <java/util/TimeZone.h>
28 #include <java/lang/StringBuffer.h>
29 #include <java/lang/Process.h>
30 #include <java/lang/ConcreteProcess.h>
31 #include <java/lang/ClassLoader.h>
32 #include <gnu/gcj/runtime/StackTrace.h>
33 #include <java/lang/ArrayIndexOutOfBoundsException.h>
34
35 #include <jni.h>
36
37 #ifdef HAVE_PWD_H
38 #include <pwd.h>
39 #endif
40 #include <errno.h>
41
42 #ifdef HAVE_UNAME
43 #include <sys/utsname.h>
44 #endif
45
46 #ifdef HAVE_LOCALE_H
47 #include <locale.h>
48 #endif
49
50 #ifdef HAVE_LANGINFO_H
51 #include <langinfo.h>
52 #endif
53
54 \f
55
56 #ifdef USE_LTDL
57 #include <ltdl.h>
58
59 /* FIXME: we don't always need this.  The next libtool will let us use
60    AC_LTDL_PREOPEN to see if we do.  */
61 extern const lt_dlsymlist lt_preloaded_symbols[1] = { { 0, 0 } };
62
63 struct lookup_data
64 {
65   const char *symname;
66   void *result;
67 };
68
69 static int
70 find_symbol (lt_dlhandle handle, lt_ptr_t data)
71 {
72   lookup_data *ld = (lookup_data *) data;
73   ld->result = lt_dlsym (handle, ld->symname);
74   return ld->result != NULL;
75 }
76
77 void *
78 _Jv_FindSymbolInExecutable (const char *symname)
79 {
80   lookup_data data;
81   data.symname = symname;
82   data.result = NULL;
83   lt_dlforeach (find_symbol, (lt_ptr_t) &data);
84   return data.result;
85 }
86
87 void
88 _Jv_SetDLLSearchPath (const char *path)
89 {
90   lt_dlsetsearchpath (path);
91 }
92
93 #else
94
95 void *
96 _Jv_FindSymbolInExecutable (const char *)
97 {
98   return NULL;
99 }
100
101 void
102 _Jv_SetDLLSearchPath (const char *)
103 {
104   // Nothing.
105 }
106
107 #endif /* USE_LTDL */
108
109 \f
110
111 extern int _Jv_argc;
112 extern const char **_Jv_argv;
113   // our process' command line arguments
114
115 void
116 java::lang::Runtime::exitInternal (jint status)
117 {
118   // Make status right for Unix.  This is perhaps strange.
119   if (status < 0 || status > 255)
120     status = 255;
121
122   if (finalizeOnExit)
123     _Jv_RunAllFinalizers ();
124
125   // Delete all files registered with File.deleteOnExit()
126   gnu::gcj::runtime::FileDeleter::deleteOnExitNow ();
127
128   ::exit (status);
129 }
130
131 jlong
132 java::lang::Runtime::freeMemory (void)
133 {
134   return _Jv_GCFreeMemory ();
135 }
136
137 void
138 java::lang::Runtime::gc (void)
139 {
140   _Jv_RunGC ();
141 }
142
143 #ifdef USE_LTDL
144 // List of names for JNI_OnLoad.
145 static const char *onload_names[] = _Jv_platform_onload_names;
146 #endif
147
148 void
149 java::lang::Runtime::_load (jstring path, jboolean do_search)
150 {
151   JvSynchronize sync (this);
152   using namespace java::lang;
153 #ifdef USE_LTDL
154   jint len = _Jv_GetStringUTFLength (path);
155   char buf[len + 1 + strlen (_Jv_platform_solib_prefix)
156            + strlen (_Jv_platform_solib_suffix)];
157   int offset = 0;
158   if (do_search)
159     {
160       strcpy (buf, _Jv_platform_solib_prefix);
161       offset = strlen (_Jv_platform_solib_prefix);
162     }
163   jsize total = JvGetStringUTFRegion (path, 0, path->length(), &buf[offset]);
164   buf[offset + total] = '\0';
165
166   char *lib_name = buf;
167
168   if (do_search)
169     {
170       ClassLoader *sys = ClassLoader::getSystemClassLoader();
171       ClassLoader *look = NULL;
172       gnu::gcj::runtime::StackTrace *t = new gnu::gcj::runtime::StackTrace(10);
173       try
174         {
175           for (int i = 0; i < 10; ++i)
176             {
177               jclass klass = t->classAt(i);
178               if (klass != NULL)
179                 {
180                   ClassLoader *loader = klass->getClassLoaderInternal();
181                   if (loader != NULL && loader != sys)
182                     {
183                       look = loader;
184                       break;
185                     }
186                 }
187             }
188         }
189       catch (::java::lang::ArrayIndexOutOfBoundsException *e)
190         {
191         }
192
193       if (look != NULL)
194         {
195           // Don't include solib prefix in string passed to
196           // findLibrary.
197           jstring name = look->findLibrary(JvNewStringUTF(&buf[offset]));
198           if (name != NULL)
199             {
200               len = _Jv_GetStringUTFLength (name);
201               lib_name = (char *) _Jv_AllocBytes(len + 1);
202               total = JvGetStringUTFRegion (name, 0,
203                                             name->length(), lib_name);
204               lib_name[total] = '\0';
205               // Don't append suffixes any more; we have the full file
206               // name.
207               do_search = false;
208             }
209         }
210     }
211
212   lt_dlhandle h;
213   // FIXME: make sure path is absolute.
214   {
215     // Synchronize on java.lang.Class. This is to protect the class chain from
216     // concurrent modification by class registration calls which may be run
217     // during the dlopen().
218     JvSynchronize sync (&java::lang::Class::class$);
219     h = do_search ? lt_dlopenext (lib_name) : lt_dlopen (lib_name);
220   }
221   if (h == NULL)
222     {
223       const char *msg = lt_dlerror ();
224       jstring str = path->concat (JvNewStringLatin1 (": "));
225       str = str->concat (JvNewStringLatin1 (msg));
226       throw new UnsatisfiedLinkError (str);
227     }
228
229   // Search for JNI_OnLoad function.
230   void *onload = NULL;
231   const char **name = onload_names;
232   while (*name != NULL)
233     {
234       onload = lt_dlsym (h, *name);
235       if (onload != NULL)
236         break;
237       ++name;
238     }
239
240   if (onload != NULL)
241     {
242       JavaVM *vm = _Jv_GetJavaVM ();
243       if (vm == NULL)
244         {
245           // FIXME: what?
246           return;
247         }
248       jint vers = ((jint (JNICALL *) (JavaVM *, void *)) onload) (vm, NULL);
249       if (vers != JNI_VERSION_1_1 && vers != JNI_VERSION_1_2
250           && vers != JNI_VERSION_1_4)
251         {
252           // FIXME: unload the library.
253           throw new UnsatisfiedLinkError (JvNewStringLatin1 ("unrecognized version from JNI_OnLoad"));
254         }
255     }
256 #else
257   throw new UnknownError
258     (JvNewStringLatin1 (do_search
259                         ? "Runtime.loadLibrary not implemented"
260                         : "Runtime.load not implemented"));
261 #endif /* USE_LTDL */
262 }
263
264 jboolean
265 java::lang::Runtime::loadLibraryInternal (jstring lib)
266 {
267   JvSynchronize sync (this);
268   using namespace java::lang;
269 #ifdef USE_LTDL
270   jint len = _Jv_GetStringUTFLength (lib);
271   char buf[len + 1];
272   jsize total = JvGetStringUTFRegion (lib, 0, lib->length(), buf);
273   buf[total] = '\0';
274   // FIXME: make sure path is absolute.
275   lt_dlhandle h = lt_dlopenext (buf);
276   return h != NULL;
277 #else
278   return false;
279 #endif /* USE_LTDL */
280 }
281
282 void
283 java::lang::Runtime::init (void)
284 {
285 #ifdef USE_LTDL
286   lt_dlinit ();
287   // Make sure self is opened.
288   lt_dlopen (NULL);
289 #endif
290 }
291
292 void
293 java::lang::Runtime::runFinalization (void)
294 {
295   gnu::gcj::runtime::FinalizerThread::finalizerReady ();
296 }
297
298 jlong
299 java::lang::Runtime::totalMemory (void)
300 {
301   return _Jv_GCTotalMemory ();
302 }
303
304 jlong
305 java::lang::Runtime::maxMemory (void)
306 {
307   // We don't have a maximum.  FIXME: we might if we ask the GC for
308   // one.
309   return Long::MAX_VALUE;
310 }
311
312 void
313 java::lang::Runtime::traceInstructions (jboolean)
314 {
315   // Do nothing.
316 }
317
318 void
319 java::lang::Runtime::traceMethodCalls (jboolean)
320 {
321   // Do nothing.
322 }
323
324 #if ! defined (DEFAULT_FILE_ENCODING) && defined (HAVE_ICONV) \
325     && defined (HAVE_NL_LANGINFO)
326
327 static char *
328 file_encoding ()
329 {
330   setlocale (LC_CTYPE, "");
331   char *e = nl_langinfo (CODESET);
332   if (e == NULL || *e == '\0')
333     e = "8859_1";
334   return e;
335 }
336
337 #define DEFAULT_FILE_ENCODING file_encoding ()
338
339 #endif
340
341 #ifndef DEFAULT_FILE_ENCODING
342 #define DEFAULT_FILE_ENCODING "8859_1"
343 #endif
344
345 static char *default_file_encoding = DEFAULT_FILE_ENCODING;
346
347 #if HAVE_GETPWUID_R
348 /* Use overload resolution to find out the signature of getpwuid_r.  */
349
350   /* This is Posix getpwuid_r.  */
351 template <typename T_uid, typename T_passwd, typename T_buf, typename T_len>
352 static inline int
353 getpwuid_adaptor(int (*getpwuid_r)(T_uid user_id, T_passwd *pwd_r,
354                                    T_buf *buf_r, T_len len_r,
355                                    T_passwd **pwd_entry_ptr),
356                  uid_t user_id, struct passwd *pwd_r,
357                  char *buf_r, size_t len_r, struct passwd **pwd_entry)
358 {
359   return getpwuid_r (user_id, pwd_r, buf_r, len_r, pwd_entry);
360 }
361
362 /* This is used on HPUX 10.20 */
363 template <typename T_uid, typename T_passwd, typename T_buf, typename T_len>
364 static inline int
365 getpwuid_adaptor(int (*getpwuid_r)(T_uid user_id, T_passwd *pwd_r,
366                                    T_buf *buf_r, T_len len_r),
367                  uid_t user_id, struct passwd *pwd_r,
368                  char *buf_r, size_t len_r, struct passwd **pwd_entry)
369 {
370   return getpwuid_r (user_id, pwd_r, buf_r, len_r);
371 }
372
373 /* This is used on IRIX 5.2.  */
374 template <typename T_uid, typename T_passwd, typename T_buf, typename T_len>
375 static inline int
376 getpwuid_adaptor(T_passwd * (*getpwuid_r)(T_uid user_id, T_passwd *pwd_r,
377                                           T_buf *buf_r, T_len len_r),
378                  uid_t user_id, struct passwd *pwd_r,
379                  char *buf_r, size_t len_r, struct passwd **pwd_entry)
380 {
381   *pwd_entry = getpwuid_r (user_id, pwd_r, buf_r, len_r);
382   return (*pwd_entry == NULL) ? errno : 0;
383 }
384 #endif
385
386 void
387 java::lang::Runtime::insertSystemProperties (java::util::Properties *newprops)
388 {
389   // A convenience define.
390 #define SET(Prop,Val) \
391         newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
392
393   // A mixture of the Java Product Versioning Specification
394   // (introduced in 1.2), and earlier versioning properties.
395   SET ("java.version", GCJVERSION);
396   SET ("java.vendor", "Free Software Foundation, Inc.");
397   SET ("java.vendor.url", "http://gcc.gnu.org/java/");
398   SET ("java.class.version", "46.0");
399   SET ("java.vm.specification.version", "1.0");
400   SET ("java.vm.specification.name", "Java(tm) Virtual Machine Specification");
401   SET ("java.vm.specification.vendor", "Sun Microsystems Inc.");
402   SET ("java.vm.version", __VERSION__);
403   SET ("java.vm.vendor", "Free Software Foundation, Inc.");
404   SET ("java.vm.name", "GNU libgcj");
405   SET ("java.specification.version", "1.3");
406   SET ("java.specification.name", "Java(tm) Platform API Specification");
407   SET ("java.specification.vendor", "Sun Microsystems Inc.");
408
409   char value[100];
410 #define NAME "GNU libgcj "
411   strcpy (value, NAME);
412   strncpy (value + sizeof (NAME) - 1, __VERSION__,
413            sizeof(value) - sizeof(NAME));
414   value[sizeof (value) - 1] = '\0';
415   jstring version = JvNewStringLatin1 (value);
416   newprops->put (JvNewStringLatin1 ("java.fullversion"), version);
417   newprops->put (JvNewStringLatin1 ("java.vm.info"), version);
418
419   // This definition is rather arbitrary: we choose $(prefix).  In
420   // part we do this because most people specify only --prefix and
421   // nothing else when installing gcj.  Plus, people are free to
422   // redefine `java.home' with `-D' if necessary.
423   SET ("java.home", PREFIX);
424   SET ("gnu.classpath.home", PREFIX);
425   // This is set to $(libdir) because we use this to find .security
426   // files at runtime.
427   char val2[sizeof ("file://") + sizeof (LIBDIR) + 1];
428   strcpy (val2, "file://");
429   strcat (val2, LIBDIR);
430   SET ("gnu.classpath.home.url", val2);
431
432   SET ("file.encoding", default_file_encoding);
433
434 #ifdef HAVE_UNAME
435   struct utsname u;
436   if (! uname (&u))
437     {
438       SET ("os.name", u.sysname);
439       SET ("os.arch", u.machine);
440       SET ("os.version", u.release);
441     }
442   else
443     {
444       SET ("os.name", "unknown");
445       SET ("os.arch", "unknown");
446       SET ("os.version", "unknown");
447     }
448 #endif /* HAVE_UNAME */
449
450 #ifndef NO_GETUID
451 #ifdef HAVE_PWD_H
452   uid_t user_id = getuid ();
453   struct passwd *pwd_entry;
454
455 #ifdef HAVE_GETPWUID_R
456   struct passwd pwd_r;
457   size_t len_r = 200;
458   char *buf_r = (char *) _Jv_AllocBytes (len_r);
459
460   while (buf_r != NULL)
461     {
462       int r = getpwuid_adaptor (getpwuid_r, user_id, &pwd_r,
463                                 buf_r, len_r, &pwd_entry);
464       if (r == 0)
465         break;
466       else if (r != ERANGE)
467         {
468           pwd_entry = NULL;
469           break;
470         }
471       len_r *= 2;
472       buf_r = (char *) _Jv_AllocBytes (len_r);
473     }
474 #else
475   pwd_entry = getpwuid (user_id);
476 #endif /* HAVE_GETPWUID_R */
477
478   if (pwd_entry != NULL)
479     {
480       SET ("user.name", pwd_entry->pw_name);
481       SET ("user.home", pwd_entry->pw_dir);
482     }
483 #endif /* HAVE_PWD_H */
484 #endif /* NO_GETUID */
485
486 #ifdef HAVE_GETCWD
487 #ifdef HAVE_UNISTD_H
488   /* Use getcwd to set "user.dir". */
489   int buflen = 250;
490   char *buffer = (char *) malloc (buflen);
491   while (buffer != NULL)
492     {
493       if (getcwd (buffer, buflen) != NULL)
494         {
495           SET ("user.dir", buffer);
496           break;
497         }
498       if (errno != ERANGE)
499         break;
500       buflen = 2 * buflen;
501       buffer = (char *) realloc (buffer, buflen);
502     }
503   if (buffer != NULL)
504     free (buffer);
505 #endif /* HAVE_UNISTD_H */
506 #endif /* HAVE_GETCWD */
507
508   // Set user locale properties based on setlocale()
509 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
510   // We let the user choose the locale.  However, since Java differs
511   // from POSIX, we arbitrarily pick LC_MESSAGES as determining the
512   // Java locale.  We can't use LC_ALL because it might return a full
513   // list of all the settings.  If we don't have LC_MESSAGES then we
514   // just default to `en_US'.
515   setlocale (LC_ALL, "");
516   char *locale = setlocale (LC_MESSAGES, "");
517   if (locale && strlen (locale) >= 2)
518     {
519       char buf[3];
520       buf[2] = '\0';
521       // copy the first two chars to user.language
522       strncpy (buf, locale, 2);
523       SET ("user.language", buf);
524       // if the next char is a '_', copy the two after that to user.region
525       locale += 2;
526       if (locale[0] == '_')
527         {
528           locale++;
529           strncpy (buf, locale, 2);
530           SET ("user.region", buf);
531         }
532     }
533   else
534 #endif /* HAVE_SETLOCALE and HAVE_LC_MESSAGES */
535     {
536       SET ("user.language", "en");
537       SET ("user.region", "US");
538     }  
539
540   // Set some properties according to whatever was compiled in with
541   // `-D'.
542   for (int i = 0; _Jv_Compiler_Properties[i]; ++i)
543     {
544       const char *s, *p;
545       // Find the `='.
546       for (s = p = _Jv_Compiler_Properties[i]; *s && *s != '='; ++s)
547         ;
548       jstring name = JvNewStringLatin1 (p, s - p);
549       jstring val = JvNewStringLatin1 (*s == '=' ? s + 1 : s);
550       newprops->put (name, val);
551     }
552
553   // Set the system properties from the user's environment.
554 #ifndef DISABLE_GETENV_PROPERTIES
555   if (_Jv_Environment_Properties)
556     {
557       size_t i = 0;
558
559       while (_Jv_Environment_Properties[i].key)
560         {
561           SET (_Jv_Environment_Properties[i].key, 
562                _Jv_Environment_Properties[i].value);
563           i++;
564         }
565     }
566 #endif
567
568   if (_Jv_Jar_Class_Path)
569     newprops->put(JvNewStringLatin1 ("java.class.path"),
570                   JvNewStringLatin1 (_Jv_Jar_Class_Path));
571   else
572     {
573       // FIXME: find libgcj.zip and append its path?
574       char *classpath = ::getenv("CLASSPATH");
575       jstring cp = newprops->getProperty (JvNewStringLatin1("java.class.path"));
576       java::lang::StringBuffer *sb = new java::lang::StringBuffer ();
577       
578       if (classpath)
579         {
580           sb->append (JvNewStringLatin1 (classpath));
581           sb->append (_Jv_platform_path_separator);
582         }
583       if (cp != NULL)
584         sb->append (cp);
585       else
586         sb->append ((jchar) '.');
587       
588       newprops->put(JvNewStringLatin1 ("java.class.path"),
589                       sb->toString ());
590     }
591
592   // The name used to invoke this process (argv[0] in C).
593   SET ("gnu.gcj.progname", _Jv_argv[0]);
594
595   // Allow platform specific settings and overrides.
596   _Jv_platform_initProperties (newprops);
597
598   // If java.library.path is set, tell libltdl so we search the new
599   // directories as well.  FIXME: does this work properly on Windows?
600   String *path = newprops->getProperty(JvNewStringLatin1("java.library.path"));
601   if (path)
602     {
603       char *val = (char *) _Jv_Malloc (JvGetStringUTFLength (path) + 1);
604       jsize total = JvGetStringUTFRegion (path, 0, path->length(), val);
605       val[total] = '\0';
606       _Jv_SetDLLSearchPath (val);
607       _Jv_Free (val);
608     }
609   else
610     {
611       // Set a value for user code to see.
612       // FIXME: JDK sets this to the actual path used, including
613       // LD_LIBRARY_PATH, etc.
614       SET ("java.library.path", "");
615     }
616 }
617
618 java::lang::Process *
619 java::lang::Runtime::execInternal (jstringArray cmd,
620                                    jstringArray env,
621                                    java::io::File *dir)
622 {
623   return new java::lang::ConcreteProcess (cmd, env, dir);
624 }
625
626 jint
627 java::lang::Runtime::availableProcessors (void)
628 {
629   // FIXME: find the real value.
630   return 1;
631 }
632
633 jstring
634 java::lang::Runtime::nativeGetLibname (jstring pathname, jstring libname)
635 {
636   java::lang::StringBuffer *sb = new java::lang::StringBuffer ();
637   sb->append(pathname);
638   if (pathname->length() > 0)
639     sb->append (_Jv_platform_file_separator);
640
641   sb->append (JvNewStringLatin1 (_Jv_platform_solib_prefix));
642   sb->append(libname);
643   sb->append (JvNewStringLatin1 (_Jv_platform_solib_suffix));
644
645   return sb->toString();
646 }