| 1 |
/* |
| 2 |
* Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2026 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 17 мар. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/common/alloc.h> |
| 23 |
#include <lsp-plug.in/common/debug.h> |
| 24 |
#include <lsp-plug.in/io/Dir.h> |
| 25 |
#include <lsp-plug.in/io/File.h> |
| 26 |
#include <lsp-plug.in/ipc/Process.h> |
| 27 |
#include <lsp-plug.in/lltl/darray.h> |
| 28 |
#include <lsp-plug.in/lltl/parray.h> |
| 29 |
#include <lsp-plug.in/runtime/system.h> |
| 30 |
#include <lsp-plug.in/stdlib/stdlib.h> |
| 31 |
|
| 32 |
#ifdef PLATFORM_WINDOWS |
| 33 |
#include <windows.h> |
| 34 |
#include <shellapi.h> |
| 35 |
#include <shlwapi.h> |
| 36 |
#include <synchapi.h> |
| 37 |
#include <sysinfoapi.h> |
| 38 |
#include <winnetwk.h> |
| 39 |
#include <processthreadsapi.h> |
| 40 |
#endif /* PLATFORM_WINDOWS */ |
| 41 |
|
| 42 |
#ifdef PLATFORM_POSIX |
| 43 |
#include <errno.h> |
| 44 |
#include <pwd.h> |
| 45 |
#include <spawn.h> |
| 46 |
#include <sys/stat.h> |
| 47 |
#include <sys/time.h> |
| 48 |
#include <sys/types.h> |
| 49 |
#include <sys/wait.h> |
| 50 |
#include <time.h> |
| 51 |
#include <unistd.h> |
| 52 |
#endif /* PLATFORM_POSIX */ |
| 53 |
|
| 54 |
#ifdef PLATFORM_LINUX |
| 55 |
#include <mntent.h> |
| 56 |
#endif /* PLATFORM_LINUX */ |
| 57 |
|
| 58 |
#if defined(PLATFORM_BSD) || defined (PLATFORM_MACOSX) |
| 59 |
#include <sys/mount.h> |
| 60 |
#include <sys/param.h> |
| 61 |
#include <sys/ucred.h> |
| 62 |
#endif /* PLATFORM_BSD || PLATFORM_MACOSX */ |
| 63 |
|
| 64 |
#ifdef PLATFORM_HAIKU |
| 65 |
#include <posix/stdlib.h> |
| 66 |
#include <kernel/fs_info.h> |
| 67 |
#include <storage/Directory.h> |
| 68 |
#include <storage/Path.h> |
| 69 |
#include <storage/Volume.h> |
| 70 |
#include <storage/VolumeRoster.h> |
| 71 |
#endif /* PLATFORM_HAIKU */ |
| 72 |
|
| 73 |
#ifdef PLATFORM_POSIX |
| 74 |
#ifndef _GNU_SOURCE |
| 75 |
extern char **environ; // Define environment variables |
| 76 |
#endif /* _GNU_SOURCE */ |
| 77 |
#endif /* PLATFORM_POSIX */ |
| 78 |
|
| 79 |
namespace lsp |
| 80 |
{ |
| 81 |
namespace system |
| 82 |
{ |
| 83 |
status_t get_env_var(const LSPString *name, LSPString *dst) |
| 84 |
{ |
| 85 |
if (name == NULL) |
| 86 |
return STATUS_BAD_ARGUMENTS; |
| 87 |
|
| 88 |
#ifdef PLATFORM_WINDOWS |
| 89 |
const lsp_utf16_t *nname = name->get_utf16(); |
| 90 |
if (nname == NULL) |
| 91 |
return STATUS_NO_MEM; |
| 92 |
|
| 93 |
DWORD bufsize = ::GetEnvironmentVariableW(nname, NULL, 0); |
| 94 |
if (bufsize == 0) |
| 95 |
{ |
| 96 |
if (::GetLastError() == ERROR_ENVVAR_NOT_FOUND) |
| 97 |
return STATUS_NOT_FOUND; |
| 98 |
return STATUS_UNKNOWN_ERR; |
| 99 |
} |
| 100 |
else if (dst == NULL) |
| 101 |
return STATUS_OK; |
| 102 |
|
| 103 |
lsp_utf16_t *buf = reinterpret_cast<lsp_utf16_t *>(::malloc(bufsize * sizeof(lsp_utf16_t))); |
| 104 |
if (buf == NULL) |
| 105 |
return STATUS_NO_MEM; |
| 106 |
bufsize = ::GetEnvironmentVariableW(nname, buf, bufsize); |
| 107 |
if (bufsize == 0) |
| 108 |
{ |
| 109 |
::free(buf); |
| 110 |
return STATUS_UNKNOWN_ERR; |
| 111 |
} |
| 112 |
|
| 113 |
bool res = dst->set_utf16(buf, bufsize); |
| 114 |
::free(buf); |
| 115 |
return (res) ? STATUS_OK : STATUS_NO_MEM; |
| 116 |
#else |
| 117 |
const char *nname = name->get_native(); |
| 118 |
if (nname == NULL) |
| 119 |
return STATUS_NO_MEM; |
| 120 |
|
| 121 |
#if defined(_GNU_SOURCE) && !defined(PLATFORM_HAIKU) |
| 122 |
char *var = secure_getenv(nname); |
| 123 |
#else |
| 124 |
char *var = getenv(nname); |
| 125 |
#endif |
| 126 |
if (var == NULL) |
| 127 |
return STATUS_NOT_FOUND; |
| 128 |
if (dst != NULL) |
| 129 |
{ |
| 130 |
if (!dst->set_native(var)) |
| 131 |
return STATUS_NO_MEM; |
| 132 |
} |
| 133 |
return STATUS_OK; |
| 134 |
#endif /* PLATFORM_WINDOWS */ |
| 135 |
} |
| 136 |
|
| 137 |
status_t get_env_var(const char *name, LSPString *dst) |
| 138 |
{ |
| 139 |
if (name == NULL) |
| 140 |
return STATUS_BAD_ARGUMENTS; |
| 141 |
LSPString sname; |
| 142 |
if (!sname.set_utf8(name)) |
| 143 |
return STATUS_NO_MEM; |
| 144 |
return get_env_var(&sname, dst); |
| 145 |
} |
| 146 |
|
| 147 |
status_t set_env_var(const LSPString *name, const LSPString *value) |
| 148 |
{ |
| 149 |
#ifdef PLATFORM_WINDOWS |
| 150 |
const lsp_utf16_t *nname = name->get_utf16(); |
| 151 |
if (nname == NULL) |
| 152 |
return STATUS_NO_MEM; |
| 153 |
|
| 154 |
if (value != NULL) |
| 155 |
{ |
| 156 |
const lsp_utf16_t *nvalue = value->get_utf16(); |
| 157 |
if (nvalue == NULL) |
| 158 |
return STATUS_NO_MEM; |
| 159 |
if (::SetEnvironmentVariableW(nname, nvalue)) |
| 160 |
return STATUS_OK; |
| 161 |
} |
| 162 |
else |
| 163 |
{ |
| 164 |
if (::SetEnvironmentVariableW(nname, NULL)) |
| 165 |
return STATUS_OK; |
| 166 |
} |
| 167 |
return STATUS_UNKNOWN_ERR; |
| 168 |
#else |
| 169 |
const char *nname = name->get_native(); |
| 170 |
if (nname == NULL) |
| 171 |
return STATUS_NO_MEM; |
| 172 |
|
| 173 |
int res; |
| 174 |
if (value != NULL) |
| 175 |
{ |
| 176 |
const char *nvalue = value->get_native(); |
| 177 |
if (nvalue == NULL) |
| 178 |
return STATUS_NO_MEM; |
| 179 |
res = ::setenv(nname, nvalue, 1); |
| 180 |
} |
| 181 |
else |
| 182 |
res = ::unsetenv(nname); |
| 183 |
|
| 184 |
if (res == 0) |
| 185 |
return STATUS_OK; |
| 186 |
switch (res) |
| 187 |
{ |
| 188 |
case EINVAL: return STATUS_INVALID_VALUE; |
| 189 |
case ENOMEM: return STATUS_NO_MEM; |
| 190 |
default: break; |
| 191 |
} |
| 192 |
return STATUS_UNKNOWN_ERR; |
| 193 |
#endif /* PLATFORM_WINDOWS */ |
| 194 |
} |
| 195 |
|
| 196 |
status_t set_env_var(const char *name, const char *value) |
| 197 |
{ |
| 198 |
if (name == NULL) |
| 199 |
return STATUS_BAD_ARGUMENTS; |
| 200 |
LSPString sname; |
| 201 |
if (!sname.set_utf8(name)) |
| 202 |
return STATUS_NO_MEM; |
| 203 |
if (value == NULL) |
| 204 |
return set_env_var(&sname, NULL); |
| 205 |
|
| 206 |
LSPString svalue; |
| 207 |
if (!svalue.set_utf8(value)) |
| 208 |
return STATUS_NO_MEM; |
| 209 |
return set_env_var(&sname, &svalue); |
| 210 |
} |
| 211 |
|
| 212 |
status_t set_env_var(const char *name, const LSPString *value) |
| 213 |
{ |
| 214 |
if (name == NULL) |
| 215 |
return STATUS_BAD_ARGUMENTS; |
| 216 |
LSPString sname; |
| 217 |
if (!sname.set_utf8(name)) |
| 218 |
return STATUS_NO_MEM; |
| 219 |
return set_env_var(&sname, value); |
| 220 |
} |
| 221 |
|
| 222 |
status_t remove_env_var(const LSPString *name) |
| 223 |
{ |
| 224 |
const char *nname = name->get_native(); |
| 225 |
if (nname == NULL) |
| 226 |
return STATUS_NO_MEM; |
| 227 |
|
| 228 |
#ifdef PLATFORM_WINDOWS |
| 229 |
if (SetEnvironmentVariable(nname, NULL)) |
| 230 |
return STATUS_OK; |
| 231 |
return STATUS_UNKNOWN_ERR; |
| 232 |
#else |
| 233 |
int res = ::unsetenv(nname); |
| 234 |
if (res == 0) |
| 235 |
return STATUS_OK; |
| 236 |
switch (res) |
| 237 |
{ |
| 238 |
case EINVAL: return STATUS_INVALID_VALUE; |
| 239 |
case ENOMEM: return STATUS_NO_MEM; |
| 240 |
default: break; |
| 241 |
} |
| 242 |
return STATUS_UNKNOWN_ERR; |
| 243 |
#endif /* PLATFORM_WINDOWS */ |
| 244 |
} |
| 245 |
|
| 246 |
status_t remove_env_var(const char *name) |
| 247 |
{ |
| 248 |
if (name == NULL) |
| 249 |
return STATUS_BAD_ARGUMENTS; |
| 250 |
LSPString sname; |
| 251 |
if (!sname.set_utf8(name)) |
| 252 |
return STATUS_NO_MEM; |
| 253 |
return remove_env_var(&sname); |
| 254 |
} |
| 255 |
|
| 256 |
status_t get_home_directory(LSPString *homedir) |
| 257 |
{ |
| 258 |
if (homedir == NULL) |
| 259 |
return STATUS_BAD_ARGUMENTS; |
| 260 |
#ifdef PLATFORM_WINDOWS |
| 261 |
LSPString drv, path; |
| 262 |
status_t res = get_env_var("HOMEDRIVE", &drv); |
| 263 |
if (res != STATUS_OK) |
| 264 |
return res; |
| 265 |
res = get_env_var("HOMEPATH", &path); |
| 266 |
if (res != STATUS_OK) |
| 267 |
return res; |
| 268 |
if (!drv.append(&path)) |
| 269 |
return STATUS_NO_MEM; |
| 270 |
|
| 271 |
homedir->take(&drv); |
| 272 |
return STATUS_OK; |
| 273 |
#else |
| 274 |
return get_env_var("HOME", homedir); |
| 275 |
#endif /* PLATFORM_WINDOWS */ |
| 276 |
} |
| 277 |
|
| 278 |
status_t get_home_directory(io::Path *homedir) |
| 279 |
{ |
| 280 |
if (homedir == NULL) |
| 281 |
return STATUS_BAD_ARGUMENTS; |
| 282 |
LSPString path; |
| 283 |
status_t res = get_home_directory(&path); |
| 284 |
if (res != STATUS_OK) |
| 285 |
return res; |
| 286 |
return homedir->set(&path); |
| 287 |
} |
| 288 |
|
| 289 |
status_t get_user_config_path(LSPString *path) |
| 290 |
{ |
| 291 |
if (path == NULL) |
| 292 |
return STATUS_BAD_ARGUMENTS; |
| 293 |
LSPString upath; |
| 294 |
|
| 295 |
#ifdef PLATFORM_WINDOWS |
| 296 |
status_t res = get_env_var("LOCALAPPDATA", &upath); |
| 297 |
if (res != STATUS_OK) |
| 298 |
{ |
| 299 |
res = get_env_var("APPDATA", &upath); |
| 300 |
if (res != STATUS_OK) |
| 301 |
{ |
| 302 |
res = get_home_directory(&upath); |
| 303 |
if (res != STATUS_OK) |
| 304 |
return res; |
| 305 |
if (!upath.append_ascii(FILE_SEPARATOR_S ".config")) |
| 306 |
return STATUS_NO_MEM; |
| 307 |
} |
| 308 |
} |
| 309 |
#elif defined(PLATFORM_HAIKU) |
| 310 |
status_t res = get_env_var("HOME", &upath); |
| 311 |
if (res != STATUS_OK) |
| 312 |
return res; |
| 313 |
if (!upath.append_ascii( |
| 314 |
FILE_SEPARATOR_S "config" |
| 315 |
FILE_SEPARATOR_S "settings")) |
| 316 |
return STATUS_NO_MEM; |
| 317 |
#else |
| 318 |
status_t res = get_env_var("XDG_CONFIG_HOME", &upath); |
| 319 |
if (res != STATUS_OK) |
| 320 |
{ |
| 321 |
res = get_env_var("HOME", &upath); |
| 322 |
if (res != STATUS_OK) |
| 323 |
return res; |
| 324 |
if (!upath.append_ascii(FILE_SEPARATOR_S ".config")) |
| 325 |
return STATUS_NO_MEM; |
| 326 |
} |
| 327 |
#endif /* PLATFORM_WINDOWS */ |
| 328 |
|
| 329 |
path->swap(&upath); |
| 330 |
return STATUS_OK; |
| 331 |
} |
| 332 |
|
| 333 |
status_t get_user_config_path(io::Path *path) |
| 334 |
{ |
| 335 |
if (path == NULL) |
| 336 |
return STATUS_BAD_ARGUMENTS; |
| 337 |
LSPString upath; |
| 338 |
status_t res = get_user_config_path(&upath); |
| 339 |
if (res != STATUS_OK) |
| 340 |
return res; |
| 341 |
return path->set(&upath); |
| 342 |
} |
| 343 |
|
| 344 |
#ifdef PLATFORM_WINDOWS |
| 345 |
void get_time(time_t *time) |
| 346 |
{ |
| 347 |
FILETIME t; |
| 348 |
::GetSystemTimeAsFileTime(&t); |
| 349 |
uint64_t itime = (uint64_t(t.dwHighDateTime) << 32) | t.dwLowDateTime; |
| 350 |
time->seconds = itime / 10000000; |
| 351 |
time->nanos = (itime % 10000000) * 100; |
| 352 |
} |
| 353 |
|
| 354 |
time_millis_t get_time_millis() |
| 355 |
{ |
| 356 |
FILETIME t; |
| 357 |
::GetSystemTimeAsFileTime(&t); |
| 358 |
uint64_t itime = (uint64_t(t.dwHighDateTime) << 32) | t.dwLowDateTime; |
| 359 |
return itime / 10000; |
| 360 |
} |
| 361 |
|
| 362 |
void get_localtime(localtime_t *local, const time_t *time) |
| 363 |
{ |
| 364 |
SYSTEMTIME t; |
| 365 |
|
| 366 |
// Get system file time into tmp |
| 367 |
if (time != NULL) |
| 368 |
{ |
| 369 |
FILETIME tmp, stime; |
| 370 |
uint64_t itime = (time->seconds * 10000000) | (time->nanos / 100); |
| 371 |
tmp.dwHighDateTime = DWORD(itime >> 32); |
| 372 |
tmp.dwLowDateTime = DWORD(itime); |
| 373 |
|
| 374 |
::FileTimeToLocalFileTime(&tmp, &stime); |
| 375 |
::FileTimeToSystemTime(&stime, &t); |
| 376 |
} |
| 377 |
else |
| 378 |
::GetLocalTime(&t); |
| 379 |
|
| 380 |
// Decode |
| 381 |
local->year = t.wYear; |
| 382 |
local->month = t.wMonth; |
| 383 |
local->mday = t.wDay; |
| 384 |
local->wday = t.wDayOfWeek; |
| 385 |
local->hour = t.wHour; |
| 386 |
local->min = t.wMinute; |
| 387 |
local->sec = t.wSecond; |
| 388 |
local->nanos = time->nanos; |
| 389 |
} |
| 390 |
|
| 391 |
status_t sleep_msec(size_t delay) |
| 392 |
{ |
| 393 |
HANDLE timer; |
| 394 |
LARGE_INTEGER li; |
| 395 |
|
| 396 |
timer = CreateWaitableTimerW(NULL, TRUE, NULL); |
| 397 |
if (timer == NULL) |
| 398 |
return STATUS_UNKNOWN_ERR; |
| 399 |
|
| 400 |
while (delay > 0) |
| 401 |
{ |
| 402 |
// Relative time, computed with precision of 100 ns |
| 403 |
size_t period = lsp_min(0x10000000U, delay); |
| 404 |
li.QuadPart = - LONGLONG(period) * 10000; |
| 405 |
|
| 406 |
if (!SetWaitableTimer(timer, &li, 0, NULL, NULL, FALSE)) |
| 407 |
{ |
| 408 |
CloseHandle(timer); |
| 409 |
return STATUS_UNKNOWN_ERR; |
| 410 |
} |
| 411 |
|
| 412 |
WaitForSingleObject(timer, INFINITE); |
| 413 |
delay -= period; |
| 414 |
} |
| 415 |
|
| 416 |
CloseHandle(timer); |
| 417 |
|
| 418 |
return STATUS_OK; |
| 419 |
} |
| 420 |
#else |
| 421 |
void get_time(time_t *time) |
| 422 |
{ |
| 423 |
struct timespec t; |
| 424 |
::clock_gettime(CLOCK_REALTIME, &t); |
| 425 |
|
| 426 |
time->seconds = t.tv_sec; |
| 427 |
time->nanos = uint32_t(t.tv_nsec); |
| 428 |
} |
| 429 |
|
| 430 |
time_millis_t get_time_millis() |
| 431 |
{ |
| 432 |
struct timespec t; |
| 433 |
::clock_gettime(CLOCK_REALTIME, &t); |
| 434 |
return time_millis_t(t.tv_sec) * 1000 + time_millis_t(t.tv_nsec) / 1000000; |
| 435 |
} |
| 436 |
|
| 437 |
void get_localtime(localtime_t *local, const time_t *time) |
| 438 |
{ |
| 439 |
// Store actual time to timespec struct |
| 440 |
struct timespec stime; |
| 441 |
|
| 442 |
if (time != NULL) |
| 443 |
{ |
| 444 |
stime.tv_sec = time->seconds; |
| 445 |
stime.tv_nsec = time->nanos; |
| 446 |
} |
| 447 |
else |
| 448 |
::clock_gettime(CLOCK_REALTIME, &stime); |
| 449 |
|
| 450 |
// Now convert the struct to local time |
| 451 |
struct tm *t; |
| 452 |
#if defined(_POSIX_C_SOURCE) || defined(_BSD_SOURCE) || defined(_SVID_SOURCE) |
| 453 |
struct tm ctime; |
| 454 |
t = localtime_r(&stime.tv_sec, &ctime); |
| 455 |
#else |
| 456 |
t = localtime(&stime.tv_sec); |
| 457 |
#endif |
| 458 |
|
| 459 |
local->year = t->tm_year + 1900; |
| 460 |
local->month = t->tm_mon + 1; |
| 461 |
local->mday = t->tm_mday + 1; |
| 462 |
local->wday = t->tm_wday + 1; |
| 463 |
local->hour = t->tm_hour; |
| 464 |
local->min = t->tm_min; |
| 465 |
local->sec = t->tm_sec; |
| 466 |
local->nanos = uint32_t(stime.tv_nsec); |
| 467 |
} |
| 468 |
|
| 469 |
status_t sleep_msec(size_t delay) |
| 470 |
{ |
| 471 |
if (delay <= 0) |
| 472 |
return STATUS_OK; |
| 473 |
|
| 474 |
time_millis_t ctime = get_time_millis(); |
| 475 |
const time_millis_t dtime = ctime + delay; |
| 476 |
|
| 477 |
while (ctime < dtime) |
| 478 |
{ |
| 479 |
struct timespec req, rem; |
| 480 |
size_t delta = dtime - ctime; |
| 481 |
|
| 482 |
req.tv_nsec = (delta % 1000) * 1000000; |
| 483 |
req.tv_sec = delta / 1000; |
| 484 |
rem.tv_nsec = 0; |
| 485 |
rem.tv_sec = 0; |
| 486 |
|
| 487 |
// Perform nanosleep for the specific period of time. |
| 488 |
// If function succeeded and waited the whole desired period |
| 489 |
// of time, it should return 0. |
| 490 |
if (::nanosleep(&req, &rem) == 0) |
| 491 |
break; |
| 492 |
|
| 493 |
// If the sleep was interrupted, we need to update |
| 494 |
// the remained number of time to sleep. |
| 495 |
switch (errno) |
| 496 |
{ |
| 497 |
case EINTR: |
| 498 |
req = rem; |
| 499 |
break; |
| 500 |
default: |
| 501 |
return STATUS_UNKNOWN_ERR; |
| 502 |
} |
| 503 |
|
| 504 |
// Update current time |
| 505 |
ctime = get_time_millis(); |
| 506 |
} |
| 507 |
|
| 508 |
return STATUS_OK; |
| 509 |
} |
| 510 |
#endif /* PLATFORM_WINDOWS */ |
| 511 |
|
| 512 |
#ifdef PLATFORM_WINDOWS |
| 513 |
status_t get_system_temporary_dir(LSPString *path) |
| 514 |
{ |
| 515 |
LSPString tmp; |
| 516 |
|
| 517 |
if (get_env_var("SYSTEMROOT", &tmp) != STATUS_OK) |
| 518 |
return STATUS_NOT_FOUND; |
| 519 |
|
| 520 |
if (!tmp.append(FILE_SEPARATOR_C)) |
| 521 |
return STATUS_NO_MEM; |
| 522 |
if (!tmp.append_ascii("Temp")) |
| 523 |
return STATUS_NO_MEM; |
| 524 |
|
| 525 |
tmp.swap(path); |
| 526 |
return STATUS_OK; |
| 527 |
} |
| 528 |
|
| 529 |
status_t get_system_temporary_dir(io::Path *path) |
| 530 |
{ |
| 531 |
LSPString tmp; |
| 532 |
status_t res = get_system_temporary_dir(&tmp); |
| 533 |
return (res == STATUS_OK) ? path->set(&tmp) : res; |
| 534 |
} |
| 535 |
|
| 536 |
status_t get_temporary_dir(LSPString *path) |
| 537 |
{ |
| 538 |
if (get_env_var("TEMP", path) == STATUS_OK) |
| 539 |
return STATUS_OK; |
| 540 |
|
| 541 |
if (get_env_var("TMP", path) == STATUS_OK) |
| 542 |
return STATUS_OK; |
| 543 |
|
| 544 |
return get_system_temporary_dir(path); |
| 545 |
} |
| 546 |
|
| 547 |
status_t get_temporary_dir(io::Path *path) |
| 548 |
{ |
| 549 |
LSPString tmp; |
| 550 |
status_t res = get_temporary_dir(&tmp); |
| 551 |
return (res == STATUS_OK) ? path->set(&tmp) : res; |
| 552 |
} |
| 553 |
|
| 554 |
status_t get_user_login(LSPString *user) |
| 555 |
{ |
| 556 |
size_t capacity = 0x40; |
| 557 |
WCHAR *buf = reinterpret_cast<WCHAR *>(malloc(capacity * sizeof(WCHAR))); |
| 558 |
if (buf == NULL) |
| 559 |
return STATUS_NO_MEM; |
| 560 |
lsp_finally { |
| 561 |
free(buf); |
| 562 |
}; |
| 563 |
|
| 564 |
while (true) |
| 565 |
{ |
| 566 |
DWORD buf_size = capacity; |
| 567 |
if (GetUserNameW(buf, &buf_size)) |
| 568 |
{ |
| 569 |
return (user->set_utf16(buf)) ? STATUS_OK : STATUS_NO_MEM; |
| 570 |
} |
| 571 |
|
| 572 |
DWORD error = GetLastError(); |
| 573 |
switch (error) |
| 574 |
{ |
| 575 |
case ERROR_INSUFFICIENT_BUFFER: |
| 576 |
break; |
| 577 |
case ERROR_NOT_ENOUGH_MEMORY: |
| 578 |
return STATUS_NO_MEM; |
| 579 |
default: |
| 580 |
return STATUS_UNKNOWN_ERR; |
| 581 |
} |
| 582 |
|
| 583 |
// Re-allocate data |
| 584 |
capacity = lsp_max(capacity << 1, buf_size + 1); |
| 585 |
WCHAR *new_buf = reinterpret_cast<WCHAR *>(realloc(buf, capacity * sizeof(WCHAR))); |
| 586 |
if (new_buf == NULL) |
| 587 |
return STATUS_NO_MEM; |
| 588 |
buf = new_buf; |
| 589 |
} |
| 590 |
} |
| 591 |
#else |
| 592 |
status_t get_user_login(LSPString *user) |
| 593 |
{ |
| 594 |
size_t capacity = 0x400; |
| 595 |
char *buf = reinterpret_cast<char *>(malloc(capacity)); |
| 596 |
if (buf == NULL) |
| 597 |
return STATUS_NO_MEM; |
| 598 |
lsp_finally { |
| 599 |
free(buf); |
| 600 |
}; |
| 601 |
|
| 602 |
int error; |
| 603 |
struct passwd pwd, *rpwd = NULL; |
| 604 |
|
| 605 |
while (true) |
| 606 |
{ |
| 607 |
error = getpwuid_r(geteuid(), &pwd, buf, capacity, &rpwd); |
| 608 |
if (rpwd != NULL) |
| 609 |
return (user->set_native(rpwd->pw_name)) ? STATUS_OK : STATUS_NO_MEM; |
| 610 |
|
| 611 |
switch (error) |
| 612 |
{ |
| 613 |
case 0: |
| 614 |
case ENOENT: |
| 615 |
case ESRCH: |
| 616 |
case EBADF: |
| 617 |
case EPERM: |
| 618 |
return STATUS_NOT_FOUND; |
| 619 |
|
| 620 |
case EINTR: |
| 621 |
case EIO: |
| 622 |
case EMFILE: |
| 623 |
case ENFILE: |
| 624 |
return STATUS_IO_ERROR; |
| 625 |
|
| 626 |
case ENOMEM: |
| 627 |
return STATUS_NO_MEM; |
| 628 |
|
| 629 |
case ERANGE: |
| 630 |
break; |
| 631 |
|
| 632 |
default: |
| 633 |
return STATUS_UNKNOWN_ERR; |
| 634 |
} |
| 635 |
|
| 636 |
// Re-allocate data |
| 637 |
capacity <<= 1; |
| 638 |
if (capacity > 0x10000) |
| 639 |
break; |
| 640 |
|
| 641 |
char *new_buf = reinterpret_cast<char *>(realloc(buf, capacity)); |
| 642 |
if (new_buf == NULL) |
| 643 |
return STATUS_NO_MEM; |
| 644 |
buf = new_buf; |
| 645 |
} |
| 646 |
|
| 647 |
while (true) |
| 648 |
{ |
| 649 |
error = getlogin_r(buf, capacity); |
| 650 |
switch (error) |
| 651 |
{ |
| 652 |
case 0: |
| 653 |
return (user->set_native(buf)) ? STATUS_OK : STATUS_NO_MEM; |
| 654 |
case ERANGE: |
| 655 |
break; |
| 656 |
case EMFILE: |
| 657 |
case ENFILE: |
| 658 |
case ENXIO: |
| 659 |
case ENOTTY: |
| 660 |
return STATUS_IO_ERROR; |
| 661 |
case ENOENT: |
| 662 |
return STATUS_NOT_FOUND; |
| 663 |
case ENOMEM: |
| 664 |
return STATUS_NO_MEM; |
| 665 |
default: |
| 666 |
return STATUS_UNKNOWN_ERR; |
| 667 |
} |
| 668 |
|
| 669 |
// Re-allocate data |
| 670 |
capacity <<= 1; |
| 671 |
char *new_buf = reinterpret_cast<char *>(realloc(buf, capacity)); |
| 672 |
if (new_buf == NULL) |
| 673 |
return STATUS_NO_MEM; |
| 674 |
buf = new_buf; |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
status_t get_system_temporary_dir(LSPString *path) |
| 679 |
{ |
| 680 |
if (path == NULL) |
| 681 |
return STATUS_BAD_ARGUMENTS; |
| 682 |
return (path->set_ascii("/tmp")) ? STATUS_OK : STATUS_NO_MEM; |
| 683 |
} |
| 684 |
|
| 685 |
status_t get_system_temporary_dir(io::Path *path) |
| 686 |
{ |
| 687 |
if (path == NULL) |
| 688 |
return STATUS_BAD_ARGUMENTS; |
| 689 |
return path->set("/tmp"); |
| 690 |
} |
| 691 |
|
| 692 |
status_t get_temporary_dir(LSPString *path) |
| 693 |
{ |
| 694 |
return get_system_temporary_dir(path); |
| 695 |
} |
| 696 |
|
| 697 |
status_t get_temporary_dir(io::Path *path) |
| 698 |
{ |
| 699 |
return get_system_temporary_dir(path); |
| 700 |
} |
| 701 |
#endif /* PLATFORM_WINDOWS */ |
| 702 |
|
| 703 |
status_t get_current_dir(LSPString *path) |
| 704 |
{ |
| 705 |
return io::Dir::get_current(path); |
| 706 |
} |
| 707 |
|
| 708 |
status_t get_current_dir(io::Path *path) |
| 709 |
{ |
| 710 |
return io::Dir::get_current(path); |
| 711 |
} |
| 712 |
|
| 713 |
status_t follow_url(const char *url) |
| 714 |
{ |
| 715 |
LSPString turl; |
| 716 |
if (!turl.set_utf8(url)) |
| 717 |
return STATUS_NO_MEM; |
| 718 |
return follow_url(&turl); |
| 719 |
} |
| 720 |
|
| 721 |
status_t follow_url(const LSPString *url) |
| 722 |
{ |
| 723 |
#if defined(PLATFORM_WINDOWS) |
| 724 |
const HINSTANCE hInst = ::ShellExecuteW( |
| 725 |
NULL, // Not associated with window |
| 726 |
L"open", // Open hyperlink |
| 727 |
url->get_utf16(), // The file to execute |
| 728 |
NULL, // Parameters |
| 729 |
NULL, // Directory |
| 730 |
SW_SHOWNORMAL // Show command |
| 731 |
); |
| 732 |
|
| 733 |
if ((INT_PTR)hInst <= 32) |
| 734 |
return STATUS_UNKNOWN_ERR; |
| 735 |
#elif defined(PLATFORM_HAIKU) |
| 736 |
status_t res; |
| 737 |
ipc::Process p; |
| 738 |
|
| 739 |
if ((res = p.set_command("open")) != STATUS_OK) |
| 740 |
return STATUS_OK; |
| 741 |
if ((res = p.add_arg(url)) != STATUS_OK) |
| 742 |
return STATUS_OK; |
| 743 |
if ((res = p.launch()) != STATUS_OK) |
| 744 |
return STATUS_OK; |
| 745 |
p.wait(); |
| 746 |
#else |
| 747 |
status_t res; |
| 748 |
ipc::Process p; |
| 749 |
|
| 750 |
if ((res = p.set_command("xdg-open")) != STATUS_OK) |
| 751 |
return STATUS_OK; |
| 752 |
if ((res = p.add_arg(url)) != STATUS_OK) |
| 753 |
return STATUS_OK; |
| 754 |
if ((res = p.launch()) != STATUS_OK) |
| 755 |
return STATUS_OK; |
| 756 |
p.wait(); |
| 757 |
#endif /* PLATFORM_WINDOWS */ |
| 758 |
|
| 759 |
return STATUS_OK; |
| 760 |
} |
| 761 |
|
| 762 |
static inline bool match_string(const LSPString *s, const char **list) |
| 763 |
{ |
| 764 |
for (; *list != NULL; ++list) |
| 765 |
if (s->equals_ascii(*list)) |
| 766 |
return true; |
| 767 |
return false; |
| 768 |
} |
| 769 |
|
| 770 |
static inline bool is_dummy_fs(const LSPString *fs_type, bool bind) |
| 771 |
{ |
| 772 |
static const char *dummy_fs_types[] = |
| 773 |
{ |
| 774 |
"autofs", |
| 775 |
"proc", |
| 776 |
"debugfs", |
| 777 |
"devpts", |
| 778 |
"fusectl", |
| 779 |
"fuse.portal", |
| 780 |
"mqueue", |
| 781 |
"rpc_pipefs", |
| 782 |
"sysfs", |
| 783 |
"devfs", |
| 784 |
"kernfs", |
| 785 |
"ignore", |
| 786 |
NULL |
| 787 |
}; |
| 788 |
|
| 789 |
if ((fs_type->equals_ascii("bind")) && (!bind)) |
| 790 |
return true; |
| 791 |
|
| 792 |
return match_string(fs_type, dummy_fs_types); |
| 793 |
} |
| 794 |
|
| 795 |
static inline bool is_remote_fs(const LSPString *fs_type, const LSPString *fs_name) |
| 796 |
{ |
| 797 |
#ifdef PLATFORM_WINDOWS |
| 798 |
// TODO |
| 799 |
#endif /* PLATFORM_WINDOWS */ |
| 800 |
|
| 801 |
static const char *network_fs_types[] = |
| 802 |
{ |
| 803 |
"smbfs", |
| 804 |
"smb3", |
| 805 |
"cifs", |
| 806 |
"nfs", |
| 807 |
NULL |
| 808 |
}; |
| 809 |
|
| 810 |
static const char *network_fs_names[] = |
| 811 |
{ |
| 812 |
"acfs", |
| 813 |
"afs", |
| 814 |
"coda", |
| 815 |
"auristorfs", |
| 816 |
"fhgfs", |
| 817 |
"gpfs", |
| 818 |
"ibrix", |
| 819 |
"ocfs2", |
| 820 |
"vxfs", |
| 821 |
"nfs", |
| 822 |
"-hosts", |
| 823 |
NULL |
| 824 |
}; |
| 825 |
|
| 826 |
if (fs_name->index_of(':') >= 0) |
| 827 |
return true; |
| 828 |
if (fs_name->starts_with_ascii("\\\\")) |
| 829 |
return match_string(fs_type, network_fs_types); |
| 830 |
|
| 831 |
return match_string(fs_name, network_fs_names); |
| 832 |
} |
| 833 |
|
| 834 |
#ifdef PLATFORM_POSIX |
| 835 |
bool is_posix_drive(const LSPString *path) |
| 836 |
{ |
| 837 |
if (!path->starts_with(FILE_SEPARATOR_C)) |
| 838 |
return false; |
| 839 |
|
| 840 |
struct stat st; |
| 841 |
if (::stat(path->get_native(), &st) != 0) |
| 842 |
return false; |
| 843 |
|
| 844 |
int mode = st.st_mode & S_IFMT; |
| 845 |
return (mode == S_IFBLK) || (mode == S_IFCHR); |
| 846 |
} |
| 847 |
#endif /* PLATFORM_POSIX */ |
| 848 |
|
| 849 |
#ifdef PLATFORM_LINUX |
| 850 |
static inline char *move_forward(char *s, char *end, size_t n) |
| 851 |
{ |
| 852 |
while (n--) |
| 853 |
{ |
| 854 |
s = static_cast<char *>(memchr(s, ' ', end - s)); |
| 855 |
if (s == NULL) |
| 856 |
break; |
| 857 |
++s; |
| 858 |
} |
| 859 |
return s; |
| 860 |
} |
| 861 |
|
| 862 |
static inline status_t read_field(LSPString *dst, char *s, char *end) |
| 863 |
{ |
| 864 |
char *e = static_cast<char *>(memchr(s, ' ', end - s)); |
| 865 |
if (e == NULL) |
| 866 |
return STATUS_BAD_FORMAT; |
| 867 |
|
| 868 |
// Process string in-place to remove some escapings |
| 869 |
size_t v = 0; |
| 870 |
size_t l = e - s; |
| 871 |
for (size_t i = 0; i < l; i++) |
| 872 |
{ |
| 873 |
if ((s[i] == '\\') && ((i + 4) < l) && |
| 874 |
(s[i + 1] >= '0') && (s[i + 1] <= '3') && |
| 875 |
(s[i + 2] >= '0') && (s[i + 2] <= '7') && |
| 876 |
(s[i + 3] >= '0') && (s[i + 3] <= '7')) |
| 877 |
{ |
| 878 |
s[v++] = (s[i + 1] - '0') * 64 + (s[i + 2] - '0') * 8 + (s[i + 3] - '0'); |
| 879 |
i += 3; |
| 880 |
} |
| 881 |
else |
| 882 |
s[v++] = s[i]; |
| 883 |
} |
| 884 |
|
| 885 |
return (dst->set_utf8(s, v)) ? STATUS_OK : STATUS_NO_MEM; |
| 886 |
} |
| 887 |
|
| 888 |
static status_t read_linux_mountinfo(lltl::parray<volume_info_t> *volumes) |
| 889 |
{ |
| 890 |
status_t res; |
| 891 |
lltl::parray<volume_info_t> list; |
| 892 |
lsp_finally { free_volume_info(&list); }; |
| 893 |
|
| 894 |
// Open the file |
| 895 |
FILE *fd = fopen("/proc/self/mountinfo", "r"); |
| 896 |
if (fd == NULL) |
| 897 |
return STATUS_NOT_SUPPORTED; |
| 898 |
lsp_finally { fclose(fd); }; |
| 899 |
|
| 900 |
// Line |
| 901 |
char *line = NULL; |
| 902 |
size_t line_cap = 0; |
| 903 |
lsp_finally { |
| 904 |
if (line != NULL) |
| 905 |
free(line); |
| 906 |
}; |
| 907 |
|
| 908 |
// Read the mountinfo file |
| 909 |
// 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue |
| 910 |
// (1)(2)(3) (4) (5) (6) (7) (8) (9) (10) (11) |
| 911 |
// |
| 912 |
// (1) mount ID: unique identifier of the mount (may be reused after umount) |
| 913 |
// (2) parent ID: ID of parent (or of self for the top of the mount tree) |
| 914 |
// (3) major:minor: value of st_dev for files on filesystem |
| 915 |
// (4) root: root of the mount within the filesystem |
| 916 |
// (5) mount point: mount point relative to the process's root |
| 917 |
// (6) mount options: per mount options |
| 918 |
// (7) optional fields: zero or more fields of the form "tag[:value]" |
| 919 |
// (8) separator: marks the end of the optional fields |
| 920 |
// (9) filesystem type: name of filesystem of the form "type[.subtype]" |
| 921 |
// (10) mount source: filesystem specific information or "none" |
| 922 |
// (11) super options: per super block options |
| 923 |
// |
| 924 |
// Parsers should ignore all unrecognised optional fields. Currently the |
| 925 |
// possible optional fields are: |
| 926 |
// |
| 927 |
// shared:X mount is shared in peer group X |
| 928 |
// master:X mount is slave to peer group X |
| 929 |
// propagate_from:X mount is slave and receives propagation from peer group X (*) |
| 930 |
// unbindable mount is unbindable |
| 931 |
ssize_t len; |
| 932 |
char *end, *s; |
| 933 |
|
| 934 |
while ((len = getline(&line, &line_cap, fd)) >= 0) |
| 935 |
{ |
| 936 |
// Create record |
| 937 |
volume_info_t *info = new volume_info_t(); |
| 938 |
if (info == NULL) |
| 939 |
return STATUS_NO_MEM; |
| 940 |
else if (!list.add(info)) |
| 941 |
{ |
| 942 |
delete info; |
| 943 |
return STATUS_NO_MEM; |
| 944 |
} |
| 945 |
|
| 946 |
// Parse the line |
| 947 |
end = &line[len]; |
| 948 |
// root |
| 949 |
if ((s = move_forward(line, end, 3)) == NULL) |
| 950 |
return STATUS_BAD_FORMAT; |
| 951 |
if ((res = read_field(&info->root, s, end)) != STATUS_OK) |
| 952 |
return res; |
| 953 |
// mount point |
| 954 |
if ((s = move_forward(s, end, 1)) == NULL) |
| 955 |
return STATUS_BAD_FORMAT; |
| 956 |
if ((res = read_field(&info->target, s, end)) != STATUS_OK) |
| 957 |
return res; |
| 958 |
// end of the optional fields |
| 959 |
if ((s = strstr(s, " - ")) == NULL) |
| 960 |
return STATUS_BAD_FORMAT; |
| 961 |
s += 3; |
| 962 |
// filesystem type |
| 963 |
if ((res = read_field(&info->name, s, end)) != STATUS_OK) |
| 964 |
return res; |
| 965 |
// mount source |
| 966 |
if ((s = move_forward(s, end, 1)) == NULL) |
| 967 |
return STATUS_BAD_FORMAT; |
| 968 |
if ((res = read_field(&info->device, s, end)) != STATUS_OK) |
| 969 |
return res; |
| 970 |
|
| 971 |
// Produce final record |
| 972 |
info->flags = 0; |
| 973 |
if (is_dummy_fs(&info->name, false)) |
| 974 |
info->flags |= VF_DUMMY; |
| 975 |
if (is_remote_fs(&info->device, &info->name)) |
| 976 |
info->flags |= VF_REMOTE; |
| 977 |
if (is_posix_drive(&info->device)) |
| 978 |
info->flags |= VF_DRIVE; |
| 979 |
} |
| 980 |
|
| 981 |
if (!feof(fd)) |
| 982 |
return STATUS_IO_ERROR; |
| 983 |
|
| 984 |
// Commit and return |
| 985 |
list.swap(volumes); |
| 986 |
return STATUS_OK; |
| 987 |
} |
| 988 |
|
| 989 |
static status_t read_linux_mntent(const char *path, lltl::parray<volume_info_t> *volumes) |
| 990 |
{ |
| 991 |
struct mntent *mnt; |
| 992 |
lltl::parray<volume_info_t> list; |
| 993 |
lsp_finally { free_volume_info(&list); }; |
| 994 |
|
| 995 |
// Open the file |
| 996 |
FILE *fd = setmntent(path, "r"); |
| 997 |
if (fd == NULL) |
| 998 |
return STATUS_NOT_SUPPORTED; |
| 999 |
lsp_finally { endmntent(fd); }; |
| 1000 |
|
| 1001 |
// Parse records |
| 1002 |
while ((mnt = getmntent(fd)) != NULL) |
| 1003 |
{ |
| 1004 |
// Create record |
| 1005 |
volume_info_t *info = new volume_info_t(); |
| 1006 |
if (info == NULL) |
| 1007 |
return STATUS_NO_MEM; |
| 1008 |
else if (!list.add(info)) |
| 1009 |
{ |
| 1010 |
delete info; |
| 1011 |
return STATUS_NO_MEM; |
| 1012 |
} |
| 1013 |
|
| 1014 |
// Fill fields |
| 1015 |
bool bind = hasmntopt (mnt, "bind"); |
| 1016 |
if (!info->device.set_utf8(mnt->mnt_fsname)) |
| 1017 |
return STATUS_NO_MEM; |
| 1018 |
if (!info->target.set_utf8(mnt->mnt_dir)) |
| 1019 |
return STATUS_NO_MEM; |
| 1020 |
if (!info->root.set_ascii(FILE_SEPARATOR_S)) |
| 1021 |
return STATUS_NO_MEM; |
| 1022 |
if (!info->name.set_utf8(mnt->mnt_type)) |
| 1023 |
return STATUS_NO_MEM; |
| 1024 |
|
| 1025 |
// Produce final record |
| 1026 |
info->flags = 0; |
| 1027 |
if (is_dummy_fs(&info->name, bind)) |
| 1028 |
info->flags |= VF_DUMMY; |
| 1029 |
if (is_remote_fs(&info->device, &info->name)) |
| 1030 |
info->flags |= VF_REMOTE; |
| 1031 |
if (is_posix_drive(&info->device)) |
| 1032 |
info->flags |= VF_DRIVE; |
| 1033 |
} |
| 1034 |
|
| 1035 |
// Commit and return |
| 1036 |
list.swap(volumes); |
| 1037 |
return STATUS_OK; |
| 1038 |
} |
| 1039 |
#endif /* PLATFORM_LINUX */ |
| 1040 |
|
| 1041 |
#if defined(PLATFORM_BSD) || defined(PLATFORM_MACOSX) |
| 1042 |
static status_t read_fsstat(lltl::darray<struct statfs> *list) |
| 1043 |
{ |
| 1044 |
if (!list->add_n(32)) |
| 1045 |
return STATUS_NO_MEM; |
| 1046 |
|
| 1047 |
while (true) |
| 1048 |
{ |
| 1049 |
int res = getfsstat(list->array(), int(list->size() * sizeof(struct statfs)), MNT_WAIT); |
| 1050 |
if (res < 0) |
| 1051 |
{ |
| 1052 |
int error = errno; |
| 1053 |
switch (error) |
| 1054 |
{ |
| 1055 |
case EFAULT: return STATUS_INVALID_VALUE; |
| 1056 |
case EIO: return STATUS_IO_ERROR; |
| 1057 |
default: return STATUS_UNKNOWN_ERR; |
| 1058 |
} |
| 1059 |
} |
| 1060 |
else if (size_t(res) < list->size()) |
| 1061 |
{ |
| 1062 |
list->pop_n(list->size() - size_t(res)); |
| 1063 |
return STATUS_OK; |
| 1064 |
} |
| 1065 |
|
| 1066 |
// Grow the size of the list |
| 1067 |
if (!list->add_n(list->size() >> 1)) |
| 1068 |
return STATUS_NO_MEM; |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
static status_t read_fsstat_mntinfo(lltl::parray<volume_info_t> *volumes) |
| 1073 |
{ |
| 1074 |
// Read fsstat data |
| 1075 |
lltl::darray<struct statfs> fspl; |
| 1076 |
status_t res = read_fsstat(&fspl); |
| 1077 |
if (res != STATUS_OK) |
| 1078 |
return res; |
| 1079 |
|
| 1080 |
// Parse fsstat data |
| 1081 |
lltl::parray<volume_info_t> list; |
| 1082 |
lsp_finally { free_volume_info(&list); }; |
| 1083 |
|
| 1084 |
for (size_t i=0, n=fspl.size(); i<n; ++i) |
| 1085 |
{ |
| 1086 |
// Process one single structure |
| 1087 |
const struct statfs *fsp = fspl.uget(i); |
| 1088 |
|
| 1089 |
// Create record |
| 1090 |
volume_info_t *info = new volume_info_t(); |
| 1091 |
if (info == NULL) |
| 1092 |
return STATUS_NO_MEM; |
| 1093 |
else if (!list.add(info)) |
| 1094 |
{ |
| 1095 |
delete info; |
| 1096 |
return STATUS_NO_MEM; |
| 1097 |
} |
| 1098 |
|
| 1099 |
// Fill fields |
| 1100 |
if (!info->device.set_utf8(fsp->f_mntfromname)) |
| 1101 |
return STATUS_NO_MEM; |
| 1102 |
if (!info->target.set_utf8(fsp->f_mntonname)) |
| 1103 |
return STATUS_NO_MEM; |
| 1104 |
if (!info->root.set_ascii(FILE_SEPARATOR_S)) |
| 1105 |
return STATUS_NO_MEM; |
| 1106 |
if (!info->name.set_utf8(fsp->f_fstypename)) |
| 1107 |
return STATUS_NO_MEM; |
| 1108 |
|
| 1109 |
// Produce final record |
| 1110 |
info->flags = 0; |
| 1111 |
if (is_dummy_fs(&info->name, false)) |
| 1112 |
info->flags |= VF_DUMMY; |
| 1113 |
if (is_remote_fs(&info->device, &info->name)) |
| 1114 |
info->flags |= VF_REMOTE; |
| 1115 |
if (is_posix_drive(&info->device)) |
| 1116 |
info->flags |= VF_DRIVE; |
| 1117 |
} |
| 1118 |
|
| 1119 |
// Commit and return |
| 1120 |
list.swap(volumes); |
| 1121 |
return STATUS_OK; |
| 1122 |
} |
| 1123 |
#endif /* PLATFORM_BSD || PLATFORM_MACOSX */ |
| 1124 |
|
| 1125 |
#ifdef PLATFORM_HAIKU |
| 1126 |
static status_t read_haiku_mntinfo(lltl::parray<volume_info_t> *volumes) |
| 1127 |
{ |
| 1128 |
lltl::parray<volume_info_t> list; |
| 1129 |
lsp_finally { free_volume_info(&list); }; |
| 1130 |
|
| 1131 |
BVolumeRoster roster; |
| 1132 |
BVolume bvol; |
| 1133 |
|
| 1134 |
while (roster.GetNextVolume(&bvol) == B_OK) |
| 1135 |
{ |
| 1136 |
fs_info info; |
| 1137 |
if (fs_stat_dev(bvol.Device(), &info) != B_OK) |
| 1138 |
continue; |
| 1139 |
|
| 1140 |
volume_info_t *vi = new volume_info_t(); |
| 1141 |
if (vi == NULL) |
| 1142 |
return STATUS_NO_MEM; |
| 1143 |
if (!list.add(vi)) |
| 1144 |
{ |
| 1145 |
delete vi; |
| 1146 |
return STATUS_NO_MEM; |
| 1147 |
} |
| 1148 |
|
| 1149 |
if (!vi->device.set_utf8(info.device_name)) |
| 1150 |
return STATUS_NO_MEM; |
| 1151 |
if (!vi->name.set_utf8(info.fsh_name)) |
| 1152 |
return STATUS_NO_MEM; |
| 1153 |
if (!vi->root.set_ascii(FILE_SEPARATOR_S)) |
| 1154 |
return STATUS_NO_MEM; |
| 1155 |
|
| 1156 |
BDirectory bdir; |
| 1157 |
if (bvol.GetRootDirectory(&bdir) == B_OK) |
| 1158 |
{ |
| 1159 |
BPath bpath(&bdir, NULL); |
| 1160 |
if (bpath.InitCheck() == B_OK && bpath.Path() != NULL) |
| 1161 |
if (!vi->target.set_utf8(bpath.Path())) |
| 1162 |
return STATUS_NO_MEM; |
| 1163 |
} |
| 1164 |
|
| 1165 |
vi->flags = 0; |
| 1166 |
|
| 1167 |
if ((info.flags & B_FS_IS_PERSISTENT) == 0) |
| 1168 |
vi->flags |= VF_DUMMY; |
| 1169 |
if ((info.flags & B_FS_IS_SHARED) || is_remote_fs(&vi->device, &vi->name)) |
| 1170 |
vi->flags |= VF_REMOTE; |
| 1171 |
if ((info.flags & B_FS_IS_REMOVABLE) || is_posix_drive(&vi->device)) |
| 1172 |
vi->flags |= VF_DRIVE; |
| 1173 |
} |
| 1174 |
|
| 1175 |
list.swap(volumes); |
| 1176 |
return STATUS_OK; |
| 1177 |
} |
| 1178 |
#endif /* PLATFORM_HAIKU */ |
| 1179 |
|
| 1180 |
#ifdef PLATFORM_WINDOWS |
| 1181 |
status_t read_pathnames(const WCHAR *volume, WCHAR **list, size_t *cap) |
| 1182 |
{ |
| 1183 |
DWORD cap_req = 0; |
| 1184 |
|
| 1185 |
// Obtain volume information |
| 1186 |
while (true) |
| 1187 |
{ |
| 1188 |
if (GetVolumePathNamesForVolumeNameW(volume, *list, *cap, &cap_req)) |
| 1189 |
return STATUS_OK; |
| 1190 |
if (GetLastError() != ERROR_MORE_DATA) |
| 1191 |
return STATUS_UNKNOWN_ERR; |
| 1192 |
|
| 1193 |
// Allocate the desired chunk of memory |
| 1194 |
cap_req = lsp::align_size(cap_req, 0x20); |
| 1195 |
WCHAR *buf = static_cast<WCHAR *>(realloc(*list, (cap_req + 1) * sizeof(WCHAR))); |
| 1196 |
if (buf == NULL) |
| 1197 |
return STATUS_NO_MEM; |
| 1198 |
*list = buf; |
| 1199 |
*cap = cap_req; |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
status_t read_windows_mntinfo(lltl::parray<volume_info_t> *volumes) |
| 1204 |
{ |
| 1205 |
status_t res; |
| 1206 |
lltl::parray<volume_info_t> list; |
| 1207 |
lsp_finally { free_volume_info(&list); }; |
| 1208 |
|
| 1209 |
// Read the value |
| 1210 |
WCHAR *vol_name = static_cast<WCHAR *>(malloc((MAX_PATH + 4) * 3 * sizeof(WCHAR))); |
| 1211 |
if (vol_name == NULL) |
| 1212 |
return STATUS_NO_MEM; |
| 1213 |
lsp_finally { free(vol_name); }; |
| 1214 |
WCHAR *dev_name = &vol_name[MAX_PATH + 4]; |
| 1215 |
WCHAR *fs_name = &dev_name[MAX_PATH + 4]; |
| 1216 |
HANDLE h = INVALID_HANDLE_VALUE; |
| 1217 |
|
| 1218 |
// Allocate place for the volume names |
| 1219 |
size_t pth_cap = 0; |
| 1220 |
WCHAR *pth_name = NULL; |
| 1221 |
lsp_finally { |
| 1222 |
if (pth_name != NULL) |
| 1223 |
free(pth_name); |
| 1224 |
}; |
| 1225 |
|
| 1226 |
// Start volume scanning |
| 1227 |
if ((h = FindFirstVolumeW(vol_name, MAX_PATH + 4)) == INVALID_HANDLE_VALUE) |
| 1228 |
return STATUS_NOT_SUPPORTED; |
| 1229 |
lsp_finally { FindVolumeClose(h); }; |
| 1230 |
|
| 1231 |
do |
| 1232 |
{ |
| 1233 |
// Skip the \\?\ prefix and remove the trailing backslash. |
| 1234 |
WCHAR idx = wcslen(vol_name) - 1; |
| 1235 |
|
| 1236 |
if ((vol_name[0] != L'\\') || |
| 1237 |
(vol_name[1] != L'\\') || |
| 1238 |
(vol_name[2] != L'?') || |
| 1239 |
(vol_name[3] != L'\\') || |
| 1240 |
(vol_name[idx] != L'\\')) |
| 1241 |
continue; |
| 1242 |
|
| 1243 |
// Obtain the device name |
| 1244 |
vol_name[idx] = '\0'; |
| 1245 |
DWORD chars = QueryDosDeviceW(&vol_name[4], dev_name, MAX_PATH); |
| 1246 |
vol_name[idx] = '\\'; |
| 1247 |
if (chars == 0) |
| 1248 |
continue; |
| 1249 |
|
| 1250 |
// Obtain the list of path names |
| 1251 |
if ((res = read_pathnames(vol_name, &pth_name, &pth_cap)) != STATUS_OK) |
| 1252 |
return res; |
| 1253 |
|
| 1254 |
// Do simple stuff if path names have been not obtained |
| 1255 |
if (*pth_name == '\0') |
| 1256 |
{ |
| 1257 |
// Create record |
| 1258 |
volume_info_t *info = new volume_info_t(); |
| 1259 |
if (info == NULL) |
| 1260 |
return STATUS_NO_MEM; |
| 1261 |
else if (!list.add(info)) |
| 1262 |
{ |
| 1263 |
delete info; |
| 1264 |
return STATUS_NO_MEM; |
| 1265 |
} |
| 1266 |
|
| 1267 |
// Fill volume name field |
| 1268 |
if (!info->device.set_utf16(vol_name)) |
| 1269 |
return STATUS_NO_MEM; |
| 1270 |
if (!info->root.set_utf16(dev_name)) |
| 1271 |
return STATUS_NO_MEM; |
| 1272 |
if (!info->target.set_ascii("")) |
| 1273 |
return STATUS_NO_MEM; |
| 1274 |
if (!info->name.set_ascii("")) |
| 1275 |
return STATUS_NO_MEM; |
| 1276 |
|
| 1277 |
// Obtain flags |
| 1278 |
UINT drv_type = GetDriveTypeW(dev_name); |
| 1279 |
switch (drv_type) |
| 1280 |
{ |
| 1281 |
case DRIVE_REMOVABLE: |
| 1282 |
case DRIVE_FIXED: |
| 1283 |
case DRIVE_CDROM: |
| 1284 |
case DRIVE_RAMDISK: |
| 1285 |
info->flags = VF_DRIVE; |
| 1286 |
break; |
| 1287 |
case DRIVE_REMOTE: |
| 1288 |
info->flags = VF_REMOTE; |
| 1289 |
break; |
| 1290 |
default: |
| 1291 |
info->flags = 0; |
| 1292 |
break; |
| 1293 |
} |
| 1294 |
continue; |
| 1295 |
} |
| 1296 |
|
| 1297 |
for (WCHAR *vol_drive = pth_name; *vol_drive != '\0';) |
| 1298 |
{ |
| 1299 |
size_t len = wcslen(vol_drive); |
| 1300 |
lsp_finally { vol_drive = &vol_drive[len + 1]; }; |
| 1301 |
|
| 1302 |
// Obtain the volume information |
| 1303 |
if (!GetVolumeInformationW(vol_drive, NULL, 0, NULL, NULL, NULL, fs_name, MAX_PATH + 4)) |
| 1304 |
fs_name[0] = '\0'; |
| 1305 |
|
| 1306 |
// Create record |
| 1307 |
volume_info_t *info = new volume_info_t(); |
| 1308 |
if (info == NULL) |
| 1309 |
return STATUS_NO_MEM; |
| 1310 |
else if (!list.add(info)) |
| 1311 |
{ |
| 1312 |
delete info; |
| 1313 |
return STATUS_NO_MEM; |
| 1314 |
} |
| 1315 |
|
| 1316 |
// Fill volume name field |
| 1317 |
if (!info->device.set_utf16(vol_name)) |
| 1318 |
return STATUS_NO_MEM; |
| 1319 |
if (!info->root.set_utf16(dev_name)) |
| 1320 |
return STATUS_NO_MEM; |
| 1321 |
if (!info->target.set_utf16(vol_drive)) |
| 1322 |
return STATUS_NO_MEM; |
| 1323 |
if (!info->name.set_utf16(fs_name)) |
| 1324 |
return STATUS_NO_MEM; |
| 1325 |
|
| 1326 |
// Obtain flags |
| 1327 |
UINT drv_type = GetDriveTypeW(vol_drive); |
| 1328 |
switch (drv_type) |
| 1329 |
{ |
| 1330 |
case DRIVE_REMOVABLE: |
| 1331 |
case DRIVE_FIXED: |
| 1332 |
case DRIVE_CDROM: |
| 1333 |
case DRIVE_RAMDISK: |
| 1334 |
info->flags = VF_DRIVE; |
| 1335 |
break; |
| 1336 |
case DRIVE_REMOTE: |
| 1337 |
info->flags = VF_REMOTE; |
| 1338 |
break; |
| 1339 |
default: |
| 1340 |
info->flags = 0; |
| 1341 |
break; |
| 1342 |
} |
| 1343 |
} |
| 1344 |
|
| 1345 |
} while (FindNextVolumeW(h, vol_name, MAX_PATH)); |
| 1346 |
|
| 1347 |
// Commit and return |
| 1348 |
list.swap(volumes); |
| 1349 |
return STATUS_OK; |
| 1350 |
} |
| 1351 |
|
| 1352 |
status_t read_windows_netinfo(lltl::parray<volume_info_t> *volumes) |
| 1353 |
{ |
| 1354 |
lltl::parray<volume_info_t> list; |
| 1355 |
lsp_finally { free_volume_info(&list); }; |
| 1356 |
|
| 1357 |
// Open the enumeration |
| 1358 |
NETRESOURCEW *local = NULL; |
| 1359 |
HANDLE hEnum = NULL; |
| 1360 |
DWORD dwResult = WNetOpenEnumW(RESOURCE_CONNECTED, RESOURCETYPE_DISK, 0, local, &hEnum); |
| 1361 |
if (dwResult != NO_ERROR) |
| 1362 |
return STATUS_NOT_SUPPORTED; |
| 1363 |
lsp_finally { WNetCloseEnum(hEnum); }; |
| 1364 |
|
| 1365 |
// Iterate over resources |
| 1366 |
DWORD cEntries = -1; |
| 1367 |
DWORD cbBuffer = 16384; |
| 1368 |
local = static_cast<NETRESOURCEW *>(GlobalAlloc(GPTR, cbBuffer)); |
| 1369 |
if (local == NULL) |
| 1370 |
return STATUS_NO_MEM; |
| 1371 |
lsp_finally { GlobalFree(local); }; |
| 1372 |
|
| 1373 |
// Perform enumeration |
| 1374 |
while (true) |
| 1375 |
{ |
| 1376 |
dwResult = WNetEnumResourceW(hEnum, &cEntries, local, &cbBuffer); |
| 1377 |
if (dwResult == ERROR_NO_MORE_ITEMS) |
| 1378 |
break; |
| 1379 |
if (dwResult == ERROR_MORE_DATA) |
| 1380 |
{ |
| 1381 |
NETRESOURCEW *pnew = static_cast<NETRESOURCEW *>(GlobalReAlloc(local, cbBuffer, GPTR)); |
| 1382 |
if (pnew == NULL) |
| 1383 |
return STATUS_NO_MEM; |
| 1384 |
local = pnew; |
| 1385 |
continue; |
| 1386 |
} |
| 1387 |
if (dwResult != NO_ERROR) |
| 1388 |
return STATUS_UNKNOWN_ERR; |
| 1389 |
|
| 1390 |
// Process each entry |
| 1391 |
for (DWORD i = 0; i < cEntries; i++) |
| 1392 |
{ |
| 1393 |
NETRESOURCEW *item = &local[i]; |
| 1394 |
|
| 1395 |
if (item->dwScope != RESOURCE_CONNECTED) |
| 1396 |
continue; |
| 1397 |
if (item->dwType != RESOURCETYPE_DISK) |
| 1398 |
continue; |
| 1399 |
if ((item->dwDisplayType != RESOURCEDISPLAYTYPE_SHARE) && |
| 1400 |
(item->dwDisplayType != RESOURCEDISPLAYTYPE_DIRECTORY)) |
| 1401 |
continue; |
| 1402 |
|
| 1403 |
// Create record |
| 1404 |
volume_info_t *info = new volume_info_t(); |
| 1405 |
if (info == NULL) |
| 1406 |
return STATUS_NO_MEM; |
| 1407 |
else if (!list.add(info)) |
| 1408 |
{ |
| 1409 |
delete info; |
| 1410 |
return STATUS_NO_MEM; |
| 1411 |
} |
| 1412 |
|
| 1413 |
// Fill volume name field |
| 1414 |
if (!info->device.set_utf16(item->lpProvider)) |
| 1415 |
return STATUS_NO_MEM; |
| 1416 |
if (!info->root.set_utf16(item->lpRemoteName)) |
| 1417 |
return STATUS_NO_MEM; |
| 1418 |
if (!info->target.set_utf16(item->lpLocalName)) |
| 1419 |
return STATUS_NO_MEM; |
| 1420 |
if (!info->target.ends_with(FILE_SEPARATOR_C)) |
| 1421 |
{ |
| 1422 |
if (!info->target.append(FILE_SEPARATOR_C)) |
| 1423 |
return STATUS_NO_MEM; |
| 1424 |
} |
| 1425 |
if ((item->lpComment != NULL) && (!info->name.set_utf16(item->lpComment))) |
| 1426 |
return STATUS_NO_MEM; |
| 1427 |
|
| 1428 |
info->flags = VF_REMOTE; |
| 1429 |
} |
| 1430 |
} |
| 1431 |
|
| 1432 |
// Commit and return |
| 1433 |
if (!volumes->add(&list)) |
| 1434 |
return STATUS_NO_MEM; |
| 1435 |
|
| 1436 |
list.flush(); |
| 1437 |
return STATUS_OK; |
| 1438 |
} |
| 1439 |
#endif /* PLATFORM_WINDOWS */ |
| 1440 |
|
| 1441 |
static status_t get_volume_info_impl(lltl::parray<volume_info_t> *volumes) |
| 1442 |
{ |
| 1443 |
status_t res = STATUS_NOT_IMPLEMENTED; |
| 1444 |
#if defined(PLATFORM_LINUX) |
| 1445 |
if ((res = read_linux_mountinfo(volumes)) != STATUS_NOT_SUPPORTED) |
| 1446 |
return res; |
| 1447 |
if ((res = read_linux_mntent("/proc/self/mounts", volumes)) != STATUS_NOT_SUPPORTED) |
| 1448 |
return res; |
| 1449 |
if ((res = read_linux_mntent("/proc/mounts", volumes)) != STATUS_NOT_SUPPORTED) |
| 1450 |
return res; |
| 1451 |
if ((res = read_linux_mntent("/etc/mtab", volumes)) != STATUS_NOT_SUPPORTED) |
| 1452 |
return res; |
| 1453 |
#elif defined(PLATFORM_BSD) || defined(PLATFORM_MACOSX) |
| 1454 |
if ((res = read_fsstat_mntinfo(volumes)) != STATUS_NOT_SUPPORTED) |
| 1455 |
return res; |
| 1456 |
#elif defined(PLATFORM_HAIKU) |
| 1457 |
res = read_haiku_mntinfo(volumes); |
| 1458 |
#elif defined(PLATFORM_WINDOWS) |
| 1459 |
res = read_windows_mntinfo(volumes); |
| 1460 |
if (res == STATUS_OK) |
| 1461 |
res = read_windows_netinfo(volumes); |
| 1462 |
#endif |
| 1463 |
|
| 1464 |
return res; |
| 1465 |
} |
| 1466 |
|
| 1467 |
status_t get_volume_info(lltl::parray<volume_info_t> *volumes) |
| 1468 |
{ |
| 1469 |
// Verify input |
| 1470 |
if (volumes == NULL) |
| 1471 |
return STATUS_BAD_ARGUMENTS; |
| 1472 |
|
| 1473 |
// Read volumes to temporary array |
| 1474 |
lltl::parray<volume_info_t> temp; |
| 1475 |
lsp_finally { free_volume_info(&temp); }; |
| 1476 |
status_t res = get_volume_info_impl(&temp); |
| 1477 |
|
| 1478 |
// Commit changes if operation succeeded |
| 1479 |
if (res == STATUS_OK) |
| 1480 |
temp.swap(volumes); |
| 1481 |
|
| 1482 |
return res; |
| 1483 |
} |
| 1484 |
|
| 1485 |
void free_volume_info(lltl::parray<volume_info_t> *volumes) |
| 1486 |
{ |
| 1487 |
if (volumes == NULL) |
| 1488 |
return; |
| 1489 |
|
| 1490 |
for (size_t i=0, n=volumes->size(); i<n; ++i) |
| 1491 |
{ |
| 1492 |
volume_info_t *p = volumes->uget(i); |
| 1493 |
if (p != NULL) |
| 1494 |
delete p; |
| 1495 |
} |
| 1496 |
volumes->flush(); |
| 1497 |
} |
| 1498 |
|
| 1499 |
size_t page_size() |
| 1500 |
{ |
| 1501 |
#ifdef PLATFORM_WINDOWS |
| 1502 |
SYSTEM_INFO os_sysinfo; |
| 1503 |
GetSystemInfo(&os_sysinfo); |
| 1504 |
|
| 1505 |
return os_sysinfo.dwPageSize; |
| 1506 |
#else |
| 1507 |
return sysconf(_SC_PAGESIZE); |
| 1508 |
#endif /* PLATFORM_WINDOWS */ |
| 1509 |
} |
| 1510 |
|
| 1511 |
size_t system_cores() |
| 1512 |
{ |
| 1513 |
#ifdef PLATFORM_WINDOWS |
| 1514 |
SYSTEM_INFO os_sysinfo; |
| 1515 |
GetSystemInfo(&os_sysinfo); |
| 1516 |
|
| 1517 |
return os_sysinfo.dwNumberOfProcessors;; |
| 1518 |
#else |
| 1519 |
return sysconf(_SC_NPROCESSORS_ONLN); |
| 1520 |
#endif /* PLATFORM_WINDOWS */ |
| 1521 |
} |
| 1522 |
|
| 1523 |
#ifdef PLATFORM_WINDOWS |
| 1524 |
static status_t append_arg_escaped(LSPString *dst, const LSPString *value) |
| 1525 |
{ |
| 1526 |
if (value->is_empty()) |
| 1527 |
return (dst->append_ascii("\"\"")) ? STATUS_OK : STATUS_NO_MEM; |
| 1528 |
|
| 1529 |
for (size_t i=0, n=value->length(); i<n; ++i) |
| 1530 |
{ |
| 1531 |
lsp_wchar_t ch = value->char_at(i); |
| 1532 |
switch (ch) |
| 1533 |
{ |
| 1534 |
case ' ': |
| 1535 |
if (!dst->append_ascii("\" \"", 3)) |
| 1536 |
return STATUS_NO_MEM; |
| 1537 |
break; |
| 1538 |
case '"': |
| 1539 |
if (!dst->append_ascii("\\\"", 2)) |
| 1540 |
return STATUS_NO_MEM; |
| 1541 |
break; |
| 1542 |
default: |
| 1543 |
if (!dst->append(ch)) |
| 1544 |
return STATUS_NO_MEM; |
| 1545 |
break; |
| 1546 |
} |
| 1547 |
} |
| 1548 |
|
| 1549 |
return STATUS_OK; |
| 1550 |
} |
| 1551 |
|
| 1552 |
static status_t run_program(const LSPString *path, const LSPString *args) |
| 1553 |
{ |
| 1554 |
// Check that executable file exists |
| 1555 |
LSPString xpath; |
| 1556 |
LPCWSTR ppath = path->get_utf16(); |
| 1557 |
if (!PathFileExistsW(ppath)) |
| 1558 |
{ |
| 1559 |
// Try .EXE and .COM extensions |
| 1560 |
if (path->ends_with_ascii_nocase(".exe")) |
| 1561 |
return STATUS_NOT_FOUND; |
| 1562 |
if (path->ends_with_ascii_nocase(".com")) |
| 1563 |
return STATUS_NOT_FOUND; |
| 1564 |
// Try ".EXE" |
| 1565 |
if (!xpath.set(path)) |
| 1566 |
return STATUS_NO_MEM; |
| 1567 |
if (!xpath.append_ascii(".exe")) |
| 1568 |
return STATUS_NO_MEM; |
| 1569 |
ppath = xpath.get_utf16(); |
| 1570 |
if (!PathFileExistsW(ppath)) |
| 1571 |
{ |
| 1572 |
xpath.set_length(xpath.length() - 4); |
| 1573 |
if (!xpath.append_ascii(".com")) |
| 1574 |
return STATUS_NO_MEM; |
| 1575 |
ppath = xpath.get_utf16(); |
| 1576 |
if (!PathFileExistsW(ppath)) |
| 1577 |
return STATUS_NOT_FOUND; |
| 1578 |
} |
| 1579 |
} |
| 1580 |
|
| 1581 |
// Launch child process |
| 1582 |
STARTUPINFOW si; |
| 1583 |
PROCESS_INFORMATION pi; |
| 1584 |
|
| 1585 |
ZeroMemory( &si, sizeof(STARTUPINFOW) ); |
| 1586 |
ZeroMemory( &pi, sizeof(PROCESS_INFORMATION) ); |
| 1587 |
si.cb = sizeof(si); |
| 1588 |
|
| 1589 |
// Start the child process. |
| 1590 |
LPWSTR str_args = args->clone_utf16(); |
| 1591 |
if (str_args == NULL) |
| 1592 |
return STATUS_NO_MEM; |
| 1593 |
lsp_finally { free(str_args); }; |
| 1594 |
|
| 1595 |
// Create process |
| 1596 |
if(!::CreateProcessW( |
| 1597 |
ppath, // Module name (use command line) |
| 1598 |
str_args, // Command line |
| 1599 |
NULL, // Process handle not inheritable |
| 1600 |
NULL, // Thread handle not inheritable |
| 1601 |
FALSE, // Set handle inheritance to FALSE |
| 1602 |
DETACHED_PROCESS | CREATE_UNICODE_ENVIRONMENT, // Flags |
| 1603 |
NULL, // Set-up environment block |
| 1604 |
NULL, // Use parent's starting directory |
| 1605 |
&si, // Pointer to STARTUPINFO structure |
| 1606 |
&pi // Pointer to PROCESS_INFORMATION structure |
| 1607 |
)) |
| 1608 |
{ |
| 1609 |
const DWORD error = ::GetLastError(); |
| 1610 |
lsp_warn("Failed to create child process (%d)\n", int(error)); |
| 1611 |
|
| 1612 |
switch (error) |
| 1613 |
{ |
| 1614 |
case ERROR_FILE_NOT_FOUND: |
| 1615 |
case ERROR_PATH_NOT_FOUND: |
| 1616 |
return STATUS_NOT_FOUND; |
| 1617 |
default: |
| 1618 |
return STATUS_UNKNOWN_ERR; |
| 1619 |
} |
| 1620 |
} |
| 1621 |
|
| 1622 |
CloseHandle(pi.hProcess); |
| 1623 |
CloseHandle(pi.hThread); |
| 1624 |
|
| 1625 |
return STATUS_OK; |
| 1626 |
} |
| 1627 |
|
| 1628 |
static status_t shell_execute(const LSPString *cmd, const LSPString *args) |
| 1629 |
{ |
| 1630 |
// If path contains separator, do not lookup PATH |
| 1631 |
if (cmd->index_of(FILE_SEPARATOR_C) >= 0) |
| 1632 |
return run_program(cmd, args); |
| 1633 |
|
| 1634 |
// Lookup PATH |
| 1635 |
LSPString path, program; |
| 1636 |
status_t res = get_env_var("PATH", &path); |
| 1637 |
if (res != STATUS_OK) |
| 1638 |
return res; |
| 1639 |
|
| 1640 |
ssize_t start = 0, count = path.length(), found = 0; |
| 1641 |
do |
| 1642 |
{ |
| 1643 |
// Lookup for separator |
| 1644 |
found = path.index_of(start, ';'); |
| 1645 |
const ssize_t next = (found >= 0) ? found : count; |
| 1646 |
|
| 1647 |
if (start < next) |
| 1648 |
{ |
| 1649 |
if (!program.set(&path, start, found)) |
| 1650 |
return STATUS_NO_MEM; |
| 1651 |
if (!program.ends_with(FILE_SEPARATOR_C)) |
| 1652 |
{ |
| 1653 |
if (!program.append(FILE_SEPARATOR_C)) |
| 1654 |
return STATUS_NO_MEM; |
| 1655 |
} |
| 1656 |
if (!program.append(cmd)) |
| 1657 |
return STATUS_NO_MEM; |
| 1658 |
lsp_trace("%s", program.get_utf8()); |
| 1659 |
if ((res = run_program(&program, args)) == STATUS_OK) |
| 1660 |
return res; |
| 1661 |
} |
| 1662 |
|
| 1663 |
start = found + 1; |
| 1664 |
} while (found >= 0); |
| 1665 |
|
| 1666 |
return STATUS_NOT_FOUND; |
| 1667 |
} |
| 1668 |
|
| 1669 |
status_t exec_detached(const char *program, const char * const *argv) |
| 1670 |
{ |
| 1671 |
status_t res; |
| 1672 |
LSPString args, arg; |
| 1673 |
|
| 1674 |
for (; (argv != NULL) && (*argv != NULL); ++argv) |
| 1675 |
{ |
| 1676 |
// Add separator |
| 1677 |
if (!args.is_empty()) |
| 1678 |
{ |
| 1679 |
if (!args.append(' ')) |
| 1680 |
return STATUS_NO_MEM; |
| 1681 |
} |
| 1682 |
if (!arg.set_native(*argv)) |
| 1683 |
return STATUS_NO_MEM; |
| 1684 |
|
| 1685 |
if ((res = append_arg_escaped(&args, &arg)) != STATUS_OK) |
| 1686 |
return res; |
| 1687 |
} |
| 1688 |
|
| 1689 |
if (!arg.set_native(program)) |
| 1690 |
return STATUS_NO_MEM; |
| 1691 |
|
| 1692 |
return shell_execute(&arg, &args); |
| 1693 |
} |
| 1694 |
|
| 1695 |
status_t exec_detached(const LSPString *program, const LSPString * const *argv) |
| 1696 |
{ |
| 1697 |
status_t res; |
| 1698 |
LSPString args; |
| 1699 |
|
| 1700 |
for (; (argv != NULL) && (*argv != NULL); ++argv) |
| 1701 |
{ |
| 1702 |
// Add separator |
| 1703 |
if (!args.is_empty()) |
| 1704 |
{ |
| 1705 |
if (!args.append(' ')) |
| 1706 |
return STATUS_NO_MEM; |
| 1707 |
} |
| 1708 |
if ((res = append_arg_escaped(&args, *argv)) != STATUS_OK) |
| 1709 |
return res; |
| 1710 |
} |
| 1711 |
|
| 1712 |
return shell_execute(program, &args); |
| 1713 |
} |
| 1714 |
|
| 1715 |
#else |
| 1716 |
|
| 1717 |
static void execvp_process(const char *cmd, char * const *argv, bool soft_exit) |
| 1718 |
{ |
| 1719 |
fprintf(stderr, "Executing process...\n"); |
| 1720 |
|
| 1721 |
// Detach the process |
| 1722 |
setsid(); |
| 1723 |
|
| 1724 |
// Launch the process |
| 1725 |
if (strchr(cmd, FILE_SEPARATOR_C) != NULL) |
| 1726 |
::execv(cmd, argv); |
| 1727 |
else |
| 1728 |
::execvp(cmd, argv); |
| 1729 |
|
| 1730 |
lsp_trace("execve failed for pid=%d, code=%d\n", int(getpid())); |
| 1731 |
|
| 1732 |
// Return error only if ::execvp failed |
| 1733 |
if (soft_exit) |
| 1734 |
::_exit(STATUS_UNKNOWN_ERR); |
| 1735 |
else |
| 1736 |
::exit(STATUS_UNKNOWN_ERR); |
| 1737 |
} |
| 1738 |
|
| 1739 |
#ifdef POSIX_SPAWN_SETSID |
| 1740 |
static status_t spawn_process(const char *cmd, char * const *argv, char * const *envp) |
| 1741 |
{ |
| 1742 |
lsp_trace("Creating child process using posix_spawn..."); |
| 1743 |
|
| 1744 |
// Initialize spawn routines |
| 1745 |
posix_spawnattr_t attr; |
| 1746 |
if (::posix_spawnattr_init(&attr)) |
| 1747 |
return STATUS_UNKNOWN_ERR; |
| 1748 |
|
| 1749 |
int flags = POSIX_SPAWN_SETSID; |
| 1750 |
#if defined(__USE_GNU) || (defined(POSIX_SPAWN_USEVFORK)) |
| 1751 |
flags |= POSIX_SPAWN_USEVFORK; // Prefer vfork over fork |
| 1752 |
#endif |
| 1753 |
// Prever vfork() over fork() |
| 1754 |
if (::posix_spawnattr_setflags(&attr, flags)) |
| 1755 |
{ |
| 1756 |
::posix_spawnattr_destroy(&attr); |
| 1757 |
return STATUS_UNKNOWN_ERR; |
| 1758 |
} |
| 1759 |
|
| 1760 |
posix_spawn_file_actions_t actions; |
| 1761 |
if (::posix_spawn_file_actions_init(&actions)) |
| 1762 |
{ |
| 1763 |
::posix_spawnattr_destroy(&attr); |
| 1764 |
return STATUS_UNKNOWN_ERR; |
| 1765 |
} |
| 1766 |
|
| 1767 |
// Perform posix_spawn() |
| 1768 |
pid_t pid; |
| 1769 |
status_t res = STATUS_OK; |
| 1770 |
while (true) |
| 1771 |
{ |
| 1772 |
int x = ::posix_spawnp(&pid, cmd, &actions, &attr, argv, envp); |
| 1773 |
switch (x) |
| 1774 |
{ |
| 1775 |
case 0: break; |
| 1776 |
case EAGAIN: continue; |
| 1777 |
case ENOMEM: res = STATUS_NO_MEM; break; |
| 1778 |
default: res = STATUS_UNKNOWN_ERR; break; |
| 1779 |
} |
| 1780 |
break; |
| 1781 |
} |
| 1782 |
|
| 1783 |
::posix_spawn_file_actions_destroy(&actions); |
| 1784 |
::posix_spawnattr_destroy(&attr); |
| 1785 |
|
| 1786 |
return res; |
| 1787 |
} |
| 1788 |
#endif /* POSIX_SPAWN_SETSID */ |
| 1789 |
|
| 1790 |
static status_t vfork_process(const char *cmd, char * const *argv) |
| 1791 |
{ |
| 1792 |
fprintf(stderr, "Creating child process using vfork()...\n"); |
| 1793 |
errno = 0; |
| 1794 |
|
| 1795 |
#ifdef PLATFORM_MACOSX |
| 1796 |
#if defined(COMPILER_CLANG) |
| 1797 |
#pragma clang diagnostic push |
| 1798 |
#pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 1799 |
#elif defined(COMPILER_GCC) |
| 1800 |
#pragma GCC diagnostic push |
| 1801 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 1802 |
#endif |
| 1803 |
#endif /* PLATFORM_MACOSX */ |
| 1804 |
|
| 1805 |
// vfork() is deprecated in MacOS, we'll hide deprectation warning for that as |
| 1806 |
// vfork() is a fallback case for posix_spawn. |
| 1807 |
pid_t pid = ::vfork(); |
| 1808 |
|
| 1809 |
#ifdef PLATFORM_MACOSX |
| 1810 |
#if defined(COMPILER_CLANG) |
| 1811 |
#pragma clang diagnostic pop |
| 1812 |
#elif defined(COMPILER_GCC) |
| 1813 |
#pragma GCC diagnostic pop |
| 1814 |
#endif |
| 1815 |
#endif /* PLATFORM_MACOSX */ |
| 1816 |
|
| 1817 |
if (pid < 0) |
| 1818 |
{ |
| 1819 |
// Failed to vfork()? |
| 1820 |
int code = errno; |
| 1821 |
switch (code) |
| 1822 |
{ |
| 1823 |
case ENOMEM: return STATUS_NO_MEM; |
| 1824 |
case EAGAIN: return STATUS_NO_MEM; |
| 1825 |
default: return STATUS_UNKNOWN_ERR; |
| 1826 |
} |
| 1827 |
} |
| 1828 |
else if (pid == 0) // The child process stuff |
| 1829 |
execvp_process(cmd, argv, true); |
| 1830 |
|
| 1831 |
return STATUS_OK; |
| 1832 |
} |
| 1833 |
|
| 1834 |
static status_t fork_process(const char *cmd, char * const *argv) |
| 1835 |
{ |
| 1836 |
lsp_trace("Creating child process using fork()..."); |
| 1837 |
errno = 0; |
| 1838 |
pid_t pid = ::fork(); |
| 1839 |
|
| 1840 |
if (pid < 0) |
| 1841 |
{ |
| 1842 |
// Failed to fork()? |
| 1843 |
int code = errno; |
| 1844 |
switch (code) |
| 1845 |
{ |
| 1846 |
case ENOMEM: return STATUS_NO_MEM; |
| 1847 |
case EAGAIN: return STATUS_NO_MEM; |
| 1848 |
default: return STATUS_UNKNOWN_ERR; |
| 1849 |
} |
| 1850 |
} |
| 1851 |
else if (pid == 0) // The child process stuff |
| 1852 |
execvp_process(cmd, argv, false); |
| 1853 |
|
| 1854 |
return STATUS_OK; |
| 1855 |
} |
| 1856 |
|
| 1857 |
static inline status_t do_exec_detached(const char *program, char **argv) |
| 1858 |
{ |
| 1859 |
status_t res = STATUS_UNKNOWN_ERR; |
| 1860 |
|
| 1861 |
// Different behaviour, depending on POSIX_SPAWN_USEVFORK presence |
| 1862 |
#ifdef POSIX_SPAWN_SETSID |
| 1863 |
if (res != STATUS_OK) |
| 1864 |
res = spawn_process(program, argv, environ); |
| 1865 |
#endif /* POSIX_SPAWN_SETSID */ |
| 1866 |
|
| 1867 |
if (res != STATUS_OK) |
| 1868 |
res = vfork_process(program, argv); |
| 1869 |
if (res != STATUS_OK) |
| 1870 |
res = fork_process(program, argv); |
| 1871 |
|
| 1872 |
return res; |
| 1873 |
} |
| 1874 |
|
| 1875 |
status_t exec_detached(const char *program, const char * const *args) |
| 1876 |
{ |
| 1877 |
if (program == NULL) |
| 1878 |
return STATUS_BAD_ARGUMENTS; |
| 1879 |
|
| 1880 |
// Fill list of arguments |
| 1881 |
lltl::parray<char> vargs; |
| 1882 |
lsp_finally { |
| 1883 |
for (lltl::iterator<char> it = vargs.values(); it; ++it) |
| 1884 |
free(it.get()); |
| 1885 |
}; |
| 1886 |
|
| 1887 |
// Put first name of program to the list |
| 1888 |
char *arg = strdup(program); |
| 1889 |
if (arg == NULL) |
| 1890 |
return STATUS_NO_MEM; |
| 1891 |
if (!vargs.add(arg)) |
| 1892 |
{ |
| 1893 |
free(arg); |
| 1894 |
return STATUS_NO_MEM; |
| 1895 |
} |
| 1896 |
|
| 1897 |
// Put program arguments to the list |
| 1898 |
for (; (args != NULL) && (*args != NULL); ++args) |
| 1899 |
{ |
| 1900 |
arg = strdup(*args); |
| 1901 |
if (arg == NULL) |
| 1902 |
return STATUS_NO_MEM; |
| 1903 |
if (!vargs.add(arg)) |
| 1904 |
{ |
| 1905 |
free(arg); |
| 1906 |
return STATUS_NO_MEM; |
| 1907 |
} |
| 1908 |
} |
| 1909 |
|
| 1910 |
// Put terminating NULL pointer |
| 1911 |
if (!vargs.add(static_cast<char *>(NULL))) |
| 1912 |
return STATUS_NO_MEM; |
| 1913 |
|
| 1914 |
return do_exec_detached(program, vargs.array()); |
| 1915 |
} |
| 1916 |
|
| 1917 |
status_t exec_detached(const LSPString *program, const LSPString * const *args) |
| 1918 |
{ |
| 1919 |
if (program == NULL) |
| 1920 |
return STATUS_BAD_ARGUMENTS; |
| 1921 |
|
| 1922 |
// Fill list of arguments |
| 1923 |
lltl::parray<char> vargs; |
| 1924 |
lsp_finally { |
| 1925 |
for (lltl::iterator<char> it = vargs.values(); it; ++it) |
| 1926 |
free(it.get()); |
| 1927 |
}; |
| 1928 |
|
| 1929 |
// Put first name of program to the list |
| 1930 |
char *arg = program->clone_native(); |
| 1931 |
if (arg == NULL) |
| 1932 |
return STATUS_NO_MEM; |
| 1933 |
if (!vargs.add(arg)) |
| 1934 |
{ |
| 1935 |
free(arg); |
| 1936 |
return STATUS_NO_MEM; |
| 1937 |
} |
| 1938 |
|
| 1939 |
// Put program arguments to the list |
| 1940 |
for (; (args != NULL) && (*args != NULL); ++args) |
| 1941 |
{ |
| 1942 |
arg = (*args)->clone_native(); |
| 1943 |
if (arg == NULL) |
| 1944 |
return STATUS_NO_MEM; |
| 1945 |
if (!vargs.add(arg)) |
| 1946 |
{ |
| 1947 |
free(arg); |
| 1948 |
return STATUS_NO_MEM; |
| 1949 |
} |
| 1950 |
} |
| 1951 |
|
| 1952 |
// Put terminating NULL pointer |
| 1953 |
if (!vargs.add(static_cast<char *>(NULL))) |
| 1954 |
return STATUS_NO_MEM; |
| 1955 |
|
| 1956 |
return do_exec_detached(program->get_native(), vargs.array()); |
| 1957 |
} |
| 1958 |
#endif /* PLATFORM_WINDOWS */ |
| 1959 |
|
| 1960 |
} /* namespace system */ |
| 1961 |
} /* namespace lsp */ |
| 1962 |
|
| 1963 |
|