1 // posix.cc -- Helper functions for POSIX-flavored OSs.
3 /* Copyright (C) 2000, 2001, 2002, 2006 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
25 #include <java-stack.h>
26 #include <java/lang/Thread.h>
27 #include <java/io/InterruptedIOException.h>
28 #include <java/util/Properties.h>
31 extern "C" unsigned long long _clock (void);
34 #if defined(HAVE_PROC_SELF_EXE)
35 static char exec_name
[20];
36 // initialized in _Jv_platform_initialize()
39 const char *_Jv_ThisExecutable (void)
41 #if defined(DISABLE_MAIN_ARGS)
42 return "[Embedded App]";
43 #elif defined(HAVE_PROC_SELF_EXE)
45 // initialized in _Jv_platform_initialize()
47 return _Jv_GetSafeArg (0);
51 // gettimeofday implementation.
53 _Jv_platform_gettimeofday ()
55 #if defined (HAVE_GETTIMEOFDAY)
57 gettimeofday (&tv
, NULL
);
58 return (tv
.tv_sec
* 1000LL) + (tv
.tv_usec
/ 1000LL);
59 #elif defined (HAVE_TIME)
60 return time (NULL
) * 1000LL;
61 #elif defined (HAVE_FTIME)
64 return (t
.time
* 1000LL) + t
.millitm
;
69 // In the absence of any function, time remains forever fixed.
75 _Jv_platform_nanotime ()
77 #ifdef HAVE_CLOCK_GETTIME
80 #ifdef CLOCK_MONOTONIC
82 #elif defined (CLOCK_HIGHRES)
87 if (clock_gettime (id
, &now
) == 0)
89 jlong result
= (jlong
) now
.tv_sec
;
90 result
= result
* 1000000000LL + now
.tv_nsec
;
93 // clock_gettime failed, but we can fall through.
94 #endif // HAVE_CLOCK_GETTIME
95 #if defined (HAVE_GETTIMEOFDAY)
98 gettimeofday (&tv
, NULL
);
99 return (tv
.tv_sec
* 1000000000LL) + tv
.tv_usec
* 1000LL;
102 return _Jv_platform_gettimeofday () * 1000000LL;
106 // Platform-specific VM initialization.
108 _Jv_platform_initialize (void)
110 #if defined (HAVE_SIGACTION)
111 // We only want this on POSIX systems.
112 struct sigaction act
;
113 act
.sa_handler
= SIG_IGN
;
114 sigemptyset (&act
.sa_mask
);
116 sigaction (SIGPIPE
, &act
, NULL
);
118 signal (SIGPIPE
, SIG_IGN
);
121 #if defined (HAVE_PROC_SELF_EXE)
122 // Compute our executable name
123 sprintf (exec_name
, "/proc/%d/exe", getpid ());
127 // Set platform-specific System properties.
129 _Jv_platform_initProperties (java::util::Properties
* newprops
)
131 // A convenience define.
132 #define SET(Prop,Val) \
133 newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
135 SET ("file.separator", "/");
136 SET ("path.separator", ":");
137 SET ("line.separator", "\n");
138 const char *tmpdir
= ::getenv("TMPDIR");
141 SET ("java.io.tmpdir", tmpdir
);
142 const char *zoneinfodir
= ::getenv("TZDATA");
144 zoneinfodir
= "/usr/share/zoneinfo";
145 SET ("gnu.java.util.zoneinfo.dir", zoneinfodir
);
149 internal_gettimeofday (struct timeval
*result
)
151 #if defined (HAVE_GETTIMEOFDAY)
152 gettimeofday (result
, NULL
);
154 jlong val
= _Jv_platform_gettimeofday ();
155 result
->tv_sec
= val
/ 1000;
156 result
->tv_usec
= (val
% 1000) * 1000;
157 #endif /* HAVE_GETTIMEOFDAY */
160 // A wrapper for select() which ignores EINTR.
162 _Jv_select (int n
, fd_set
*readfds
, fd_set
*writefds
,
163 fd_set
*exceptfds
, struct timeval
*timeout
)
166 // If we have a timeout, compute the absolute ending time.
167 struct timeval end
, delay
;
170 internal_gettimeofday (&end
);
171 end
.tv_usec
+= timeout
->tv_usec
;
172 if (end
.tv_usec
>= 1000000)
175 end
.tv_usec
-= 1000000;
177 end
.tv_sec
+= timeout
->tv_sec
;
183 delay
.tv_sec
= delay
.tv_usec
= 0;
188 int r
= select (n
, readfds
, writefds
, exceptfds
,
189 timeout
? &delay
: NULL
);
190 if (r
!= -1 || errno
!= EINTR
)
193 // Here we know we got EINTR.
194 if (java::lang::Thread::interrupted ())
195 throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
197 struct timeval after
;
200 internal_gettimeofday (&after
);
201 // Now compute new timeout argument.
202 delay
.tv_usec
= end
.tv_usec
- after
.tv_usec
;
203 delay
.tv_sec
= end
.tv_sec
- after
.tv_sec
;
204 if (delay
.tv_usec
< 0)
207 delay
.tv_usec
+= 1000000;
209 if (delay
.tv_sec
< 0)
211 // We assume that the user wants a valid select() call
212 // more than precise timing. So if we get a series of
213 // EINTR we just keep trying with delay 0 until we get a
219 #else /* HAVE_SELECT */
224 // Given an address, find the object that defines it and the nearest
225 // defined symbol to that address. Returns 0 if no object defines this
228 _Jv_platform_dladdr (void *addr
, _Jv_AddrInfo
*info
)
232 #if defined (HAVE_DLFCN_H) && defined (HAVE_DLADDR)
234 ret_val
= dladdr (addr
, &addr_info
);
237 info
->file_name
= addr_info
.dli_fname
;
238 info
->base
= addr_info
.dli_fbase
;
239 info
->sym_name
= addr_info
.dli_sname
;
240 info
->sym_addr
= addr_info
.dli_saddr
;
243 info
->file_name
= NULL
;
245 info
->sym_name
= NULL
;
246 info
->sym_addr
= NULL
;