ReturnCode ProfilerSocket::State::bindToPath()

in jvmti-access/src/main/jni/ProfilerSocket.cpp [18:48]


        ReturnCode ProfilerSocket::State::bindToPath(JNIEnv* jniEnv, jstring filepath) {

            if(socketFilePath != "") {
                return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "Socket has already been bound to ", socketFilePath);
            }
            if(filepath == nullptr) {
                return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "The provided filepath is null");
            }

            jboolean isCopy;
            sockaddr_un addr = {};
            addr.sun_family = AF_UNIX;
            const char* pathCstr = jniEnv->GetStringUTFChars(filepath, &isCopy);
            std::string pathStr(pathCstr);
            jniEnv->ReleaseStringUTFChars(filepath, pathCstr);

            size_t maxLen = sizeof(addr.sun_path) - 1;
            if (pathStr.length() > maxLen) {
                return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "The provided filepath '", pathStr,"' is too long, max allowed character count is ", maxLen);
            }
            if (pathStr.empty()) {
                return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "The provided filepath is empty");
            }
            strncpy(addr.sun_path, pathStr.c_str(), maxLen);

            if (bind(socketFd, (sockaddr*)&addr, sizeof(addr)) != 0) {
                return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "Could not bind socket to the given filepath, error is ", errno);    
            }
            socketFilePath = pathStr;
            return ReturnCode::SUCCESS;
        }