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     if (clazz == NULL)
  59         return;
  60 
  61     entry_name = (*env)->GetFieldID(env, clazz, "name", "[B");
  62     entry_dir = (*env)->GetFieldID(env, clazz, "dir", "[B");
  63     entry_fstype = (*env)->GetFieldID(env, clazz, "fstype", "[B");
  64     entry_options = (*env)->GetFieldID(env, clazz, "opts", "[B");
  65     entry_dev = (*env)->GetFieldID(env, clazz, "dev", "J");
  66 }
  67 
  68 JNIEXPORT jint JNICALL
  69 Java_sun_nio_fs_SolarisNativeDispatcher_facl(JNIEnv* env, jclass this, jint fd,
  70     jint cmd, jint nentries, jlong address)
  71 {
  72     void* aclbufp = jlong_to_ptr(address);
  73     int n = -1;
  74 
  75     n = facl((int)fd, (int)cmd, (int)nentries, aclbufp);
  76     if (n == -1) {
  77         throwUnixException(env, errno);
  78     }
  79     return (jint)n;
  80 }
  81 
  82 JNIEXPORT jint JNICALL
  83 Java_sun_nio_fs_SolarisNativeDispatcher_getextmntent(JNIEnv* env, jclass this,
  84     jlong value, jobject entry)
  85 {
  86     struct extmnttab ent;
  87     FILE* fp = jlong_to_ptr(value);
  88     jsize len;
  89     jbyteArray bytes;
  90     char* name;
  91     char* dir;
  92     char* fstype;
  93     char* options;
  94     dev_t dev;
  95 
  96     if (getextmntent(fp, &ent, 0))
  97         return -1;
  98     name = ent.mnt_special;
  99     dir = ent.mnt_mountp;
 100     fstype = ent.mnt_fstype;
 101     options = ent.mnt_mntopts;
 102     dev = makedev(ent.mnt_major, ent.mnt_minor);
 103     if (dev == NODEV) {
 104         throwUnixException(env, errno);
 105         return -1;
 106     }
 107 
 108     len = strlen(name);
 109     bytes = (*env)->NewByteArray(env, len);
 110     if (bytes == NULL)
 111         return -1;
 112     (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)name);
 113     (*env)->SetObjectField(env, entry, entry_name, bytes);
 114 
 115     len = strlen(dir);
 116     bytes = (*env)->NewByteArray(env, len);
 117     if (bytes == NULL)
 118         return -1;
 119     (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)dir);
 120     (*env)->SetObjectField(env, entry, entry_dir, bytes);
 121 
 122     len = strlen(fstype);
 123     bytes = (*env)->NewByteArray(env, len);
 124     if (bytes == NULL)
 125         return -1;
 126     (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)fstype);
 127     (*env)->SetObjectField(env, entry, entry_fstype, bytes);
 128 
 129     len = strlen(options);
 130     bytes = (*env)->NewByteArray(env, len);
 131     if (bytes == NULL)
 132         return -1;
 133     (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)options);
 134     (*env)->SetObjectField(env, entry, entry_options, bytes);
 135 
 136     if (dev != 0)
 137         (*env)->SetLongField(env, entry, entry_dev, (jlong)dev);
 138 
 139     return 0;
 140 }