ReturnCode ProfilerSocket::openSocket()

in jvmti-access/src/main/jni/ProfilerSocket.cpp [52:81]


        ReturnCode ProfilerSocket::openSocket(JNIEnv* jniEnv, jstring filepath) {
            std::lock_guard<std::mutex> guard(mutex);
            if(state != nullptr) {
                return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "Profiler socket already opened!");
            }

            int socketFd = socket(PF_UNIX, SOCK_DGRAM, 0);
            if (socketFd == -1) {
                return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "Could not create SOCK_DGRAM domain socket, error is ", errno);
            }
            std::unique_ptr<State> newState = std::make_unique<State>(socketFd);

            int flags = fcntl(socketFd, F_GETFL, 0);
            if (flags == -1) {
                return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "Could not read fnctl flags from socket, error is ", errno);
            }

            int fnctlResult = fcntl(socketFd, F_SETFL, flags | O_NONBLOCK);
            if (fnctlResult != 0){
                return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "Could not configure socket to be non-blocking, error is ", errno);                
            }

            auto bindRes = newState->bindToPath(jniEnv, filepath);
            if (bindRes != ReturnCode::SUCCESS) {
                return bindRes;
            }

            state = std::move(newState);
            return ReturnCode::SUCCESS;
        }