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 #if defined(HAVE_PROC_SELF_EXE)
29 static char exec_name
[20];
30 // initialized in _Jv_platform_initialize()
33 const char *_Jv_ThisExecutable (void)
35 #if defined(DISABLE_MAIN_ARGS)
36 return "[Embedded App]";
37 #elif defined(HAVE_PROC_SELF_EXE)
39 // initialized in _Jv_platform_initialize()
41 return _Jv_GetSafeArg (0);
45 // gettimeofday implementation.
47 _Jv_platform_gettimeofday ()
49 #if defined (HAVE_GETTIMEOFDAY)
51 gettimeofday (&tv
, NULL
);
52 return (tv
.tv_sec
* 1000LL) + (tv
.tv_usec
/ 1000LL);
53 #elif defined (HAVE_TIME)
54 return time (NULL
) * 1000LL;
55 #elif defined (HAVE_FTIME)
58 return (t
.time
* 1000LL) + t
.millitm
;
63 // In the absence of any function, time remains forever fixed.
68 // Platform-specific VM initialization.
70 _Jv_platform_initialize (void)
72 #if defined (HAVE_SIGACTION)
73 // We only want this on POSIX systems.
75 act
.sa_handler
= SIG_IGN
;
76 sigemptyset (&act
.sa_mask
);
78 sigaction (SIGPIPE
, &act
, NULL
);
80 signal (SIGPIPE
, SIG_IGN
);
83 #if defined (HAVE_PROC_SELF_EXE)
84 // Compute our executable name
85 sprintf (exec_name
, "/proc/%d/exe", getpid ());
89 // Set platform-specific System properties.
91 _Jv_platform_initProperties (java::util::Properties
* newprops
)
93 // A convenience define.
94 #define SET(Prop,Val) \
95 newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
97 SET ("file.separator", "/");
98 SET ("path.separator", ":");
99 SET ("line.separator", "\n");
100 char *tmpdir
= ::getenv("TMPDIR");
103 SET ("java.io.tmpdir", tmpdir
);
107 internal_gettimeofday (struct timeval
*result
)
109 #if defined (HAVE_GETTIMEOFDAY)
110 gettimeofday (result
, NULL
);
112 jlong val
= _Jv_platform_gettimeofday ();
113 result
->tv_sec
= val
/ 1000;
114 result
->tv_usec
= (val
% 1000) * 1000;
115 #endif /* HAVE_GETTIMEOFDAY */
118 // A wrapper for select() which ignores EINTR.
120 _Jv_select (int n
, fd_set
*readfds
, fd_set
*writefds
,
121 fd_set
*exceptfds
, struct timeval
*timeout
)
124 // If we have a timeout, compute the absolute ending time.
125 struct timeval end
, delay
;
128 internal_gettimeofday (&end
);
129 end
.tv_usec
+= timeout
->tv_usec
;
130 if (end
.tv_usec
>= 1000000)
133 end
.tv_usec
-= 1000000;
135 end
.tv_sec
+= timeout
->tv_sec
;
141 delay
.tv_sec
= delay
.tv_usec
= 0;
146 int r
= select (n
, readfds
, writefds
, exceptfds
,
147 timeout
? &delay
: NULL
);
148 if (r
!= -1 || errno
!= EINTR
)
151 // Here we know we got EINTR.
152 if (java::lang::Thread::interrupted ())
153 throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
155 struct timeval after
;
158 internal_gettimeofday (&after
);
159 // Now compute new timeout argument.
160 delay
.tv_usec
= end
.tv_usec
- after
.tv_usec
;
161 delay
.tv_sec
= end
.tv_sec
- after
.tv_sec
;
162 if (delay
.tv_usec
< 0)
165 delay
.tv_usec
+= 1000000;
167 if (delay
.tv_sec
< 0)
169 // We assume that the user wants a valid select() call
170 // more than precise timing. So if we get a series of
171 // EINTR we just keep trying with delay 0 until we get a
177 #else /* HAVE_SELECT */