unistd.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ScopedValueRollback.h>
  7. #include <AK/String.h>
  8. #include <AK/Vector.h>
  9. #include <alloca.h>
  10. #include <assert.h>
  11. #include <bits/pthread_integration.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <getopt.h>
  15. #include <grp.h>
  16. #include <pwd.h>
  17. #include <stdarg.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sys/ioctl.h>
  22. #include <sys/mman.h>
  23. #include <sys/select.h>
  24. #include <sys/types.h>
  25. #include <syscall.h>
  26. #include <termios.h>
  27. #include <time.h>
  28. #include <unistd.h>
  29. extern "C" {
  30. #ifdef NO_TLS
  31. static int s_cached_tid = 0;
  32. #else
  33. static __thread int s_cached_tid = 0;
  34. #endif
  35. static int s_cached_pid = 0;
  36. int chown(const char* pathname, uid_t uid, gid_t gid)
  37. {
  38. if (!pathname) {
  39. errno = EFAULT;
  40. return -1;
  41. }
  42. Syscall::SC_chown_params params { { pathname, strlen(pathname) }, uid, gid };
  43. int rc = syscall(SC_chown, &params);
  44. __RETURN_WITH_ERRNO(rc, rc, -1);
  45. }
  46. int fchown(int fd, uid_t uid, gid_t gid)
  47. {
  48. int rc = syscall(SC_fchown, fd, uid, gid);
  49. __RETURN_WITH_ERRNO(rc, rc, -1);
  50. }
  51. pid_t fork()
  52. {
  53. __pthread_fork_prepare();
  54. int rc = syscall(SC_fork);
  55. if (rc == 0) {
  56. s_cached_tid = 0;
  57. s_cached_pid = 0;
  58. __pthread_fork_child();
  59. } else if (rc != -1) {
  60. __pthread_fork_parent();
  61. }
  62. __RETURN_WITH_ERRNO(rc, rc, -1);
  63. }
  64. pid_t vfork()
  65. {
  66. return fork();
  67. }
  68. int execv(const char* path, char* const argv[])
  69. {
  70. return execve(path, argv, environ);
  71. }
  72. int execve(const char* filename, char* const argv[], char* const envp[])
  73. {
  74. size_t arg_count = 0;
  75. for (size_t i = 0; argv[i]; ++i)
  76. ++arg_count;
  77. size_t env_count = 0;
  78. for (size_t i = 0; envp[i]; ++i)
  79. ++env_count;
  80. auto copy_strings = [&](auto& vec, size_t count, auto& output) {
  81. output.length = count;
  82. for (size_t i = 0; vec[i]; ++i) {
  83. output.strings[i].characters = vec[i];
  84. output.strings[i].length = strlen(vec[i]);
  85. }
  86. };
  87. Syscall::SC_execve_params params;
  88. params.arguments.strings = (Syscall::StringArgument*)alloca(arg_count * sizeof(Syscall::StringArgument));
  89. params.environment.strings = (Syscall::StringArgument*)alloca(env_count * sizeof(Syscall::StringArgument));
  90. params.path = { filename, strlen(filename) };
  91. copy_strings(argv, arg_count, params.arguments);
  92. copy_strings(envp, env_count, params.environment);
  93. int rc = syscall(SC_execve, &params);
  94. __RETURN_WITH_ERRNO(rc, rc, -1);
  95. }
  96. int execvpe(const char* filename, char* const argv[], char* const envp[])
  97. {
  98. if (strchr(filename, '/'))
  99. return execve(filename, argv, envp);
  100. ScopedValueRollback errno_rollback(errno);
  101. String path = getenv("PATH");
  102. if (path.is_empty())
  103. path = "/bin:/usr/bin";
  104. auto parts = path.split(':');
  105. for (auto& part : parts) {
  106. auto candidate = String::formatted("{}/{}", part, filename);
  107. int rc = execve(candidate.characters(), argv, envp);
  108. if (rc < 0 && errno != ENOENT) {
  109. errno_rollback.set_override_rollback_value(errno);
  110. dbgln("execvpe() failed on attempt ({}) with {}", candidate, strerror(errno));
  111. return rc;
  112. }
  113. }
  114. errno_rollback.set_override_rollback_value(ENOENT);
  115. dbgln("execvpe() leaving :(");
  116. return -1;
  117. }
  118. int execvp(const char* filename, char* const argv[])
  119. {
  120. int rc = execvpe(filename, argv, environ);
  121. int saved_errno = errno;
  122. dbgln("execvp({}, ...) about to return {} with errno={}", filename, rc, saved_errno);
  123. errno = saved_errno;
  124. return rc;
  125. }
  126. int execl(const char* filename, const char* arg0, ...)
  127. {
  128. Vector<const char*, 16> args;
  129. args.append(arg0);
  130. va_list ap;
  131. va_start(ap, arg0);
  132. for (;;) {
  133. const char* arg = va_arg(ap, const char*);
  134. if (!arg)
  135. break;
  136. args.append(arg);
  137. }
  138. va_end(ap);
  139. args.append(nullptr);
  140. return execve(filename, const_cast<char* const*>(args.data()), environ);
  141. }
  142. int execle(char const* filename, char const* arg0, ...)
  143. {
  144. Vector<char const*> args;
  145. args.append(arg0);
  146. va_list ap;
  147. va_start(ap, arg0);
  148. char const* arg;
  149. do {
  150. arg = va_arg(ap, char const*);
  151. args.append(arg);
  152. } while (arg);
  153. auto argv = const_cast<char* const*>(args.data());
  154. auto envp = const_cast<char* const*>(va_arg(ap, char* const*));
  155. va_end(ap);
  156. return execve(filename, argv, envp);
  157. }
  158. int execlp(const char* filename, const char* arg0, ...)
  159. {
  160. Vector<const char*, 16> args;
  161. args.append(arg0);
  162. va_list ap;
  163. va_start(ap, arg0);
  164. for (;;) {
  165. const char* arg = va_arg(ap, const char*);
  166. if (!arg)
  167. break;
  168. args.append(arg);
  169. }
  170. va_end(ap);
  171. args.append(nullptr);
  172. return execvpe(filename, const_cast<char* const*>(args.data()), environ);
  173. }
  174. uid_t geteuid()
  175. {
  176. return syscall(SC_geteuid);
  177. }
  178. gid_t getegid()
  179. {
  180. return syscall(SC_getegid);
  181. }
  182. uid_t getuid()
  183. {
  184. return syscall(SC_getuid);
  185. }
  186. gid_t getgid()
  187. {
  188. return syscall(SC_getgid);
  189. }
  190. pid_t getpid()
  191. {
  192. int cached_pid = s_cached_pid;
  193. if (!cached_pid) {
  194. cached_pid = syscall(SC_getpid);
  195. s_cached_pid = cached_pid;
  196. }
  197. return cached_pid;
  198. }
  199. pid_t getppid()
  200. {
  201. return syscall(SC_getppid);
  202. }
  203. int getresuid(uid_t* ruid, uid_t* euid, uid_t* suid)
  204. {
  205. return syscall(SC_getresuid, ruid, euid, suid);
  206. }
  207. int getresgid(gid_t* rgid, gid_t* egid, gid_t* sgid)
  208. {
  209. return syscall(SC_getresgid, rgid, egid, sgid);
  210. }
  211. pid_t getsid(pid_t pid)
  212. {
  213. int rc = syscall(SC_getsid, pid);
  214. __RETURN_WITH_ERRNO(rc, rc, -1);
  215. }
  216. pid_t setsid()
  217. {
  218. int rc = syscall(SC_setsid);
  219. __RETURN_WITH_ERRNO(rc, rc, -1);
  220. }
  221. pid_t tcgetpgrp(int fd)
  222. {
  223. pid_t pgrp;
  224. int rc = ioctl(fd, TIOCGPGRP, &pgrp);
  225. if (rc < 0)
  226. return rc;
  227. return pgrp;
  228. }
  229. int tcsetpgrp(int fd, pid_t pgid)
  230. {
  231. return ioctl(fd, TIOCSPGRP, pgid);
  232. }
  233. int setpgid(pid_t pid, pid_t pgid)
  234. {
  235. int rc = syscall(SC_setpgid, pid, pgid);
  236. __RETURN_WITH_ERRNO(rc, rc, -1);
  237. }
  238. pid_t getpgid(pid_t pid)
  239. {
  240. int rc = syscall(SC_getpgid, pid);
  241. __RETURN_WITH_ERRNO(rc, rc, -1);
  242. }
  243. pid_t getpgrp()
  244. {
  245. int rc = syscall(SC_getpgrp);
  246. __RETURN_WITH_ERRNO(rc, rc, -1);
  247. }
  248. ssize_t read(int fd, void* buf, size_t count)
  249. {
  250. int rc = syscall(SC_read, fd, buf, count);
  251. __RETURN_WITH_ERRNO(rc, rc, -1);
  252. }
  253. ssize_t pread(int fd, void* buf, size_t count, off_t offset)
  254. {
  255. // FIXME: This is not thread safe and should be implemented in the kernel instead.
  256. off_t old_offset = lseek(fd, 0, SEEK_CUR);
  257. lseek(fd, offset, SEEK_SET);
  258. ssize_t nread = read(fd, buf, count);
  259. lseek(fd, old_offset, SEEK_SET);
  260. return nread;
  261. }
  262. ssize_t write(int fd, const void* buf, size_t count)
  263. {
  264. int rc = syscall(SC_write, fd, buf, count);
  265. __RETURN_WITH_ERRNO(rc, rc, -1);
  266. }
  267. ssize_t pwrite(int fd, const void* buf, size_t count, off_t offset)
  268. {
  269. // FIXME: This is not thread safe and should be implemented in the kernel instead.
  270. off_t old_offset = lseek(fd, 0, SEEK_CUR);
  271. lseek(fd, offset, SEEK_SET);
  272. ssize_t nwritten = write(fd, buf, count);
  273. lseek(fd, old_offset, SEEK_SET);
  274. return nwritten;
  275. }
  276. int ttyname_r(int fd, char* buffer, size_t size)
  277. {
  278. int rc = syscall(SC_ttyname, fd, buffer, size);
  279. __RETURN_WITH_ERRNO(rc, rc, -1);
  280. }
  281. static char ttyname_buf[32];
  282. char* ttyname(int fd)
  283. {
  284. if (ttyname_r(fd, ttyname_buf, sizeof(ttyname_buf)) < 0)
  285. return nullptr;
  286. return ttyname_buf;
  287. }
  288. int close(int fd)
  289. {
  290. int rc = syscall(SC_close, fd);
  291. __RETURN_WITH_ERRNO(rc, rc, -1);
  292. }
  293. int chdir(const char* path)
  294. {
  295. if (!path) {
  296. errno = EFAULT;
  297. return -1;
  298. }
  299. int rc = syscall(SC_chdir, path, strlen(path));
  300. __RETURN_WITH_ERRNO(rc, rc, -1);
  301. }
  302. int fchdir(int fd)
  303. {
  304. int rc = syscall(SC_fchdir, fd);
  305. __RETURN_WITH_ERRNO(rc, rc, -1);
  306. }
  307. char* getcwd(char* buffer, size_t size)
  308. {
  309. if (buffer && size == 0) {
  310. // POSIX requires that we set errno to EINVAL here, but in our syscall it makes sense to
  311. // allow "probing" the Kernel with a zero-sized buffer, and it does not return -EINVAL.
  312. // So we have to inject EINVAL here.
  313. errno = EINVAL;
  314. return nullptr;
  315. }
  316. bool self_allocated = false;
  317. if (!buffer) {
  318. size = size ? size : 64;
  319. buffer = (char*)malloc(size);
  320. self_allocated = true;
  321. }
  322. int rc = syscall(SC_getcwd, buffer, size);
  323. if (rc < 0) {
  324. if (self_allocated)
  325. free(buffer);
  326. errno = -rc;
  327. return nullptr;
  328. }
  329. size_t actual_size = static_cast<size_t>(rc);
  330. if (actual_size <= size) {
  331. return buffer;
  332. }
  333. // If we get here, the current directory path was silently truncated.
  334. if (!self_allocated) {
  335. // In this case, POSIX causes information loss: the caller cannot learn about the ideal
  336. // buffer size. This is the reason we went with silently truncation instead.
  337. errno = ERANGE;
  338. return nullptr;
  339. }
  340. // Try again.
  341. free(buffer);
  342. size = actual_size;
  343. buffer = (char*)malloc(size);
  344. rc = syscall(SC_getcwd, buffer, size);
  345. if (rc < 0) {
  346. // Can only happen if we lose a race. Let's pretend we lost the race in the first place.
  347. free(buffer);
  348. errno = -rc;
  349. return nullptr;
  350. }
  351. actual_size = static_cast<size_t>(rc);
  352. if (actual_size < size) {
  353. // If we're here, then cwd has become longer while we were looking at it. (Race with another thread?)
  354. // There's not much we can do, unless we want to loop endlessly
  355. // in this case. Let's leave it up to the caller whether to loop.
  356. free(buffer);
  357. errno = EAGAIN;
  358. return nullptr;
  359. }
  360. return buffer;
  361. }
  362. char* getwd(char* buf)
  363. {
  364. if (buf == nullptr) {
  365. errno = EINVAL;
  366. return nullptr;
  367. }
  368. auto* p = getcwd(buf, PATH_MAX);
  369. if (errno == ERANGE) {
  370. // POSIX quirk
  371. errno = ENAMETOOLONG;
  372. }
  373. return p;
  374. }
  375. unsigned int sleep(unsigned int seconds)
  376. {
  377. struct timespec ts = { seconds, 0 };
  378. if (clock_nanosleep(CLOCK_MONOTONIC_COARSE, 0, &ts, nullptr) < 0)
  379. return ts.tv_sec;
  380. return 0;
  381. }
  382. int usleep(useconds_t usec)
  383. {
  384. struct timespec ts = { (long)(usec / 1000000), (long)(usec % 1000000) * 1000 };
  385. return clock_nanosleep(CLOCK_MONOTONIC_COARSE, 0, &ts, nullptr);
  386. }
  387. int gethostname(char* buffer, size_t size)
  388. {
  389. int rc = syscall(SC_gethostname, buffer, size);
  390. __RETURN_WITH_ERRNO(rc, rc, -1);
  391. }
  392. int sethostname(const char* hostname, ssize_t size)
  393. {
  394. int rc = syscall(SC_sethostname, hostname, size);
  395. __RETURN_WITH_ERRNO(rc, rc, -1);
  396. }
  397. ssize_t readlink(const char* path, char* buffer, size_t size)
  398. {
  399. Syscall::SC_readlink_params params { { path, strlen(path) }, { buffer, size } };
  400. int rc = syscall(SC_readlink, &params);
  401. // Return the number of bytes placed in the buffer, not the full path size.
  402. __RETURN_WITH_ERRNO(rc, min((size_t)rc, size), -1);
  403. }
  404. off_t lseek(int fd, off_t offset, int whence)
  405. {
  406. int rc = syscall(SC_lseek, fd, &offset, whence);
  407. __RETURN_WITH_ERRNO(rc, offset, -1);
  408. }
  409. int link(const char* old_path, const char* new_path)
  410. {
  411. if (!old_path || !new_path) {
  412. errno = EFAULT;
  413. return -1;
  414. }
  415. Syscall::SC_link_params params { { old_path, strlen(old_path) }, { new_path, strlen(new_path) } };
  416. int rc = syscall(SC_link, &params);
  417. __RETURN_WITH_ERRNO(rc, rc, -1);
  418. }
  419. int unlink(const char* pathname)
  420. {
  421. int rc = syscall(SC_unlink, pathname, strlen(pathname));
  422. __RETURN_WITH_ERRNO(rc, rc, -1);
  423. }
  424. int symlink(const char* target, const char* linkpath)
  425. {
  426. if (!target || !linkpath) {
  427. errno = EFAULT;
  428. return -1;
  429. }
  430. Syscall::SC_symlink_params params { { target, strlen(target) }, { linkpath, strlen(linkpath) } };
  431. int rc = syscall(SC_symlink, &params);
  432. __RETURN_WITH_ERRNO(rc, rc, -1);
  433. }
  434. int rmdir(const char* pathname)
  435. {
  436. if (!pathname) {
  437. errno = EFAULT;
  438. return -1;
  439. }
  440. int rc = syscall(SC_rmdir, pathname, strlen(pathname));
  441. __RETURN_WITH_ERRNO(rc, rc, -1);
  442. }
  443. int isatty(int fd)
  444. {
  445. return fcntl(fd, F_ISTTY);
  446. }
  447. int dup(int old_fd)
  448. {
  449. return fcntl(old_fd, F_DUPFD, 0);
  450. }
  451. int dup2(int old_fd, int new_fd)
  452. {
  453. int rc = syscall(SC_dup2, old_fd, new_fd);
  454. __RETURN_WITH_ERRNO(rc, rc, -1);
  455. }
  456. int setgroups(size_t size, const gid_t* list)
  457. {
  458. int rc = syscall(SC_setgroups, size, list);
  459. __RETURN_WITH_ERRNO(rc, rc, -1);
  460. }
  461. int getgroups(int size, gid_t list[])
  462. {
  463. int rc = syscall(SC_getgroups, size, list);
  464. __RETURN_WITH_ERRNO(rc, rc, -1);
  465. }
  466. int pipe(int pipefd[2])
  467. {
  468. return pipe2(pipefd, 0);
  469. }
  470. int pipe2(int pipefd[2], int flags)
  471. {
  472. int rc = syscall(SC_pipe, pipefd, flags);
  473. __RETURN_WITH_ERRNO(rc, rc, -1);
  474. }
  475. unsigned int alarm(unsigned int seconds)
  476. {
  477. return syscall(SC_alarm, seconds);
  478. }
  479. int seteuid(uid_t euid)
  480. {
  481. int rc = syscall(SC_seteuid, euid);
  482. __RETURN_WITH_ERRNO(rc, rc, -1);
  483. }
  484. int setegid(gid_t egid)
  485. {
  486. int rc = syscall(SC_setegid, egid);
  487. __RETURN_WITH_ERRNO(rc, rc, -1);
  488. }
  489. int setuid(uid_t uid)
  490. {
  491. int rc = syscall(SC_setuid, uid);
  492. __RETURN_WITH_ERRNO(rc, rc, -1);
  493. }
  494. int setgid(gid_t gid)
  495. {
  496. int rc = syscall(SC_setgid, gid);
  497. __RETURN_WITH_ERRNO(rc, rc, -1);
  498. }
  499. int setreuid(uid_t ruid, uid_t euid)
  500. {
  501. int rc = syscall(SC_setreuid, ruid, euid);
  502. __RETURN_WITH_ERRNO(rc, rc, -1);
  503. }
  504. int setresuid(uid_t ruid, uid_t euid, uid_t suid)
  505. {
  506. int rc = syscall(SC_setresuid, ruid, euid, suid);
  507. __RETURN_WITH_ERRNO(rc, rc, -1);
  508. }
  509. int setresgid(gid_t rgid, gid_t egid, gid_t sgid)
  510. {
  511. int rc = syscall(SC_setresgid, rgid, egid, sgid);
  512. __RETURN_WITH_ERRNO(rc, rc, -1);
  513. }
  514. int access(const char* pathname, int mode)
  515. {
  516. if (!pathname) {
  517. errno = EFAULT;
  518. return -1;
  519. }
  520. int rc = syscall(SC_access, pathname, strlen(pathname), mode);
  521. __RETURN_WITH_ERRNO(rc, rc, -1);
  522. }
  523. int mknod(const char* pathname, mode_t mode, dev_t dev)
  524. {
  525. if (!pathname) {
  526. errno = EFAULT;
  527. return -1;
  528. }
  529. Syscall::SC_mknod_params params { { pathname, strlen(pathname) }, mode, dev };
  530. int rc = syscall(SC_mknod, &params);
  531. __RETURN_WITH_ERRNO(rc, rc, -1);
  532. }
  533. long fpathconf([[maybe_unused]] int fd, int name)
  534. {
  535. switch (name) {
  536. case _PC_NAME_MAX:
  537. return NAME_MAX;
  538. case _PC_PATH_MAX:
  539. return PATH_MAX;
  540. case _PC_VDISABLE:
  541. return _POSIX_VDISABLE;
  542. case _PC_LINK_MAX:
  543. return LINK_MAX;
  544. }
  545. VERIFY_NOT_REACHED();
  546. }
  547. long pathconf([[maybe_unused]] const char* path, int name)
  548. {
  549. switch (name) {
  550. case _PC_NAME_MAX:
  551. return NAME_MAX;
  552. case _PC_PATH_MAX:
  553. return PATH_MAX;
  554. case _PC_PIPE_BUF:
  555. return PIPE_BUF;
  556. case _PC_LINK_MAX:
  557. return LINK_MAX;
  558. }
  559. VERIFY_NOT_REACHED();
  560. }
  561. void _exit(int status)
  562. {
  563. syscall(SC_exit, status);
  564. VERIFY_NOT_REACHED();
  565. }
  566. void sync()
  567. {
  568. syscall(SC_sync);
  569. }
  570. static String getlogin_buffer;
  571. char* getlogin()
  572. {
  573. if (getlogin_buffer.is_null()) {
  574. if (auto* passwd = getpwuid(getuid())) {
  575. getlogin_buffer = String(passwd->pw_name);
  576. }
  577. endpwent();
  578. }
  579. return const_cast<char*>(getlogin_buffer.characters());
  580. }
  581. int ftruncate(int fd, off_t length)
  582. {
  583. int rc = syscall(SC_ftruncate, fd, &length);
  584. __RETURN_WITH_ERRNO(rc, rc, -1);
  585. }
  586. int truncate(const char* path, off_t length)
  587. {
  588. int fd = open(path, O_RDWR | O_CREAT, 0666);
  589. if (fd < 0)
  590. return fd;
  591. int rc = ftruncate(fd, length);
  592. int saved_errno = errno;
  593. if (int close_rc = close(fd); close_rc < 0)
  594. return close_rc;
  595. errno = saved_errno;
  596. return rc;
  597. }
  598. int gettid()
  599. {
  600. int cached_tid = s_cached_tid;
  601. if (!cached_tid) {
  602. cached_tid = syscall(SC_gettid);
  603. s_cached_tid = cached_tid;
  604. }
  605. return cached_tid;
  606. }
  607. void sysbeep()
  608. {
  609. syscall(SC_beep);
  610. }
  611. int fsync([[maybe_unused]] int fd)
  612. {
  613. dbgln("FIXME: Implement fsync()");
  614. return 0;
  615. }
  616. int halt()
  617. {
  618. int rc = syscall(SC_halt);
  619. __RETURN_WITH_ERRNO(rc, rc, -1);
  620. }
  621. int reboot()
  622. {
  623. int rc = syscall(SC_reboot);
  624. __RETURN_WITH_ERRNO(rc, rc, -1);
  625. }
  626. int mount(int source_fd, const char* target, const char* fs_type, int flags)
  627. {
  628. if (!target || !fs_type) {
  629. errno = EFAULT;
  630. return -1;
  631. }
  632. Syscall::SC_mount_params params {
  633. source_fd,
  634. { target, strlen(target) },
  635. { fs_type, strlen(fs_type) },
  636. flags
  637. };
  638. int rc = syscall(SC_mount, &params);
  639. __RETURN_WITH_ERRNO(rc, rc, -1);
  640. }
  641. int umount(const char* mountpoint)
  642. {
  643. int rc = syscall(SC_umount, mountpoint, strlen(mountpoint));
  644. __RETURN_WITH_ERRNO(rc, rc, -1);
  645. }
  646. void dump_backtrace()
  647. {
  648. syscall(SC_dump_backtrace);
  649. }
  650. int get_process_name(char* buffer, int buffer_size)
  651. {
  652. int rc = syscall(SC_get_process_name, buffer, buffer_size);
  653. __RETURN_WITH_ERRNO(rc, rc, -1);
  654. }
  655. int set_process_name(const char* name, size_t name_length)
  656. {
  657. int rc = syscall(SC_set_process_name, name, name_length);
  658. __RETURN_WITH_ERRNO(rc, rc, -1);
  659. }
  660. int pledge(const char* promises, const char* execpromises)
  661. {
  662. Syscall::SC_pledge_params params {
  663. { promises, promises ? strlen(promises) : 0 },
  664. { execpromises, execpromises ? strlen(execpromises) : 0 }
  665. };
  666. int rc = syscall(SC_pledge, &params);
  667. __RETURN_WITH_ERRNO(rc, rc, -1);
  668. }
  669. int unveil(const char* path, const char* permissions)
  670. {
  671. Syscall::SC_unveil_params params {
  672. { path, path ? strlen(path) : 0 },
  673. { permissions, permissions ? strlen(permissions) : 0 }
  674. };
  675. int rc = syscall(SC_unveil, &params);
  676. __RETURN_WITH_ERRNO(rc, rc, -1);
  677. }
  678. char* getpass(const char* prompt)
  679. {
  680. dbgln("FIXME: getpass('{}')", prompt);
  681. TODO();
  682. }
  683. long sysconf(int name)
  684. {
  685. int rc = syscall(SC_sysconf, name);
  686. __RETURN_WITH_ERRNO(rc, rc, -1);
  687. }
  688. int getpagesize()
  689. {
  690. return PAGE_SIZE;
  691. }
  692. int pause()
  693. {
  694. return select(0, nullptr, nullptr, nullptr, nullptr);
  695. }
  696. }