unistd.cpp 16 KB

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