unistd.cpp 18 KB

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