| 231 | } |
| 232 | |
| 233 | std::filesystem::path getLockDir() |
| 234 | { |
| 235 | auto logOnce = [](const std::filesystem::path &dir) |
| 236 | { |
| 237 | static bool logged = false; |
| 238 | if (!logged) |
| 239 | { |
| 240 | util::Log() << "Using shared memory lock directory: " << dir; |
| 241 | logged = true; |
| 242 | } |
| 243 | }; |
| 244 | |
| 245 | // 1. Explicit override via environment variable |
| 246 | if (const char *env = std::getenv("SHM_LOCK_DIR")) |
| 247 | { |
| 248 | std::filesystem::path dir(env); |
| 249 | if (!tryLockDir(dir)) |
| 250 | { |
| 251 | throw util::exception("SHM_LOCK_DIR directory is not usable: " + dir.string() + |
| 252 | SOURCE_REF); |
| 253 | } |
| 254 | logOnce(dir); |
| 255 | return dir; |
| 256 | } |
| 257 | |
| 258 | // 2. Platform-specific candidates |
| 259 | #ifdef __linux__ |
| 260 | // /dev/shm is a RAM-backed tmpfs that is always local and never cleaned by |
| 261 | // systemd-tmpfiles-clean. It is only cleared on reboot, which matches the |
| 262 | // lifetime of System V shared memory segments. |
| 263 | { |
| 264 | auto dir = std::filesystem::path("/dev/shm") / ("osrm-" + std::to_string(::getuid())); |
| 265 | if (tryLockDir(dir)) |
| 266 | { |
| 267 | logOnce(dir); |
| 268 | return dir; |
| 269 | } |
| 270 | } |
| 271 | #endif |
| 272 | |
| 273 | // 3. User home directory |
| 274 | { |
| 275 | const char *home = std::getenv("HOME"); |
| 276 | #ifdef _WIN32 |
| 277 | if (!home) |
| 278 | home = std::getenv("USERPROFILE"); |
| 279 | #endif |
| 280 | if (home) |
| 281 | { |
| 282 | auto dir = std::filesystem::path(home) / ".osrm"; |
| 283 | if (tryLockDir(dir)) |
| 284 | { |
| 285 | logOnce(dir); |
| 286 | return dir; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 |
no test coverage detected