1 // posix.cc -- Helper functions for POSIX-flavored OSs.
3 /* Copyright (C) 2000, 2001, 2002 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
20 #include <java/lang/Thread.h>
21 #include <java/io/InterruptedIOException.h>
22 #include <java/util/Properties.h>
25 extern "C" unsigned long long _clock (void);
28 // platform-specific executable name
29 extern const char **_Jv_argv
;
31 #if defined(HAVE_PROC_SELF_EXE)
32 static char exec_name
[20];
33 // initialized in _Jv_platform_initialize()
36 const char *_Jv_ThisExecutable (void)
38 #if defined(DISABLE_MAIN_ARGS)
39 return "[Embedded App]";
40 #elif defined(HAVE_PROC_SELF_EXE)
42 // initialized in _Jv_platform_initialize()
48 // gettimeofday implementation.
50 _Jv_platform_gettimeofday ()
52 #if defined (HAVE_GETTIMEOFDAY)
54 gettimeofday (&tv
, NULL
);
55 return (tv
.tv_sec
* 1000LL) + (tv
.tv_usec
/ 1000LL);
56 #elif defined (HAVE_TIME)
57 return time (NULL
) * 1000LL;
58 #elif defined (HAVE_FTIME)
61 return (t
.time
* 1000LL) + t
.millitm
;
66 // In the absence of any function, time remains forever fixed.
71 // Platform-specific VM initialization.
73 _Jv_platform_initialize (void)
75 #if defined (HAVE_SIGACTION)
76 // We only want this on POSIX systems.
78 act
.sa_handler
= SIG_IGN
;
79 sigemptyset (&act
.sa_mask
);
81 sigaction (SIGPIPE
, &act
, NULL
);
83 signal (SIGPIPE
, SIG_IGN
);
86 #if defined (HAVE_PROC_SELF_EXE)
87 // Compute our executable name
88 sprintf (exec_name
, "/proc/%d/exe", getpid ());
92 // Set platform-specific System properties.
94 _Jv_platform_initProperties (java::util::Properties
* newprops
)
96 // A convenience define.
97 #define SET(Prop,Val) \
98 newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
100 SET ("file.separator", "/");
101 SET ("path.separator", ":");
102 SET ("line.separator", "\n");
103 char *tmpdir
= ::getenv("TMPDIR");
106 SET ("java.io.tmpdir", tmpdir
);
110 internal_gettimeofday (struct timeval
*result
)
112 #if defined (HAVE_GETTIMEOFDAY)
113 gettimeofday (result
, NULL
);
115 jlong val
= _Jv_platform_gettimeofday ();
116 result
->tv_sec
= val
/ 1000;
117 result
->tv_usec
= (val
% 1000) * 1000;
118 #endif /* HAVE_GETTIMEOFDAY */
121 // A wrapper for select() which ignores EINTR.
123 _Jv_select (int n
, fd_set
*readfds
, fd_set
*writefds
,
124 fd_set
*exceptfds
, struct timeval
*timeout
)
127 // If we have a timeout, compute the absolute ending time.
128 struct timeval end
, delay
;
131 internal_gettimeofday (&end
);
132 end
.tv_usec
+= timeout
->tv_usec
;
133 if (end
.tv_usec
>= 1000000)
136 end
.tv_usec
-= 1000000;
138 end
.tv_sec
+= timeout
->tv_sec
;
144 delay
.tv_sec
= delay
.tv_usec
= 0;
149 int r
= select (n
, readfds
, writefds
, exceptfds
,
150 timeout
? &delay
: NULL
);
151 if (r
!= -1 || errno
!= EINTR
)
154 // Here we know we got EINTR.
155 if (java::lang::Thread::interrupted ())
156 throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
158 struct timeval after
;
161 internal_gettimeofday (&after
);
162 // Now compute new timeout argument.
163 delay
.tv_usec
= end
.tv_usec
- after
.tv_usec
;
164 delay
.tv_sec
= end
.tv_sec
- after
.tv_sec
;
165 if (delay
.tv_usec
< 0)
168 delay
.tv_usec
+= 1000000;
170 if (delay
.tv_sec
< 0)
172 // We assume that the user wants a valid select() call
173 // more than precise timing. So if we get a series of
174 // EINTR we just keep trying with delay 0 until we get a
180 #else /* HAVE_SELECT */