1 |
756 |
jeremybenn |
// natSystemProperties.cc - Implementation of native side of
|
2 |
|
|
// SystemProperties class.
|
3 |
|
|
|
4 |
|
|
/* Copyright (C) 2005, 2006 Free Software Foundation
|
5 |
|
|
|
6 |
|
|
This file is part of libgcj.
|
7 |
|
|
|
8 |
|
|
This software is copyrighted work licensed under the terms of the
|
9 |
|
|
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
10 |
|
|
details. */
|
11 |
|
|
|
12 |
|
|
#include <config.h>
|
13 |
|
|
#include <platform.h>
|
14 |
|
|
|
15 |
|
|
#include <stdlib.h>
|
16 |
|
|
#include <errno.h>
|
17 |
|
|
|
18 |
|
|
#ifdef HAVE_PWD_H
|
19 |
|
|
#include <pwd.h>
|
20 |
|
|
#endif
|
21 |
|
|
|
22 |
|
|
#ifdef HAVE_UNAME
|
23 |
|
|
#include <sys/utsname.h>
|
24 |
|
|
#endif
|
25 |
|
|
|
26 |
|
|
#ifdef HAVE_LOCALE_H
|
27 |
|
|
#include <locale.h>
|
28 |
|
|
#endif
|
29 |
|
|
|
30 |
|
|
#ifdef HAVE_LANGINFO_H
|
31 |
|
|
#include <langinfo.h>
|
32 |
|
|
#endif
|
33 |
|
|
|
34 |
|
|
#include <gcj/cni.h>
|
35 |
|
|
#include <jvm.h>
|
36 |
|
|
#include <java-props.h>
|
37 |
|
|
#include <gnu/classpath/SystemProperties.h>
|
38 |
|
|
#include <java/lang/String.h>
|
39 |
|
|
#include <jni.h>
|
40 |
|
|
|
41 |
|
|
char *_Jv_Module_Load_Path = NULL;
|
42 |
|
|
|
43 |
|
|
#ifdef USE_LTDL
|
44 |
|
|
#include <ltdl.h>
|
45 |
|
|
|
46 |
|
|
void
|
47 |
|
|
_Jv_SetDLLSearchPath (const char *path)
|
48 |
|
|
{
|
49 |
|
|
_Jv_Module_Load_Path = strdup (path);
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
#else
|
53 |
|
|
|
54 |
|
|
void
|
55 |
|
|
_Jv_SetDLLSearchPath (const char *)
|
56 |
|
|
{
|
57 |
|
|
// Nothing.
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
#endif /* USE_LTDL */
|
61 |
|
|
|
62 |
|
|
#if ! defined (DEFAULT_FILE_ENCODING) && defined (HAVE_ICONV) \
|
63 |
|
|
&& defined (HAVE_NL_LANGINFO)
|
64 |
|
|
|
65 |
|
|
static const char *
|
66 |
|
|
file_encoding ()
|
67 |
|
|
{
|
68 |
|
|
setlocale (LC_CTYPE, "");
|
69 |
|
|
const char *e = nl_langinfo (CODESET);
|
70 |
|
|
if (e == NULL || *e == '\0')
|
71 |
|
|
e = "8859_1";
|
72 |
|
|
return e;
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
#define DEFAULT_FILE_ENCODING file_encoding ()
|
76 |
|
|
|
77 |
|
|
#endif
|
78 |
|
|
|
79 |
|
|
#ifndef DEFAULT_FILE_ENCODING
|
80 |
|
|
#define DEFAULT_FILE_ENCODING "8859_1"
|
81 |
|
|
#endif
|
82 |
|
|
|
83 |
|
|
static const char *default_file_encoding = DEFAULT_FILE_ENCODING;
|
84 |
|
|
|
85 |
|
|
#if defined(HAVE_GETPWUID_R) && defined(_POSIX_PTHREAD_SEMANTICS)
|
86 |
|
|
/* Use overload resolution to find out the signature of getpwuid_r. */
|
87 |
|
|
|
88 |
|
|
/* This is Posix getpwuid_r. */
|
89 |
|
|
template <typename T_uid, typename T_passwd, typename T_buf, typename T_len>
|
90 |
|
|
static inline int
|
91 |
|
|
getpwuid_adaptor(int (*getpwuid_r)(T_uid user_id, T_passwd *pwd_r,
|
92 |
|
|
T_buf *buf_r, T_len len_r,
|
93 |
|
|
T_passwd **pwd_entry_ptr),
|
94 |
|
|
uid_t user_id, struct passwd *pwd_r,
|
95 |
|
|
char *buf_r, size_t len_r, struct passwd **pwd_entry)
|
96 |
|
|
{
|
97 |
|
|
return getpwuid_r (user_id, pwd_r, buf_r, len_r, pwd_entry);
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
/* This is used on HPUX 10.20 */
|
101 |
|
|
template <typename T_uid, typename T_passwd, typename T_buf, typename T_len>
|
102 |
|
|
static inline int
|
103 |
|
|
getpwuid_adaptor(int (*getpwuid_r)(T_uid user_id, T_passwd *pwd_r,
|
104 |
|
|
T_buf *buf_r, T_len len_r),
|
105 |
|
|
uid_t user_id, struct passwd *pwd_r,
|
106 |
|
|
char *buf_r, size_t len_r, struct passwd **pwd_entry)
|
107 |
|
|
{
|
108 |
|
|
return getpwuid_r (user_id, pwd_r, buf_r, len_r);
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
/* This is used on IRIX 5.2. */
|
112 |
|
|
template <typename T_uid, typename T_passwd, typename T_buf, typename T_len>
|
113 |
|
|
static inline int
|
114 |
|
|
getpwuid_adaptor(T_passwd * (*getpwuid_r)(T_uid user_id, T_passwd *pwd_r,
|
115 |
|
|
T_buf *buf_r, T_len len_r),
|
116 |
|
|
uid_t user_id, struct passwd *pwd_r,
|
117 |
|
|
char *buf_r, size_t len_r, struct passwd **pwd_entry)
|
118 |
|
|
{
|
119 |
|
|
*pwd_entry = getpwuid_r (user_id, pwd_r, buf_r, len_r);
|
120 |
|
|
return (*pwd_entry == NULL) ? errno : 0;
|
121 |
|
|
}
|
122 |
|
|
#endif
|
123 |
|
|
|
124 |
|
|
// Prepend GCJ_VERSIONED_LIBDIR to a module search path stored in a
|
125 |
|
|
// Java string, if the path is not already prefixed by
|
126 |
|
|
// GCJ_VERSIONED_LIBDIR. Return a newly JvMalloc'd char buffer. The
|
127 |
|
|
// result should be freed using JvFree. See
|
128 |
|
|
// _Jv_PrependVersionedLibdir in prims.cc.
|
129 |
|
|
static char*
|
130 |
|
|
PrependVersionedLibdir (::java::lang::String* libpath)
|
131 |
|
|
{
|
132 |
|
|
char* retval = 0;
|
133 |
|
|
|
134 |
|
|
// Extract a C char array from libpath.
|
135 |
|
|
char* val = (char*) _Jv_Malloc (JvGetStringUTFLength (libpath) + 1);
|
136 |
|
|
jsize total = JvGetStringUTFRegion (libpath, 0, libpath->length(), val);
|
137 |
|
|
val[total] = '\0';
|
138 |
|
|
retval = _Jv_PrependVersionedLibdir (val);
|
139 |
|
|
JvFree (val);
|
140 |
|
|
|
141 |
|
|
return retval;
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
void
|
145 |
|
|
gnu::classpath::SystemProperties::insertSystemProperties (::java::util::Properties *newprops)
|
146 |
|
|
{
|
147 |
|
|
// A convenience define.
|
148 |
|
|
#define SET(Prop,Val) \
|
149 |
|
|
newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
|
150 |
|
|
|
151 |
|
|
// A mixture of the Java Product Versioning Specification
|
152 |
|
|
// (introduced in 1.2), and earlier versioning properties. Some
|
153 |
|
|
// programs rely on seeing values that they expect, so we claim to
|
154 |
|
|
// be a 1.4-ish VM for their sake.
|
155 |
|
|
SET ("java.version", JV_VERSION);
|
156 |
|
|
SET ("java.runtime.version", JV_VERSION);
|
157 |
|
|
SET ("java.vendor", "Free Software Foundation, Inc.");
|
158 |
|
|
SET ("java.vendor.url", "http://gcc.gnu.org/java/");
|
159 |
|
|
SET ("java.class.version", "49.0");
|
160 |
|
|
SET ("java.vm.specification.version", "1.0");
|
161 |
|
|
SET ("java.vm.specification.name", "Java(tm) Virtual Machine Specification");
|
162 |
|
|
SET ("java.vm.specification.vendor", "Sun Microsystems Inc.");
|
163 |
|
|
SET ("java.vm.version", __VERSION__);
|
164 |
|
|
SET ("java.vm.vendor", "Free Software Foundation, Inc.");
|
165 |
|
|
SET ("java.vm.name", "GNU libgcj");
|
166 |
|
|
SET ("java.specification.version", JV_API_VERSION);
|
167 |
|
|
SET ("java.specification.name", "Java(tm) Platform API Specification");
|
168 |
|
|
SET ("java.specification.vendor", "Sun Microsystems Inc.");
|
169 |
|
|
|
170 |
|
|
char value[100];
|
171 |
|
|
#define NAME "GNU libgcj "
|
172 |
|
|
strcpy (value, NAME);
|
173 |
|
|
strncpy (value + sizeof (NAME) - 1, __VERSION__,
|
174 |
|
|
sizeof(value) - sizeof(NAME));
|
175 |
|
|
value[sizeof (value) - 1] = '\0';
|
176 |
|
|
jstring version = JvNewStringLatin1 (value);
|
177 |
|
|
newprops->put (JvNewStringLatin1 ("java.fullversion"), version);
|
178 |
|
|
newprops->put (JvNewStringLatin1 ("java.vm.info"), version);
|
179 |
|
|
|
180 |
|
|
// This definition is rather arbitrary: we choose $(prefix). In
|
181 |
|
|
// part we do this because most people specify only --prefix and
|
182 |
|
|
// nothing else when installing gcj. Plus, people are free to
|
183 |
|
|
// redefine `java.home' with `-D' if necessary.
|
184 |
|
|
SET ("java.home", JAVA_HOME);
|
185 |
|
|
SET ("gnu.classpath.home", PREFIX);
|
186 |
|
|
// This is set to $(toolexeclibdir) because we use this to find
|
187 |
|
|
// .security files at runtime.
|
188 |
|
|
char val2[sizeof ("file://") + sizeof (TOOLEXECLIBDIR) + 1];
|
189 |
|
|
strcpy (val2, "file://");
|
190 |
|
|
strcat (val2, TOOLEXECLIBDIR);
|
191 |
|
|
SET ("gnu.classpath.home.url", val2);
|
192 |
|
|
|
193 |
|
|
SET ("file.encoding", default_file_encoding);
|
194 |
|
|
|
195 |
|
|
#ifdef HAVE_UNAME
|
196 |
|
|
struct utsname u;
|
197 |
|
|
if (! uname (&u))
|
198 |
|
|
{
|
199 |
|
|
SET ("os.name", u.sysname);
|
200 |
|
|
SET ("os.version", u.release);
|
201 |
|
|
|
202 |
|
|
// Normalize x86 architecture names to "i386" (except on Windows, which
|
203 |
|
|
// is handled in win32.cc).
|
204 |
|
|
if (u.machine[0] == 'i'
|
205 |
|
|
&& u.machine[1] != 0
|
206 |
|
|
&& u.machine[2] == '8'
|
207 |
|
|
&& u.machine[3] == '6'
|
208 |
|
|
&& u.machine[4] == 0)
|
209 |
|
|
SET ("os.arch", "i386");
|
210 |
|
|
else
|
211 |
|
|
SET ("os.arch", u.machine);
|
212 |
|
|
}
|
213 |
|
|
else
|
214 |
|
|
{
|
215 |
|
|
SET ("os.name", "unknown");
|
216 |
|
|
SET ("os.arch", "unknown");
|
217 |
|
|
SET ("os.version", "unknown");
|
218 |
|
|
}
|
219 |
|
|
#endif /* HAVE_UNAME */
|
220 |
|
|
|
221 |
|
|
#ifndef NO_GETUID
|
222 |
|
|
#ifdef HAVE_PWD_H
|
223 |
|
|
uid_t user_id = getuid ();
|
224 |
|
|
struct passwd *pwd_entry;
|
225 |
|
|
|
226 |
|
|
#if defined(HAVE_GETPWUID_R) && defined(_POSIX_PTHREAD_SEMANTICS)
|
227 |
|
|
struct passwd pwd_r;
|
228 |
|
|
size_t len_r = 200;
|
229 |
|
|
char *buf_r = (char *) _Jv_AllocBytes (len_r);
|
230 |
|
|
|
231 |
|
|
while (buf_r != NULL)
|
232 |
|
|
{
|
233 |
|
|
int r = getpwuid_adaptor (getpwuid_r, user_id, &pwd_r,
|
234 |
|
|
buf_r, len_r, &pwd_entry);
|
235 |
|
|
if (r == 0)
|
236 |
|
|
break;
|
237 |
|
|
else if (r != ERANGE)
|
238 |
|
|
{
|
239 |
|
|
pwd_entry = NULL;
|
240 |
|
|
break;
|
241 |
|
|
}
|
242 |
|
|
len_r *= 2;
|
243 |
|
|
buf_r = (char *) _Jv_AllocBytes (len_r);
|
244 |
|
|
}
|
245 |
|
|
#else
|
246 |
|
|
pwd_entry = getpwuid (user_id);
|
247 |
|
|
#endif /* HAVE_GETPWUID_R */
|
248 |
|
|
|
249 |
|
|
if (pwd_entry != NULL)
|
250 |
|
|
{
|
251 |
|
|
SET ("user.name", pwd_entry->pw_name);
|
252 |
|
|
SET ("user.home", pwd_entry->pw_dir);
|
253 |
|
|
SET ("gnu.gcj.user.realname", pwd_entry->pw_gecos);
|
254 |
|
|
}
|
255 |
|
|
#endif /* HAVE_PWD_H */
|
256 |
|
|
#endif /* NO_GETUID */
|
257 |
|
|
|
258 |
|
|
#ifdef HAVE_GETCWD
|
259 |
|
|
#ifdef HAVE_UNISTD_H
|
260 |
|
|
/* Use getcwd to set "user.dir". */
|
261 |
|
|
int buflen = 250;
|
262 |
|
|
char *buffer = (char *) malloc (buflen);
|
263 |
|
|
while (buffer != NULL)
|
264 |
|
|
{
|
265 |
|
|
if (getcwd (buffer, buflen) != NULL)
|
266 |
|
|
{
|
267 |
|
|
SET ("user.dir", buffer);
|
268 |
|
|
break;
|
269 |
|
|
}
|
270 |
|
|
if (errno != ERANGE)
|
271 |
|
|
break;
|
272 |
|
|
buflen = 2 * buflen;
|
273 |
|
|
char *orig_buf = buffer;
|
274 |
|
|
buffer = (char *) realloc (buffer, buflen);
|
275 |
|
|
if (buffer == NULL)
|
276 |
|
|
free (orig_buf);
|
277 |
|
|
}
|
278 |
|
|
if (buffer != NULL)
|
279 |
|
|
free (buffer);
|
280 |
|
|
#endif /* HAVE_UNISTD_H */
|
281 |
|
|
#endif /* HAVE_GETCWD */
|
282 |
|
|
|
283 |
|
|
// Set user locale properties based on setlocale()
|
284 |
|
|
#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
|
285 |
|
|
// We let the user choose the locale. However, since Java differs
|
286 |
|
|
// from POSIX, we arbitrarily pick LC_MESSAGES as determining the
|
287 |
|
|
// Java locale. We can't use LC_ALL because it might return a full
|
288 |
|
|
// list of all the settings. If we don't have LC_MESSAGES then we
|
289 |
|
|
// just default to `en_US'.
|
290 |
|
|
setlocale (LC_ALL, "");
|
291 |
|
|
char *locale = setlocale (LC_MESSAGES, "");
|
292 |
|
|
if (locale && strlen (locale) >= 2)
|
293 |
|
|
{
|
294 |
|
|
char buf[3];
|
295 |
|
|
buf[2] = '\0';
|
296 |
|
|
// copy the first two chars to user.language
|
297 |
|
|
strncpy (buf, locale, 2);
|
298 |
|
|
SET ("user.language", buf);
|
299 |
|
|
// if the next char is a '_', copy the two after that to user.region
|
300 |
|
|
locale += 2;
|
301 |
|
|
if (locale[0] == '_')
|
302 |
|
|
{
|
303 |
|
|
locale++;
|
304 |
|
|
strncpy (buf, locale, 2);
|
305 |
|
|
SET ("user.region", buf);
|
306 |
|
|
}
|
307 |
|
|
}
|
308 |
|
|
else
|
309 |
|
|
#endif /* HAVE_SETLOCALE and HAVE_LC_MESSAGES */
|
310 |
|
|
{
|
311 |
|
|
SET ("user.language", "en");
|
312 |
|
|
SET ("user.region", "US");
|
313 |
|
|
}
|
314 |
|
|
|
315 |
|
|
// Set the java extension directories property if it has not yet been
|
316 |
|
|
// specified.
|
317 |
|
|
::java::lang::String *extdirs = newprops->getProperty(JvNewStringLatin1("java.ext.dirs"));
|
318 |
|
|
if (! extdirs)
|
319 |
|
|
SET ("java.ext.dirs", JAVA_EXT_DIRS);
|
320 |
|
|
|
321 |
|
|
// The endorsed directories that libgcj knows about by default.
|
322 |
|
|
// This is a way to get other jars into the boot class loader
|
323 |
|
|
// without overriding java.endorsed.dirs.
|
324 |
|
|
SET ("gnu.gcj.runtime.endorsed.dirs", GCJ_ENDORSED_DIRS);
|
325 |
|
|
|
326 |
|
|
// The path to libgcj's boot classes
|
327 |
|
|
SET ("sun.boot.class.path", BOOT_CLASS_PATH);
|
328 |
|
|
|
329 |
|
|
// If there is a default system database, set it.
|
330 |
|
|
SET ("gnu.gcj.precompiled.db.path", LIBGCJ_DEFAULT_DATABASE);
|
331 |
|
|
|
332 |
|
|
// Set some properties according to whatever was compiled in with
|
333 |
|
|
// `-D'. Important: after this point, the only properties that
|
334 |
|
|
// should be set are those which either the user cannot meaningfully
|
335 |
|
|
// override, or which augment whatever value the user has provided.
|
336 |
|
|
for (int i = 0; i < _Jv_Properties_Count; ++i)
|
337 |
|
|
{
|
338 |
|
|
const char *s, *p;
|
339 |
|
|
// Find the `='.
|
340 |
|
|
for (s = p = _Jv_Compiler_Properties[i]; *s && *s != '='; ++s)
|
341 |
|
|
;
|
342 |
|
|
jstring name = JvNewStringLatin1 (p, s - p);
|
343 |
|
|
jstring val = JvNewStringLatin1 (*s == '=' ? s + 1 : s);
|
344 |
|
|
newprops->put (name, val);
|
345 |
|
|
}
|
346 |
|
|
|
347 |
|
|
// Set the system properties from the user's environment.
|
348 |
|
|
#ifndef DISABLE_GETENV_PROPERTIES
|
349 |
|
|
if (_Jv_Environment_Properties)
|
350 |
|
|
{
|
351 |
|
|
size_t i = 0;
|
352 |
|
|
|
353 |
|
|
while (_Jv_Environment_Properties[i].key)
|
354 |
|
|
{
|
355 |
|
|
SET (_Jv_Environment_Properties[i].key,
|
356 |
|
|
_Jv_Environment_Properties[i].value);
|
357 |
|
|
i++;
|
358 |
|
|
}
|
359 |
|
|
}
|
360 |
|
|
#endif
|
361 |
|
|
|
362 |
|
|
// The name used to invoke this process (argv[0] in C).
|
363 |
|
|
SET ("gnu.gcj.progname", _Jv_GetSafeArg (0));
|
364 |
|
|
|
365 |
|
|
// Allow platform specific settings and overrides.
|
366 |
|
|
_Jv_platform_initProperties (newprops);
|
367 |
|
|
|
368 |
|
|
// If java.library.path is set, tell libltdl so we search the new
|
369 |
|
|
// directories as well.
|
370 |
|
|
::java::lang::String *path = newprops->getProperty(JvNewStringLatin1("java.library.path"));
|
371 |
|
|
if (path)
|
372 |
|
|
{
|
373 |
|
|
// Prepend GCJ_VERSIONED_LIBDIR to the module load path so that
|
374 |
|
|
// libgcj will find its own JNI libraries, like libgtkpeer.so.
|
375 |
|
|
char* val = PrependVersionedLibdir (path);
|
376 |
|
|
_Jv_SetDLLSearchPath (val);
|
377 |
|
|
_Jv_Free (val);
|
378 |
|
|
}
|
379 |
|
|
else
|
380 |
|
|
{
|
381 |
|
|
// Set a value for user code to see.
|
382 |
|
|
#ifdef USE_LTDL
|
383 |
|
|
char *libpath = getenv (LTDL_SHLIBPATH_VAR);
|
384 |
|
|
char* val = _Jv_PrependVersionedLibdir (libpath);
|
385 |
|
|
SET ("java.library.path", val);
|
386 |
|
|
_Jv_SetDLLSearchPath (val);
|
387 |
|
|
_Jv_Free (val);
|
388 |
|
|
#else
|
389 |
|
|
SET ("java.library.path", "");
|
390 |
|
|
#endif
|
391 |
|
|
}
|
392 |
|
|
|
393 |
|
|
// If java.class.path is still not set then set it according to the
|
394 |
|
|
// CLASSPATH environment variable if given. See gij.cc main () and
|
395 |
|
|
// prims.cc _Jv_CreateJavaVM () for all the ways this could have
|
396 |
|
|
// been set much earlier.
|
397 |
|
|
// If CLASSPATH isn't set or if the path is empty fall back to "."
|
398 |
|
|
path = newprops->getProperty(JvNewStringLatin1("java.class.path"));
|
399 |
|
|
if (!path)
|
400 |
|
|
{
|
401 |
|
|
char *classpath = getenv("CLASSPATH");
|
402 |
|
|
if (classpath && classpath[0] != 0)
|
403 |
|
|
{
|
404 |
|
|
path = JvNewStringLatin1 (classpath);
|
405 |
|
|
newprops->put(JvNewStringLatin1 ("java.class.path"), path);
|
406 |
|
|
}
|
407 |
|
|
}
|
408 |
|
|
|
409 |
|
|
if (!path || path->length() == 0)
|
410 |
|
|
SET ("java.class.path", ".");
|
411 |
|
|
}
|
412 |
|
|
|
413 |
|
|
jboolean
|
414 |
|
|
gnu::classpath::SystemProperties::isWordsBigEndian (void)
|
415 |
|
|
{
|
416 |
|
|
union
|
417 |
|
|
{
|
418 |
|
|
long lval;
|
419 |
|
|
char cval;
|
420 |
|
|
} u;
|
421 |
|
|
|
422 |
|
|
u.lval = 1;
|
423 |
|
|
return u.cval == 0;
|
424 |
|
|
}
|
425 |
|
|
|