unistd.cpp 28 KB

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