unistd.cpp 18 KB

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