unistd.cpp 18 KB

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