OSDN Git Service

41702dfffb90e4cc062435d467179d52f994aa39
[pf3gnuchains/gcc-fork.git] / libjava / posix.cc
1 // posix.cc -- Helper functions for POSIX-flavored OSs.
2
3 /* Copyright (C) 2000, 2001, 2002, 2006  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
13 #include "posix.h"
14
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <signal.h>
18 #include <stdio.h>
19
20 #ifdef HAVE_DLFCN_H
21 #include <dlfcn.h>
22 #endif
23
24 #include <jvm.h>
25 #include <java-stack.h>
26 #include <java/lang/Thread.h>
27 #include <java/io/InterruptedIOException.h>
28 #include <java/util/Properties.h>
29
30 #if defined (ECOS)
31 extern "C" unsigned long long _clock (void);
32 #endif
33
34 #if defined(HAVE_PROC_SELF_EXE)
35 static char exec_name[20];
36   // initialized in _Jv_platform_initialize()
37 #endif
38
39 const char *_Jv_ThisExecutable (void)
40 {
41 #if defined(DISABLE_MAIN_ARGS)
42   return "[Embedded App]";
43 #elif defined(HAVE_PROC_SELF_EXE)
44   return exec_name;
45     // initialized in _Jv_platform_initialize()
46 #else
47   return _Jv_GetSafeArg (0);
48 #endif
49 }
50
51 // gettimeofday implementation.
52 jlong
53 _Jv_platform_gettimeofday ()
54 {
55 #if defined (HAVE_GETTIMEOFDAY)
56   timeval tv;
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)
62   struct timeb t;
63   ftime (&t);
64   return (t.time * 1000LL) + t.millitm;
65 #elif defined (ECOS)
66   // FIXME.
67   return _clock();
68 #else
69   // In the absence of any function, time remains forever fixed.
70   return 23000;
71 #endif
72 }
73
74 jlong
75 _Jv_platform_nanotime ()
76 {
77 #ifdef HAVE_CLOCK_GETTIME
78   struct timespec now;
79   clockid_t id;
80 #ifdef CLOCK_MONOTONIC
81   id = CLOCK_MONOTONIC;
82 #elif defined (CLOCK_HIGHRES)
83   id = CLOCK_HIGHRES;
84 #else
85   id = CLOCK_REALTIME;
86 #endif
87   if (clock_gettime (id, &now) == 0)
88     {
89       jlong result = (jlong) now.tv_sec;
90       result = result * 1000 * 1000 + now.tv_nsec;
91       return result;
92     }
93   // clock_gettime failed, but we can fall through.
94 #endif // HAVE_CLOCK_GETTIME
95   return _Jv_platform_gettimeofday () * 1000LL;
96 }
97
98 // Platform-specific VM initialization.
99 void
100 _Jv_platform_initialize (void)
101 {
102 #if defined (HAVE_SIGACTION)
103   // We only want this on POSIX systems.
104   struct sigaction act;
105   act.sa_handler = SIG_IGN;
106   sigemptyset (&act.sa_mask);
107   act.sa_flags = 0;
108   sigaction (SIGPIPE, &act, NULL);
109 #else
110   signal (SIGPIPE, SIG_IGN);
111 #endif
112
113 #if defined (HAVE_PROC_SELF_EXE)
114   // Compute our executable name
115   sprintf (exec_name, "/proc/%d/exe", getpid ());
116 #endif
117 }
118
119 // Set platform-specific System properties.
120 void
121 _Jv_platform_initProperties (java::util::Properties* newprops)
122 {
123   // A convenience define.
124 #define SET(Prop,Val) \
125   newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
126
127   SET ("file.separator", "/");
128   SET ("path.separator", ":");
129   SET ("line.separator", "\n");
130   const char *tmpdir = ::getenv("TMPDIR");
131   if (! tmpdir)
132     tmpdir = "/tmp";
133   SET ("java.io.tmpdir", tmpdir);
134 }
135
136 static inline void
137 internal_gettimeofday (struct timeval *result)
138 {
139 #if defined (HAVE_GETTIMEOFDAY)
140   gettimeofday (result, NULL);
141 #else
142   jlong val = _Jv_platform_gettimeofday ();
143   result->tv_sec = val / 1000;
144   result->tv_usec = (val % 1000) * 1000;
145 #endif /* HAVE_GETTIMEOFDAY */
146 }
147
148 // A wrapper for select() which ignores EINTR.
149 int
150 _Jv_select (int n, fd_set *readfds, fd_set  *writefds,
151             fd_set *exceptfds, struct timeval *timeout)
152 {
153 #ifdef HAVE_SELECT
154   // If we have a timeout, compute the absolute ending time.
155   struct timeval end, delay;
156   if (timeout)
157     {
158       internal_gettimeofday (&end);
159       end.tv_usec += timeout->tv_usec;
160       if (end.tv_usec >= 1000000)
161         {
162           ++end.tv_sec;
163           end.tv_usec -= 1000000;
164         }
165       end.tv_sec += timeout->tv_sec;
166       delay = *timeout;
167     }
168   else
169     {
170       // Placate compiler.
171       delay.tv_sec = delay.tv_usec = 0;
172     }
173
174   while (1)
175     {
176       int r = select (n, readfds, writefds, exceptfds,
177                       timeout ? &delay : NULL);
178       if (r != -1 || errno != EINTR)
179         return r;
180
181       // Here we know we got EINTR.
182       if (java::lang::Thread::interrupted ())
183         throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
184
185       struct timeval after;
186       if (timeout)
187         {
188           internal_gettimeofday (&after);
189           // Now compute new timeout argument.
190           delay.tv_usec = end.tv_usec - after.tv_usec;
191           delay.tv_sec = end.tv_sec - after.tv_sec;
192           if (delay.tv_usec < 0)
193             {
194               --delay.tv_sec;
195               delay.tv_usec += 1000000;
196             }
197           if (delay.tv_sec < 0)
198             {
199               // We assume that the user wants a valid select() call
200               // more than precise timing.  So if we get a series of
201               // EINTR we just keep trying with delay 0 until we get a
202               // valid result.
203               delay.tv_sec = 0;
204             }
205         }
206     }
207 #else /* HAVE_SELECT */
208   return 0;
209 #endif
210 }
211
212 // Given an address, find the object that defines it and the nearest
213 // defined symbol to that address.  Returns 0 if no object defines this
214 // address.
215 int
216 _Jv_platform_dladdr (const void *addr, _Jv_AddrInfo *info)
217 {
218   int ret_val = 0;
219
220 #if defined (HAVE_DLFCN_H) && defined (HAVE_DLADDR)
221   Dl_info addr_info;
222   ret_val = dladdr (addr, &addr_info);
223   if (ret_val != 0)
224     {
225       info->file_name = addr_info.dli_fname;
226       info->base = addr_info.dli_fbase;
227       info->sym_name = addr_info.dli_sname;
228       info->sym_addr = addr_info.dli_saddr;
229     }
230 #else
231   info->file_name = NULL;
232   info->base = NULL;
233   info->sym_name = NULL;
234   info->sym_addr = NULL;
235 #endif
236
237   return ret_val;
238 }