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* msg; 312 jsize len; 313 jbyteArray bytes; 314 315 #ifdef _AIX 316 /* strerror() is not thread-safe on AIX so we have to use strerror_r() */ 317 char buffer[256]; 318 msg = (strerror_r((int)error, buffer, 256) == 0) ? buffer : "Error while calling strerror_r"; 319 #else 320 msg = strerror((int)error); 321 #endif 322 len = strlen(msg); 323 bytes = (*env)->NewByteArray(env, len); 324 if (bytes != NULL) { 325 (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)msg); 326 } 327 return bytes; 328 } 329 330 JNIEXPORT jint 331 Java_sun_nio_fs_UnixNativeDispatcher_dup(JNIEnv* env, jclass this, jint fd) { 332 333 int res = -1; 334 335 RESTARTABLE(dup((int)fd), res); 336 if (res == -1) { 337 throwUnixException(env, errno); 338 } 339 return (jint)res; 340 } 341 342 JNIEXPORT jlong JNICALL 343 Java_sun_nio_fs_UnixNativeDispatcher_fopen0(JNIEnv* env, jclass this, 344 jlong pathAddress, jlong modeAddress) 345 { 346 FILE* fp = NULL; 347 const char* path = (const char*)jlong_to_ptr(pathAddress); 348 const char* mode = (const char*)jlong_to_ptr(modeAddress); 349 350 do { 351 fp = fopen(path, mode); 352 } while (fp == NULL && errno == EINTR); 353 354 if (fp == NULL) { 355 throwUnixException(env, errno); 356 } 357 358 return ptr_to_jlong(fp); 359 } 360 361 JNIEXPORT void JNICALL 362 Java_sun_nio_fs_UnixNativeDispatcher_fclose(JNIEnv* env, jclass this, jlong stream) 363 { 364 FILE* fp = jlong_to_ptr(stream); 365 366 if (fclose(fp) == EOF && errno != EINTR) { 367 throwUnixException(env, errno); 368 } 369 } 370 371 JNIEXPORT jint JNICALL 372 Java_sun_nio_fs_UnixNativeDispatcher_open0(JNIEnv* env, jclass this, 373 jlong pathAddress, jint oflags, jint mode) 374 { 375 jint fd; 376 const char* path = (const char*)jlong_to_ptr(pathAddress); 377 378 RESTARTABLE(open64(path, (int)oflags, (mode_t)mode), fd); 379 if (fd == -1) { 380 throwUnixException(env, errno); 381 } 382 return fd; 383 } 384 385 JNIEXPORT jint JNICALL 386 Java_sun_nio_fs_UnixNativeDispatcher_openat0(JNIEnv* env, jclass this, jint dfd, 387 jlong pathAddress, jint oflags, jint mode) 388 { 389 jint fd; 390 const char* path = (const char*)jlong_to_ptr(pathAddress); 391 392 if (my_openat64_func == NULL) { 393 JNU_ThrowInternalError(env, "should not reach here"); 394 return -1; 395 } 396 397 RESTARTABLE((*my_openat64_func)(dfd, path, (int)oflags, (mode_t)mode), fd); 398 if (fd == -1) { 399 throwUnixException(env, errno); 400 } 401 return fd; 402 } 403 404 JNIEXPORT void JNICALL 405 Java_sun_nio_fs_UnixNativeDispatcher_close(JNIEnv* env, jclass this, jint fd) { 406 int err; 407 /* TDB - need to decide if EIO and other errors should cause exception */ 408 RESTARTABLE(close((int)fd), err); 409 } 410 411 JNIEXPORT jint JNICALL 412 Java_sun_nio_fs_UnixNativeDispatcher_read(JNIEnv* env, jclass this, jint fd, 413 jlong address, jint nbytes) 414 { 415 ssize_t n; 416 void* bufp = jlong_to_ptr(address); 417 RESTARTABLE(read((int)fd, bufp, (size_t)nbytes), n); 418 if (n == -1) { 419 throwUnixException(env, errno); 420 } 421 return (jint)n; 422 } 423 424 JNIEXPORT jint JNICALL 425 Java_sun_nio_fs_UnixNativeDispatcher_write(JNIEnv* env, jclass this, jint fd, 426 jlong address, jint nbytes) 427 { 428 ssize_t n; 429 void* bufp = jlong_to_ptr(address); 430 RESTARTABLE(write((int)fd, bufp, (size_t)nbytes), n); 431 if (n == -1) { 432 throwUnixException(env, errno); 433 } 434 return (jint)n; 435 } 436 437 /** 438 * Copy stat64 members into sun.nio.fs.UnixFileAttributes 439 */ 440 static void prepAttributes(JNIEnv* env, struct stat64* buf, jobject attrs) { 441 (*env)->SetIntField(env, attrs, attrs_st_mode, (jint)buf->st_mode); 442 (*env)->SetLongField(env, attrs, attrs_st_ino, (jlong)buf->st_ino); 443 (*env)->SetLongField(env, attrs, attrs_st_dev, (jlong)buf->st_dev); 444 (*env)->SetLongField(env, attrs, attrs_st_rdev, (jlong)buf->st_rdev); 445 (*env)->SetIntField(env, attrs, attrs_st_nlink, (jint)buf->st_nlink); 446 (*env)->SetIntField(env, attrs, attrs_st_uid, (jint)buf->st_uid); 447 (*env)->SetIntField(env, attrs, attrs_st_gid, (jint)buf->st_gid); 448 (*env)->SetLongField(env, attrs, attrs_st_size, (jlong)buf->st_size); 449 (*env)->SetLongField(env, attrs, attrs_st_atime_sec, (jlong)buf->st_atime); 450 (*env)->SetLongField(env, attrs, attrs_st_mtime_sec, (jlong)buf->st_mtime); 451 (*env)->SetLongField(env, attrs, attrs_st_ctime_sec, (jlong)buf->st_ctime); 452 453 #ifdef _DARWIN_FEATURE_64_BIT_INODE 454 (*env)->SetLongField(env, attrs, attrs_st_birthtime_sec, (jlong)buf->st_birthtime); 455 #endif 456 457 #if (_POSIX_C_SOURCE >= 200809L) || defined(__solaris__) 458 (*env)->SetLongField(env, attrs, attrs_st_atime_nsec, (jlong)buf->st_atim.tv_nsec); 459 (*env)->SetLongField(env, attrs, attrs_st_mtime_nsec, (jlong)buf->st_mtim.tv_nsec); 460 (*env)->SetLongField(env, attrs, attrs_st_ctime_nsec, (jlong)buf->st_ctim.tv_nsec); 461 #endif 462 } 463 464 JNIEXPORT void JNICALL 465 Java_sun_nio_fs_UnixNativeDispatcher_stat0(JNIEnv* env, jclass this, 466 jlong pathAddress, jobject attrs) 467 { 468 int err; 469 struct stat64 buf; 470 const char* path = (const char*)jlong_to_ptr(pathAddress); 471 472 RESTARTABLE(stat64(path, &buf), err); 473 if (err == -1) { 474 throwUnixException(env, errno); 475 } else { 476 prepAttributes(env, &buf, attrs); 477 } 478 } 479 480 JNIEXPORT void JNICALL 481 Java_sun_nio_fs_UnixNativeDispatcher_lstat0(JNIEnv* env, jclass this, 482 jlong pathAddress, jobject attrs) 483 { 484 int err; 485 struct stat64 buf; 486 const char* path = (const char*)jlong_to_ptr(pathAddress); 487 488 RESTARTABLE(lstat64(path, &buf), err); 489 if (err == -1) { 490 throwUnixException(env, errno); 491 } else { 492 prepAttributes(env, &buf, attrs); 493 } 494 } 495 496 JNIEXPORT void JNICALL 497 Java_sun_nio_fs_UnixNativeDispatcher_fstat(JNIEnv* env, jclass this, jint fd, 498 jobject attrs) 499 { 500 int err; 501 struct stat64 buf; 502 503 RESTARTABLE(fstat64((int)fd, &buf), err); 504 if (err == -1) { 505 throwUnixException(env, errno); 506 } else { 507 prepAttributes(env, &buf, attrs); 508 } 509 } 510 511 JNIEXPORT void JNICALL 512 Java_sun_nio_fs_UnixNativeDispatcher_fstatat0(JNIEnv* env, jclass this, jint dfd, 513 jlong pathAddress, jint flag, jobject attrs) 514 { 515 int err; 516 struct stat64 buf; 517 const char* path = (const char*)jlong_to_ptr(pathAddress); 518 519 if (my_fstatat64_func == NULL) { 520 JNU_ThrowInternalError(env, "should not reach here"); 521 return; 522 } 523 RESTARTABLE((*my_fstatat64_func)((int)dfd, path, &buf, (int)flag), err); 524 if (err == -1) { 525 throwUnixException(env, errno); 526 } else { 527 prepAttributes(env, &buf, attrs); 528 } 529 } 530 531 JNIEXPORT void JNICALL 532 Java_sun_nio_fs_UnixNativeDispatcher_chmod0(JNIEnv* env, jclass this, 533 jlong pathAddress, jint mode) 534 { 535 int err; 536 const char* path = (const char*)jlong_to_ptr(pathAddress); 537 538 RESTARTABLE(chmod(path, (mode_t)mode), err); 539 if (err == -1) { 540 throwUnixException(env, errno); 541 } 542 } 543 544 JNIEXPORT void JNICALL 545 Java_sun_nio_fs_UnixNativeDispatcher_fchmod(JNIEnv* env, jclass this, jint filedes, 546 jint mode) 547 { 548 int err; 549 550 RESTARTABLE(fchmod((int)filedes, (mode_t)mode), err); 551 if (err == -1) { 552 throwUnixException(env, errno); 553 } 554 } 555 556 557 JNIEXPORT void JNICALL 558 Java_sun_nio_fs_UnixNativeDispatcher_chown0(JNIEnv* env, jclass this, 559 jlong pathAddress, jint uid, jint gid) 560 { 561 int err; 562 const char* path = (const char*)jlong_to_ptr(pathAddress); 563 564 RESTARTABLE(chown(path, (uid_t)uid, (gid_t)gid), err); 565 if (err == -1) { 566 throwUnixException(env, errno); 567 } 568 } 569 570 JNIEXPORT void JNICALL 571 Java_sun_nio_fs_UnixNativeDispatcher_lchown0(JNIEnv* env, jclass this, jlong pathAddress, jint uid, jint gid) 572 { 573 int err; 574 const char* path = (const char*)jlong_to_ptr(pathAddress); 575 576 RESTARTABLE(lchown(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_fchown(JNIEnv* env, jclass this, jint filedes, jint uid, jint gid) 584 { 585 int err; 586 587 RESTARTABLE(fchown(filedes, (uid_t)uid, (gid_t)gid), err); 588 if (err == -1) { 589 throwUnixException(env, errno); 590 } 591 } 592 593 JNIEXPORT void JNICALL 594 Java_sun_nio_fs_UnixNativeDispatcher_utimes0(JNIEnv* env, jclass this, 595 jlong pathAddress, jlong accessTime, jlong modificationTime) 596 { 597 int err; 598 struct timeval times[2]; 599 const char* path = (const char*)jlong_to_ptr(pathAddress); 600 601 times[0].tv_sec = accessTime / 1000000; 602 times[0].tv_usec = accessTime % 1000000; 603 604 times[1].tv_sec = modificationTime / 1000000; 605 times[1].tv_usec = modificationTime % 1000000; 606 607 RESTARTABLE(utimes(path, ×[0]), err); 608 if (err == -1) { 609 throwUnixException(env, errno); 610 } 611 } 612 613 JNIEXPORT void JNICALL 614 Java_sun_nio_fs_UnixNativeDispatcher_futimes(JNIEnv* env, jclass this, jint filedes, 615 jlong accessTime, jlong modificationTime) 616 { 617 struct timeval times[2]; 618 int err = 0; 619 620 times[0].tv_sec = accessTime / 1000000; 621 times[0].tv_usec = accessTime % 1000000; 622 623 times[1].tv_sec = modificationTime / 1000000; 624 times[1].tv_usec = modificationTime % 1000000; 625 626 #ifdef _ALLBSD_SOURCE 627 RESTARTABLE(futimes(filedes, ×[0]), err); 628 #else 629 if (my_futimesat_func == NULL) { 630 JNU_ThrowInternalError(env, "my_ftimesat_func is NULL"); 631 return; 632 } 633 RESTARTABLE((*my_futimesat_func)(filedes, NULL, ×[0]), err); 634 #endif 635 if (err == -1) { 636 throwUnixException(env, errno); 637 } 638 } 639 640 JNIEXPORT jlong JNICALL 641 Java_sun_nio_fs_UnixNativeDispatcher_opendir0(JNIEnv* env, jclass this, 642 jlong pathAddress) 643 { 644 DIR* dir; 645 const char* path = (const char*)jlong_to_ptr(pathAddress); 646 647 /* EINTR not listed as a possible error */ 648 dir = opendir(path); 649 if (dir == NULL) { 650 throwUnixException(env, errno); 651 } 652 return ptr_to_jlong(dir); 653 } 654 655 JNIEXPORT jlong JNICALL 656 Java_sun_nio_fs_UnixNativeDispatcher_fdopendir(JNIEnv* env, jclass this, int dfd) { 657 DIR* dir; 658 659 if (my_fdopendir_func == NULL) { 660 JNU_ThrowInternalError(env, "should not reach here"); 661 return (jlong)-1; 662 } 663 664 /* EINTR not listed as a possible error */ 665 dir = (*my_fdopendir_func)((int)dfd); 666 if (dir == NULL) { 667 throwUnixException(env, errno); 668 } 669 return ptr_to_jlong(dir); 670 } 671 672 JNIEXPORT void JNICALL 673 Java_sun_nio_fs_UnixNativeDispatcher_closedir(JNIEnv* env, jclass this, jlong dir) { 674 DIR* dirp = jlong_to_ptr(dir); 675 676 if (closedir(dirp) == -1 && errno != EINTR) { 677 throwUnixException(env, errno); 678 } 679 } 680 681 JNIEXPORT jbyteArray JNICALL 682 Java_sun_nio_fs_UnixNativeDispatcher_readdir(JNIEnv* env, jclass this, jlong value) { 683 struct dirent64* result; 684 struct { 685 struct dirent64 buf; 686 char name_extra[PATH_MAX + 1 - sizeof result->d_name]; 687 } entry; 688 struct dirent64* ptr = &entry.buf; 689 int res; 690 DIR* dirp = jlong_to_ptr(value); 691 692 /* EINTR not listed as a possible error */ 693 /* TDB: reentrant version probably not required here */ 694 res = readdir64_r(dirp, ptr, &result); 695 696 #ifdef _AIX 697 /* On AIX, readdir_r() returns EBADF (i.e. '9') and sets 'result' to NULL for the */ 698 /* directory stream end. Otherwise, 'errno' will contain the error code. */ 699 if (res != 0) { 700 res = (result == NULL && res == EBADF) ? 0 : errno; 701 } 702 #endif 703 704 if (res != 0) { 705 throwUnixException(env, res); 706 return NULL; 707 } else { 708 if (result == NULL) { 709 return NULL; 710 } else { 711 jsize len = strlen(ptr->d_name); 712 jbyteArray bytes = (*env)->NewByteArray(env, len); 713 if (bytes != NULL) { 714 (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)(ptr->d_name)); 715 } 716 return bytes; 717 } 718 } 719 } 720 721 JNIEXPORT void JNICALL 722 Java_sun_nio_fs_UnixNativeDispatcher_mkdir0(JNIEnv* env, jclass this, 723 jlong pathAddress, jint mode) 724 { 725 const char* path = (const char*)jlong_to_ptr(pathAddress); 726 727 /* EINTR not listed as a possible error */ 728 if (mkdir(path, (mode_t)mode) == -1) { 729 throwUnixException(env, errno); 730 } 731 } 732 733 JNIEXPORT void JNICALL 734 Java_sun_nio_fs_UnixNativeDispatcher_rmdir0(JNIEnv* env, jclass this, 735 jlong pathAddress) 736 { 737 const char* path = (const char*)jlong_to_ptr(pathAddress); 738 739 /* EINTR not listed as a possible error */ 740 if (rmdir(path) == -1) { 741 throwUnixException(env, errno); 742 } 743 } 744 745 JNIEXPORT void JNICALL 746 Java_sun_nio_fs_UnixNativeDispatcher_link0(JNIEnv* env, jclass this, 747 jlong existingAddress, jlong newAddress) 748 { 749 int err; 750 const char* existing = (const char*)jlong_to_ptr(existingAddress); 751 const char* newname = (const char*)jlong_to_ptr(newAddress); 752 753 RESTARTABLE(link(existing, newname), err); 754 if (err == -1) { 755 throwUnixException(env, errno); 756 } 757 } 758 759 760 JNIEXPORT void JNICALL 761 Java_sun_nio_fs_UnixNativeDispatcher_unlink0(JNIEnv* env, jclass this, 762 jlong pathAddress) 763 { 764 const char* path = (const char*)jlong_to_ptr(pathAddress); 765 766 /* EINTR not listed as a possible error */ 767 if (unlink(path) == -1) { 768 throwUnixException(env, errno); 769 } 770 } 771 772 JNIEXPORT void JNICALL 773 Java_sun_nio_fs_UnixNativeDispatcher_unlinkat0(JNIEnv* env, jclass this, jint dfd, 774 jlong pathAddress, jint flags) 775 { 776 const char* path = (const char*)jlong_to_ptr(pathAddress); 777 778 if (my_unlinkat_func == NULL) { 779 JNU_ThrowInternalError(env, "should not reach here"); 780 return; 781 } 782 783 /* EINTR not listed as a possible error */ 784 if ((*my_unlinkat_func)((int)dfd, path, (int)flags) == -1) { 785 throwUnixException(env, errno); 786 } 787 } 788 789 JNIEXPORT void JNICALL 790 Java_sun_nio_fs_UnixNativeDispatcher_rename0(JNIEnv* env, jclass this, 791 jlong fromAddress, jlong toAddress) 792 { 793 const char* from = (const char*)jlong_to_ptr(fromAddress); 794 const char* to = (const char*)jlong_to_ptr(toAddress); 795 796 /* EINTR not listed as a possible error */ 797 if (rename(from, to) == -1) { 798 throwUnixException(env, errno); 799 } 800 } 801 802 JNIEXPORT void JNICALL 803 Java_sun_nio_fs_UnixNativeDispatcher_renameat0(JNIEnv* env, jclass this, 804 jint fromfd, jlong fromAddress, jint tofd, jlong toAddress) 805 { 806 const char* from = (const char*)jlong_to_ptr(fromAddress); 807 const char* to = (const char*)jlong_to_ptr(toAddress); 808 809 if (my_renameat_func == NULL) { 810 JNU_ThrowInternalError(env, "should not reach here"); 811 return; 812 } 813 814 /* EINTR not listed as a possible error */ 815 if ((*my_renameat_func)((int)fromfd, from, (int)tofd, to) == -1) { 816 throwUnixException(env, errno); 817 } 818 } 819 820 JNIEXPORT void JNICALL 821 Java_sun_nio_fs_UnixNativeDispatcher_symlink0(JNIEnv* env, jclass this, 822 jlong targetAddress, jlong linkAddress) 823 { 824 const char* target = (const char*)jlong_to_ptr(targetAddress); 825 const char* link = (const char*)jlong_to_ptr(linkAddress); 826 827 /* EINTR not listed as a possible error */ 828 if (symlink(target, link) == -1) { 829 throwUnixException(env, errno); 830 } 831 } 832 833 JNIEXPORT jbyteArray JNICALL 834 Java_sun_nio_fs_UnixNativeDispatcher_readlink0(JNIEnv* env, jclass this, 835 jlong pathAddress) 836 { 837 jbyteArray result = NULL; 838 char target[PATH_MAX+1]; 839 const char* path = (const char*)jlong_to_ptr(pathAddress); 840 841 /* EINTR not listed as a possible error */ 842 int n = readlink(path, target, sizeof(target)); 843 if (n == -1) { 844 throwUnixException(env, errno); 845 } else { 846 jsize len; 847 if (n == sizeof(target)) { 848 n--; 849 } 850 target[n] = '\0'; 851 len = (jsize)strlen(target); 852 result = (*env)->NewByteArray(env, len); 853 if (result != NULL) { 854 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)target); 855 } 856 } 857 return result; 858 } 859 860 JNIEXPORT jbyteArray JNICALL 861 Java_sun_nio_fs_UnixNativeDispatcher_realpath0(JNIEnv* env, jclass this, 862 jlong pathAddress) 863 { 864 jbyteArray result = NULL; 865 char resolved[PATH_MAX+1]; 866 const char* path = (const char*)jlong_to_ptr(pathAddress); 867 868 /* EINTR not listed as a possible error */ 869 if (realpath(path, resolved) == NULL) { 870 throwUnixException(env, errno); 871 } else { 872 jsize len = (jsize)strlen(resolved); 873 result = (*env)->NewByteArray(env, len); 874 if (result != NULL) { 875 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)resolved); 876 } 877 } 878 return result; 879 } 880 881 JNIEXPORT void JNICALL 882 Java_sun_nio_fs_UnixNativeDispatcher_access0(JNIEnv* env, jclass this, 883 jlong pathAddress, jint amode) 884 { 885 int err; 886 const char* path = (const char*)jlong_to_ptr(pathAddress); 887 888 RESTARTABLE(access(path, (int)amode), err); 889 if (err == -1) { 890 throwUnixException(env, errno); 891 } 892 } 893 894 JNIEXPORT void JNICALL 895 Java_sun_nio_fs_UnixNativeDispatcher_statvfs0(JNIEnv* env, jclass this, 896 jlong pathAddress, jobject attrs) 897 { 898 int err; 899 struct statvfs64 buf; 900 const char* path = (const char*)jlong_to_ptr(pathAddress); 901 902 903 RESTARTABLE(statvfs64(path, &buf), err); 904 if (err == -1) { 905 throwUnixException(env, errno); 906 } else { 907 #ifdef _AIX 908 /* AIX returns ULONG_MAX in buf.f_blocks for the /proc file system. */ 909 /* This is too big for a Java signed long and fools various tests. */ 910 if (buf.f_blocks == ULONG_MAX) { 911 buf.f_blocks = 0; 912 } 913 /* The number of free or available blocks can never exceed the total number of blocks */ 914 if (buf.f_blocks == 0) { 915 buf.f_bfree = 0; 916 buf.f_bavail = 0; 917 } 918 #endif 919 (*env)->SetLongField(env, attrs, attrs_f_frsize, long_to_jlong(buf.f_frsize)); 920 (*env)->SetLongField(env, attrs, attrs_f_blocks, long_to_jlong(buf.f_blocks)); 921 (*env)->SetLongField(env, attrs, attrs_f_bfree, long_to_jlong(buf.f_bfree)); 922 (*env)->SetLongField(env, attrs, attrs_f_bavail, long_to_jlong(buf.f_bavail)); 923 } 924 } 925 926 JNIEXPORT jlong JNICALL 927 Java_sun_nio_fs_UnixNativeDispatcher_pathconf0(JNIEnv* env, jclass this, 928 jlong pathAddress, jint name) 929 { 930 long err; 931 const char* path = (const char*)jlong_to_ptr(pathAddress); 932 933 err = pathconf(path, (int)name); 934 if (err == -1) { 935 throwUnixException(env, errno); 936 } 937 return (jlong)err; 938 } 939 940 JNIEXPORT jlong JNICALL 941 Java_sun_nio_fs_UnixNativeDispatcher_fpathconf(JNIEnv* env, jclass this, 942 jint fd, jint name) 943 { 944 long err; 945 946 err = fpathconf((int)fd, (int)name); 947 if (err == -1) { 948 throwUnixException(env, errno); 949 } 950 return (jlong)err; 951 } 952 953 JNIEXPORT void JNICALL 954 Java_sun_nio_fs_UnixNativeDispatcher_mknod0(JNIEnv* env, jclass this, 955 jlong pathAddress, jint mode, jlong dev) 956 { 957 int err; 958 const char* path = (const char*)jlong_to_ptr(pathAddress); 959 960 RESTARTABLE(mknod(path, (mode_t)mode, (dev_t)dev), err); 961 if (err == -1) { 962 throwUnixException(env, errno); 963 } 964 } 965 966 JNIEXPORT jbyteArray JNICALL 967 Java_sun_nio_fs_UnixNativeDispatcher_getpwuid(JNIEnv* env, jclass this, jint uid) 968 { 969 jbyteArray result = NULL; 970 int buflen; 971 char* pwbuf; 972 973 /* allocate buffer for password record */ 974 buflen = (int)sysconf(_SC_GETPW_R_SIZE_MAX); 975 if (buflen == -1) 976 buflen = ENT_BUF_SIZE; 977 pwbuf = (char*)malloc(buflen); 978 if (pwbuf == NULL) { 979 JNU_ThrowOutOfMemoryError(env, "native heap"); 980 } else { 981 struct passwd pwent; 982 struct passwd* p = NULL; 983 int res = 0; 984 985 errno = 0; 986 #ifdef __solaris__ 987 RESTARTABLE_RETURN_PTR(getpwuid_r((uid_t)uid, &pwent, pwbuf, (size_t)buflen), p); 988 #else 989 RESTARTABLE(getpwuid_r((uid_t)uid, &pwent, pwbuf, (size_t)buflen, &p), res); 990 #endif 991 992 if (res != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') { 993 /* not found or error */ 994 if (errno == 0) 995 errno = ENOENT; 996 throwUnixException(env, errno); 997 } else { 998 jsize len = strlen(p->pw_name); 999 result = (*env)->NewByteArray(env, len); 1000 if (result != NULL) { 1001 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)(p->pw_name)); 1002 } 1003 } 1004 free(pwbuf); 1005 } 1006 1007 return result; 1008 } 1009 1010 1011 JNIEXPORT jbyteArray JNICALL 1012 Java_sun_nio_fs_UnixNativeDispatcher_getgrgid(JNIEnv* env, jclass this, jint gid) 1013 { 1014 jbyteArray result = NULL; 1015 int buflen; 1016 int retry; 1017 1018 /* initial size of buffer for group record */ 1019 buflen = (int)sysconf(_SC_GETGR_R_SIZE_MAX); 1020 if (buflen == -1) 1021 buflen = ENT_BUF_SIZE; 1022 1023 do { 1024 struct group grent; 1025 struct group* g = NULL; 1026 int res = 0; 1027 1028 char* grbuf = (char*)malloc(buflen); 1029 if (grbuf == NULL) { 1030 JNU_ThrowOutOfMemoryError(env, "native heap"); 1031 return NULL; 1032 } 1033 1034 errno = 0; 1035 #ifdef __solaris__ 1036 RESTARTABLE_RETURN_PTR(getgrgid_r((gid_t)gid, &grent, grbuf, (size_t)buflen), g); 1037 #else 1038 RESTARTABLE(getgrgid_r((gid_t)gid, &grent, grbuf, (size_t)buflen, &g), res); 1039 #endif 1040 1041 retry = 0; 1042 if (res != 0 || g == NULL || g->gr_name == NULL || *(g->gr_name) == '\0') { 1043 /* not found or error */ 1044 if (errno == ERANGE) { 1045 /* insufficient buffer size so need larger buffer */ 1046 buflen += ENT_BUF_SIZE; 1047 retry = 1; 1048 } else { 1049 if (errno == 0) 1050 errno = ENOENT; 1051 throwUnixException(env, errno); 1052 } 1053 } else { 1054 jsize len = strlen(g->gr_name); 1055 result = (*env)->NewByteArray(env, len); 1056 if (result != NULL) { 1057 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)(g->gr_name)); 1058 } 1059 } 1060 1061 free(grbuf); 1062 1063 } while (retry); 1064 1065 return result; 1066 } 1067 1068 JNIEXPORT jint JNICALL 1069 Java_sun_nio_fs_UnixNativeDispatcher_getpwnam0(JNIEnv* env, jclass this, 1070 jlong nameAddress) 1071 { 1072 jint uid = -1; 1073 int buflen; 1074 char* pwbuf; 1075 1076 /* allocate buffer for password record */ 1077 buflen = (int)sysconf(_SC_GETPW_R_SIZE_MAX); 1078 if (buflen == -1) 1079 buflen = ENT_BUF_SIZE; 1080 pwbuf = (char*)malloc(buflen); 1081 if (pwbuf == NULL) { 1082 JNU_ThrowOutOfMemoryError(env, "native heap"); 1083 } else { 1084 struct passwd pwent; 1085 struct passwd* p = NULL; 1086 int res = 0; 1087 const char* name = (const char*)jlong_to_ptr(nameAddress); 1088 1089 errno = 0; 1090 #ifdef __solaris__ 1091 RESTARTABLE_RETURN_PTR(getpwnam_r(name, &pwent, pwbuf, (size_t)buflen), p); 1092 #else 1093 RESTARTABLE(getpwnam_r(name, &pwent, pwbuf, (size_t)buflen, &p), res); 1094 #endif 1095 1096 if (res != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') { 1097 /* not found or error */ 1098 if (errno != 0 && errno != ENOENT && errno != ESRCH) 1099 throwUnixException(env, errno); 1100 } else { 1101 uid = p->pw_uid; 1102 } 1103 free(pwbuf); 1104 } 1105 1106 return uid; 1107 } 1108 1109 JNIEXPORT jint JNICALL 1110 Java_sun_nio_fs_UnixNativeDispatcher_getgrnam0(JNIEnv* env, jclass this, 1111 jlong nameAddress) 1112 { 1113 jint gid = -1; 1114 int buflen, retry; 1115 1116 /* initial size of buffer for group record */ 1117 buflen = (int)sysconf(_SC_GETGR_R_SIZE_MAX); 1118 if (buflen == -1) 1119 buflen = ENT_BUF_SIZE; 1120 1121 do { 1122 struct group grent; 1123 struct group* g = NULL; 1124 int res = 0; 1125 char *grbuf; 1126 const char* name = (const char*)jlong_to_ptr(nameAddress); 1127 1128 grbuf = (char*)malloc(buflen); 1129 if (grbuf == NULL) { 1130 JNU_ThrowOutOfMemoryError(env, "native heap"); 1131 return -1; 1132 } 1133 1134 errno = 0; 1135 #ifdef __solaris__ 1136 RESTARTABLE_RETURN_PTR(getgrnam_r(name, &grent, grbuf, (size_t)buflen), g); 1137 #else 1138 RESTARTABLE(getgrnam_r(name, &grent, grbuf, (size_t)buflen, &g), res); 1139 #endif 1140 1141 retry = 0; 1142 if (res != 0 || g == NULL || g->gr_name == NULL || *(g->gr_name) == '\0') { 1143 /* not found or error */ 1144 if (errno != 0 && errno != ENOENT && errno != ESRCH) { 1145 if (errno == ERANGE) { 1146 /* insufficient buffer size so need larger buffer */ 1147 buflen += ENT_BUF_SIZE; 1148 retry = 1; 1149 } else { 1150 throwUnixException(env, errno); 1151 } 1152 } 1153 } else { 1154 gid = g->gr_gid; 1155 } 1156 1157 free(grbuf); 1158 1159 } while (retry); 1160 1161 return gid; 1162 }