unistd.cpp 29 KB

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