File: ipc_socket_client.cpp

package info (click to toggle)
intel-compute-runtime 26.05.37020.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,596 kB
  • sloc: cpp: 976,037; lisp: 2,096; sh: 704; makefile: 162
file content (182 lines) | stat: -rw-r--r-- 6,235 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (C) 2026 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/source/os_interface/linux/ipc_socket_client.h"

#include "shared/source/debug_settings/debug_settings_manager.h"
#include "shared/source/os_interface/linux/sys_calls.h"

#include <algorithm>
#include <cstddef>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

namespace NEO {

IpcSocketClient::IpcSocketClient() = default;

IpcSocketClient::~IpcSocketClient() {
    disconnect();
}

bool IpcSocketClient::connectToServer(const std::string &socketPath) {
    if (isConnected()) {
        return true;
    }

    clientSocket = NEO::SysCalls::socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
    if (clientSocket == -1) {
        PRINT_STRING(debugManager.flags.PrintDebugMessages.get(), stderr,
                     "IpcSocketClient: Failed to create socket: %s\n", strerror(errno));
        return false;
    }

    struct timeval timeout;
    timeout.tv_sec = socketTimeout / 1000;
    timeout.tv_usec = (socketTimeout % 1000) * 1000;

    NEO::SysCalls::setsockopt(clientSocket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
    NEO::SysCalls::setsockopt(clientSocket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));

    struct sockaddr_un addr = {};
    addr.sun_family = AF_UNIX;
    size_t nameLen = std::min(socketPath.size(), sizeof(addr.sun_path) - 1);
    addr.sun_path[0] = '\0';
    memcpy(addr.sun_path + 1, socketPath.data(), nameLen);
    socklen_t addrLen = static_cast<socklen_t>(offsetof(struct sockaddr_un, sun_path) + 1 + nameLen);

    if (NEO::SysCalls::connect(clientSocket, reinterpret_cast<struct sockaddr *>(&addr), addrLen) == -1) {
        PRINT_STRING(debugManager.flags.PrintDebugMessages.get(), stderr,
                     "IpcSocketClient: Failed to connect to abstract server '%s': %s\n",
                     socketPath.c_str(), strerror(errno));
        NEO::SysCalls::close(clientSocket);
        clientSocket = -1;
        return false;
    }

    serverSocketPath = socketPath;
    PRINT_STRING(debugManager.flags.PrintDebugMessages.get(), stderr,
                 "IpcSocketClient: Connected to abstract server name=%s\n", socketPath.c_str());
    return true;
}

void IpcSocketClient::disconnect() {
    if (clientSocket != -1) {
        NEO::SysCalls::close(clientSocket);
        clientSocket = -1;
        serverSocketPath.clear();
    }
}

int IpcSocketClient::requestHandle(uint64_t handleId) {
    if (!isConnected()) {
        return -1;
    }

    IpcSocketMessage msg;
    msg.type = IpcSocketMessageType::requestHandle;
    msg.processId = SysCalls::getpid();
    msg.handleId = handleId;
    msg.payloadSize = 0;

    if (!sendMessage(msg)) {
        return -1;
    }

    IpcSocketResponsePayload responsePayload = {};
    int receivedFd = receiveFileDescriptor(&responsePayload, sizeof(responsePayload));

    if (receivedFd == -1) {
        IpcSocketMessage response;
        if (receiveMessage(response, &responsePayload, sizeof(responsePayload))) {
            if (response.type == IpcSocketMessageType::responseHandle &&
                response.handleId == handleId && !responsePayload.success) {
                PRINT_STRING(debugManager.flags.PrintDebugMessages.get(), stderr,
                             "IpcSocketClient: Server reported failure for handle %lu\n", handleId);
            }
        }
        return -1;
    }

    if (!responsePayload.success) {
        NEO::SysCalls::close(receivedFd);
        return -1;
    }

    PRINT_STRING(debugManager.flags.PrintDebugMessages.get(), stderr,
                 "IpcSocketClient: Received handle %lu as fd %d\n", handleId, receivedFd);
    return receivedFd;
}

int IpcSocketClient::receiveFileDescriptor(void *data, size_t dataSize) {
    struct msghdr msg = {};
    struct iovec iov;
    char cmsgBuffer[CMSG_SPACE(sizeof(int))];

    if (data && dataSize > 0) {
        iov.iov_base = data;
        iov.iov_len = dataSize;
        msg.msg_iov = &iov;
        msg.msg_iovlen = 1;
    }

    msg.msg_control = cmsgBuffer;
    msg.msg_controllen = sizeof(cmsgBuffer);

    if (NEO::SysCalls::recvmsg(clientSocket, &msg, 0) == -1) {
        PRINT_STRING(debugManager.flags.PrintDebugMessages.get(), stderr,
                     "IpcSocketClient: Failed to receive message: %s\n", strerror(errno));
        return -1;
    }

    struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
    if (cmsg && cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
        return *reinterpret_cast<int *>(CMSG_DATA(cmsg));
    }

    return -1;
}

bool IpcSocketClient::sendMessage(const IpcSocketMessage &msg, const void *payload) {
    if (NEO::SysCalls::send(clientSocket, &msg, sizeof(msg), 0) != sizeof(msg)) {
        PRINT_STRING(debugManager.flags.PrintDebugMessages.get(), stderr,
                     "IpcSocketClient: Failed to send message header: %s\n", strerror(errno));
        return false;
    }

    if (payload && msg.payloadSize > 0) {
        if (NEO::SysCalls::send(clientSocket, payload, msg.payloadSize, 0) != static_cast<ssize_t>(msg.payloadSize)) {
            PRINT_STRING(debugManager.flags.PrintDebugMessages.get(), stderr,
                         "IpcSocketClient: Failed to send message payload: %s\n", strerror(errno));
            return false;
        }
    }

    return true;
}

bool IpcSocketClient::receiveMessage(IpcSocketMessage &msg, void *payload, size_t maxPayloadSize) {
    if (NEO::SysCalls::recv(clientSocket, &msg, sizeof(msg), MSG_WAITALL) != sizeof(msg)) {
        PRINT_STRING(debugManager.flags.PrintDebugMessages.get(), stderr,
                     "IpcSocketClient: Failed to receive message header: %s\n", strerror(errno));
        return false;
    }

    if (payload && msg.payloadSize > 0 && maxPayloadSize >= msg.payloadSize) {
        if (NEO::SysCalls::recv(clientSocket, payload, msg.payloadSize, MSG_WAITALL) != static_cast<ssize_t>(msg.payloadSize)) {
            PRINT_STRING(debugManager.flags.PrintDebugMessages.get(), stderr,
                         "IpcSocketClient: Failed to receive message payload: %s\n", strerror(errno));
            return false;
        }
    }

    return true;
}

} // namespace NEO