unistd.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ScopeGuard.h>
  7. #include <AK/ScopedValueRollback.h>
  8. #include <AK/String.h>
  9. #include <AK/Vector.h>
  10. #include <alloca.h>
  11. #include <assert.h>
  12. #include <bits/pthread_integration.h>
  13. #include <dirent.h>
  14. #include <errno.h>
  15. #include <fcntl.h>
  16. #include <getopt.h>
  17. #include <grp.h>
  18. #include <pwd.h>
  19. #include <stdarg.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/mman.h>
  25. #include <sys/resource.h>
  26. #include <sys/select.h>
  27. #include <sys/stat.h>
  28. #include <sys/sysmacros.h>
  29. #include <sys/types.h>
  30. #include <syscall.h>
  31. #include <termios.h>
  32. #include <time.h>
  33. #include <unistd.h>
  34. extern "C" {
  35. #ifdef NO_TLS
  36. static int s_cached_tid = 0;
  37. #else
  38. static __thread int s_cached_tid = 0;
  39. #endif
  40. static int s_cached_pid = 0;
  41. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/lchown.html
  42. int lchown(char const* pathname, uid_t uid, gid_t gid)
  43. {
  44. if (!pathname) {
  45. errno = EFAULT;
  46. return -1;
  47. }
  48. Syscall::SC_chown_params params { { pathname, strlen(pathname) }, uid, gid, AT_FDCWD, false };
  49. int rc = syscall(SC_chown, &params);
  50. __RETURN_WITH_ERRNO(rc, rc, -1);
  51. }
  52. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/chown.html
  53. int chown(char const* pathname, uid_t uid, gid_t gid)
  54. {
  55. if (!pathname) {
  56. errno = EFAULT;
  57. return -1;
  58. }
  59. Syscall::SC_chown_params params { { pathname, strlen(pathname) }, uid, gid, AT_FDCWD, true };
  60. int rc = syscall(SC_chown, &params);
  61. __RETURN_WITH_ERRNO(rc, rc, -1);
  62. }
  63. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchown.html
  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. int fchownat(int fd, char const* pathname, uid_t uid, gid_t gid, int flags)
  70. {
  71. if (!pathname) {
  72. errno = EFAULT;
  73. return -1;
  74. }
  75. Syscall::SC_chown_params params { { pathname, strlen(pathname) }, uid, gid, fd, !(flags & AT_SYMLINK_NOFOLLOW) };
  76. int rc = syscall(SC_chown, &params);
  77. __RETURN_WITH_ERRNO(rc, rc, -1);
  78. }
  79. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html
  80. pid_t fork()
  81. {
  82. __pthread_fork_prepare();
  83. int rc = syscall(SC_fork);
  84. if (rc == 0) {
  85. s_cached_tid = 0;
  86. s_cached_pid = 0;
  87. __pthread_fork_child();
  88. } else if (rc != -1) {
  89. __pthread_fork_parent();
  90. }
  91. __RETURN_WITH_ERRNO(rc, rc, -1);
  92. }
  93. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/vfork.html
  94. pid_t vfork()
  95. {
  96. return fork();
  97. }
  98. // Non-POSIX, but present in BSDs and Linux
  99. // https://man.openbsd.org/daemon.3
  100. int daemon(int nochdir, int noclose)
  101. {
  102. pid_t pid = fork();
  103. if (pid < 0)
  104. return -1;
  105. // exit parent, continue execution in child
  106. if (pid > 0)
  107. _exit(0);
  108. pid = setsid();
  109. if (pid < 0)
  110. return -1;
  111. if (nochdir == 0)
  112. (void)chdir("/");
  113. if (noclose == 0) {
  114. // redirect stdout and stderr to /dev/null
  115. int fd = open("/dev/null", O_WRONLY);
  116. if (fd < 0)
  117. return -1;
  118. (void)close(STDOUT_FILENO);
  119. (void)close(STDERR_FILENO);
  120. (void)dup2(fd, STDOUT_FILENO);
  121. (void)dup2(fd, STDERR_FILENO);
  122. (void)close(fd);
  123. }
  124. return 0;
  125. }
  126. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/execv.html
  127. int execv(char const* path, char* const argv[])
  128. {
  129. return execve(path, argv, environ);
  130. }
  131. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/execve.html
  132. int execve(char const* filename, char* const argv[], char* const envp[])
  133. {
  134. size_t arg_count = 0;
  135. for (size_t i = 0; argv[i]; ++i)
  136. ++arg_count;
  137. size_t env_count = 0;
  138. for (size_t i = 0; envp[i]; ++i)
  139. ++env_count;
  140. auto copy_strings = [&](auto& vec, size_t count, auto& output) {
  141. output.length = count;
  142. for (size_t i = 0; vec[i]; ++i) {
  143. output.strings[i].characters = vec[i];
  144. output.strings[i].length = strlen(vec[i]);
  145. }
  146. };
  147. Syscall::SC_execve_params params;
  148. params.arguments.strings = (Syscall::StringArgument*)alloca(arg_count * sizeof(Syscall::StringArgument));
  149. params.environment.strings = (Syscall::StringArgument*)alloca(env_count * sizeof(Syscall::StringArgument));
  150. params.path = { filename, strlen(filename) };
  151. copy_strings(argv, arg_count, params.arguments);
  152. copy_strings(envp, env_count, params.environment);
  153. int rc = syscall(SC_execve, &params);
  154. __RETURN_WITH_ERRNO(rc, rc, -1);
  155. }
  156. int execvpe(char const* filename, char* const argv[], char* const envp[])
  157. {
  158. if (strchr(filename, '/'))
  159. return execve(filename, argv, envp);
  160. ScopedValueRollback errno_rollback(errno);
  161. String path = getenv("PATH");
  162. if (path.is_empty())
  163. path = "/bin:/usr/bin";
  164. auto parts = path.split(':');
  165. for (auto& part : parts) {
  166. auto candidate = String::formatted("{}/{}", part, filename);
  167. int rc = execve(candidate.characters(), argv, envp);
  168. if (rc < 0 && errno != ENOENT) {
  169. errno_rollback.set_override_rollback_value(errno);
  170. dbgln("execvpe() failed on attempt ({}) with {}", candidate, strerror(errno));
  171. return rc;
  172. }
  173. }
  174. errno_rollback.set_override_rollback_value(ENOENT);
  175. dbgln("execvpe() leaving :(");
  176. return -1;
  177. }
  178. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/execvp.html
  179. int execvp(char const* filename, char* const argv[])
  180. {
  181. int rc = execvpe(filename, argv, environ);
  182. int saved_errno = errno;
  183. dbgln("execvp({}, ...) about to return {} with errno={}", filename, rc, saved_errno);
  184. errno = saved_errno;
  185. return rc;
  186. }
  187. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/execl.html
  188. int execl(char const* filename, char const* arg0, ...)
  189. {
  190. Vector<char const*, 16> args;
  191. args.append(arg0);
  192. va_list ap;
  193. va_start(ap, arg0);
  194. for (;;) {
  195. char const* arg = va_arg(ap, char const*);
  196. if (!arg)
  197. break;
  198. args.append(arg);
  199. }
  200. va_end(ap);
  201. args.append(nullptr);
  202. return execve(filename, const_cast<char* const*>(args.data()), environ);
  203. }
  204. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/execle.html
  205. int execle(char const* filename, char const* arg0, ...)
  206. {
  207. Vector<char const*> args;
  208. args.append(arg0);
  209. va_list ap;
  210. va_start(ap, arg0);
  211. char const* arg;
  212. do {
  213. arg = va_arg(ap, char const*);
  214. args.append(arg);
  215. } while (arg);
  216. auto argv = const_cast<char* const*>(args.data());
  217. auto envp = const_cast<char* const*>(va_arg(ap, char* const*));
  218. va_end(ap);
  219. return execve(filename, argv, envp);
  220. }
  221. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/execlp.html
  222. int execlp(char const* filename, char const* arg0, ...)
  223. {
  224. Vector<char const*, 16> args;
  225. args.append(arg0);
  226. va_list ap;
  227. va_start(ap, arg0);
  228. for (;;) {
  229. char const* arg = va_arg(ap, char const*);
  230. if (!arg)
  231. break;
  232. args.append(arg);
  233. }
  234. va_end(ap);
  235. args.append(nullptr);
  236. return execvpe(filename, const_cast<char* const*>(args.data()), environ);
  237. }
  238. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getuid.html
  239. uid_t geteuid()
  240. {
  241. return syscall(SC_geteuid);
  242. }
  243. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getegid.html
  244. gid_t getegid()
  245. {
  246. return syscall(SC_getegid);
  247. }
  248. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getuid.html
  249. uid_t getuid()
  250. {
  251. return syscall(SC_getuid);
  252. }
  253. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getgid.html
  254. gid_t getgid()
  255. {
  256. return syscall(SC_getgid);
  257. }
  258. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpid.html
  259. pid_t getpid()
  260. {
  261. int cached_pid = s_cached_pid;
  262. if (!cached_pid) {
  263. cached_pid = syscall(SC_getpid);
  264. s_cached_pid = cached_pid;
  265. }
  266. return cached_pid;
  267. }
  268. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getppid.html
  269. pid_t getppid()
  270. {
  271. return syscall(SC_getppid);
  272. }
  273. int getresuid(uid_t* ruid, uid_t* euid, uid_t* suid)
  274. {
  275. return syscall(SC_getresuid, ruid, euid, suid);
  276. }
  277. int getresgid(gid_t* rgid, gid_t* egid, gid_t* sgid)
  278. {
  279. return syscall(SC_getresgid, rgid, egid, sgid);
  280. }
  281. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsid.html
  282. pid_t getsid(pid_t pid)
  283. {
  284. int rc = syscall(SC_getsid, pid);
  285. __RETURN_WITH_ERRNO(rc, rc, -1);
  286. }
  287. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/setsid.html
  288. pid_t setsid()
  289. {
  290. int rc = syscall(SC_setsid);
  291. __RETURN_WITH_ERRNO(rc, rc, -1);
  292. }
  293. pid_t tcgetpgrp(int fd)
  294. {
  295. pid_t pgrp;
  296. int rc = ioctl(fd, TIOCGPGRP, &pgrp);
  297. if (rc < 0)
  298. return rc;
  299. return pgrp;
  300. }
  301. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcgetpgrp.html
  302. int tcsetpgrp(int fd, pid_t pgid)
  303. {
  304. return ioctl(fd, TIOCSPGRP, pgid);
  305. }
  306. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/setpgid.html
  307. int setpgid(pid_t pid, pid_t pgid)
  308. {
  309. int rc = syscall(SC_setpgid, pid, pgid);
  310. __RETURN_WITH_ERRNO(rc, rc, -1);
  311. }
  312. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpgid.html
  313. pid_t getpgid(pid_t pid)
  314. {
  315. int rc = syscall(SC_getpgid, pid);
  316. __RETURN_WITH_ERRNO(rc, rc, -1);
  317. }
  318. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpgrp.html
  319. pid_t getpgrp()
  320. {
  321. int rc = syscall(SC_getpgrp);
  322. __RETURN_WITH_ERRNO(rc, rc, -1);
  323. }
  324. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html
  325. ssize_t read(int fd, void* buf, size_t count)
  326. {
  327. int rc = syscall(SC_read, fd, buf, count);
  328. __RETURN_WITH_ERRNO(rc, rc, -1);
  329. }
  330. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pread.html
  331. ssize_t pread(int fd, void* buf, size_t count, off_t offset)
  332. {
  333. int rc = syscall(SC_pread, fd, buf, count, &offset);
  334. __RETURN_WITH_ERRNO(rc, rc, -1);
  335. }
  336. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html
  337. ssize_t write(int fd, void const* buf, size_t count)
  338. {
  339. int rc = syscall(SC_write, fd, buf, count);
  340. __RETURN_WITH_ERRNO(rc, rc, -1);
  341. }
  342. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pwrite.html
  343. ssize_t pwrite(int fd, void const* buf, size_t count, off_t offset)
  344. {
  345. // FIXME: This is not thread safe and should be implemented in the kernel instead.
  346. off_t old_offset = lseek(fd, 0, SEEK_CUR);
  347. lseek(fd, offset, SEEK_SET);
  348. ssize_t nwritten = write(fd, buf, count);
  349. lseek(fd, old_offset, SEEK_SET);
  350. return nwritten;
  351. }
  352. // Note: Be sure to send to directory_name parameter a directory name ended with trailing slash.
  353. static int ttyname_r_for_directory(char const* directory_name, dev_t device_mode, ino_t inode_number, char* buffer, size_t size)
  354. {
  355. DIR* dirstream = opendir(directory_name);
  356. if (!dirstream) {
  357. return -1;
  358. }
  359. auto close_dir_stream_on_exit = ScopeGuard([dirstream] {
  360. closedir(dirstream);
  361. });
  362. struct dirent* entry = nullptr;
  363. char* name_path = nullptr;
  364. // FIXME: Use LibCore DirIterator here instead
  365. while ((entry = readdir(dirstream)) != nullptr) {
  366. if (((ino_t)entry->d_ino == inode_number)
  367. && strcmp(entry->d_name, "stdin")
  368. && strcmp(entry->d_name, "stdout")
  369. && strcmp(entry->d_name, "stderr")) {
  370. size_t name_length = strlen(directory_name) + strlen(entry->d_name) + 1;
  371. if (name_length > size) {
  372. errno = ERANGE;
  373. return -1;
  374. }
  375. name_path = (char*)malloc(name_length);
  376. memset(name_path, 0, name_length);
  377. memcpy(name_path, directory_name, strlen(directory_name));
  378. memcpy(&name_path[strlen(directory_name)], entry->d_name, strlen(entry->d_name));
  379. struct stat st;
  380. if (lstat(name_path, &st) < 0) {
  381. free(name_path);
  382. name_path = nullptr;
  383. continue;
  384. }
  385. if (device_mode == st.st_rdev) {
  386. memset(buffer, 0, name_length);
  387. memcpy(buffer, name_path, name_length);
  388. free(name_path);
  389. return 0;
  390. }
  391. }
  392. }
  393. free(name_path);
  394. return -1;
  395. }
  396. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/ttyname_r.html
  397. int ttyname_r(int fd, char* buffer, size_t size)
  398. {
  399. struct stat stat;
  400. if (fstat(fd, &stat) < 0)
  401. return -1;
  402. dev_t major_minor_numbers = stat.st_rdev;
  403. ino_t inode_number = stat.st_ino;
  404. if (ttyname_r_for_directory("/dev/", major_minor_numbers, inode_number, buffer, size) < 0) {
  405. if (ttyname_r_for_directory("/dev/pts/", major_minor_numbers, inode_number, buffer, size) < 0) {
  406. errno = ENOTTY;
  407. return -1;
  408. }
  409. }
  410. return 0;
  411. }
  412. static char ttyname_buf[32];
  413. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/ttyname.html
  414. char* ttyname(int fd)
  415. {
  416. if (ttyname_r(fd, ttyname_buf, sizeof(ttyname_buf)) < 0)
  417. return nullptr;
  418. return ttyname_buf;
  419. }
  420. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html
  421. int close(int fd)
  422. {
  423. int rc = syscall(SC_close, fd);
  424. __RETURN_WITH_ERRNO(rc, rc, -1);
  425. }
  426. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/chdir.html
  427. int chdir(char const* path)
  428. {
  429. if (!path) {
  430. errno = EFAULT;
  431. return -1;
  432. }
  433. int rc = syscall(SC_chdir, path, strlen(path));
  434. __RETURN_WITH_ERRNO(rc, rc, -1);
  435. }
  436. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchdir.html
  437. int fchdir(int fd)
  438. {
  439. int rc = syscall(SC_fchdir, fd);
  440. __RETURN_WITH_ERRNO(rc, rc, -1);
  441. }
  442. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html
  443. char* getcwd(char* buffer, size_t size)
  444. {
  445. if (buffer && size == 0) {
  446. // POSIX requires that we set errno to EINVAL here, but in our syscall it makes sense to
  447. // allow "probing" the Kernel with a zero-sized buffer, and it does not return -EINVAL.
  448. // So we have to inject EINVAL here.
  449. errno = EINVAL;
  450. return nullptr;
  451. }
  452. bool self_allocated = false;
  453. if (!buffer) {
  454. size = size ? size : 64;
  455. buffer = (char*)malloc(size);
  456. self_allocated = true;
  457. }
  458. int rc = syscall(SC_getcwd, buffer, size);
  459. if (rc < 0) {
  460. if (self_allocated)
  461. free(buffer);
  462. errno = -rc;
  463. return nullptr;
  464. }
  465. size_t actual_size = static_cast<size_t>(rc);
  466. if (actual_size <= size) {
  467. return buffer;
  468. }
  469. // If we get here, the current directory path was silently truncated.
  470. if (!self_allocated) {
  471. // In this case, POSIX causes information loss: the caller cannot learn about the ideal
  472. // buffer size. This is the reason we went with silently truncation instead.
  473. errno = ERANGE;
  474. return nullptr;
  475. }
  476. // Try again.
  477. free(buffer);
  478. size = actual_size;
  479. buffer = (char*)malloc(size);
  480. rc = syscall(SC_getcwd, buffer, size);
  481. if (rc < 0) {
  482. // Can only happen if we lose a race. Let's pretend we lost the race in the first place.
  483. free(buffer);
  484. errno = -rc;
  485. return nullptr;
  486. }
  487. actual_size = static_cast<size_t>(rc);
  488. if (actual_size < size) {
  489. // If we're here, then cwd has become longer while we were looking at it. (Race with another thread?)
  490. // There's not much we can do, unless we want to loop endlessly
  491. // in this case. Let's leave it up to the caller whether to loop.
  492. free(buffer);
  493. errno = EAGAIN;
  494. return nullptr;
  495. }
  496. return buffer;
  497. }
  498. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getwd.html
  499. char* getwd(char* buf)
  500. {
  501. if (buf == nullptr) {
  502. errno = EINVAL;
  503. return nullptr;
  504. }
  505. auto* p = getcwd(buf, PATH_MAX);
  506. if (errno == ERANGE) {
  507. // POSIX quirk
  508. errno = ENAMETOOLONG;
  509. }
  510. return p;
  511. }
  512. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sleep.html
  513. unsigned int sleep(unsigned int seconds)
  514. {
  515. struct timespec ts = { seconds, 0 };
  516. if (clock_nanosleep(CLOCK_MONOTONIC_COARSE, 0, &ts, nullptr) < 0)
  517. return ts.tv_sec;
  518. return 0;
  519. }
  520. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/usleep.html
  521. int usleep(useconds_t usec)
  522. {
  523. struct timespec ts = { (long)(usec / 1000000), (long)(usec % 1000000) * 1000 };
  524. return clock_nanosleep(CLOCK_MONOTONIC_COARSE, 0, &ts, nullptr);
  525. }
  526. int gethostname(char* buffer, size_t size)
  527. {
  528. int rc = syscall(SC_gethostname, buffer, size);
  529. __RETURN_WITH_ERRNO(rc, rc, -1);
  530. }
  531. int sethostname(char const* hostname, ssize_t size)
  532. {
  533. int rc = syscall(SC_sethostname, hostname, size);
  534. __RETURN_WITH_ERRNO(rc, rc, -1);
  535. }
  536. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html
  537. ssize_t readlink(char const* path, char* buffer, size_t size)
  538. {
  539. Syscall::SC_readlink_params params { { path, strlen(path) }, { buffer, size } };
  540. int rc = syscall(SC_readlink, &params);
  541. // Return the number of bytes placed in the buffer, not the full path size.
  542. __RETURN_WITH_ERRNO(rc, min((size_t)rc, size), -1);
  543. }
  544. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/lseek.html
  545. off_t lseek(int fd, off_t offset, int whence)
  546. {
  547. int rc = syscall(SC_lseek, fd, &offset, whence);
  548. __RETURN_WITH_ERRNO(rc, offset, -1);
  549. }
  550. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/link.html
  551. int link(char const* old_path, char const* new_path)
  552. {
  553. if (!old_path || !new_path) {
  554. errno = EFAULT;
  555. return -1;
  556. }
  557. Syscall::SC_link_params params { { old_path, strlen(old_path) }, { new_path, strlen(new_path) } };
  558. int rc = syscall(SC_link, &params);
  559. __RETURN_WITH_ERRNO(rc, rc, -1);
  560. }
  561. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/unlink.html
  562. int unlink(char const* pathname)
  563. {
  564. int rc = syscall(SC_unlink, pathname, strlen(pathname));
  565. __RETURN_WITH_ERRNO(rc, rc, -1);
  566. }
  567. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/symlink.html
  568. int symlink(char const* target, char const* linkpath)
  569. {
  570. if (!target || !linkpath) {
  571. errno = EFAULT;
  572. return -1;
  573. }
  574. Syscall::SC_symlink_params params { { target, strlen(target) }, { linkpath, strlen(linkpath) } };
  575. int rc = syscall(SC_symlink, &params);
  576. __RETURN_WITH_ERRNO(rc, rc, -1);
  577. }
  578. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/rmdir.html
  579. int rmdir(char const* pathname)
  580. {
  581. if (!pathname) {
  582. errno = EFAULT;
  583. return -1;
  584. }
  585. int rc = syscall(SC_rmdir, pathname, strlen(pathname));
  586. __RETURN_WITH_ERRNO(rc, rc, -1);
  587. }
  588. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/isatty.html
  589. int isatty(int fd)
  590. {
  591. return fcntl(fd, F_ISTTY);
  592. }
  593. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/dup.html
  594. int dup(int old_fd)
  595. {
  596. return fcntl(old_fd, F_DUPFD, 0);
  597. }
  598. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/dup2.html
  599. int dup2(int old_fd, int new_fd)
  600. {
  601. int rc = syscall(SC_dup2, old_fd, new_fd);
  602. __RETURN_WITH_ERRNO(rc, rc, -1);
  603. }
  604. int setgroups(size_t size, gid_t const* list)
  605. {
  606. int rc = syscall(SC_setgroups, size, list);
  607. __RETURN_WITH_ERRNO(rc, rc, -1);
  608. }
  609. int getgroups(int size, gid_t list[])
  610. {
  611. int rc = syscall(SC_getgroups, size, list);
  612. __RETURN_WITH_ERRNO(rc, rc, -1);
  613. }
  614. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pipe.html
  615. int pipe(int pipefd[2])
  616. {
  617. return pipe2(pipefd, 0);
  618. }
  619. //
  620. int pipe2(int pipefd[2], int flags)
  621. {
  622. int rc = syscall(SC_pipe, pipefd, flags);
  623. __RETURN_WITH_ERRNO(rc, rc, -1);
  624. }
  625. unsigned int alarm(unsigned int seconds)
  626. {
  627. return syscall(SC_alarm, seconds);
  628. }
  629. int seteuid(uid_t euid)
  630. {
  631. int rc = syscall(SC_seteuid, euid);
  632. __RETURN_WITH_ERRNO(rc, rc, -1);
  633. }
  634. int setegid(gid_t egid)
  635. {
  636. int rc = syscall(SC_setegid, egid);
  637. __RETURN_WITH_ERRNO(rc, rc, -1);
  638. }
  639. int setuid(uid_t uid)
  640. {
  641. int rc = syscall(SC_setuid, uid);
  642. __RETURN_WITH_ERRNO(rc, rc, -1);
  643. }
  644. int setgid(gid_t gid)
  645. {
  646. int rc = syscall(SC_setgid, gid);
  647. __RETURN_WITH_ERRNO(rc, rc, -1);
  648. }
  649. int setreuid(uid_t ruid, uid_t euid)
  650. {
  651. int rc = syscall(SC_setreuid, ruid, euid);
  652. __RETURN_WITH_ERRNO(rc, rc, -1);
  653. }
  654. int setresuid(uid_t ruid, uid_t euid, uid_t suid)
  655. {
  656. int rc = syscall(SC_setresuid, ruid, euid, suid);
  657. __RETURN_WITH_ERRNO(rc, rc, -1);
  658. }
  659. int setresgid(gid_t rgid, gid_t egid, gid_t sgid)
  660. {
  661. int rc = syscall(SC_setresgid, rgid, egid, sgid);
  662. __RETURN_WITH_ERRNO(rc, rc, -1);
  663. }
  664. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/access.html
  665. int access(char const* pathname, int mode)
  666. {
  667. if (!pathname) {
  668. errno = EFAULT;
  669. return -1;
  670. }
  671. int rc = syscall(SC_access, pathname, strlen(pathname), mode);
  672. __RETURN_WITH_ERRNO(rc, rc, -1);
  673. }
  674. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/mknod.html
  675. int mknod(char const* pathname, mode_t mode, dev_t dev)
  676. {
  677. if (!pathname) {
  678. errno = EFAULT;
  679. return -1;
  680. }
  681. Syscall::SC_mknod_params params { { pathname, strlen(pathname) }, mode, dev };
  682. int rc = syscall(SC_mknod, &params);
  683. __RETURN_WITH_ERRNO(rc, rc, -1);
  684. }
  685. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fpathconf.html
  686. long fpathconf([[maybe_unused]] int fd, int name)
  687. {
  688. switch (name) {
  689. case _PC_NAME_MAX:
  690. return NAME_MAX;
  691. case _PC_PATH_MAX:
  692. return PATH_MAX;
  693. case _PC_VDISABLE:
  694. return _POSIX_VDISABLE;
  695. case _PC_LINK_MAX:
  696. return LINK_MAX;
  697. }
  698. VERIFY_NOT_REACHED();
  699. }
  700. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pathconf.html
  701. long pathconf([[maybe_unused]] char const* path, int name)
  702. {
  703. switch (name) {
  704. case _PC_NAME_MAX:
  705. return NAME_MAX;
  706. case _PC_PATH_MAX:
  707. return PATH_MAX;
  708. case _PC_PIPE_BUF:
  709. return PIPE_BUF;
  710. case _PC_LINK_MAX:
  711. return LINK_MAX;
  712. }
  713. VERIFY_NOT_REACHED();
  714. }
  715. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/_exit.html
  716. void _exit(int status)
  717. {
  718. syscall(SC_exit, status);
  719. VERIFY_NOT_REACHED();
  720. }
  721. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sync.html
  722. void sync()
  723. {
  724. syscall(SC_sync);
  725. }
  726. static String getlogin_buffer;
  727. char* getlogin()
  728. {
  729. if (getlogin_buffer.is_null()) {
  730. if (auto* passwd = getpwuid(getuid())) {
  731. getlogin_buffer = String(passwd->pw_name);
  732. }
  733. endpwent();
  734. }
  735. return const_cast<char*>(getlogin_buffer.characters());
  736. }
  737. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html
  738. int ftruncate(int fd, off_t length)
  739. {
  740. int rc = syscall(SC_ftruncate, fd, &length);
  741. __RETURN_WITH_ERRNO(rc, rc, -1);
  742. }
  743. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/truncate.html
  744. int truncate(char const* path, off_t length)
  745. {
  746. int fd = open(path, O_RDWR | O_CREAT, 0666);
  747. if (fd < 0)
  748. return fd;
  749. int rc = ftruncate(fd, length);
  750. int saved_errno = errno;
  751. if (int close_rc = close(fd); close_rc < 0)
  752. return close_rc;
  753. errno = saved_errno;
  754. return rc;
  755. }
  756. int gettid()
  757. {
  758. int cached_tid = s_cached_tid;
  759. if (!cached_tid) {
  760. cached_tid = syscall(SC_gettid);
  761. s_cached_tid = cached_tid;
  762. }
  763. return cached_tid;
  764. }
  765. int sysbeep()
  766. {
  767. int rc = syscall(SC_beep);
  768. __RETURN_WITH_ERRNO(rc, rc, -1);
  769. }
  770. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fsync.html
  771. int fsync(int fd)
  772. {
  773. int rc = syscall(SC_fsync, fd);
  774. __RETURN_WITH_ERRNO(rc, rc, -1);
  775. }
  776. int mount(int source_fd, char const* target, char const* fs_type, int flags)
  777. {
  778. if (!target || !fs_type) {
  779. errno = EFAULT;
  780. return -1;
  781. }
  782. Syscall::SC_mount_params params {
  783. { target, strlen(target) },
  784. { fs_type, strlen(fs_type) },
  785. source_fd,
  786. flags
  787. };
  788. int rc = syscall(SC_mount, &params);
  789. __RETURN_WITH_ERRNO(rc, rc, -1);
  790. }
  791. int umount(char const* mountpoint)
  792. {
  793. int rc = syscall(SC_umount, mountpoint, strlen(mountpoint));
  794. __RETURN_WITH_ERRNO(rc, rc, -1);
  795. }
  796. void dump_backtrace()
  797. {
  798. syscall(SC_dump_backtrace);
  799. }
  800. int get_process_name(char* buffer, int buffer_size)
  801. {
  802. int rc = syscall(SC_get_process_name, buffer, buffer_size);
  803. __RETURN_WITH_ERRNO(rc, rc, -1);
  804. }
  805. int set_process_name(char const* name, size_t name_length)
  806. {
  807. int rc = syscall(SC_set_process_name, name, name_length);
  808. __RETURN_WITH_ERRNO(rc, rc, -1);
  809. }
  810. int pledge(char const* promises, char const* execpromises)
  811. {
  812. Syscall::SC_pledge_params params {
  813. { promises, promises ? strlen(promises) : 0 },
  814. { execpromises, execpromises ? strlen(execpromises) : 0 }
  815. };
  816. int rc = syscall(SC_pledge, &params);
  817. __RETURN_WITH_ERRNO(rc, rc, -1);
  818. }
  819. int unveil(char const* path, char const* permissions)
  820. {
  821. Syscall::SC_unveil_params params {
  822. { path, path ? strlen(path) : 0 },
  823. { permissions, permissions ? strlen(permissions) : 0 }
  824. };
  825. int rc = syscall(SC_unveil, &params);
  826. __RETURN_WITH_ERRNO(rc, rc, -1);
  827. }
  828. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpass.html
  829. char* getpass(char const* prompt)
  830. {
  831. dbgln("FIXME: getpass('{}')", prompt);
  832. TODO();
  833. }
  834. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html
  835. long sysconf(int name)
  836. {
  837. int rc = syscall(SC_sysconf, name);
  838. __RETURN_WITH_ERRNO(rc, rc, -1);
  839. }
  840. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpagesize.html
  841. int getpagesize()
  842. {
  843. return PAGE_SIZE;
  844. }
  845. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pause.html
  846. int pause()
  847. {
  848. return select(0, nullptr, nullptr, nullptr, nullptr);
  849. }
  850. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/chroot.html
  851. int chroot(char const* path)
  852. {
  853. dbgln("FIXME: chroot(\"{}\")", path);
  854. return -1;
  855. }
  856. // https://pubs.opengroup.org/onlinepubs/7908799/xsh/getdtablesize.html
  857. int getdtablesize()
  858. {
  859. rlimit dtablesize;
  860. int rc = getrlimit(RLIMIT_NOFILE, &dtablesize);
  861. __RETURN_WITH_ERRNO(rc, dtablesize.rlim_cur, rc);
  862. }
  863. }