Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/aix/native/sun/tools/attach/AixVirtualMachine.c
38829 views
/*1* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.2* Copyright 2015 SAP AG. 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation. Oracle designates this8* particular file as subject to the "Classpath" exception as provided9* 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 WITHOUT12* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or13* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License14* version 2 for more details (a copy is included in the LICENSE file that15* accompanied this code).16*17* You should have received a copy of the GNU General Public License version18* 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 USA22* or visit www.oracle.com if you need additional information or have any23* questions.24*/2526#include "jni.h"27#include "jni_util.h"28#include "jvm.h"2930#include <stdio.h>31#include <stdlib.h>32#include <string.h>33#include <errno.h>34#include <unistd.h>35#include <signal.h>36#include <dirent.h>37#include <ctype.h>38#include <sys/types.h>39#include <sys/socket.h>40#include <sys/stat.h>41#include <sys/un.h>4243/*44* Based on 'LinuxVirtualMachine.c'. Non-relevant code has been removed and all45* occurrences of the string "Linux" have been replaced by "Aix".46*/4748#include "sun_tools_attach_AixVirtualMachine.h"4950#define RESTARTABLE(_cmd, _result) do { \51do { \52_result = _cmd; \53} while((_result == -1) && (errno == EINTR)); \54} while(0)555657/*58* Class: sun_tools_attach_AixVirtualMachine59* Method: socket60* Signature: ()I61*/62JNIEXPORT jint JNICALL Java_sun_tools_attach_AixVirtualMachine_socket63(JNIEnv *env, jclass cls)64{65int fd = socket(PF_UNIX, SOCK_STREAM, 0);66if (fd == -1) {67JNU_ThrowIOExceptionWithLastError(env, "socket");68}69/* added time out values */70else {71struct timeval tv;72tv.tv_sec = 2 * 60;73tv.tv_usec = 0;7475setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv));76setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char*)&tv, sizeof(tv));77}78return (jint)fd;79}8081/*82* Class: sun_tools_attach_AixVirtualMachine83* Method: connect84* Signature: (ILjava/lang/String;)I85*/86JNIEXPORT void JNICALL Java_sun_tools_attach_AixVirtualMachine_connect87(JNIEnv *env, jclass cls, jint fd, jstring path)88{89jboolean isCopy;90const char* p = GetStringPlatformChars(env, path, &isCopy);91if (p != NULL) {92struct sockaddr_un addr;93int err = 0;9495/* added missing structure initialization */96memset(&addr,0, sizeof(addr));97addr.sun_family = AF_UNIX;98strcpy(addr.sun_path, p);99/* We must call bind with the actual socketaddr length. This is obligatory for AS400. */100if (connect(fd, (struct sockaddr*)&addr, SUN_LEN(&addr)) == -1) {101err = errno;102}103104if (isCopy) {105JNU_ReleaseStringPlatformChars(env, path, p);106}107108/*109* If the connect failed then we throw the appropriate exception110* here (can't throw it before releasing the string as can't call111* JNI with pending exception)112*/113if (err != 0) {114if (err == ENOENT) {115JNU_ThrowByName(env, "java/io/FileNotFoundException", NULL);116} else {117char* msg = strdup(strerror(err));118JNU_ThrowIOException(env, msg);119if (msg != NULL) {120free(msg);121}122}123}124}125}126127128/*129* Structure and callback function used to send a QUIT signal to all130* children of a given process131*/132typedef struct {133pid_t ppid;134} SendQuitContext;135136static void SendQuitCallback(const pid_t pid, void* user_data) {137SendQuitContext* context = (SendQuitContext*)user_data;138pid_t parent = getParent(pid);139if (parent == context->ppid) {140kill(pid, SIGQUIT);141}142}143144/*145* Class: sun_tools_attach_AixVirtualMachine146* Method: sendQuitTo147* Signature: (I)V148*/149JNIEXPORT void JNICALL Java_sun_tools_attach_AixVirtualMachine_sendQuitTo150(JNIEnv *env, jclass cls, jint pid)151{152if (kill((pid_t)pid, SIGQUIT)) {153JNU_ThrowIOExceptionWithLastError(env, "kill");154}155}156157/*158* Class: sun_tools_attach_AixVirtualMachine159* Method: checkPermissions160* Signature: (Ljava/lang/String;)V161*/162JNIEXPORT void JNICALL Java_sun_tools_attach_AixVirtualMachine_checkPermissions163(JNIEnv *env, jclass cls, jstring path)164{165jboolean isCopy;166const char* p = GetStringPlatformChars(env, path, &isCopy);167if (p != NULL) {168struct stat64 sb;169uid_t uid, gid;170int res;171/* added missing initialization of the stat64 buffer */172memset(&sb, 0, sizeof(struct stat64));173174/*175* Check that the path is owned by the effective uid/gid of this176* process. Also check that group/other access is not allowed.177*/178uid = geteuid();179gid = getegid();180181res = stat64(p, &sb);182if (res != 0) {183/* save errno */184res = errno;185}186187if (res == 0) {188char msg[100];189jboolean isError = JNI_FALSE;190if (sb.st_uid != uid) {191jio_snprintf(msg, sizeof(msg)-1,192"file should be owned by the current user (which is %d) but is owned by %d", uid, sb.st_uid);193isError = JNI_TRUE;194} else if (sb.st_gid != gid) {195jio_snprintf(msg, sizeof(msg)-1,196"file's group should be the current group (which is %d) but the group is %d", gid, sb.st_gid);197isError = JNI_TRUE;198} else if ((sb.st_mode & (S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) != 0) {199jio_snprintf(msg, sizeof(msg)-1,200"file should only be readable and writable by the owner but has 0%03o access", sb.st_mode & 0777);201isError = JNI_TRUE;202}203if (isError) {204char buf[256];205jio_snprintf(buf, sizeof(buf)-1, "well-known file %s is not secure: %s", p, msg);206JNU_ThrowIOException(env, buf);207}208} else {209char* msg = strdup(strerror(res));210JNU_ThrowIOException(env, msg);211if (msg != NULL) {212free(msg);213}214}215216if (isCopy) {217JNU_ReleaseStringPlatformChars(env, path, p);218}219}220}221222/*223* Class: sun_tools_attach_AixVirtualMachine224* Method: close225* Signature: (I)V226*/227JNIEXPORT void JNICALL Java_sun_tools_attach_AixVirtualMachine_close228(JNIEnv *env, jclass cls, jint fd)229{230int res;231/* Fixed deadlock when this call of close by the client is not seen by the attach server232* which has accepted the (very short) connection already and is waiting for the request. But read don't get a byte,233* because the close is lost without shutdown.234*/235shutdown(fd, 2);236RESTARTABLE(close(fd), res);237}238239/*240* Class: sun_tools_attach_AixVirtualMachine241* Method: read242* Signature: (I[BI)I243*/244JNIEXPORT jint JNICALL Java_sun_tools_attach_AixVirtualMachine_read245(JNIEnv *env, jclass cls, jint fd, jbyteArray ba, jint off, jint baLen)246{247unsigned char buf[128];248size_t len = sizeof(buf);249ssize_t n;250251size_t remaining = (size_t)(baLen - off);252if (len > remaining) {253len = remaining;254}255256RESTARTABLE(read(fd, buf, len), n);257if (n == -1) {258JNU_ThrowIOExceptionWithLastError(env, "read");259} else {260if (n == 0) {261n = -1; // EOF262} else {263(*env)->SetByteArrayRegion(env, ba, off, (jint)n, (jbyte *)(buf));264}265}266return n;267}268269/*270* Class: sun_tools_attach_AixVirtualMachine271* Method: write272* Signature: (I[B)V273*/274JNIEXPORT void JNICALL Java_sun_tools_attach_AixVirtualMachine_write275(JNIEnv *env, jclass cls, jint fd, jbyteArray ba, jint off, jint bufLen)276{277size_t remaining = bufLen;278do {279unsigned char buf[128];280size_t len = sizeof(buf);281int n;282283if (len > remaining) {284len = remaining;285}286(*env)->GetByteArrayRegion(env, ba, off, len, (jbyte *)buf);287288RESTARTABLE(write(fd, buf, len), n);289if (n > 0) {290off += n;291remaining -= n;292} else {293JNU_ThrowIOExceptionWithLastError(env, "write");294return;295}296297} while (remaining > 0);298}299300301