1 /*
   2  * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <stdio.h>
  27 #include <stdlib.h>
  28 #include <limits.h>
  29 #include <fcntl.h>
  30 #include <dirent.h>
  31 #include <unistd.h>
  32 #include <pwd.h>
  33 #include <grp.h>
  34 #include <errno.h>
  35 #include <dlfcn.h>
  36 #include <sys/types.h>
  37 #include <sys/stat.h>
  38 #ifdef MACOSX
  39 #include <sys/param.h>
  40 #include <sys/mount.h>
  41 #else
  42 #include <sys/statvfs.h>
  43 #endif
  44 #include <sys/time.h>
  45 
  46 #ifdef __solaris__
  47 #include <strings.h>
  48 #endif
  49 
  50 #if defined(__linux__) || defined(_AIX)
  51 #include <string.h>
  52 #endif
  53 
  54 #ifdef _ALLBSD_SOURCE
  55 #include <string.h>
  56 
  57 #define stat64 stat
  58 #ifndef MACOSX
  59 #define statvfs64 statvfs
  60 #endif
  61 
  62 #define open64 open
  63 #define fstat64 fstat
  64 #define lstat64 lstat
  65 #define dirent64 dirent
  66 #define readdir64_r readdir_r
  67 #endif
  68 
  69 #include "jni.h"
  70 #include "jni_util.h"
  71 #include "jlong.h"
  72 
  73 #include "sun_nio_fs_UnixNativeDispatcher.h"
  74 
  75 /**
  76  * Size of password or group entry when not available via sysconf
  77  */
  78 #define ENT_BUF_SIZE   1024
  79 
  80 #define RESTARTABLE(_cmd, _result) do { \
  81   do { \
  82     _result = _cmd; \
  83   } while((_result == -1) && (errno == EINTR)); \
  84 } while(0)
  85 
  86 #define RESTARTABLE_RETURN_PTR(_cmd, _result) do { \
  87   do { \
  88     _result = _cmd; \
  89   } while((_result == NULL) && (errno == EINTR)); \
  90 } while(0)
  91 
  92 static jfieldID attrs_st_mode;
  93 static jfieldID attrs_st_ino;
  94 static jfieldID attrs_st_dev;
  95 static jfieldID attrs_st_rdev;
  96 static jfieldID attrs_st_nlink;
  97 static jfieldID attrs_st_uid;
  98 static jfieldID attrs_st_gid;
  99 static jfieldID attrs_st_size;
 100 static jfieldID attrs_st_atime_sec;
 101 static jfieldID attrs_st_atime_nsec;
 102 static jfieldID attrs_st_mtime_sec;
 103 static jfieldID attrs_st_mtime_nsec;
 104 static jfieldID attrs_st_ctime_sec;
 105 static jfieldID attrs_st_ctime_nsec;
 106 
 107 #ifdef _DARWIN_FEATURE_64_BIT_INODE
 108 static jfieldID attrs_st_birthtime_sec;
 109 #endif
 110 
 111 static jfieldID attrs_f_frsize;
 112 static jfieldID attrs_f_blocks;
 113 static jfieldID attrs_f_bfree;
 114 static jfieldID attrs_f_bavail;
 115 
 116 static jfieldID entry_name;
 117 static jfieldID entry_dir;
 118 static jfieldID entry_fstype;
 119 static jfieldID entry_options;
 120 static jfieldID entry_dev;
 121 
 122 /**
 123  * System calls that may not be available at run time.
 124  */
 125 typedef int openat64_func(int, const char *, int, ...);
 126 typedef int fstatat64_func(int, const char *, struct stat64 *, int);
 127 typedef int unlinkat_func(int, const char*, int);
 128 typedef int renameat_func(int, const char*, int, const char*);
 129 typedef int futimesat_func(int, const char *, const struct timeval *);
 130 typedef DIR* fdopendir_func(int);
 131 
 132 static openat64_func* my_openat64_func = NULL;
 133 static fstatat64_func* my_fstatat64_func = NULL;
 134 static unlinkat_func* my_unlinkat_func = NULL;
 135 static renameat_func* my_renameat_func = NULL;
 136 static futimesat_func* my_futimesat_func = NULL;
 137 static fdopendir_func* my_fdopendir_func = NULL;
 138 
 139 /**
 140  * fstatat missing from glibc on Linux. Temporary workaround
 141  * for x86/x64.
 142  */
 143 #if defined(__linux__) && defined(__i386)
 144 #define FSTATAT64_SYSCALL_AVAILABLE
 145 static int fstatat64_wrapper(int dfd, const char *path,
 146                              struct stat64 *statbuf, int flag)
 147 {
 148     #ifndef __NR_fstatat64
 149     #define __NR_fstatat64  300
 150     #endif
 151     return syscall(__NR_fstatat64, dfd, path, statbuf, flag);
 152 }
 153 #endif
 154 
 155 #if defined(__linux__) && defined(__x86_64__)
 156 #define FSTATAT64_SYSCALL_AVAILABLE
 157 static int fstatat64_wrapper(int dfd, const char *path,
 158                              struct stat64 *statbuf, int flag)
 159 {
 160     #ifndef __NR_newfstatat
 161     #define __NR_newfstatat  262
 162     #endif
 163     return syscall(__NR_newfstatat, dfd, path, statbuf, flag);
 164 }
 165 #endif
 166 
 167 /**
 168  * Call this to throw an internal UnixException when a system/library
 169  * call fails
 170  */
 171 static void throwUnixException(JNIEnv* env, int errnum) {
 172     jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException",
 173         "(I)V", errnum);
 174     if (x != NULL) {
 175         (*env)->Throw(env, x);
 176     }
 177 }
 178 
 179 /**
 180  * Initialization
 181  */
 182 JNIEXPORT jint JNICALL
 183 Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this)
 184 {
 185     jint capabilities = 0;
 186     jclass clazz;
 187 
 188     clazz = (*env)->FindClass(env, "sun/nio/fs/UnixFileAttributes");
 189     CHECK_NULL_RETURN(clazz, 0);
 190     attrs_st_mode = (*env)->GetFieldID(env, clazz, "st_mode", "I");
 191     CHECK_NULL_RETURN(attrs_st_mode, 0);
 192     attrs_st_ino = (*env)->GetFieldID(env, clazz, "st_ino", "J");
 193     CHECK_NULL_RETURN(attrs_st_ino, 0);
 194     attrs_st_dev = (*env)->GetFieldID(env, clazz, "st_dev", "J");
 195     CHECK_NULL_RETURN(attrs_st_dev, 0);
 196     attrs_st_rdev = (*env)->GetFieldID(env, clazz, "st_rdev", "J");
 197     CHECK_NULL_RETURN(attrs_st_rdev, 0);
 198     attrs_st_nlink = (*env)->GetFieldID(env, clazz, "st_nlink", "I");
 199     CHECK_NULL_RETURN(attrs_st_nlink, 0);
 200     attrs_st_uid = (*env)->GetFieldID(env, clazz, "st_uid", "I");
 201     CHECK_NULL_RETURN(attrs_st_uid, 0);
 202     attrs_st_gid = (*env)->GetFieldID(env, clazz, "st_gid", "I");
 203     CHECK_NULL_RETURN(attrs_st_gid, 0);
 204     attrs_st_size = (*env)->GetFieldID(env, clazz, "st_size", "J");
 205     CHECK_NULL_RETURN(attrs_st_size, 0);
 206     attrs_st_atime_sec = (*env)->GetFieldID(env, clazz, "st_atime_sec", "J");
 207     CHECK_NULL_RETURN(attrs_st_atime_sec, 0);
 208     attrs_st_atime_nsec = (*env)->GetFieldID(env, clazz, "st_atime_nsec", "J");
 209     CHECK_NULL_RETURN(attrs_st_atime_nsec, 0);
 210     attrs_st_mtime_sec = (*env)->GetFieldID(env, clazz, "st_mtime_sec", "J");
 211     CHECK_NULL_RETURN(attrs_st_mtime_sec, 0);
 212     attrs_st_mtime_nsec = (*env)->GetFieldID(env, clazz, "st_mtime_nsec", "J");
 213     CHECK_NULL_RETURN(attrs_st_mtime_nsec, 0);
 214     attrs_st_ctime_sec = (*env)->GetFieldID(env, clazz, "st_ctime_sec", "J");
 215     CHECK_NULL_RETURN(attrs_st_ctime_sec, 0);
 216     attrs_st_ctime_nsec = (*env)->GetFieldID(env, clazz, "st_ctime_nsec", "J");
 217     CHECK_NULL_RETURN(attrs_st_ctime_nsec, 0);
 218 
 219 #ifdef _DARWIN_FEATURE_64_BIT_INODE
 220     attrs_st_birthtime_sec = (*env)->GetFieldID(env, clazz, "st_birthtime_sec", "J");
 221     CHECK_NULL_RETURN(attrs_st_birthtime_sec, 0);
 222 #endif
 223 
 224     clazz = (*env)->FindClass(env, "sun/nio/fs/UnixFileStoreAttributes");
 225     CHECK_NULL_RETURN(clazz, 0);
 226     attrs_f_frsize = (*env)->GetFieldID(env, clazz, "f_frsize", "J");
 227     CHECK_NULL_RETURN(attrs_f_frsize, 0);
 228     attrs_f_blocks = (*env)->GetFieldID(env, clazz, "f_blocks", "J");
 229     CHECK_NULL_RETURN(attrs_f_blocks, 0);
 230     attrs_f_bfree = (*env)->GetFieldID(env, clazz, "f_bfree", "J");
 231     CHECK_NULL_RETURN(attrs_f_bfree, 0);
 232     attrs_f_bavail = (*env)->GetFieldID(env, clazz, "f_bavail", "J");
 233     CHECK_NULL_RETURN(attrs_f_bavail, 0);
 234 
 235     clazz = (*env)->FindClass(env, "sun/nio/fs/UnixMountEntry");
 236     CHECK_NULL_RETURN(clazz, 0);
 237     entry_name = (*env)->GetFieldID(env, clazz, "name", "[B");
 238     CHECK_NULL_RETURN(entry_name, 0);
 239     entry_dir = (*env)->GetFieldID(env, clazz, "dir", "[B");
 240     CHECK_NULL_RETURN(entry_dir, 0);
 241     entry_fstype = (*env)->GetFieldID(env, clazz, "fstype", "[B");
 242     CHECK_NULL_RETURN(entry_fstype, 0);
 243     entry_options = (*env)->GetFieldID(env, clazz, "opts", "[B");
 244     CHECK_NULL_RETURN(entry_options, 0);
 245     entry_dev = (*env)->GetFieldID(env, clazz, "dev", "J");
 246     CHECK_NULL_RETURN(entry_dev, 0);
 247 
 248     /* system calls that might not be available at run time */
 249 
 250 #if (defined(__solaris__) && defined(_LP64)) || defined(_ALLBSD_SOURCE)
 251     /* Solaris 64-bit does not have openat64/fstatat64 */
 252     my_openat64_func = (openat64_func*)dlsym(RTLD_DEFAULT, "openat");
 253     my_fstatat64_func = (fstatat64_func*)dlsym(RTLD_DEFAULT, "fstatat");
 254 #else
 255     my_openat64_func = (openat64_func*) dlsym(RTLD_DEFAULT, "openat64");
 256     my_fstatat64_func = (fstatat64_func*) dlsym(RTLD_DEFAULT, "fstatat64");
 257 #endif
 258     my_unlinkat_func = (unlinkat_func*) dlsym(RTLD_DEFAULT, "unlinkat");
 259     my_renameat_func = (renameat_func*) dlsym(RTLD_DEFAULT, "renameat");
 260     my_futimesat_func = (futimesat_func*) dlsym(RTLD_DEFAULT, "futimesat");
 261     my_fdopendir_func = (fdopendir_func*) dlsym(RTLD_DEFAULT, "fdopendir");
 262 
 263 #if defined(FSTATAT64_SYSCALL_AVAILABLE)
 264     /* fstatat64 missing from glibc */
 265     if (my_fstatat64_func == NULL)
 266         my_fstatat64_func = (fstatat64_func*)&fstatat64_wrapper;
 267 #endif
 268 
 269     /* supports futimes or futimesat */
 270 
 271 #ifdef _ALLBSD_SOURCE
 272     capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_FUTIMES;
 273 #else
 274     if (my_futimesat_func != NULL)
 275         capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_FUTIMES;
 276 #endif
 277 
 278     /* supports openat, etc. */
 279 
 280     if (my_openat64_func != NULL &&  my_fstatat64_func != NULL &&
 281         my_unlinkat_func != NULL && my_renameat_func != NULL &&
 282         my_futimesat_func != NULL && my_fdopendir_func != NULL)
 283     {
 284         capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_OPENAT;
 285     }
 286 
 287     /* supports file birthtime */
 288 
 289 #ifdef _DARWIN_FEATURE_64_BIT_INODE
 290     capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_BIRTHTIME;
 291 #endif
 292 
 293     return capabilities;
 294 }
 295 
 296 JNIEXPORT jbyteArray JNICALL
 297 Java_sun_nio_fs_UnixNativeDispatcher_getcwd(JNIEnv* env, jclass this) {
 298     jbyteArray result = NULL;
 299     char buf[PATH_MAX+1];
 300 
 301     /* EINTR not listed as a possible error */
 302     char* cwd = getcwd(buf, sizeof(buf));
 303     if (cwd == NULL) {
 304         throwUnixException(env, errno);
 305     } else {
 306         jsize len = (jsize)strlen(buf);
 307         result = (*env)->NewByteArray(env, len);
 308         if (result != NULL) {
 309             (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)buf);
 310         }
 311     }
 312     return result;
 313 }
 314 
 315 JNIEXPORT jbyteArray
 316 Java_sun_nio_fs_UnixNativeDispatcher_strerror(JNIEnv* env, jclass this, jint error)
 317 {
 318     char* msg;
 319     jsize len;
 320     jbyteArray bytes;
 321 
 322 #ifdef _AIX
 323     /* strerror() is not thread-safe on AIX so we have to use strerror_r() */
 324     char buffer[256];
 325     msg = (strerror_r((int)error, buffer, 256) == 0) ? buffer : "Error while calling strerror_r";
 326 #else
 327     msg = strerror((int)error);
 328 #endif
 329     len = strlen(msg);
 330     bytes = (*env)->NewByteArray(env, len);
 331     if (bytes != NULL) {
 332         (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)msg);
 333     }
 334     return bytes;
 335 }
 336 
 337 JNIEXPORT jint
 338 Java_sun_nio_fs_UnixNativeDispatcher_dup(JNIEnv* env, jclass this, jint fd) {
 339 
 340     int res = -1;
 341 
 342     RESTARTABLE(dup((int)fd), res);
 343     if (res == -1) {
 344         throwUnixException(env, errno);
 345     }
 346     return (jint)res;
 347 }
 348 
 349 JNIEXPORT jlong JNICALL
 350 Java_sun_nio_fs_UnixNativeDispatcher_fopen0(JNIEnv* env, jclass this,
 351     jlong pathAddress, jlong modeAddress)
 352 {
 353     FILE* fp = NULL;
 354     const char* path = (const char*)jlong_to_ptr(pathAddress);
 355     const char* mode = (const char*)jlong_to_ptr(modeAddress);
 356 
 357     do {
 358         fp = fopen(path, mode);
 359     } while (fp == NULL && errno == EINTR);
 360 
 361     if (fp == NULL) {
 362         throwUnixException(env, errno);
 363     }
 364 
 365     return ptr_to_jlong(fp);
 366 }
 367 
 368 JNIEXPORT void JNICALL
 369 Java_sun_nio_fs_UnixNativeDispatcher_fclose(JNIEnv* env, jclass this, jlong stream)
 370 {
 371     FILE* fp = jlong_to_ptr(stream);
 372 
 373     /* NOTE: fclose() wrapper is only used with read-only streams.
 374      * If it ever is used with write streams, it might be better to add
 375      * RESTARTABLE(fflush(fp)) before closing, to make sure the stream
 376      * is completely written even if fclose() failed.
 377      */
 378     if (fclose(fp) == EOF && errno != EINTR) {
 379         throwUnixException(env, errno);
 380     }
 381 }
 382 
 383 JNIEXPORT jint JNICALL
 384 Java_sun_nio_fs_UnixNativeDispatcher_open0(JNIEnv* env, jclass this,
 385     jlong pathAddress, jint oflags, jint mode)
 386 {
 387     jint fd;
 388     const char* path = (const char*)jlong_to_ptr(pathAddress);
 389 
 390     RESTARTABLE(open64(path, (int)oflags, (mode_t)mode), fd);
 391     if (fd == -1) {
 392         throwUnixException(env, errno);
 393     }
 394     return fd;
 395 }
 396 
 397 JNIEXPORT jint JNICALL
 398 Java_sun_nio_fs_UnixNativeDispatcher_openat0(JNIEnv* env, jclass this, jint dfd,
 399     jlong pathAddress, jint oflags, jint mode)
 400 {
 401     jint fd;
 402     const char* path = (const char*)jlong_to_ptr(pathAddress);
 403 
 404     if (my_openat64_func == NULL) {
 405         JNU_ThrowInternalError(env, "should not reach here");
 406         return -1;
 407     }
 408 
 409     RESTARTABLE((*my_openat64_func)(dfd, path, (int)oflags, (mode_t)mode), fd);
 410     if (fd == -1) {
 411         throwUnixException(env, errno);
 412     }
 413     return fd;
 414 }
 415 
 416 JNIEXPORT void JNICALL
 417 Java_sun_nio_fs_UnixNativeDispatcher_close(JNIEnv* env, jclass this, jint fd) {
 418     int err;
 419     /* TDB - need to decide if EIO and other errors should cause exception */
 420     RESTARTABLE(close((int)fd), err);
 421 }
 422 
 423 JNIEXPORT jint JNICALL
 424 Java_sun_nio_fs_UnixNativeDispatcher_read(JNIEnv* env, jclass this, jint fd,
 425     jlong address, jint nbytes)
 426 {
 427     ssize_t n;
 428     void* bufp = jlong_to_ptr(address);
 429     RESTARTABLE(read((int)fd, bufp, (size_t)nbytes), n);
 430     if (n == -1) {
 431         throwUnixException(env, errno);
 432     }
 433     return (jint)n;
 434 }
 435 
 436 JNIEXPORT jint JNICALL
 437 Java_sun_nio_fs_UnixNativeDispatcher_write(JNIEnv* env, jclass this, jint fd,
 438     jlong address, jint nbytes)
 439 {
 440     ssize_t n;
 441     void* bufp = jlong_to_ptr(address);
 442     RESTARTABLE(write((int)fd, bufp, (size_t)nbytes), n);
 443     if (n == -1) {
 444         throwUnixException(env, errno);
 445     }
 446     return (jint)n;
 447 }
 448 
 449 /**
 450  * Copy stat64 members into sun.nio.fs.UnixFileAttributes
 451  */
 452 static void prepAttributes(JNIEnv* env, struct stat64* buf, jobject attrs) {
 453     (*env)->SetIntField(env, attrs, attrs_st_mode, (jint)buf->st_mode);
 454     (*env)->SetLongField(env, attrs, attrs_st_ino, (jlong)buf->st_ino);
 455     (*env)->SetLongField(env, attrs, attrs_st_dev, (jlong)buf->st_dev);
 456     (*env)->SetLongField(env, attrs, attrs_st_rdev, (jlong)buf->st_rdev);
 457     (*env)->SetIntField(env, attrs, attrs_st_nlink, (jint)buf->st_nlink);
 458     (*env)->SetIntField(env, attrs, attrs_st_uid, (jint)buf->st_uid);
 459     (*env)->SetIntField(env, attrs, attrs_st_gid, (jint)buf->st_gid);
 460     (*env)->SetLongField(env, attrs, attrs_st_size, (jlong)buf->st_size);
 461     (*env)->SetLongField(env, attrs, attrs_st_atime_sec, (jlong)buf->st_atime);
 462     (*env)->SetLongField(env, attrs, attrs_st_mtime_sec, (jlong)buf->st_mtime);
 463     (*env)->SetLongField(env, attrs, attrs_st_ctime_sec, (jlong)buf->st_ctime);
 464 
 465 #ifdef _DARWIN_FEATURE_64_BIT_INODE
 466     (*env)->SetLongField(env, attrs, attrs_st_birthtime_sec, (jlong)buf->st_birthtime);
 467 #endif
 468 
 469 #if (_POSIX_C_SOURCE >= 200809L) || defined(__solaris__)
 470     (*env)->SetLongField(env, attrs, attrs_st_atime_nsec, (jlong)buf->st_atim.tv_nsec);
 471     (*env)->SetLongField(env, attrs, attrs_st_mtime_nsec, (jlong)buf->st_mtim.tv_nsec);
 472     (*env)->SetLongField(env, attrs, attrs_st_ctime_nsec, (jlong)buf->st_ctim.tv_nsec);
 473 #endif
 474 }
 475 
 476 JNIEXPORT void JNICALL
 477 Java_sun_nio_fs_UnixNativeDispatcher_stat0(JNIEnv* env, jclass this,
 478     jlong pathAddress, jobject attrs)
 479 {
 480     int err;
 481     struct stat64 buf;
 482     const char* path = (const char*)jlong_to_ptr(pathAddress);
 483 
 484     RESTARTABLE(stat64(path, &buf), err);
 485     if (err == -1) {
 486         throwUnixException(env, errno);
 487     } else {
 488         prepAttributes(env, &buf, attrs);
 489     }
 490 }
 491 
 492 JNIEXPORT void JNICALL
 493 Java_sun_nio_fs_UnixNativeDispatcher_lstat0(JNIEnv* env, jclass this,
 494     jlong pathAddress, jobject attrs)
 495 {
 496     int err;
 497     struct stat64 buf;
 498     const char* path = (const char*)jlong_to_ptr(pathAddress);
 499 
 500     RESTARTABLE(lstat64(path, &buf), err);
 501     if (err == -1) {
 502         throwUnixException(env, errno);
 503     } else {
 504         prepAttributes(env, &buf, attrs);
 505     }
 506 }
 507 
 508 JNIEXPORT void JNICALL
 509 Java_sun_nio_fs_UnixNativeDispatcher_fstat(JNIEnv* env, jclass this, jint fd,
 510     jobject attrs)
 511 {
 512     int err;
 513     struct stat64 buf;
 514 
 515     RESTARTABLE(fstat64((int)fd, &buf), err);
 516     if (err == -1) {
 517         throwUnixException(env, errno);
 518     } else {
 519         prepAttributes(env, &buf, attrs);
 520     }
 521 }
 522 
 523 JNIEXPORT void JNICALL
 524 Java_sun_nio_fs_UnixNativeDispatcher_fstatat0(JNIEnv* env, jclass this, jint dfd,
 525     jlong pathAddress, jint flag, jobject attrs)
 526 {
 527     int err;
 528     struct stat64 buf;
 529     const char* path = (const char*)jlong_to_ptr(pathAddress);
 530 
 531     if (my_fstatat64_func == NULL) {
 532         JNU_ThrowInternalError(env, "should not reach here");
 533         return;
 534     }
 535     RESTARTABLE((*my_fstatat64_func)((int)dfd, path, &buf, (int)flag), err);
 536     if (err == -1) {
 537         throwUnixException(env, errno);
 538     } else {
 539         prepAttributes(env, &buf, attrs);
 540     }
 541 }
 542 
 543 JNIEXPORT void JNICALL
 544 Java_sun_nio_fs_UnixNativeDispatcher_chmod0(JNIEnv* env, jclass this,
 545     jlong pathAddress, jint mode)
 546 {
 547     int err;
 548     const char* path = (const char*)jlong_to_ptr(pathAddress);
 549 
 550     RESTARTABLE(chmod(path, (mode_t)mode), err);
 551     if (err == -1) {
 552         throwUnixException(env, errno);
 553     }
 554 }
 555 
 556 JNIEXPORT void JNICALL
 557 Java_sun_nio_fs_UnixNativeDispatcher_fchmod(JNIEnv* env, jclass this, jint filedes,
 558     jint mode)
 559 {
 560     int err;
 561 
 562     RESTARTABLE(fchmod((int)filedes, (mode_t)mode), err);
 563     if (err == -1) {
 564         throwUnixException(env, errno);
 565     }
 566 }
 567 
 568 
 569 JNIEXPORT void JNICALL
 570 Java_sun_nio_fs_UnixNativeDispatcher_chown0(JNIEnv* env, jclass this,
 571     jlong pathAddress, jint uid, jint gid)
 572 {
 573     int err;
 574     const char* path = (const char*)jlong_to_ptr(pathAddress);
 575 
 576     RESTARTABLE(chown(path, (uid_t)uid, (gid_t)gid), err);
 577     if (err == -1) {
 578         throwUnixException(env, errno);
 579     }
 580 }
 581 
 582 JNIEXPORT void JNICALL
 583 Java_sun_nio_fs_UnixNativeDispatcher_lchown0(JNIEnv* env, jclass this, jlong pathAddress, jint uid, jint gid)
 584 {
 585     int err;
 586     const char* path = (const char*)jlong_to_ptr(pathAddress);
 587 
 588     RESTARTABLE(lchown(path, (uid_t)uid, (gid_t)gid), err);
 589     if (err == -1) {
 590         throwUnixException(env, errno);
 591     }
 592 }
 593 
 594 JNIEXPORT void JNICALL
 595 Java_sun_nio_fs_UnixNativeDispatcher_fchown(JNIEnv* env, jclass this, jint filedes, jint uid, jint gid)
 596 {
 597     int err;
 598 
 599     RESTARTABLE(fchown(filedes, (uid_t)uid, (gid_t)gid), err);
 600     if (err == -1) {
 601         throwUnixException(env, errno);
 602     }
 603 }
 604 
 605 JNIEXPORT void JNICALL
 606 Java_sun_nio_fs_UnixNativeDispatcher_utimes0(JNIEnv* env, jclass this,
 607     jlong pathAddress, jlong accessTime, jlong modificationTime)
 608 {
 609     int err;
 610     struct timeval times[2];
 611     const char* path = (const char*)jlong_to_ptr(pathAddress);
 612 
 613     times[0].tv_sec = accessTime / 1000000;
 614     times[0].tv_usec = accessTime % 1000000;
 615 
 616     times[1].tv_sec = modificationTime / 1000000;
 617     times[1].tv_usec = modificationTime % 1000000;
 618 
 619     RESTARTABLE(utimes(path, &times[0]), err);
 620     if (err == -1) {
 621         throwUnixException(env, errno);
 622     }
 623 }
 624 
 625 JNIEXPORT void JNICALL
 626 Java_sun_nio_fs_UnixNativeDispatcher_futimes(JNIEnv* env, jclass this, jint filedes,
 627     jlong accessTime, jlong modificationTime)
 628 {
 629     struct timeval times[2];
 630     int err = 0;
 631 
 632     times[0].tv_sec = accessTime / 1000000;
 633     times[0].tv_usec = accessTime % 1000000;
 634 
 635     times[1].tv_sec = modificationTime / 1000000;
 636     times[1].tv_usec = modificationTime % 1000000;
 637 
 638 #ifdef _ALLBSD_SOURCE
 639     RESTARTABLE(futimes(filedes, &times[0]), err);
 640 #else
 641     if (my_futimesat_func == NULL) {
 642         JNU_ThrowInternalError(env, "my_ftimesat_func is NULL");
 643         return;
 644     }
 645     RESTARTABLE((*my_futimesat_func)(filedes, NULL, &times[0]), err);
 646 #endif
 647     if (err == -1) {
 648         throwUnixException(env, errno);
 649     }
 650 }
 651 
 652 JNIEXPORT jlong JNICALL
 653 Java_sun_nio_fs_UnixNativeDispatcher_opendir0(JNIEnv* env, jclass this,
 654     jlong pathAddress)
 655 {
 656     DIR* dir;
 657     const char* path = (const char*)jlong_to_ptr(pathAddress);
 658 
 659     /* EINTR not listed as a possible error */
 660     dir = opendir(path);
 661     if (dir == NULL) {
 662         throwUnixException(env, errno);
 663     }
 664     return ptr_to_jlong(dir);
 665 }
 666 
 667 JNIEXPORT jlong JNICALL
 668 Java_sun_nio_fs_UnixNativeDispatcher_fdopendir(JNIEnv* env, jclass this, int dfd) {
 669     DIR* dir;
 670 
 671     if (my_fdopendir_func == NULL) {
 672         JNU_ThrowInternalError(env, "should not reach here");
 673         return (jlong)-1;
 674     }
 675 
 676     /* EINTR not listed as a possible error */
 677     dir = (*my_fdopendir_func)((int)dfd);
 678     if (dir == NULL) {
 679         throwUnixException(env, errno);
 680     }
 681     return ptr_to_jlong(dir);
 682 }
 683 
 684 JNIEXPORT void JNICALL
 685 Java_sun_nio_fs_UnixNativeDispatcher_closedir(JNIEnv* env, jclass this, jlong dir) {
 686     DIR* dirp = jlong_to_ptr(dir);
 687 
 688     if (closedir(dirp) == -1 && errno != EINTR) {
 689         throwUnixException(env, errno);
 690     }
 691 }
 692 
 693 JNIEXPORT jbyteArray JNICALL
 694 Java_sun_nio_fs_UnixNativeDispatcher_readdir(JNIEnv* env, jclass this, jlong value) {
 695     struct dirent64* result;
 696     struct {
 697         struct dirent64 buf;
 698         char name_extra[PATH_MAX + 1 - sizeof result->d_name];
 699     } entry;
 700     struct dirent64* ptr = &entry.buf;
 701     int res;
 702     DIR* dirp = jlong_to_ptr(value);
 703 
 704     /* EINTR not listed as a possible error */
 705     /* TDB: reentrant version probably not required here */
 706     res = readdir64_r(dirp, ptr, &result);
 707 
 708 #ifdef _AIX
 709     /* On AIX, readdir_r() returns EBADF (i.e. '9') and sets 'result' to NULL for the */
 710     /* directory stream end. Otherwise, 'errno' will contain the error code. */
 711     if (res != 0) {
 712         res = (result == NULL && res == EBADF) ? 0 : errno;
 713     }
 714 #endif
 715 
 716     if (res != 0) {
 717         throwUnixException(env, res);
 718         return NULL;
 719     } else {
 720         if (result == NULL) {
 721             return NULL;
 722         } else {
 723             jsize len = strlen(ptr->d_name);
 724             jbyteArray bytes = (*env)->NewByteArray(env, len);
 725             if (bytes != NULL) {
 726                 (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)(ptr->d_name));
 727             }
 728             return bytes;
 729         }
 730     }
 731 }
 732 
 733 JNIEXPORT void JNICALL
 734 Java_sun_nio_fs_UnixNativeDispatcher_mkdir0(JNIEnv* env, jclass this,
 735     jlong pathAddress, jint mode)
 736 {
 737     const char* path = (const char*)jlong_to_ptr(pathAddress);
 738 
 739     /* EINTR not listed as a possible error */
 740     if (mkdir(path, (mode_t)mode) == -1) {
 741         throwUnixException(env, errno);
 742     }
 743 }
 744 
 745 JNIEXPORT void JNICALL
 746 Java_sun_nio_fs_UnixNativeDispatcher_rmdir0(JNIEnv* env, jclass this,
 747     jlong pathAddress)
 748 {
 749     const char* path = (const char*)jlong_to_ptr(pathAddress);
 750 
 751     /* EINTR not listed as a possible error */
 752     if (rmdir(path) == -1) {
 753         throwUnixException(env, errno);
 754     }
 755 }
 756 
 757 JNIEXPORT void JNICALL
 758 Java_sun_nio_fs_UnixNativeDispatcher_link0(JNIEnv* env, jclass this,
 759     jlong existingAddress, jlong newAddress)
 760 {
 761     int err;
 762     const char* existing = (const char*)jlong_to_ptr(existingAddress);
 763     const char* newname = (const char*)jlong_to_ptr(newAddress);
 764 
 765     RESTARTABLE(link(existing, newname), err);
 766     if (err == -1) {
 767         throwUnixException(env, errno);
 768     }
 769 }
 770 
 771 
 772 JNIEXPORT void JNICALL
 773 Java_sun_nio_fs_UnixNativeDispatcher_unlink0(JNIEnv* env, jclass this,
 774     jlong pathAddress)
 775 {
 776     const char* path = (const char*)jlong_to_ptr(pathAddress);
 777 
 778     /* EINTR not listed as a possible error */
 779     if (unlink(path) == -1) {
 780         throwUnixException(env, errno);
 781     }
 782 }
 783 
 784 JNIEXPORT void JNICALL
 785 Java_sun_nio_fs_UnixNativeDispatcher_unlinkat0(JNIEnv* env, jclass this, jint dfd,
 786                                                jlong pathAddress, jint flags)
 787 {
 788     const char* path = (const char*)jlong_to_ptr(pathAddress);
 789 
 790     if (my_unlinkat_func == NULL) {
 791         JNU_ThrowInternalError(env, "should not reach here");
 792         return;
 793     }
 794 
 795     /* EINTR not listed as a possible error */
 796     if ((*my_unlinkat_func)((int)dfd, path, (int)flags) == -1) {
 797         throwUnixException(env, errno);
 798     }
 799 }
 800 
 801 JNIEXPORT void JNICALL
 802 Java_sun_nio_fs_UnixNativeDispatcher_rename0(JNIEnv* env, jclass this,
 803     jlong fromAddress, jlong toAddress)
 804 {
 805     const char* from = (const char*)jlong_to_ptr(fromAddress);
 806     const char* to = (const char*)jlong_to_ptr(toAddress);
 807 
 808     /* EINTR not listed as a possible error */
 809     if (rename(from, to) == -1) {
 810         throwUnixException(env, errno);
 811     }
 812 }
 813 
 814 JNIEXPORT void JNICALL
 815 Java_sun_nio_fs_UnixNativeDispatcher_renameat0(JNIEnv* env, jclass this,
 816     jint fromfd, jlong fromAddress, jint tofd, jlong toAddress)
 817 {
 818     const char* from = (const char*)jlong_to_ptr(fromAddress);
 819     const char* to = (const char*)jlong_to_ptr(toAddress);
 820 
 821     if (my_renameat_func == NULL) {
 822         JNU_ThrowInternalError(env, "should not reach here");
 823         return;
 824     }
 825 
 826     /* EINTR not listed as a possible error */
 827     if ((*my_renameat_func)((int)fromfd, from, (int)tofd, to) == -1) {
 828         throwUnixException(env, errno);
 829     }
 830 }
 831 
 832 JNIEXPORT void JNICALL
 833 Java_sun_nio_fs_UnixNativeDispatcher_symlink0(JNIEnv* env, jclass this,
 834     jlong targetAddress, jlong linkAddress)
 835 {
 836     const char* target = (const char*)jlong_to_ptr(targetAddress);
 837     const char* link = (const char*)jlong_to_ptr(linkAddress);
 838 
 839     /* EINTR not listed as a possible error */
 840     if (symlink(target, link) == -1) {
 841         throwUnixException(env, errno);
 842     }
 843 }
 844 
 845 JNIEXPORT jbyteArray JNICALL
 846 Java_sun_nio_fs_UnixNativeDispatcher_readlink0(JNIEnv* env, jclass this,
 847     jlong pathAddress)
 848 {
 849     jbyteArray result = NULL;
 850     char target[PATH_MAX+1];
 851     const char* path = (const char*)jlong_to_ptr(pathAddress);
 852 
 853     /* EINTR not listed as a possible error */
 854     int n = readlink(path, target, sizeof(target));
 855     if (n == -1) {
 856         throwUnixException(env, errno);
 857     } else {
 858         jsize len;
 859         if (n == sizeof(target)) {
 860             n--;
 861         }
 862         target[n] = '\0';
 863         len = (jsize)strlen(target);
 864         result = (*env)->NewByteArray(env, len);
 865         if (result != NULL) {
 866             (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)target);
 867         }
 868     }
 869     return result;
 870 }
 871 
 872 JNIEXPORT jbyteArray JNICALL
 873 Java_sun_nio_fs_UnixNativeDispatcher_realpath0(JNIEnv* env, jclass this,
 874     jlong pathAddress)
 875 {
 876     jbyteArray result = NULL;
 877     char resolved[PATH_MAX+1];
 878     const char* path = (const char*)jlong_to_ptr(pathAddress);
 879 
 880     /* EINTR not listed as a possible error */
 881     if (realpath(path, resolved) == NULL) {
 882         throwUnixException(env, errno);
 883     } else {
 884         jsize len = (jsize)strlen(resolved);
 885         result = (*env)->NewByteArray(env, len);
 886         if (result != NULL) {
 887             (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)resolved);
 888         }
 889     }
 890     return result;
 891 }
 892 
 893 JNIEXPORT void JNICALL
 894 Java_sun_nio_fs_UnixNativeDispatcher_access0(JNIEnv* env, jclass this,
 895     jlong pathAddress, jint amode)
 896 {
 897     int err;
 898     const char* path = (const char*)jlong_to_ptr(pathAddress);
 899 
 900     RESTARTABLE(access(path, (int)amode), err);
 901     if (err == -1) {
 902         throwUnixException(env, errno);
 903     }
 904 }
 905 
 906 JNIEXPORT void JNICALL
 907 Java_sun_nio_fs_UnixNativeDispatcher_statvfs0(JNIEnv* env, jclass this,
 908     jlong pathAddress, jobject attrs)
 909 {
 910     int err;
 911 #ifdef MACOSX
 912     struct statfs buf;
 913 #else
 914     struct statvfs64 buf;
 915 #endif
 916     const char* path = (const char*)jlong_to_ptr(pathAddress);
 917 
 918 #ifdef MACOSX
 919     RESTARTABLE(statfs(path, &buf), err);
 920 #else
 921     RESTARTABLE(statvfs64(path, &buf), err);
 922 #endif
 923     if (err == -1) {
 924         throwUnixException(env, errno);
 925     } else {
 926 #ifdef _AIX
 927         /* AIX returns ULONG_MAX in buf.f_blocks for the /proc file system. */
 928         /* This is too big for a Java signed long and fools various tests.  */
 929         if (buf.f_blocks == ULONG_MAX) {
 930             buf.f_blocks = 0;
 931         }
 932         /* The number of free or available blocks can never exceed the total number of blocks */
 933         if (buf.f_blocks == 0) {
 934             buf.f_bfree = 0;
 935             buf.f_bavail = 0;
 936         }
 937 #endif
 938 #ifdef MACOSX
 939         (*env)->SetLongField(env, attrs, attrs_f_frsize, long_to_jlong(buf.f_bsize));
 940 #else
 941         (*env)->SetLongField(env, attrs, attrs_f_frsize, long_to_jlong(buf.f_frsize));
 942 #endif
 943         (*env)->SetLongField(env, attrs, attrs_f_blocks, long_to_jlong(buf.f_blocks));
 944         (*env)->SetLongField(env, attrs, attrs_f_bfree,  long_to_jlong(buf.f_bfree));
 945         (*env)->SetLongField(env, attrs, attrs_f_bavail, long_to_jlong(buf.f_bavail));
 946     }
 947 }
 948 
 949 JNIEXPORT jlong JNICALL
 950 Java_sun_nio_fs_UnixNativeDispatcher_pathconf0(JNIEnv* env, jclass this,
 951     jlong pathAddress, jint name)
 952 {
 953     long err;
 954     const char* path = (const char*)jlong_to_ptr(pathAddress);
 955 
 956     err = pathconf(path, (int)name);
 957     if (err == -1) {
 958         throwUnixException(env, errno);
 959     }
 960     return (jlong)err;
 961 }
 962 
 963 JNIEXPORT jlong JNICALL
 964 Java_sun_nio_fs_UnixNativeDispatcher_fpathconf(JNIEnv* env, jclass this,
 965     jint fd, jint name)
 966 {
 967     long err;
 968 
 969     err = fpathconf((int)fd, (int)name);
 970     if (err == -1) {
 971         throwUnixException(env, errno);
 972     }
 973     return (jlong)err;
 974 }
 975 
 976 JNIEXPORT void JNICALL
 977 Java_sun_nio_fs_UnixNativeDispatcher_mknod0(JNIEnv* env, jclass this,
 978     jlong pathAddress, jint mode, jlong dev)
 979 {
 980     int err;
 981     const char* path = (const char*)jlong_to_ptr(pathAddress);
 982 
 983     RESTARTABLE(mknod(path, (mode_t)mode, (dev_t)dev), err);
 984     if (err == -1) {
 985         throwUnixException(env, errno);
 986     }
 987 }
 988 
 989 JNIEXPORT jbyteArray JNICALL
 990 Java_sun_nio_fs_UnixNativeDispatcher_getpwuid(JNIEnv* env, jclass this, jint uid)
 991 {
 992     jbyteArray result = NULL;
 993     int buflen;
 994     char* pwbuf;
 995 
 996     /* allocate buffer for password record */
 997     buflen = (int)sysconf(_SC_GETPW_R_SIZE_MAX);
 998     if (buflen == -1)
 999         buflen = ENT_BUF_SIZE;
1000     pwbuf = (char*)malloc(buflen);
1001     if (pwbuf == NULL) {
1002         JNU_ThrowOutOfMemoryError(env, "native heap");
1003     } else {
1004         struct passwd pwent;
1005         struct passwd* p = NULL;
1006         int res = 0;
1007 
1008         errno = 0;
1009         #ifdef __solaris__
1010             RESTARTABLE_RETURN_PTR(getpwuid_r((uid_t)uid, &pwent, pwbuf, (size_t)buflen), p);
1011         #else
1012             RESTARTABLE(getpwuid_r((uid_t)uid, &pwent, pwbuf, (size_t)buflen, &p), res);
1013         #endif
1014 
1015         if (res != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
1016             /* not found or error */
1017             if (errno == 0)
1018                 errno = ENOENT;
1019             throwUnixException(env, errno);
1020         } else {
1021             jsize len = strlen(p->pw_name);
1022             result = (*env)->NewByteArray(env, len);
1023             if (result != NULL) {
1024                 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)(p->pw_name));
1025             }
1026         }
1027         free(pwbuf);
1028     }
1029 
1030     return result;
1031 }
1032 
1033 
1034 JNIEXPORT jbyteArray JNICALL
1035 Java_sun_nio_fs_UnixNativeDispatcher_getgrgid(JNIEnv* env, jclass this, jint gid)
1036 {
1037     jbyteArray result = NULL;
1038     int buflen;
1039     int retry;
1040 
1041     /* initial size of buffer for group record */
1042     buflen = (int)sysconf(_SC_GETGR_R_SIZE_MAX);
1043     if (buflen == -1)
1044         buflen = ENT_BUF_SIZE;
1045 
1046     do {
1047         struct group grent;
1048         struct group* g = NULL;
1049         int res = 0;
1050 
1051         char* grbuf = (char*)malloc(buflen);
1052         if (grbuf == NULL) {
1053             JNU_ThrowOutOfMemoryError(env, "native heap");
1054             return NULL;
1055         }
1056 
1057         errno = 0;
1058         #ifdef __solaris__
1059             RESTARTABLE_RETURN_PTR(getgrgid_r((gid_t)gid, &grent, grbuf, (size_t)buflen), g);
1060         #else
1061             RESTARTABLE(getgrgid_r((gid_t)gid, &grent, grbuf, (size_t)buflen, &g), res);
1062         #endif
1063 
1064         retry = 0;
1065         if (res != 0 || g == NULL || g->gr_name == NULL || *(g->gr_name) == '\0') {
1066             /* not found or error */
1067             if (errno == ERANGE) {
1068                 /* insufficient buffer size so need larger buffer */
1069                 buflen += ENT_BUF_SIZE;
1070                 retry = 1;
1071             } else {
1072                 if (errno == 0)
1073                     errno = ENOENT;
1074                 throwUnixException(env, errno);
1075             }
1076         } else {
1077             jsize len = strlen(g->gr_name);
1078             result = (*env)->NewByteArray(env, len);
1079             if (result != NULL) {
1080                 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)(g->gr_name));
1081             }
1082         }
1083 
1084         free(grbuf);
1085 
1086     } while (retry);
1087 
1088     return result;
1089 }
1090 
1091 JNIEXPORT jint JNICALL
1092 Java_sun_nio_fs_UnixNativeDispatcher_getpwnam0(JNIEnv* env, jclass this,
1093     jlong nameAddress)
1094 {
1095     jint uid = -1;
1096     int buflen;
1097     char* pwbuf;
1098 
1099     /* allocate buffer for password record */
1100     buflen = (int)sysconf(_SC_GETPW_R_SIZE_MAX);
1101     if (buflen == -1)
1102         buflen = ENT_BUF_SIZE;
1103     pwbuf = (char*)malloc(buflen);
1104     if (pwbuf == NULL) {
1105         JNU_ThrowOutOfMemoryError(env, "native heap");
1106     } else {
1107         struct passwd pwent;
1108         struct passwd* p = NULL;
1109         int res = 0;
1110         const char* name = (const char*)jlong_to_ptr(nameAddress);
1111 
1112         errno = 0;
1113         #ifdef __solaris__
1114             RESTARTABLE_RETURN_PTR(getpwnam_r(name, &pwent, pwbuf, (size_t)buflen), p);
1115         #else
1116             RESTARTABLE(getpwnam_r(name, &pwent, pwbuf, (size_t)buflen, &p), res);
1117         #endif
1118 
1119         if (res != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
1120             /* not found or error */
1121             if (errno != 0 && errno != ENOENT && errno != ESRCH)
1122                 throwUnixException(env, errno);
1123         } else {
1124             uid = p->pw_uid;
1125         }
1126         free(pwbuf);
1127     }
1128 
1129     return uid;
1130 }
1131 
1132 JNIEXPORT jint JNICALL
1133 Java_sun_nio_fs_UnixNativeDispatcher_getgrnam0(JNIEnv* env, jclass this,
1134     jlong nameAddress)
1135 {
1136     jint gid = -1;
1137     int buflen, retry;
1138 
1139     /* initial size of buffer for group record */
1140     buflen = (int)sysconf(_SC_GETGR_R_SIZE_MAX);
1141     if (buflen == -1)
1142         buflen = ENT_BUF_SIZE;
1143 
1144     do {
1145         struct group grent;
1146         struct group* g = NULL;
1147         int res = 0;
1148         char *grbuf;
1149         const char* name = (const char*)jlong_to_ptr(nameAddress);
1150 
1151         grbuf = (char*)malloc(buflen);
1152         if (grbuf == NULL) {
1153             JNU_ThrowOutOfMemoryError(env, "native heap");
1154             return -1;
1155         }
1156 
1157         errno = 0;
1158         #ifdef __solaris__
1159             RESTARTABLE_RETURN_PTR(getgrnam_r(name, &grent, grbuf, (size_t)buflen), g);
1160         #else
1161             RESTARTABLE(getgrnam_r(name, &grent, grbuf, (size_t)buflen, &g), res);
1162         #endif
1163 
1164         retry = 0;
1165         if (res != 0 || g == NULL || g->gr_name == NULL || *(g->gr_name) == '\0') {
1166             /* not found or error */
1167             if (errno != 0 && errno != ENOENT && errno != ESRCH) {
1168                 if (errno == ERANGE) {
1169                     /* insufficient buffer size so need larger buffer */
1170                     buflen += ENT_BUF_SIZE;
1171                     retry = 1;
1172                 } else {
1173                     throwUnixException(env, errno);
1174                 }
1175             }
1176         } else {
1177             gid = g->gr_gid;
1178         }
1179 
1180         free(grbuf);
1181 
1182     } while (retry);
1183 
1184     return gid;
1185 }