ladybird/Ports/OpenJDK/patches/0007-java.base-Update-native-modules-to-support-Serenity.patch
Timur Sultanov 1f886f94bd Ports: Add OpenJDK port
Port of OpenJDK 17.0.2, zero VM only.

More work needed to get the full hotspot VM up and running :^)

Co-Authored-By: Andrew Kaster <akaster@serenityos.org>
2022-06-19 09:05:35 +02:00

606 lines
20 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Timur Sultanov <SultanovTS@yandex.ru>
Date: Wed, 16 Feb 2022 21:06:23 +0300
Subject: [PATCH] java.base: Update native modules to support Serenity
---
.../nio/ch/SocketOptionRegistry.java.template | 21 +++++
src/java.base/share/native/libjava/io_util.h | 5 +-
src/java.base/share/native/libjli/jli_util.h | 3 +
src/java.base/share/native/libzip/zip_util.c | 2 +-
.../sun/nio/fs/UnixConstants.java.template | 92 ++++++++++++++++++-
.../native/libjava/ProcessHandleImpl_unix.c | 2 +-
.../unix/native/libjava/TimeZone_md.c | 4 +-
.../unix/native/libjava/io_util_md.c | 2 +-
.../unix/native/libjava/io_util_md.h | 6 +-
src/java.base/unix/native/libjsig/jsig.c | 4 +
.../unix/native/libnet/Inet4AddressImpl.c | 3 +
.../unix/native/libnet/Inet6AddressImpl.c | 4 +
.../unix/native/libnet/NetworkInterface.c | 11 ++-
.../unix/native/libnet/net_util_md.h | 4 +
.../unix/native/libnio/MappedMemoryUtils.c | 4 +
.../native/libnio/ch/DatagramDispatcher.c | 1 +
.../unix/native/libnio/ch/FileChannelImpl.c | 3 +
.../native/libnio/ch/FileDispatcherImpl.c | 7 +-
src/java.base/unix/native/libnio/ch/FileKey.c | 2 +-
.../unix/native/libnio/ch/NativeThread.c | 2 +-
src/java.base/unix/native/libnio/ch/Net.c | 2 +-
.../native/libnio/fs/UnixNativeDispatcher.c | 39 +++++++-
22 files changed, 207 insertions(+), 16 deletions(-)
diff --git a/src/java.base/share/classes/sun/nio/ch/SocketOptionRegistry.java.template b/src/java.base/share/classes/sun/nio/ch/SocketOptionRegistry.java.template
index 0672ced15..55bde3569 100644
--- a/src/java.base/share/classes/sun/nio/ch/SocketOptionRegistry.java.template
+++ b/src/java.base/share/classes/sun/nio/ch/SocketOptionRegistry.java.template
@@ -50,6 +50,24 @@
#endif
#endif
+#define SO_RCVTIMEO 0
+#define SO_SNDTIMEO 1
+#define SO_TYPE 2
+#define SO_ERROR 3
+#define SO_PEERCRED 4
+#define SO_SNDBUF 5
+#define SO_RCVBUF 6
+#define SO_DEBUG 7
+#define SO_REUSEADDR 8
+#define SO_BINDTODEVICE 9
+#define SO_KEEPALIVE 10
+#define SO_TIMESTAMP 11
+#define SO_BROADCAST 12
+#define SO_LINGER 13
+#define SO_ACCEPTCONN 14
+#define SO_DONTROUTE 15
+#define SO_OOBINLINE 16
+
/* To be able to name the Java constants the same as the C constants without
having the preprocessor rewrite those identifiers, add PREFIX_ to all
identifiers matching a C constant. The PREFIX_ is filtered out in the
@@ -125,6 +143,9 @@ class SocketOptionRegistry {
#ifdef AF_INET6
// IPPROTO_IPV6 is 41
+#ifdef SERENITY
+#define IPV6_TCLASS 1
+#endif
map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_TOS,
StandardProtocolFamily.INET6), new OptionKey(41, IPV6_TCLASS));
map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_IF,
diff --git a/src/java.base/share/native/libjava/io_util.h b/src/java.base/share/native/libjava/io_util.h
index 6e960c034..cbd1d087e 100644
--- a/src/java.base/share/native/libjava/io_util.h
+++ b/src/java.base/share/native/libjava/io_util.h
@@ -30,11 +30,14 @@ extern jfieldID IO_fd_fdID;
extern jfieldID IO_handle_fdID;
extern jfieldID IO_append_fdID;
-#ifdef _ALLBSD_SOURCE
+#if defined(_ALLBSD_SOURCE) || defined(SERENITY)
#include <fcntl.h>
#ifndef O_SYNC
#define O_SYNC O_FSYNC
#endif
+#if defined(SERENITY)
+#define O_DSYNC O_SYNC
+#endif
#ifndef O_DSYNC
#define O_DSYNC O_FSYNC
#endif
diff --git a/src/java.base/share/native/libjli/jli_util.h b/src/java.base/share/native/libjli/jli_util.h
index 3512b1e96..e60f7581f 100644
--- a/src/java.base/share/native/libjli/jli_util.h
+++ b/src/java.base/share/native/libjli/jli_util.h
@@ -108,6 +108,9 @@ JLI_CmdToArgs(char *cmdline);
#define _LARGFILE64_SOURCE
#define JLI_Lseek lseek64
#endif
+#ifdef SERENITY
+#define JLI_Lseek lseek
+#endif
#ifdef MACOSX
#define JLI_Lseek lseek
#endif
diff --git a/src/java.base/share/native/libzip/zip_util.c b/src/java.base/share/native/libzip/zip_util.c
index fbbd9d850..eef30b9e4 100644
--- a/src/java.base/share/native/libzip/zip_util.c
+++ b/src/java.base/share/native/libzip/zip_util.c
@@ -46,7 +46,7 @@
#include "zip_util.h"
#include <zlib.h>
-#ifdef _ALLBSD_SOURCE
+#if defined(_ALLBSD_SOURCE) || defined(SERENITY)
#define off64_t off_t
#define mmap64 mmap
#endif
diff --git a/src/java.base/unix/classes/sun/nio/fs/UnixConstants.java.template b/src/java.base/unix/classes/sun/nio/fs/UnixConstants.java.template
index d60283d24..5d428521b 100644
--- a/src/java.base/unix/classes/sun/nio/fs/UnixConstants.java.template
+++ b/src/java.base/unix/classes/sun/nio/fs/UnixConstants.java.template
@@ -31,6 +31,95 @@
#include <fcntl.h>
#include <sys/stat.h>
+#ifdef SERENITY
+
+#define ESUCCESS 0
+#define EPERM 1
+#define ENOENT 2
+#define ESRCH 3
+#define EINTR 4
+#define EIO 5
+#define ENXIO 6
+#define E2BIG 7
+#define ENOEXEC 8
+#define EBADF 9
+#define ECHILD 10
+#define EAGAIN 12
+#define ENOMEM 12
+#define EACCES 13
+#define EFAULT 14
+#define ENOTBLK 15
+#define EBUSY 16
+#define EEXIST 17
+#define EXDEV 18
+#define ENODEV 19
+#define ENOTDIR 20
+#define EISDIR 21
+#define EINVAL 22
+#define ENFILE 23
+#define EMFILE 24
+#define ENOTTY 25
+#define ETXTBSY 26
+#define EFBIG 27
+#define ENOSPC 28
+#define ESPIPE 29
+#define EROFS 30
+#define EMLINK 31
+#define EPIPE 32
+#define ERANGE 33
+#define ENAMETOOLONG 34
+#define ELOOP 35
+#define EOVERFLOW 36
+#define EOPNOTSUPP 37
+#define ENOSYS 38
+#define ENOTIMPL 39
+#define EAFNOSUPPORT 40
+#define ENOTSOCK 41
+#define EADDRINUSE 42
+#define ENOTEMPTY 43
+#define EDOM 44
+#define ECONNREFUSED 45
+#define EHOSTDOWN 46
+#define EADDRNOTAVAIL 47
+#define EISCONN 48
+#define ECONNABORTED 49
+#define EALREADY 50
+#define ECONNRESET 51
+#define EDESTADDRREQ 52
+#define EHOSTUNREACH 53
+#define EILSEQ 54
+#define EMSGSIZE 55
+#define ENETDOWN 56
+#define ENETUNREACH 57
+#define ENETRESET 58
+#define ENOBUFS 59
+#define ENOLCK 60
+#define ENOMSG 61
+#define ENOPROTOOPT 62
+#define ENOTCONN 63
+#define ESHUTDOWN 64
+#define ETOOMANYREFS 65
+#define EPROTONOSUPPORT 66
+#define ESOCKTNOSUPPORT 67
+#define EDEADLK 68
+#define ETIMEDOUT 69
+#define EPROTOTYPE 70
+#define EINPROGRESS 71
+#define ENOTHREAD 72
+#define EPROTO 73
+#define ENOTSUP 74
+#define EPFNOSUPPORT 75
+#define EDQUOT 76
+#define EDIRINTOSELF 77
+#define ENOTRECOVERABLE 78
+#define ECANCELED 79
+#define EMAXERRNO 80
+
+
+#define EWOULDBLOCK EAGAIN //Serenity doesn't define it
+#define ENODATA EMAXERRNO
+#endif
+
/* To be able to name the Java constants the same as the C constants without
having the preprocessor rewrite those identifiers, add PREFIX_ to all
identifiers matching a C constant. The PREFIX_ is filtered out in the
@@ -120,7 +210,7 @@ class UnixConstants {
// fgetxattr error codes for absent attributes depend on the OS:
#ifdef _ALLBSD_SOURCE
static final int PREFIX_XATTR_NOT_FOUND = ENOATTR;
-#elif __linux__
+#elif defined(__linux__)
static final int PREFIX_XATTR_NOT_FOUND = ENODATA;
#else
// not supported (dummy values will not be used at runtime).
diff --git a/src/java.base/unix/native/libjava/ProcessHandleImpl_unix.c b/src/java.base/unix/native/libjava/ProcessHandleImpl_unix.c
index d53e88764..eddb5f169 100644
--- a/src/java.base/unix/native/libjava/ProcessHandleImpl_unix.c
+++ b/src/java.base/unix/native/libjava/ProcessHandleImpl_unix.c
@@ -488,7 +488,7 @@ void unix_getUserInfo(JNIEnv* env, jobject jinfo, uid_t uid) {
* The following functions are common on Solaris, Linux and AIX.
*/
-#if defined (__linux__) || defined(_AIX)
+#if defined (__linux__) || defined(_AIX) || defined(SERENITY)
/*
* Returns the children of the requested pid and optionally each parent and
diff --git a/src/java.base/unix/native/libjava/TimeZone_md.c b/src/java.base/unix/native/libjava/TimeZone_md.c
index 94dfc207f..2a6c3851a 100644
--- a/src/java.base/unix/native/libjava/TimeZone_md.c
+++ b/src/java.base/unix/native/libjava/TimeZone_md.c
@@ -53,7 +53,7 @@ static char *isFileIdentical(char* buf, size_t size, char *pathname);
#define filegets fgets
#define fileclose fclose
-#if defined(_ALLBSD_SOURCE)
+#if defined(_ALLBSD_SOURCE) || defined(SERENITY)
#define stat64 stat
#define lstat64 lstat
#define fstat64 fstat
@@ -75,7 +75,7 @@ static const char popularZones[][4] = {"UTC", "GMT"};
static const char *ETC_ENVIRONMENT_FILE = "/etc/environment";
#endif
-#if defined(__linux__) || defined(MACOSX)
+#if defined(__linux__) || defined(MACOSX) || defined(SERENITY)
/*
* Returns a pointer to the zone ID portion of the given zoneinfo file
diff --git a/src/java.base/unix/native/libjava/io_util_md.c b/src/java.base/unix/native/libjava/io_util_md.c
index e207c57d4..8afabc544 100644
--- a/src/java.base/unix/native/libjava/io_util_md.c
+++ b/src/java.base/unix/native/libjava/io_util_md.c
@@ -30,7 +30,7 @@
#include <string.h>
#include <unistd.h>
-#if defined(__linux__) || defined(_ALLBSD_SOURCE) || defined(_AIX)
+#if defined(__linux__) || defined(_ALLBSD_SOURCE) || defined(_AIX) || defined(SERENITY)
#include <sys/ioctl.h>
#endif
diff --git a/src/java.base/unix/native/libjava/io_util_md.h b/src/java.base/unix/native/libjava/io_util_md.h
index 3dccf64f4..3e9e7d3b0 100644
--- a/src/java.base/unix/native/libjava/io_util_md.h
+++ b/src/java.base/unix/native/libjava/io_util_md.h
@@ -66,7 +66,7 @@ FD getFD(JNIEnv *env, jobject cur, jfieldID fid);
#define IO_SetLength handleSetLength
#define IO_GetLength handleGetLength
-#ifdef _ALLBSD_SOURCE
+#if defined(_ALLBSD_SOURCE) || defined(SERENITY)
#define open64 open
#define fstat64 fstat
#define stat64 stat
@@ -77,6 +77,10 @@ FD getFD(JNIEnv *env, jobject cur, jfieldID fid);
#define IO_Lseek lseek64
#endif
+#ifdef SERENITY
+#define statvfs64 statvfs
+#endif
+
/*
* On Solaris, the handle field is unused
*/
diff --git a/src/java.base/unix/native/libjsig/jsig.c b/src/java.base/unix/native/libjsig/jsig.c
index 1108b2f9c..d891aab93 100644
--- a/src/java.base/unix/native/libjsig/jsig.c
+++ b/src/java.base/unix/native/libjsig/jsig.c
@@ -100,6 +100,10 @@ static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
bool is_sigset) {
sa_handler_t res;
+#ifdef SERENITY
+#define RTLD_NEXT 0 //stub out RTLD_NEXT
+#endif
+
if (os_signal == NULL) {
// Deprecation warning first time through
printf(HOTSPOT_VM_DISTRO " VM warning: the use of signal() and sigset() "
diff --git a/src/java.base/unix/native/libnet/Inet4AddressImpl.c b/src/java.base/unix/native/libnet/Inet4AddressImpl.c
index b165be7ce..a4cd70003 100644
--- a/src/java.base/unix/native/libnet/Inet4AddressImpl.c
+++ b/src/java.base/unix/native/libnet/Inet4AddressImpl.c
@@ -335,6 +335,7 @@ static jboolean
ping4(JNIEnv *env, jint fd, SOCKETADDRESS *sa, SOCKETADDRESS *netif,
jint timeout, jint ttl)
{
+#ifndef SERENITY
jint n, size = 60 * 1024, hlen, tmout2, seq = 1;
socklen_t len;
unsigned char sendbuf[1500], recvbuf[1500];
@@ -438,6 +439,8 @@ ping4(JNIEnv *env, jint fd, SOCKETADDRESS *sa, SOCKETADDRESS *netif,
timeout -= 1000;
} while (timeout > 0);
close(fd);
+#endif
+ //FIXME implement setsockopt(IPPROTO_IP)
return JNI_FALSE;
}
diff --git a/src/java.base/unix/native/libnet/Inet6AddressImpl.c b/src/java.base/unix/native/libnet/Inet6AddressImpl.c
index 058f3d3a7..61460fda8 100644
--- a/src/java.base/unix/native/libnet/Inet6AddressImpl.c
+++ b/src/java.base/unix/native/libnet/Inet6AddressImpl.c
@@ -29,7 +29,9 @@
#include <sys/time.h>
#include <sys/types.h>
#include <netinet/in.h>
+#ifndef SERENITY
#include <netinet/icmp6.h>
+#endif
#if defined(_ALLBSD_SOURCE)
#include <ifaddrs.h>
@@ -539,6 +541,7 @@ static jboolean
ping6(JNIEnv *env, jint fd, SOCKETADDRESS *sa, SOCKETADDRESS *netif,
jint timeout, jint ttl)
{
+#ifndef SERENITY
jint n, size = 60 * 1024, tmout2, seq = 1;
socklen_t len;
unsigned char sendbuf[1500], recvbuf[1500];
@@ -643,6 +646,7 @@ ping6(JNIEnv *env, jint fd, SOCKETADDRESS *sa, SOCKETADDRESS *netif,
timeout -= 1000;
} while (timeout > 0);
close(fd);
+#endif
return JNI_FALSE;
}
diff --git a/src/java.base/unix/native/libnet/NetworkInterface.c b/src/java.base/unix/native/libnet/NetworkInterface.c
index 990bc1bcc..fdeff2bf6 100644
--- a/src/java.base/unix/native/libnet/NetworkInterface.c
+++ b/src/java.base/unix/native/libnet/NetworkInterface.c
@@ -43,6 +43,10 @@
#include <ifaddrs.h>
#endif
+#if defined(SERENITY)
+#include <ifaddrs.h>
+#endif
+
#include "net_util.h"
#include "java_net_InetAddress.h"
@@ -1351,7 +1355,7 @@ static int getMacAddress
static int getMTU(JNIEnv *env, int sock, const char *ifname) {
struct ifreq if2;
memset((char *)&if2, 0, sizeof(if2));
- strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
+ memcpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
JNU_ThrowByNameWithMessageAndLastError
@@ -1664,7 +1668,7 @@ static int getFlags(int sock, const char *ifname, int *flags) {
#endif /* _AIX */
/** BSD **/
-#if defined(_ALLBSD_SOURCE)
+#if defined(_ALLBSD_SOURCE) || defined(SERENITY)
/*
* Opens a socket for further ioctl calls. Tries AF_INET socket first and
@@ -1803,6 +1807,7 @@ static int getMacAddress
(JNIEnv *env, const char *ifname, const struct in_addr *addr,
unsigned char *buf)
{
+#ifndef SERENITY // FIXME: define sockaddr_dl in net/if_dl.h
struct ifaddrs *ifa0, *ifa;
struct sockaddr *saddr;
int i;
@@ -1827,7 +1832,7 @@ static int getMacAddress
}
freeifaddrs(ifa0);
}
-
+#endif
return -1;
}
diff --git a/src/java.base/unix/native/libnet/net_util_md.h b/src/java.base/unix/native/libnet/net_util_md.h
index 68835987b..f99b11207 100644
--- a/src/java.base/unix/native/libnet/net_util_md.h
+++ b/src/java.base/unix/native/libnet/net_util_md.h
@@ -30,6 +30,10 @@
#include <poll.h>
#include <sys/socket.h>
+#ifdef SERENITY
+#include <netinet/in.h>
+#endif
+
/************************************************************************
* Macros and constants
*/
diff --git a/src/java.base/unix/native/libnio/MappedMemoryUtils.c b/src/java.base/unix/native/libnio/MappedMemoryUtils.c
index e90acd286..34787fff4 100644
--- a/src/java.base/unix/native/libnio/MappedMemoryUtils.c
+++ b/src/java.base/unix/native/libnio/MappedMemoryUtils.c
@@ -58,6 +58,7 @@ JNIEXPORT jboolean JNICALL
Java_java_nio_MappedMemoryUtils_isLoaded0(JNIEnv *env, jobject obj, jlong address,
jlong len, jlong numPages)
{
+#ifndef SERENITY
jboolean loaded = JNI_TRUE;
int result = 0;
long i = 0;
@@ -100,6 +101,9 @@ Java_java_nio_MappedMemoryUtils_isLoaded0(JNIEnv *env, jobject obj, jlong addres
}
free(vec);
return loaded;
+#else
+ return JNI_FALSE; //FIXME implement mincore() or equivalent
+#endif
}
diff --git a/src/java.base/unix/native/libnio/ch/FileChannelImpl.c b/src/java.base/unix/native/libnio/ch/FileChannelImpl.c
index 8cc753e6a..28fa55269 100644
--- a/src/java.base/unix/native/libnio/ch/FileChannelImpl.c
+++ b/src/java.base/unix/native/libnio/ch/FileChannelImpl.c
@@ -39,6 +39,9 @@
#include <sys/uio.h>
#define lseek64 lseek
#define mmap64 mmap
+#elif defined(SERENITY)
+#define lseek64 lseek
+#define mmap64 mmap
#endif
#include "jni.h"
diff --git a/src/java.base/unix/native/libnio/ch/FileDispatcherImpl.c b/src/java.base/unix/native/libnio/ch/FileDispatcherImpl.c
index c0c31d6ab..3a4d8a632 100644
--- a/src/java.base/unix/native/libnio/ch/FileDispatcherImpl.c
+++ b/src/java.base/unix/native/libnio/ch/FileDispatcherImpl.c
@@ -40,7 +40,7 @@
#include <sys/ioctl.h>
#endif
-#if defined(_ALLBSD_SOURCE)
+#if defined(_ALLBSD_SOURCE) || defined(SERENITY)
#define lseek64 lseek
#define stat64 stat
#define flock64 flock
@@ -54,6 +54,11 @@
#define fdatasync fsync
#endif
+#ifdef SERENITY
+#define statvfs64 statvfs
+#define fstatvfs64 fstatvfs
+#endif
+
#include "jni.h"
#include "jni_util.h"
#include "jvm.h"
diff --git a/src/java.base/unix/native/libnio/ch/FileKey.c b/src/java.base/unix/native/libnio/ch/FileKey.c
index bdb42a632..a433cdf01 100644
--- a/src/java.base/unix/native/libnio/ch/FileKey.c
+++ b/src/java.base/unix/native/libnio/ch/FileKey.c
@@ -30,7 +30,7 @@
#include "nio_util.h"
#include "sun_nio_ch_FileKey.h"
-#ifdef _ALLBSD_SOURCE
+#if defined(_ALLBSD_SOURCE) || defined(SERENITY)
#define stat64 stat
#define fstat64 fstat
diff --git a/src/java.base/unix/native/libnio/ch/NativeThread.c b/src/java.base/unix/native/libnio/ch/NativeThread.c
index 92dcb9e56..4bf09d03b 100644
--- a/src/java.base/unix/native/libnio/ch/NativeThread.c
+++ b/src/java.base/unix/native/libnio/ch/NativeThread.c
@@ -40,7 +40,7 @@
#elif defined(_AIX)
/* Also defined in net/aix_close.c */
#define INTERRUPT_SIGNAL (SIGRTMAX - 1)
-#elif defined(_ALLBSD_SOURCE)
+#elif defined(_ALLBSD_SOURCE) || defined(SERENITY)
/* Also defined in net/bsd_close.c */
#define INTERRUPT_SIGNAL SIGIO
#else
diff --git a/src/java.base/unix/native/libnio/ch/Net.c b/src/java.base/unix/native/libnio/ch/Net.c
index 42a07359d..ca1401861 100644
--- a/src/java.base/unix/native/libnio/ch/Net.c
+++ b/src/java.base/unix/native/libnio/ch/Net.c
@@ -701,7 +701,7 @@ JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_blockOrUnblock6(JNIEnv *env, jobject this, jboolean block, jobject fdo,
jbyteArray group, jint index, jbyteArray source)
{
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(SERENITY)
/* no IPv6 exclude-mode filtering for now */
return IOS_UNAVAILABLE;
#else
diff --git a/src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c b/src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c
index 9df8be1e6..993e240db 100644
--- a/src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c
+++ b/src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c
@@ -57,7 +57,7 @@
#include <string.h>
#endif
-#ifdef _ALLBSD_SOURCE
+#if defined(_ALLBSD_SOURCE) || defined(SERENITY)
#include <string.h>
#define stat64 stat
@@ -1114,6 +1114,7 @@ Java_sun_nio_fs_UnixNativeDispatcher_getgrgid(JNIEnv* env, jclass this, jint gid
int retry;
/* initial size of buffer for group record */
+#ifndef SERENITY
buflen = (int)sysconf(_SC_GETGR_R_SIZE_MAX);
if (buflen == -1)
buflen = ENT_BUF_SIZE;
@@ -1156,6 +1157,24 @@ Java_sun_nio_fs_UnixNativeDispatcher_getgrgid(JNIEnv* env, jclass this, jint gid
} while (retry);
+#else
+ // FIXME: Not thread safe, implement getgrgid_r
+ errno = 0;
+ struct group * g = getgrgid(gid);
+ if (g == NULL || g->gr_name == NULL || *(g->gr_name) == '\0') {
+ if (errno == 0)
+ errno = ENOENT;
+ throwUnixException(env, errno);
+ }
+ else {
+ jsize len = strlen(g->gr_name);
+ result = (*env)->NewByteArray(env, len);
+ if (result != NULL) {
+ (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)(g->gr_name));
+ }
+ }
+#endif
+
return result;
}
@@ -1204,6 +1223,7 @@ Java_sun_nio_fs_UnixNativeDispatcher_getgrnam0(JNIEnv* env, jclass this,
jlong nameAddress)
{
jint gid = -1;
+#ifndef SERENITY
int buflen, retry;
/* initial size of buffer for group record */
@@ -1248,6 +1268,23 @@ Java_sun_nio_fs_UnixNativeDispatcher_getgrnam0(JNIEnv* env, jclass this,
free(grbuf);
} while (retry);
+#else
+ // FIXME: Not thread safe, implement getgrnam_r
+ const char* name = (const char*)jlong_to_ptr(nameAddress);
+ errno = 0;
+ struct group * g = getgrnam(name);
+ if (g == NULL || g->gr_name == NULL || *(g->gr_name) == '\0') {
+ /* not found or error */
+ if (errno != 0 && errno != ENOENT && errno != ESRCH &&
+ errno != EBADF && errno != EPERM)
+ {
+ throwUnixException(env, errno);
+ }
+ }
+ else {
+ gid = g->gr_gid;
+ }
+#endif
return gid;
}