1 /*
   2  * Copyright (c) 2008, 2012, 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 "jni.h"
  27 #include "jni_util.h"
  28 #include "jvm.h"
  29 #include "jlong.h"
  30 
  31 #include <strings.h>
  32 #include <errno.h>
  33 #include <sys/acl.h>
  34 #include <sys/mnttab.h>
  35 #include <sys/mkdev.h>
  36 
  37 #include "jni.h"
  38 
  39 #include "sun_nio_fs_SolarisNativeDispatcher.h"
  40 
  41 static jfieldID entry_name;
  42 static jfieldID entry_dir;
  43 static jfieldID entry_fstype;
  44 static jfieldID entry_options;
  45 static jfieldID entry_dev;
  46 
  47 static void throwUnixException(JNIEnv* env, int errnum) {
  48     jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException",
  49         "(I)V", errnum);
  50     if (x != NULL) {
  51         (*env)->Throw(env, x);
  52     }
  53 }
  54 
  55 JNIEXPORT void JNICALL
  56 Java_sun_nio_fs_SolarisNativeDispatcher_init(JNIEnv *env, jclass clazz) {
  57     clazz = (*env)->FindClass(env, "sun/nio/fs/UnixMountEntry");
  58     CHECK_NULL(clazz);
  59     entry_name = (*env)->GetFieldID(env, clazz, "name", "[B");
  60     CHECK_NULL(entry_name);
  61     entry_dir = (*env)->GetFieldID(env, clazz, "dir", "[B");
  62     CHECK_NULL(entry_dir);
  63     entry_fstype = (*env)->GetFieldID(env, clazz, "fstype", "[B");
  64     CHECK_NULL(entry_fstype);
  65     entry_options = (*env)->GetFieldID(env, clazz, "opts", "[B");
  66     CHECK_NULL(entry_options);
  67     entry_dev = (*env)->GetFieldID(env, clazz, "dev", "J");
  68     CHECK_NULL(entry_dev);
  69 }
  70 
  71 JNIEXPORT jint JNICALL
  72 Java_sun_nio_fs_SolarisNativeDispatcher_facl(JNIEnv* env, jclass this, jint fd,
  73     jint cmd, jint nentries, jlong address)
  74 {
  75     void* aclbufp = jlong_to_ptr(address);
  76     int n = -1;
  77 
  78     n = facl((int)fd, (int)cmd, (int)nentries, aclbufp);
  79     if (n == -1) {
  80         throwUnixException(env, errno);
  81     }
  82     return (jint)n;
  83 }
  84 
  85 JNIEXPORT jint JNICALL
  86 Java_sun_nio_fs_SolarisNativeDispatcher_getextmntent(JNIEnv* env, jclass this,
  87     jlong value, jobject entry)
  88 {
  89     struct extmnttab ent;
  90     FILE* fp = jlong_to_ptr(value);
  91     jsize len;
  92     jbyteArray bytes;
  93     char* name;
  94     char* dir;
  95     char* fstype;
  96     char* options;
  97     dev_t dev;
  98 
  99     if (getextmntent(fp, &ent, 0))
 100         return -1;
 101     name = ent.mnt_special;
 102     dir = ent.mnt_mountp;
 103     fstype = ent.mnt_fstype;
 104     options = ent.mnt_mntopts;
 105     dev = makedev(ent.mnt_major, ent.mnt_minor);
 106     if (dev == NODEV) {
 107         throwUnixException(env, errno);
 108         return -1;
 109     }
 110 
 111     len = strlen(name);
 112     bytes = (*env)->NewByteArray(env, len);
 113     if (bytes == NULL)
 114         return -1;
 115     (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)name);
 116     (*env)->SetObjectField(env, entry, entry_name, bytes);
 117 
 118     len = strlen(dir);
 119     bytes = (*env)->NewByteArray(env, len);
 120     if (bytes == NULL)
 121         return -1;
 122     (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)dir);
 123     (*env)->SetObjectField(env, entry, entry_dir, bytes);
 124 
 125     len = strlen(fstype);
 126     bytes = (*env)->NewByteArray(env, len);
 127     if (bytes == NULL)
 128         return -1;
 129     (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)fstype);
 130     (*env)->SetObjectField(env, entry, entry_fstype, bytes);
 131 
 132     len = strlen(options);
 133     bytes = (*env)->NewByteArray(env, len);
 134     if (bytes == NULL)
 135         return -1;
 136     (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)options);
 137     (*env)->SetObjectField(env, entry, entry_options, bytes);
 138 
 139     if (dev != 0)
 140         (*env)->SetLongField(env, entry, entry_dev, (jlong)dev);
 141 
 142     return 0;
 143 }