unistd.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #include <unistd.h>
  2. #include <string.h>
  3. #include <errno.h>
  4. #include <stdarg.h>
  5. #include <assert.h>
  6. #include <grp.h>
  7. #include <pwd.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <sys/ioctl.h>
  11. #include <sys/types.h>
  12. #include <Kernel/Syscall.h>
  13. #include <AK/Vector.h>
  14. extern "C" {
  15. int chown(const char* pathname, uid_t owner, gid_t group)
  16. {
  17. (void)pathname;
  18. (void)owner;
  19. (void)group;
  20. assert(false);
  21. }
  22. pid_t fork()
  23. {
  24. int rc = syscall(SC_fork);
  25. __RETURN_WITH_ERRNO(rc, rc, -1);
  26. }
  27. int execve(const char* filename, char* const argv[], char* const envp[])
  28. {
  29. int rc = syscall(SC_execve, filename, argv, envp);
  30. __RETURN_WITH_ERRNO(rc, rc, -1);
  31. }
  32. int execvp(const char* filename, char* const argv[])
  33. {
  34. // FIXME: This should do some sort of shell-like path resolution!
  35. return execve(filename, argv, nullptr);
  36. }
  37. int execl(const char* filename, const char* arg0, ...)
  38. {
  39. Vector<const char*> args;
  40. args.append(arg0);
  41. va_list ap;
  42. va_start(ap, arg0);
  43. for (;;) {
  44. const char* arg = va_arg(ap, const char*);
  45. if (!arg)
  46. break;
  47. args.append(arg);
  48. }
  49. va_end(ap);
  50. args.append(nullptr);
  51. return execve(filename, (char* const *)args.data(), nullptr);
  52. }
  53. uid_t getuid()
  54. {
  55. return syscall(SC_getuid);
  56. }
  57. gid_t getgid()
  58. {
  59. return syscall(SC_getgid);
  60. }
  61. uid_t geteuid()
  62. {
  63. return syscall(SC_geteuid);
  64. }
  65. gid_t getegid()
  66. {
  67. return syscall(SC_getegid);
  68. }
  69. pid_t getpid()
  70. {
  71. return syscall(SC_getpid);
  72. }
  73. pid_t getppid()
  74. {
  75. return syscall(SC_getppid);
  76. }
  77. pid_t setsid()
  78. {
  79. int rc = syscall(SC_setsid);
  80. __RETURN_WITH_ERRNO(rc, rc, -1);
  81. }
  82. pid_t tcgetpgrp(int fd)
  83. {
  84. return ioctl(fd, TIOCGPGRP);
  85. }
  86. int tcsetpgrp(int fd, pid_t pgid)
  87. {
  88. return ioctl(fd, TIOCSPGRP, pgid);
  89. }
  90. int setpgid(pid_t pid, pid_t pgid)
  91. {
  92. int rc = syscall(SC_setpgid, pid, pgid);
  93. __RETURN_WITH_ERRNO(rc, rc, -1);
  94. }
  95. pid_t getpgid(pid_t pid)
  96. {
  97. int rc = syscall(SC_getpgid, pid);
  98. __RETURN_WITH_ERRNO(rc, rc, -1);
  99. }
  100. pid_t getpgrp()
  101. {
  102. int rc = syscall(SC_getpgrp);
  103. __RETURN_WITH_ERRNO(rc, rc, -1);
  104. }
  105. int open(const char* path, int options, ...)
  106. {
  107. va_list ap;
  108. va_start(ap, options);
  109. int rc = syscall(SC_open, path, options, (mode_t)va_arg(ap, unsigned));
  110. va_end(ap);
  111. __RETURN_WITH_ERRNO(rc, rc, -1);
  112. }
  113. ssize_t read(int fd, void* buf, size_t count)
  114. {
  115. int rc = syscall(SC_read, fd, buf, count);
  116. __RETURN_WITH_ERRNO(rc, rc, -1);
  117. }
  118. ssize_t write(int fd, const void* buf, size_t count)
  119. {
  120. int rc = syscall(SC_write, fd, buf, count);
  121. __RETURN_WITH_ERRNO(rc, rc, -1);
  122. }
  123. int ttyname_r(int fd, char* buffer, size_t size)
  124. {
  125. int rc = syscall(SC_ttyname_r, fd, buffer, size);
  126. __RETURN_WITH_ERRNO(rc, rc, -1);
  127. }
  128. static char ttyname_buf[32];
  129. char* ttyname(int fd)
  130. {
  131. if (ttyname_r(fd, ttyname_buf, sizeof(ttyname_buf)) < 0)
  132. return nullptr;
  133. return ttyname_buf;
  134. }
  135. int close(int fd)
  136. {
  137. int rc = syscall(SC_close, fd);
  138. __RETURN_WITH_ERRNO(rc, rc, -1);
  139. }
  140. pid_t waitpid(pid_t waitee, int* wstatus, int options)
  141. {
  142. int rc = syscall(SC_waitpid, waitee, wstatus, options);
  143. __RETURN_WITH_ERRNO(rc, rc, -1);
  144. }
  145. int lstat(const char* path, struct stat* statbuf)
  146. {
  147. int rc = syscall(SC_lstat, path, statbuf);
  148. __RETURN_WITH_ERRNO(rc, rc, -1);
  149. }
  150. int stat(const char* path, struct stat* statbuf)
  151. {
  152. int rc = syscall(SC_stat, path, statbuf);
  153. __RETURN_WITH_ERRNO(rc, rc, -1);
  154. }
  155. int fstat(int fd, struct stat *statbuf)
  156. {
  157. int rc = syscall(SC_fstat, fd, statbuf);
  158. __RETURN_WITH_ERRNO(rc, rc, -1);
  159. }
  160. int chdir(const char* path)
  161. {
  162. int rc = syscall(SC_chdir, path);
  163. __RETURN_WITH_ERRNO(rc, rc, -1);
  164. }
  165. char* getcwd(char* buffer, size_t size)
  166. {
  167. if (!buffer) {
  168. size = size ? size : PATH_MAX;
  169. buffer = (char*)malloc(size);
  170. }
  171. int rc = syscall(SC_getcwd, buffer, size);
  172. __RETURN_WITH_ERRNO(rc, buffer, nullptr);
  173. }
  174. char* getwd(char* buf)
  175. {
  176. auto* p = getcwd(buf, PATH_MAX);
  177. return p;
  178. }
  179. int sleep(unsigned seconds)
  180. {
  181. return syscall(SC_sleep, seconds);
  182. }
  183. int usleep(useconds_t usec)
  184. {
  185. return syscall(SC_usleep, usec);
  186. }
  187. int gethostname(char* buffer, size_t size)
  188. {
  189. int rc = syscall(SC_gethostname, buffer, size);
  190. __RETURN_WITH_ERRNO(rc, rc, -1);
  191. }
  192. ssize_t readlink(const char* path, char* buffer, size_t size)
  193. {
  194. int rc = syscall(SC_readlink, path, buffer, size);
  195. __RETURN_WITH_ERRNO(rc, rc, -1);
  196. }
  197. off_t lseek(int fd, off_t offset, int whence)
  198. {
  199. int rc = syscall(SC_lseek, fd, offset, whence);
  200. __RETURN_WITH_ERRNO(rc, rc, -1);
  201. }
  202. int link(const char* old_path, const char* new_path)
  203. {
  204. int rc = syscall(SC_link, old_path, new_path);
  205. __RETURN_WITH_ERRNO(rc, rc, -1);
  206. }
  207. int unlink(const char* pathname)
  208. {
  209. int rc = syscall(SC_unlink, pathname);
  210. __RETURN_WITH_ERRNO(rc, rc, -1);
  211. }
  212. int rmdir(const char* pathname)
  213. {
  214. int rc = syscall(SC_rmdir, pathname);
  215. __RETURN_WITH_ERRNO(rc, rc, -1);
  216. }
  217. int isatty(int fd)
  218. {
  219. int rc = syscall(SC_isatty, fd);
  220. __RETURN_WITH_ERRNO(rc, 1, 0);
  221. }
  222. int getdtablesize()
  223. {
  224. int rc = syscall(SC_getdtablesize);
  225. __RETURN_WITH_ERRNO(rc, rc, -1);
  226. }
  227. int dup(int old_fd)
  228. {
  229. int rc = syscall(SC_dup, old_fd);
  230. __RETURN_WITH_ERRNO(rc, rc, -1);
  231. }
  232. int dup2(int old_fd, int new_fd)
  233. {
  234. int rc = syscall(SC_dup2, old_fd, new_fd);
  235. __RETURN_WITH_ERRNO(rc, rc, -1);
  236. }
  237. int setgroups(size_t size, const gid_t* list)
  238. {
  239. int rc = syscall(SC_getgroups, size, list);
  240. __RETURN_WITH_ERRNO(rc, rc, -1);
  241. }
  242. int getgroups(int size, gid_t list[])
  243. {
  244. int rc = syscall(SC_getgroups, size, list);
  245. __RETURN_WITH_ERRNO(rc, rc, -1);
  246. }
  247. int pipe(int pipefd[2])
  248. {
  249. int rc = syscall(SC_pipe, pipefd);
  250. __RETURN_WITH_ERRNO(rc, rc, -1);
  251. }
  252. unsigned int alarm(unsigned int seconds)
  253. {
  254. return syscall(SC_alarm, seconds);
  255. }
  256. int setuid(uid_t uid)
  257. {
  258. int rc = syscall(SC_setuid, uid);
  259. __RETURN_WITH_ERRNO(rc, rc, -1);
  260. }
  261. int setgid(uid_t gid)
  262. {
  263. int rc = syscall(SC_setgid, gid);
  264. __RETURN_WITH_ERRNO(rc, rc, -1);
  265. }
  266. int access(const char* pathname, int mode)
  267. {
  268. int rc = syscall(SC_access, pathname, mode);
  269. __RETURN_WITH_ERRNO(rc, rc, -1);
  270. }
  271. int mknod(const char* pathname, mode_t, dev_t)
  272. {
  273. (void) pathname;
  274. assert(false);
  275. }
  276. long fpathconf(int fd, int name)
  277. {
  278. (void) fd;
  279. (void) name;
  280. assert(false);
  281. }
  282. long pathconf(const char* path, int name)
  283. {
  284. (void) path;
  285. (void) name;
  286. assert(false);
  287. }
  288. void _exit(int status)
  289. {
  290. syscall(SC_exit, status);
  291. assert(false);
  292. }
  293. void sync()
  294. {
  295. syscall(SC_sync);
  296. }
  297. int read_tsc(unsigned* lsw, unsigned* msw)
  298. {
  299. int rc = syscall(SC_read_tsc, lsw, msw);
  300. __RETURN_WITH_ERRNO(rc, rc, -1);
  301. }
  302. int create_shared_buffer(pid_t peer_pid, size_t size, void** buffer)
  303. {
  304. int rc = syscall(SC_create_shared_buffer, peer_pid, size, buffer);
  305. __RETURN_WITH_ERRNO(rc, rc, -1);
  306. }
  307. void* get_shared_buffer(int shared_buffer_id)
  308. {
  309. int rc = syscall(SC_get_shared_buffer, shared_buffer_id);
  310. if (rc < 0 && -rc < EMAXERRNO) {
  311. errno = -rc;
  312. return (void*)-1;
  313. }
  314. return (void*)rc;
  315. }
  316. int release_shared_buffer(int shared_buffer_id)
  317. {
  318. int rc = syscall(SC_release_shared_buffer, shared_buffer_id);
  319. __RETURN_WITH_ERRNO(rc, rc, -1);
  320. }
  321. }