annotate src/jdk.security.auth/unix/native/libjaas/Unix.c @ 51120:dccdf51b10dd

8207233: Minor improvements of jdk C-coding Reviewed-by: rriggs, prr
author goetz
date Fri, 13 Jul 2018 17:42:24 +0200
parents 71c04702a3d5
children
rev   line source
duke@2 1 /*
goetz@51120 2 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
duke@2 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@2 4 *
duke@2 5 * This code is free software; you can redistribute it and/or modify it
duke@2 6 * under the terms of the GNU General Public License version 2 only, as
ohair@5506 7 * published by the Free Software Foundation. Oracle designates this
duke@2 8 * particular file as subject to the "Classpath" exception as provided
ohair@5506 9 * by Oracle in the LICENSE file that accompanied this code.
duke@2 10 *
duke@2 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@2 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@2 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@2 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@2 15 * accompanied this code).
duke@2 16 *
duke@2 17 * You should have received a copy of the GNU General Public License version
duke@2 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@2 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@2 20 *
ohair@5506 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@5506 22 * or visit www.oracle.com if you need additional information or have any
ohair@5506 23 * questions.
duke@2 24 */
duke@2 25
duke@2 26 #include <jni.h>
bobv@33653 27 #include "jni_util.h"
duke@2 28 #include "com_sun_security_auth_module_UnixSystem.h"
duke@2 29 #include <stdio.h>
duke@2 30 #include <sys/types.h>
duke@2 31 #include <unistd.h>
duke@2 32 #include <stdlib.h>
duke@2 33 #include <string.h>
duke@2 34
dcubed@40182 35 /* For POSIX-compliant getpwuid_r on Solaris */
dcubed@40182 36 #if defined(__solaris__)
dcubed@40182 37 #define _POSIX_PTHREAD_SEMANTICS
dcubed@40182 38 #endif
dcubed@40182 39 #include <pwd.h>
dcubed@40182 40
bobv@33653 41 /*
bobv@33653 42 * Declare library specific JNI_Onload entry if static build
bobv@33653 43 */
bobv@33653 44 DEF_STATIC_JNI_OnLoad
bobv@33653 45
duke@2 46 JNIEXPORT void JNICALL
duke@2 47 Java_com_sun_security_auth_module_UnixSystem_getUnixInfo
duke@2 48 (JNIEnv *env, jobject obj) {
duke@2 49
duke@2 50 int i;
duke@2 51 char pwd_buf[1024];
duke@2 52 struct passwd *pwd;
duke@2 53 struct passwd resbuf;
chegar@14203 54 jfieldID userNameID;
chegar@14203 55 jfieldID userID;
chegar@14203 56 jfieldID groupID;
chegar@14203 57 jfieldID supplementaryGroupID;
chegar@14203 58
duke@2 59 jstring jstr;
duke@2 60 jlongArray jgroups;
duke@2 61 jlong *jgroupsAsArray;
jzavgren@16488 62 jsize numSuppGroups;
jzavgren@16488 63 gid_t *groups;
jzavgren@16488 64 jclass cls;
jzavgren@16488 65
jzavgren@16488 66 numSuppGroups = getgroups(0, NULL);
goetz@51120 67 if (numSuppGroups == -1) {
goetz@51120 68 return;
goetz@51120 69 }
jzavgren@16488 70 groups = (gid_t *)calloc(numSuppGroups, sizeof(gid_t));
jzavgren@16488 71 if (groups == NULL) {
jzavgren@16488 72 jclass cls = (*env)->FindClass(env,"java/lang/OutOfMemoryError");
alanb@22648 73 if (cls != NULL)
jzavgren@16488 74 (*env)->ThrowNew(env, cls, NULL);
jzavgren@16488 75 return;
jzavgren@16488 76 }
jzavgren@16488 77
jzavgren@16488 78 cls = (*env)->GetObjectClass(env, obj);
duke@2 79
duke@2 80 memset(pwd_buf, 0, sizeof(pwd_buf));
chegar@14203 81
duke@2 82 if (getpwuid_r(getuid(), &resbuf, pwd_buf, sizeof(pwd_buf), &pwd) == 0 &&
duke@2 83 pwd != NULL &&
duke@2 84 getgroups(numSuppGroups, groups) != -1) {
duke@2 85
chegar@14203 86 userNameID = (*env)->GetFieldID(env, cls, "username", "Ljava/lang/String;");
chegar@14203 87 if (userNameID == 0)
chegar@14203 88 goto cleanUpAndReturn;
chegar@14203 89
chegar@14203 90 userID = (*env)->GetFieldID(env, cls, "uid", "J");
chegar@14203 91 if (userID == 0)
chegar@14203 92 goto cleanUpAndReturn;
chegar@14203 93
chegar@14203 94 groupID = (*env)->GetFieldID(env, cls, "gid", "J");
chegar@14203 95 if (groupID == 0)
chegar@14203 96 goto cleanUpAndReturn;
chegar@14203 97
chegar@14203 98 supplementaryGroupID = (*env)->GetFieldID(env, cls, "groups", "[J");
chegar@14203 99 if (supplementaryGroupID == 0)
chegar@14203 100 goto cleanUpAndReturn;
chegar@14203 101
duke@2 102 jstr = (*env)->NewStringUTF(env, pwd->pw_name);
alanb@22648 103 if (jstr == NULL)
alanb@22648 104 goto cleanUpAndReturn;
chegar@14203 105 (*env)->SetObjectField(env, obj, userNameID, jstr);
duke@2 106
chegar@14203 107 (*env)->SetLongField(env, obj, userID, pwd->pw_uid);
duke@2 108
chegar@14203 109 (*env)->SetLongField(env, obj, groupID, pwd->pw_gid);
duke@2 110
duke@2 111 jgroups = (*env)->NewLongArray(env, numSuppGroups);
alanb@22648 112 if (jgroups == NULL)
alanb@22648 113 goto cleanUpAndReturn;
duke@2 114 jgroupsAsArray = (*env)->GetLongArrayElements(env, jgroups, 0);
alanb@22648 115 if (jgroupsAsArray == NULL)
alanb@22648 116 goto cleanUpAndReturn;
duke@2 117 for (i = 0; i < numSuppGroups; i++)
duke@2 118 jgroupsAsArray[i] = groups[i];
duke@2 119 (*env)->ReleaseLongArrayElements(env, jgroups, jgroupsAsArray, 0);
chegar@14203 120 (*env)->SetObjectField(env, obj, supplementaryGroupID, jgroups);
duke@2 121 }
chegar@14203 122 cleanUpAndReturn:
chegar@14203 123 free(groups);
duke@2 124 return;
duke@2 125 }