unistd.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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 <grp.h>
  35. #include <pwd.h>
  36. #include <stdarg.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <sys/ioctl.h>
  41. #include <sys/mman.h>
  42. #include <sys/types.h>
  43. #include <termios.h>
  44. #include <unistd.h>
  45. extern "C" {
  46. static __thread int s_cached_tid = 0;
  47. static int s_cached_pid = 0;
  48. int chown(const char* pathname, uid_t uid, gid_t gid)
  49. {
  50. if (!pathname) {
  51. errno = EFAULT;
  52. return -1;
  53. }
  54. Syscall::SC_chown_params params { { pathname, strlen(pathname) }, uid, gid };
  55. int rc = syscall(SC_chown, &params);
  56. __RETURN_WITH_ERRNO(rc, rc, -1);
  57. }
  58. int fchown(int fd, uid_t uid, gid_t gid)
  59. {
  60. int rc = syscall(SC_fchown, fd, uid, gid);
  61. __RETURN_WITH_ERRNO(rc, rc, -1);
  62. }
  63. pid_t fork()
  64. {
  65. int rc = syscall(SC_fork);
  66. if (rc == 0) {
  67. s_cached_tid = 0;
  68. s_cached_pid = 0;
  69. }
  70. __RETURN_WITH_ERRNO(rc, rc, -1);
  71. }
  72. int execv(const char* path, char* const argv[])
  73. {
  74. return execve(path, argv, environ);
  75. }
  76. int execve(const char* filename, char* const argv[], char* const envp[])
  77. {
  78. size_t arg_count = 0;
  79. for (size_t i = 0; argv[i]; ++i)
  80. ++arg_count;
  81. size_t env_count = 0;
  82. for (size_t i = 0; envp[i]; ++i)
  83. ++env_count;
  84. auto copy_strings = [&](auto& vec, size_t count, auto& output) {
  85. output.length = count;
  86. for (size_t i = 0; vec[i]; ++i) {
  87. output.strings[i].characters = vec[i];
  88. output.strings[i].length = strlen(vec[i]);
  89. }
  90. };
  91. Syscall::SC_execve_params params;
  92. params.arguments.strings = (Syscall::StringArgument*)alloca(arg_count * sizeof(Syscall::StringArgument));
  93. params.environment.strings = (Syscall::StringArgument*)alloca(env_count * sizeof(Syscall::StringArgument));
  94. params.path = { filename, strlen(filename) };
  95. copy_strings(argv, arg_count, params.arguments);
  96. copy_strings(envp, env_count, params.environment);
  97. int rc = syscall(SC_execve, &params);
  98. __RETURN_WITH_ERRNO(rc, rc, -1);
  99. }
  100. int execvpe(const char* filename, char* const argv[], char* const envp[])
  101. {
  102. if (strchr(filename, '/'))
  103. return execve(filename, argv, envp);
  104. ScopedValueRollback errno_rollback(errno);
  105. String path = getenv("PATH");
  106. if (path.is_empty())
  107. path = "/bin:/usr/bin";
  108. auto parts = path.split(':');
  109. for (auto& part : parts) {
  110. auto candidate = String::format("%s/%s", part.characters(), filename);
  111. int rc = execve(candidate.characters(), argv, envp);
  112. if (rc < 0 && errno != ENOENT) {
  113. errno_rollback.set_override_rollback_value(errno);
  114. dbg() << "execvpe() failed on attempt (" << candidate << ") with " << strerror(errno);
  115. return rc;
  116. }
  117. }
  118. errno_rollback.set_override_rollback_value(ENOENT);
  119. dbg() << "execvpe() leaving :(";
  120. return -1;
  121. }
  122. int execvp(const char* filename, char* const argv[])
  123. {
  124. int rc = execvpe(filename, argv, environ);
  125. int saved_errno = errno;
  126. dbg() << "execvp() about to return " << rc << " with errno=" << saved_errno;
  127. errno = saved_errno;
  128. return rc;
  129. }
  130. int execl(const char* filename, const char* arg0, ...)
  131. {
  132. Vector<const char*, 16> args;
  133. args.append(arg0);
  134. va_list ap;
  135. va_start(ap, arg0);
  136. for (;;) {
  137. const char* arg = va_arg(ap, const char*);
  138. if (!arg)
  139. break;
  140. args.append(arg);
  141. }
  142. va_end(ap);
  143. args.append(nullptr);
  144. return execve(filename, const_cast<char* const*>(args.data()), environ);
  145. }
  146. int execlp(const char* filename, const char* arg0, ...)
  147. {
  148. Vector<const char*, 16> args;
  149. args.append(arg0);
  150. va_list ap;
  151. va_start(ap, arg0);
  152. for (;;) {
  153. const char* arg = va_arg(ap, const char*);
  154. if (!arg)
  155. break;
  156. args.append(arg);
  157. }
  158. va_end(ap);
  159. args.append(nullptr);
  160. return execvpe(filename, const_cast<char* const*>(args.data()), environ);
  161. }
  162. uid_t geteuid()
  163. {
  164. return syscall(SC_geteuid);
  165. }
  166. gid_t getegid()
  167. {
  168. return syscall(SC_getegid);
  169. }
  170. uid_t getuid()
  171. {
  172. return syscall(SC_getuid);
  173. }
  174. gid_t getgid()
  175. {
  176. return syscall(SC_getgid);
  177. }
  178. pid_t getpid()
  179. {
  180. int cached_pid = s_cached_pid;
  181. if (!cached_pid) {
  182. cached_pid = syscall(SC_getpid);
  183. s_cached_pid = cached_pid;
  184. }
  185. return cached_pid;
  186. }
  187. pid_t getppid()
  188. {
  189. return syscall(SC_getppid);
  190. }
  191. int getresuid(uid_t* ruid, uid_t* euid, uid_t* suid)
  192. {
  193. return syscall(SC_getresuid, ruid, euid, suid);
  194. }
  195. int getresgid(gid_t* rgid, gid_t* egid, gid_t* sgid)
  196. {
  197. return syscall(SC_getresgid, rgid, egid, sgid);
  198. }
  199. pid_t getsid(pid_t pid)
  200. {
  201. int rc = syscall(SC_getsid, pid);
  202. __RETURN_WITH_ERRNO(rc, rc, -1);
  203. }
  204. pid_t setsid()
  205. {
  206. int rc = syscall(SC_setsid);
  207. __RETURN_WITH_ERRNO(rc, rc, -1);
  208. }
  209. pid_t tcgetpgrp(int fd)
  210. {
  211. return ioctl(fd, TIOCGPGRP);
  212. }
  213. int tcsetpgrp(int fd, pid_t pgid)
  214. {
  215. return ioctl(fd, TIOCSPGRP, pgid);
  216. }
  217. int setpgid(pid_t pid, pid_t pgid)
  218. {
  219. int rc = syscall(SC_setpgid, pid, pgid);
  220. __RETURN_WITH_ERRNO(rc, rc, -1);
  221. }
  222. pid_t getpgid(pid_t pid)
  223. {
  224. int rc = syscall(SC_getpgid, pid);
  225. __RETURN_WITH_ERRNO(rc, rc, -1);
  226. }
  227. pid_t getpgrp()
  228. {
  229. int rc = syscall(SC_getpgrp);
  230. __RETURN_WITH_ERRNO(rc, rc, -1);
  231. }
  232. ssize_t read(int fd, void* buf, size_t count)
  233. {
  234. int rc = syscall(SC_read, fd, buf, count);
  235. __RETURN_WITH_ERRNO(rc, rc, -1);
  236. }
  237. ssize_t write(int fd, const void* buf, size_t count)
  238. {
  239. int rc = syscall(SC_write, fd, buf, count);
  240. __RETURN_WITH_ERRNO(rc, rc, -1);
  241. }
  242. int ttyname_r(int fd, char* buffer, size_t size)
  243. {
  244. int rc = syscall(SC_ttyname_r, fd, buffer, size);
  245. __RETURN_WITH_ERRNO(rc, rc, -1);
  246. }
  247. static char ttyname_buf[32];
  248. char* ttyname(int fd)
  249. {
  250. if (ttyname_r(fd, ttyname_buf, sizeof(ttyname_buf)) < 0)
  251. return nullptr;
  252. return ttyname_buf;
  253. }
  254. int close(int fd)
  255. {
  256. int rc = syscall(SC_close, fd);
  257. __RETURN_WITH_ERRNO(rc, rc, -1);
  258. }
  259. static int do_stat(const char* path, struct stat* statbuf, bool follow_symlinks)
  260. {
  261. if (!path) {
  262. errno = EFAULT;
  263. return -1;
  264. }
  265. Syscall::SC_stat_params params { { path, strlen(path) }, statbuf, follow_symlinks };
  266. int rc = syscall(SC_stat, &params);
  267. __RETURN_WITH_ERRNO(rc, rc, -1);
  268. }
  269. int lstat(const char* path, struct stat* statbuf)
  270. {
  271. return do_stat(path, statbuf, false);
  272. }
  273. int stat(const char* path, struct stat* statbuf)
  274. {
  275. return do_stat(path, statbuf, true);
  276. }
  277. int fstat(int fd, struct stat* statbuf)
  278. {
  279. int rc = syscall(SC_fstat, fd, statbuf);
  280. __RETURN_WITH_ERRNO(rc, rc, -1);
  281. }
  282. int chdir(const char* path)
  283. {
  284. if (!path) {
  285. errno = EFAULT;
  286. return -1;
  287. }
  288. int rc = syscall(SC_chdir, path, strlen(path));
  289. __RETURN_WITH_ERRNO(rc, rc, -1);
  290. }
  291. int fchdir(int fd)
  292. {
  293. int rc = syscall(SC_fchdir, fd);
  294. __RETURN_WITH_ERRNO(rc, rc, -1);
  295. }
  296. char* getcwd(char* buffer, size_t size)
  297. {
  298. if (!buffer) {
  299. size = size ? size : PATH_MAX;
  300. buffer = (char*)malloc(size);
  301. }
  302. int rc = syscall(SC_getcwd, buffer, size);
  303. __RETURN_WITH_ERRNO(rc, buffer, nullptr);
  304. }
  305. char* getwd(char* buf)
  306. {
  307. auto* p = getcwd(buf, PATH_MAX);
  308. return p;
  309. }
  310. int sleep(unsigned seconds)
  311. {
  312. return syscall(SC_sleep, seconds);
  313. }
  314. int usleep(useconds_t usec)
  315. {
  316. return syscall(SC_usleep, usec);
  317. }
  318. int gethostname(char* buffer, size_t size)
  319. {
  320. int rc = syscall(SC_gethostname, buffer, size);
  321. __RETURN_WITH_ERRNO(rc, rc, -1);
  322. }
  323. int sethostname(const char* hostname, ssize_t size)
  324. {
  325. int rc = syscall(SC_sethostname, hostname, size);
  326. __RETURN_WITH_ERRNO(rc, rc, -1);
  327. }
  328. ssize_t readlink(const char* path, char* buffer, size_t size)
  329. {
  330. Syscall::SC_readlink_params params { { path, strlen(path) }, { buffer, size } };
  331. int rc = syscall(SC_readlink, &params);
  332. // Return the number of bytes placed in the buffer, not the full path size.
  333. __RETURN_WITH_ERRNO(rc, min((size_t)rc, size), -1);
  334. }
  335. off_t lseek(int fd, off_t offset, int whence)
  336. {
  337. int rc = syscall(SC_lseek, fd, offset, whence);
  338. __RETURN_WITH_ERRNO(rc, rc, -1);
  339. }
  340. int link(const char* old_path, const char* new_path)
  341. {
  342. if (!old_path || !new_path) {
  343. errno = EFAULT;
  344. return -1;
  345. }
  346. Syscall::SC_link_params params { { old_path, strlen(old_path) }, { new_path, strlen(new_path) } };
  347. int rc = syscall(SC_link, &params);
  348. __RETURN_WITH_ERRNO(rc, rc, -1);
  349. }
  350. int unlink(const char* pathname)
  351. {
  352. int rc = syscall(SC_unlink, pathname, strlen(pathname));
  353. __RETURN_WITH_ERRNO(rc, rc, -1);
  354. }
  355. int symlink(const char* target, const char* linkpath)
  356. {
  357. if (!target || !linkpath) {
  358. errno = EFAULT;
  359. return -1;
  360. }
  361. Syscall::SC_symlink_params params { { target, strlen(target) }, { linkpath, strlen(linkpath) } };
  362. int rc = syscall(SC_symlink, &params);
  363. __RETURN_WITH_ERRNO(rc, rc, -1);
  364. }
  365. int rmdir(const char* pathname)
  366. {
  367. if (!pathname) {
  368. errno = EFAULT;
  369. return -1;
  370. }
  371. int rc = syscall(SC_rmdir, pathname, strlen(pathname));
  372. __RETURN_WITH_ERRNO(rc, rc, -1);
  373. }
  374. int isatty(int fd)
  375. {
  376. return fcntl(fd, F_ISTTY);
  377. }
  378. int dup(int old_fd)
  379. {
  380. int rc = syscall(SC_dup, old_fd);
  381. __RETURN_WITH_ERRNO(rc, rc, -1);
  382. }
  383. int dup2(int old_fd, int new_fd)
  384. {
  385. int rc = syscall(SC_dup2, old_fd, new_fd);
  386. __RETURN_WITH_ERRNO(rc, rc, -1);
  387. }
  388. int setgroups(size_t size, const gid_t* list)
  389. {
  390. int rc = syscall(SC_setgroups, size, list);
  391. __RETURN_WITH_ERRNO(rc, rc, -1);
  392. }
  393. int getgroups(int size, gid_t list[])
  394. {
  395. int rc = syscall(SC_getgroups, size, list);
  396. __RETURN_WITH_ERRNO(rc, rc, -1);
  397. }
  398. int pipe(int pipefd[2])
  399. {
  400. return pipe2(pipefd, 0);
  401. }
  402. int pipe2(int pipefd[2], int flags)
  403. {
  404. int rc = syscall(SC_pipe, pipefd, flags);
  405. __RETURN_WITH_ERRNO(rc, rc, -1);
  406. }
  407. unsigned int alarm(unsigned int seconds)
  408. {
  409. return syscall(SC_alarm, seconds);
  410. }
  411. int seteuid(uid_t euid)
  412. {
  413. int rc = syscall(SC_seteuid, euid);
  414. __RETURN_WITH_ERRNO(rc, rc, -1);
  415. }
  416. int setegid(gid_t egid)
  417. {
  418. int rc = syscall(SC_setegid, egid);
  419. __RETURN_WITH_ERRNO(rc, rc, -1);
  420. }
  421. int setuid(uid_t uid)
  422. {
  423. int rc = syscall(SC_setuid, uid);
  424. __RETURN_WITH_ERRNO(rc, rc, -1);
  425. }
  426. int setgid(gid_t gid)
  427. {
  428. int rc = syscall(SC_setgid, gid);
  429. __RETURN_WITH_ERRNO(rc, rc, -1);
  430. }
  431. int setresuid(uid_t ruid, uid_t euid, uid_t suid)
  432. {
  433. int rc = syscall(SC_setresuid, ruid, euid, suid);
  434. __RETURN_WITH_ERRNO(rc, rc, -1);
  435. }
  436. int setresgid(gid_t rgid, gid_t egid, gid_t sgid)
  437. {
  438. int rc = syscall(SC_setresgid, rgid, egid, sgid);
  439. __RETURN_WITH_ERRNO(rc, rc, -1);
  440. }
  441. int access(const char* pathname, int mode)
  442. {
  443. if (!pathname) {
  444. errno = EFAULT;
  445. return -1;
  446. }
  447. int rc = syscall(SC_access, pathname, strlen(pathname), mode);
  448. __RETURN_WITH_ERRNO(rc, rc, -1);
  449. }
  450. int mknod(const char* pathname, mode_t mode, dev_t dev)
  451. {
  452. if (!pathname) {
  453. errno = EFAULT;
  454. return -1;
  455. }
  456. Syscall::SC_mknod_params params { { pathname, strlen(pathname) }, mode, dev };
  457. int rc = syscall(SC_mknod, &params);
  458. __RETURN_WITH_ERRNO(rc, rc, -1);
  459. }
  460. long fpathconf(int fd, int name)
  461. {
  462. (void)fd;
  463. (void)name;
  464. switch (name) {
  465. case _PC_PATH_MAX:
  466. return PATH_MAX;
  467. case _PC_VDISABLE:
  468. return _POSIX_VDISABLE;
  469. }
  470. ASSERT_NOT_REACHED();
  471. }
  472. long pathconf(const char* path, int name)
  473. {
  474. (void)path;
  475. switch (name) {
  476. case _PC_PATH_MAX:
  477. return PATH_MAX;
  478. case _PC_PIPE_BUF:
  479. return PIPE_BUF;
  480. }
  481. ASSERT_NOT_REACHED();
  482. }
  483. void _exit(int status)
  484. {
  485. syscall(SC_exit, status);
  486. ASSERT_NOT_REACHED();
  487. }
  488. void sync()
  489. {
  490. syscall(SC_sync);
  491. }
  492. int set_process_icon(int icon_id)
  493. {
  494. int rc = syscall(SC_set_process_icon, icon_id);
  495. __RETURN_WITH_ERRNO(rc, rc, -1);
  496. }
  497. char* getlogin()
  498. {
  499. static char __getlogin_buffer[256];
  500. if (auto* passwd = getpwuid(getuid())) {
  501. strncpy(__getlogin_buffer, passwd->pw_name, sizeof(__getlogin_buffer) - 1);
  502. endpwent();
  503. return __getlogin_buffer;
  504. }
  505. endpwent();
  506. return nullptr;
  507. }
  508. int ftruncate(int fd, off_t length)
  509. {
  510. int rc = syscall(SC_ftruncate, fd, length);
  511. __RETURN_WITH_ERRNO(rc, rc, -1);
  512. }
  513. int truncate(const char* path, off_t length)
  514. {
  515. int fd = open(path, O_RDWR | O_CREAT, 0666);
  516. if (fd < 0)
  517. return fd;
  518. int rc = ftruncate(fd, length);
  519. int saved_errno = errno;
  520. if (int close_rc = close(fd); close_rc < 0)
  521. return close_rc;
  522. errno = saved_errno;
  523. return rc;
  524. }
  525. int gettid()
  526. {
  527. int cached_tid = s_cached_tid;
  528. if (!cached_tid) {
  529. cached_tid = syscall(SC_gettid);
  530. s_cached_tid = cached_tid;
  531. }
  532. return cached_tid;
  533. }
  534. int donate(int tid)
  535. {
  536. int rc = syscall(SC_donate, tid);
  537. __RETURN_WITH_ERRNO(rc, rc, -1);
  538. }
  539. void sysbeep()
  540. {
  541. syscall(SC_beep);
  542. }
  543. int fsync(int fd)
  544. {
  545. UNUSED_PARAM(fd);
  546. dbgprintf("FIXME: Implement fsync()\n");
  547. return 0;
  548. }
  549. int halt()
  550. {
  551. int rc = syscall(SC_halt);
  552. __RETURN_WITH_ERRNO(rc, rc, -1);
  553. }
  554. int reboot()
  555. {
  556. int rc = syscall(SC_reboot);
  557. __RETURN_WITH_ERRNO(rc, rc, -1);
  558. }
  559. int mount(int source_fd, const char* target, const char* fs_type, int flags)
  560. {
  561. if (!target || !fs_type) {
  562. errno = EFAULT;
  563. return -1;
  564. }
  565. Syscall::SC_mount_params params {
  566. source_fd,
  567. { target, strlen(target) },
  568. { fs_type, strlen(fs_type) },
  569. flags
  570. };
  571. int rc = syscall(SC_mount, &params);
  572. __RETURN_WITH_ERRNO(rc, rc, -1);
  573. }
  574. int umount(const char* mountpoint)
  575. {
  576. int rc = syscall(SC_umount, mountpoint, strlen(mountpoint));
  577. __RETURN_WITH_ERRNO(rc, rc, -1);
  578. }
  579. void dump_backtrace()
  580. {
  581. syscall(SC_dump_backtrace);
  582. }
  583. int get_process_name(char* buffer, int buffer_size)
  584. {
  585. int rc = syscall(SC_get_process_name, buffer, buffer_size);
  586. __RETURN_WITH_ERRNO(rc, rc, -1);
  587. }
  588. int set_process_name(const char* name, size_t name_length)
  589. {
  590. int rc = syscall(SC_set_process_name, name, name_length);
  591. __RETURN_WITH_ERRNO(rc, rc, -1);
  592. }
  593. int chroot(const char* path)
  594. {
  595. return chroot_with_mount_flags(path, -1);
  596. }
  597. int chroot_with_mount_flags(const char* path, int mount_flags)
  598. {
  599. if (!path) {
  600. errno = EFAULT;
  601. return -1;
  602. }
  603. int rc = syscall(SC_chroot, path, strlen(path), mount_flags);
  604. __RETURN_WITH_ERRNO(rc, rc, -1);
  605. }
  606. int pledge(const char* promises, const char* execpromises)
  607. {
  608. Syscall::SC_pledge_params params {
  609. { promises, promises ? strlen(promises) : 0 },
  610. { execpromises, execpromises ? strlen(execpromises) : 0 }
  611. };
  612. int rc = syscall(SC_pledge, &params);
  613. __RETURN_WITH_ERRNO(rc, rc, -1);
  614. }
  615. int unveil(const char* path, const char* permissions)
  616. {
  617. Syscall::SC_unveil_params params {
  618. { path, path ? strlen(path) : 0 },
  619. { permissions, permissions ? strlen(permissions) : 0 }
  620. };
  621. int rc = syscall(SC_unveil, &params);
  622. __RETURN_WITH_ERRNO(rc, rc, -1);
  623. }
  624. ssize_t pread(int fd, void* buf, size_t count, off_t offset)
  625. {
  626. // FIXME: This is not thread safe and should be implemented in the kernel instead.
  627. off_t old_offset = lseek(fd, 0, SEEK_CUR);
  628. lseek(fd, offset, SEEK_SET);
  629. ssize_t nread = read(fd, buf, count);
  630. lseek(fd, old_offset, SEEK_SET);
  631. return nread;
  632. }
  633. char* getpass(const char* prompt)
  634. {
  635. dbg() << "FIXME: getpass(\"" << prompt << "\")";
  636. ASSERT_NOT_REACHED();
  637. }
  638. long sysconf(int name)
  639. {
  640. int rc = syscall(SC_sysconf, name);
  641. __RETURN_WITH_ERRNO(rc, rc, -1);
  642. }
  643. int getpagesize()
  644. {
  645. return PAGE_SIZE;
  646. }
  647. }