The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

ipc: Refactor and tidy client connection

Also move git hash check as early as possible.

authored by

Jakob Bornecrantz and committed by
Simon Zeni
332b32b7 bfffb0c5

+113 -48
+113 -48
src/xrt/ipc/client/ipc_client_connection.c
··· 200 200 return true; 201 201 } 202 202 203 - int 204 - getpid() 205 - { 206 - return GetCurrentProcessId(); 207 - } 203 + #else 208 204 209 - #else 210 205 static bool 211 206 ipc_client_socket_connect(struct ipc_connection *ipc_c) 212 207 { ··· 248 243 249 244 return true; 250 245 } 246 + 251 247 #endif 252 248 253 249 250 + static xrt_result_t 251 + ipc_client_setup_shm(struct ipc_connection *ipc_c) 252 + { 253 + /* 254 + * Get our shared memory area from the server. 255 + */ 256 + 257 + xrt_result_t xret = ipc_call_instance_get_shm_fd(ipc_c, &ipc_c->ism_handle, 1); 258 + if (xret != XRT_SUCCESS) { 259 + IPC_ERROR(ipc_c, "Failed to retrieve shm fd!"); 260 + return xret; 261 + } 262 + 263 + /* 264 + * Now map it. 265 + */ 266 + 267 + const size_t size = sizeof(struct ipc_shared_memory); 268 + 269 + #ifdef XRT_OS_WINDOWS 270 + DWORD access = FILE_MAP_READ | FILE_MAP_WRITE; 271 + 272 + ipc_c->ism = MapViewOfFile(ipc_c->ism_handle, access, 0, 0, size); 273 + #else 274 + const int flags = MAP_SHARED; 275 + const int access = PROT_READ | PROT_WRITE; 276 + 277 + ipc_c->ism = mmap(NULL, size, access, flags, ipc_c->ism_handle, 0); 278 + #endif 279 + 280 + if (ipc_c->ism == NULL) { 281 + IPC_ERROR(ipc_c, "Failed to mmap shm!"); 282 + return XRT_ERROR_IPC_FAILURE; 283 + } 284 + 285 + return XRT_SUCCESS; 286 + } 287 + 288 + static xrt_result_t 289 + ipc_client_check_git_tag(struct ipc_connection *ipc_c) 290 + { 291 + // Does the git tags match? 292 + if (strncmp(u_git_tag, ipc_c->ism->u_git_tag, IPC_VERSION_NAME_LEN) == 0) { 293 + return XRT_SUCCESS; 294 + } 295 + 296 + IPC_ERROR(ipc_c, "Monado client library version %s does not match service version %s", u_git_tag, 297 + ipc_c->ism->u_git_tag); 298 + 299 + if (!debug_get_bool_option_ipc_ignore_version()) { 300 + IPC_ERROR(ipc_c, "Set IPC_IGNORE_VERSION=1 to ignore this version conflict"); 301 + return XRT_ERROR_IPC_FAILURE; 302 + } 303 + 304 + // Error is ignored. 305 + return XRT_SUCCESS; 306 + } 307 + 308 + static xrt_result_t 309 + ipc_client_describe_client(struct ipc_connection *ipc_c, const struct xrt_instance_info *i_info) 310 + { 311 + #ifdef XRT_OS_WINDOWS 312 + DWORD pid = GetCurrentProcessId(); 313 + #else 314 + pid_t pid = getpid(); 315 + #endif 316 + 317 + struct ipc_client_description desc = {0}; 318 + desc.info = *i_info; 319 + desc.pid = pid; // Extra info. 320 + 321 + xrt_result_t xret = ipc_call_instance_describe_client(ipc_c, &desc); 322 + if (xret != XRT_SUCCESS) { 323 + IPC_ERROR(ipc_c, "Failed to set instance description!"); 324 + return xret; 325 + } 326 + 327 + return XRT_SUCCESS; 328 + } 329 + 330 + 331 + /* 332 + * 333 + * 'Exported' functions. 334 + * 335 + */ 336 + 254 337 xrt_result_t 255 338 ipc_client_connection_init(struct ipc_connection *ipc_c, 256 339 enum u_logging_level log_level, 257 340 const struct xrt_instance_info *i_info) 258 341 { 342 + xrt_result_t xret; 343 + 259 344 U_ZERO(ipc_c); 260 345 ipc_c->imc.ipc_handle = XRT_IPC_HANDLE_INVALID; 261 346 ipc_c->ism_handle = XRT_SHMEM_HANDLE_INVALID; 262 - 263 - os_mutex_init(&ipc_c->mutex); 264 - 265 347 ipc_c->log_level = log_level; 266 348 349 + // Must be done first. 350 + int ret = os_mutex_init(&ipc_c->mutex); 351 + if (ret != 0) { 352 + U_LOG_E("Failed to init mutex!"); 353 + return XRT_ERROR_IPC_FAILURE; 354 + } 355 + 356 + // Connect the service. 267 357 if (!ipc_client_socket_connect(ipc_c)) { 268 358 IPC_ERROR(ipc_c, 269 359 "Failed to connect to monado service process\n\n" ··· 280 370 return XRT_ERROR_IPC_FAILURE; 281 371 } 282 372 283 - // get our xdev shm from the server and mmap it 284 - xrt_result_t xret = ipc_call_instance_get_shm_fd(ipc_c, &ipc_c->ism_handle, 1); 373 + // Do this first so we can use it to check git tags. 374 + xret = ipc_client_setup_shm(ipc_c); 285 375 if (xret != XRT_SUCCESS) { 286 - IPC_ERROR(ipc_c, "Failed to retrieve shm fd!"); 287 - ipc_client_connection_fini(ipc_c); 288 - 289 - return xret; 376 + goto err_fini; // Already logged. 290 377 } 291 378 292 - struct ipc_client_description desc = {0}; 293 - desc.info = *i_info; 294 - desc.pid = getpid(); // Extra info. 295 - 296 - xret = ipc_call_instance_describe_client(ipc_c, &desc); 379 + // Requires shm. 380 + xret = ipc_client_check_git_tag(ipc_c); 297 381 if (xret != XRT_SUCCESS) { 298 - IPC_ERROR(ipc_c, "Failed to set instance description!"); 299 - ipc_client_connection_fini(ipc_c); 300 - 301 - 302 - return xret; 382 + goto err_fini; // Already logged. 303 383 } 304 384 305 - const size_t size = sizeof(struct ipc_shared_memory); 306 - 307 - #ifdef XRT_OS_WINDOWS 308 - ipc_c->ism = MapViewOfFile(ipc_c->ism_handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, size); 309 - #else 310 - const int flags = MAP_SHARED; 311 - const int access = PROT_READ | PROT_WRITE; 312 - 313 - ipc_c->ism = mmap(NULL, size, access, flags, ipc_c->ism_handle, 0); 314 - #endif 315 - if (ipc_c->ism == NULL) { 316 - IPC_ERROR(ipc_c, "Failed to mmap shm!"); 317 - ipc_client_connection_fini(ipc_c); 318 - return XRT_ERROR_IPC_FAILURE; 385 + // Do this last. 386 + xret = ipc_client_describe_client(ipc_c, i_info); 387 + if (xret != XRT_SUCCESS) { 388 + goto err_fini; // Already logged. 319 389 } 320 390 321 - if (strncmp(u_git_tag, ipc_c->ism->u_git_tag, IPC_VERSION_NAME_LEN) != 0) { 322 - IPC_ERROR(ipc_c, "Monado client library version %s does not match service version %s", u_git_tag, 323 - ipc_c->ism->u_git_tag); 324 - if (!debug_get_bool_option_ipc_ignore_version()) { 325 - IPC_ERROR(ipc_c, "Set IPC_IGNORE_VERSION=1 to ignore this version conflict"); 326 - 327 - return XRT_ERROR_IPC_FAILURE; 328 - } 329 - } 330 391 return XRT_SUCCESS; 392 + 393 + err_fini: 394 + ipc_client_connection_fini(ipc_c); 395 + return xret; 331 396 } 332 397 333 398 void