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
21 #include <java/lang/Thread.h>
22 #include <java/io/InterruptedIOException.h>
23 #include <java/util/Properties.h>
26 extern "C" unsigned long long _clock (void);
29 #if defined(HAVE_PROC_SELF_EXE)
30 static char exec_name
[20];
31 // initialized in _Jv_platform_initialize()
34 const char *_Jv_ThisExecutable (void)
36 #if defined(DISABLE_MAIN_ARGS)
37 return "[Embedded App]";
38 #elif defined(HAVE_PROC_SELF_EXE)
40 // initialized in _Jv_platform_initialize()
42 return _Jv_GetSafeArg (0);
46 // gettimeofday implementation.
48 _Jv_platform_gettimeofday ()
50 #if defined (HAVE_GETTIMEOFDAY)
52 gettimeofday (&tv
, NULL
);
53 return (tv
.tv_sec
* 1000LL) + (tv
.tv_usec
/ 1000LL);
54 #elif defined (HAVE_TIME)
55 return time (NULL
) * 1000LL;
56 #elif defined (HAVE_FTIME)
59 return (t
.time
* 1000LL) + t
.millitm
;
64 // In the absence of any function, time remains forever fixed.
69 // Platform-specific VM initialization.
71 _Jv_platform_initialize (void)
73 #if defined (HAVE_SIGACTION)
74 // We only want this on POSIX systems.
76 act
.sa_handler
= SIG_IGN
;
77 sigemptyset (&act
.sa_mask
);
79 sigaction (SIGPIPE
, &act
, NULL
);
81 signal (SIGPIPE
, SIG_IGN
);
84 #if defined (HAVE_PROC_SELF_EXE)
85 // Compute our executable name
86 sprintf (exec_name
, "/proc/%d/exe", getpid ());
90 // Set platform-specific System properties.
92 _Jv_platform_initProperties (java::util::Properties
* newprops
)
94 // A convenience define.
95 #define SET(Prop,Val) \
96 newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
98 SET ("file.separator", "/");
99 SET ("path.separator", ":");
100 SET ("line.separator", "\n");
101 char *tmpdir
= ::getenv("TMPDIR");
104 SET ("java.io.tmpdir", tmpdir
);
108 internal_gettimeofday (struct timeval
*result
)
110 #if defined (HAVE_GETTIMEOFDAY)
111 gettimeofday (result
, NULL
);
113 jlong val
= _Jv_platform_gettimeofday ();
114 result
->tv_sec
= val
/ 1000;
115 result
->tv_usec
= (val
% 1000) * 1000;
116 #endif /* HAVE_GETTIMEOFDAY */
119 // A wrapper for select() which ignores EINTR.
121 _Jv_select (int n
, fd_set
*readfds
, fd_set
*writefds
,
122 fd_set
*exceptfds
, struct timeval
*timeout
)
125 // If we have a timeout, compute the absolute ending time.
126 struct timeval end
, delay
;
129 internal_gettimeofday (&end
);
130 end
.tv_usec
+= timeout
->tv_usec
;
131 if (end
.tv_usec
>= 1000000)
134 end
.tv_usec
-= 1000000;
136 end
.tv_sec
+= timeout
->tv_sec
;
142 delay
.tv_sec
= delay
.tv_usec
= 0;
147 int r
= select (n
, readfds
, writefds
, exceptfds
,
148 timeout
? &delay
: NULL
);
149 if (r
!= -1 || errno
!= EINTR
)
152 // Here we know we got EINTR.
153 if (java::lang::Thread::interrupted ())
154 throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
156 struct timeval after
;
159 internal_gettimeofday (&after
);
160 // Now compute new timeout argument.
161 delay
.tv_usec
= end
.tv_usec
- after
.tv_usec
;
162 delay
.tv_sec
= end
.tv_sec
- after
.tv_sec
;
163 if (delay
.tv_usec
< 0)
166 delay
.tv_usec
+= 1000000;
168 if (delay
.tv_sec
< 0)
170 // We assume that the user wants a valid select() call
171 // more than precise timing. So if we get a series of
172 // EINTR we just keep trying with delay 0 until we get a
178 #else /* HAVE_SELECT */