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