| 386 | } |
| 387 | |
| 388 | Ipc::Ipc(string_view hostname) : mSocketFd{INVALID_SOCKET} { |
| 389 | if (hostname.empty()) { |
| 390 | if (!flatpakInfo() || flatpakInfo()->hasNetworkAccess()) { |
| 391 | hostname = "127.0.0.1:14158"; |
| 392 | } else { |
| 393 | hostname = "unix"; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | try { |
| 398 | fs::create_directories(runtimeDirectory()); |
| 399 | } catch (const fs::filesystem_error& e) { |
| 400 | tlog::warning("Runtime directory {} does not exist and could not be created: {}", runtimeDirectory(), e.what()); |
| 401 | } |
| 402 | |
| 403 | mLockName = fmt::format(".tev.{}.lock", hostname); |
| 404 | |
| 405 | const auto parts = split(hostname, ":"); |
| 406 | if (parts.size() == 1 || (parts.size() == 2 && parts.back() == "unix")) { |
| 407 | mHostInfo = UnixHost{.socketPath = runtimeDirectory() / fmt::format(".tev.{}.sock", parts.front())}; |
| 408 | tlog::debug("Initializing IPC on unix socket {}", this->hostname()); |
| 409 | } else if (parts.size() == 2) { |
| 410 | mHostInfo = IpHost{.ip = string{parts.front()}, .port = string{parts.back()}}; |
| 411 | tlog::debug("Initializing IPC on IP host {}", this->hostname()); |
| 412 | } else { |
| 413 | throw runtime_error{fmt::format("IPC hostname must not include more than one ':' symbol but is {}.", hostname)}; |
| 414 | } |
| 415 | |
| 416 | try { |
| 417 | // Networking |
| 418 | #ifdef _WIN32 |
| 419 | // FIXME: only do this once if multiple Ipc objects are created. |
| 420 | WSADATA wsaData; |
| 421 | int wsaStartupResult = WSAStartup(MAKEWORD(2, 2), &wsaData); |
| 422 | if (wsaStartupResult != NO_ERROR) { |
| 423 | throw runtime_error{fmt::format("Could not initialize WSA: {}", errorString(wsaStartupResult))}; |
| 424 | } |
| 425 | #else |
| 426 | // We don't care about getting a SIGPIPE if the display server goes away... |
| 427 | signal(SIGPIPE, SIG_IGN); |
| 428 | #endif |
| 429 | |
| 430 | if (attemptToBecomePrimaryInstance()) { |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | // If we're not the primary instance, try to connect to it as a client |
| 435 | |
| 436 | struct addrinfo addrinfo = {}, *heapaddrinfo = nullptr; |
| 437 | struct sockaddr_un unixAddr = {}; |
| 438 | |
| 439 | visit( |
| 440 | visitor{ |
| 441 | [&](const IpHost& ipHostInfo) { |
| 442 | addrinfo.ai_family = PF_UNSPEC; |
| 443 | addrinfo.ai_socktype = SOCK_STREAM; |
| 444 | |
| 445 | const int err = getaddrinfo(ipHostInfo.ip.c_str(), ipHostInfo.port.c_str(), &addrinfo, &heapaddrinfo); |
nothing calls this directly
no test coverage detected