procops.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #include <string>
  2. #include <vector>
  3. #include <sys/prctl.h>
  4. #include <sys/utsname.h>
  5. #include <sys/wait.h>
  6. #include <types/elf.hpp>
  7. #include <kernel/async/lock.hpp>
  8. #include <kernel/log.hpp>
  9. #include <kernel/process.hpp>
  10. #include <kernel/signal.hpp>
  11. #include <kernel/syscall.hpp>
  12. #include <kernel/utsname.hpp>
  13. #include <kernel/vfs.hpp>
  14. #include <kernel/vfs/dentry.hpp>
  15. using namespace kernel::syscall;
  16. #define NOT_IMPLEMENTED not_implemented(__FILE__, __LINE__)
  17. static inline void not_implemented(const char* pos, int line) {
  18. kmsgf(
  19. "[kernel] the function at %s:%d is not implemented, killing the "
  20. "pid%d...",
  21. pos, line, current_process->pid);
  22. current_thread->send_signal(SIGSYS);
  23. }
  24. int kernel::syscall::do_chdir(const char __user* path) {
  25. // TODO: use copy_from_user
  26. auto [dir, ret] = current_open(path);
  27. if (!dir || ret)
  28. return ret;
  29. if (!fs::r_dentry_is_directory(dir.get()))
  30. return -ENOTDIR;
  31. current_process->cwd = std::move(dir);
  32. return 0;
  33. }
  34. execve_retval kernel::syscall::do_execve(const std::string& exec,
  35. const std::vector<std::string>& args,
  36. const std::vector<std::string>& envs) {
  37. auto [dent, ret] = current_open(exec);
  38. if (ret)
  39. return {0, 0, ret};
  40. types::elf::elf32_load_data d{
  41. .exec_dent{std::move(dent)},
  42. .argv{args},
  43. .envp{envs},
  44. .ip{},
  45. .sp{},
  46. };
  47. current_process->files.onexec();
  48. // TODO: set cs and ss to compatibility mode
  49. if (int ret = types::elf::elf32_load(d); ret != 0) {
  50. if (ret == types::elf::ELF_LOAD_FAIL_NORETURN)
  51. kill_current(SIGSEGV);
  52. return {0, 0, ret};
  53. }
  54. current_thread->signals.on_exec();
  55. return {d.ip, d.sp, 0};
  56. }
  57. int kernel::syscall::do_exit(int status) {
  58. // TODO: terminating a thread only
  59. assert(current_process->thds.size() == 1);
  60. // terminating a whole process:
  61. procs->kill(current_process->pid, (status & 0xff) << 8);
  62. // switch to new process and continue
  63. schedule_noreturn();
  64. }
  65. int kernel::syscall::do_waitpid(pid_t waitpid, int __user* arg1, int options) {
  66. if (waitpid != -1)
  67. return -EINVAL;
  68. auto& cv = current_process->waitlist;
  69. async::lock_guard lck(current_process->mtx_waitprocs);
  70. auto& waitlist = current_process->waitprocs;
  71. // TODO: check if it is waiting for stopped process
  72. if (options & ~(WNOHANG | WUNTRACED)) {
  73. NOT_IMPLEMENTED;
  74. return -EINVAL;
  75. }
  76. while (waitlist.empty()) {
  77. if (current_process->children.empty())
  78. return -ECHILD;
  79. if (options & WNOHANG)
  80. return 0;
  81. bool interrupted = cv.wait(current_process->mtx_waitprocs);
  82. if (interrupted)
  83. return -EINTR;
  84. }
  85. for (auto iter = waitlist.begin(); iter != waitlist.end(); ++iter) {
  86. if (WIFSTOPPED(iter->code) && !(options & WUNTRACED))
  87. continue;
  88. pid_t pid = iter->pid;
  89. // TODO: copy_to_user
  90. *arg1 = iter->code;
  91. procs->remove(pid);
  92. waitlist.erase(iter);
  93. return pid;
  94. }
  95. // we should never reach here
  96. freeze();
  97. return -EINVAL;
  98. }
  99. int kernel::syscall::do_getcwd(char __user* buf, size_t buf_size) {
  100. // TODO: use copy_to_user
  101. return fs::d_path(current_process->cwd.get(),
  102. current_process->fs_context.root.get(), buf, buf_size);
  103. }
  104. pid_t kernel::syscall::do_setsid() {
  105. if (current_process->pid == current_process->pgid)
  106. return -EPERM;
  107. current_process->sid = current_process->pid;
  108. current_process->pgid = current_process->pid;
  109. // TODO: get tty* from fd or block device id
  110. tty::console->set_pgrp(current_process->pid);
  111. current_process->control_tty = tty::console;
  112. return current_process->pid;
  113. }
  114. pid_t kernel::syscall::do_getsid(pid_t pid) {
  115. auto [pproc, found] = procs->try_find(pid);
  116. if (!found)
  117. return -ESRCH;
  118. if (pproc->sid != current_process->sid)
  119. return -EPERM;
  120. return pproc->sid;
  121. }
  122. int kernel::syscall::do_setpgid(pid_t pid, pid_t pgid) {
  123. if (pgid < 0)
  124. return -EINVAL;
  125. if (pid == 0)
  126. pid = current_process->pid;
  127. if (pgid == 0)
  128. pgid = pid;
  129. auto [pproc, found] = procs->try_find(pid);
  130. if (!found)
  131. return -ESRCH;
  132. // TODO: check whether pgid and the original
  133. // pgid is in the same session
  134. pproc->pgid = pgid;
  135. return 0;
  136. }
  137. int kernel::syscall::do_set_thread_area(kernel::user::user_desc __user* ptr) {
  138. auto ret = current_thread->set_thread_area(ptr);
  139. if (ret != 0)
  140. return ret;
  141. current_thread->load_thread_area32();
  142. return 0;
  143. }
  144. pid_t kernel::syscall::do_set_tid_address(int __user* tidptr) {
  145. // TODO: copy_from_user
  146. current_thread->set_child_tid = tidptr;
  147. return current_thread->tid();
  148. }
  149. int kernel::syscall::do_prctl(int option, uintptr_t arg2) {
  150. switch (option) {
  151. case PR_SET_NAME: {
  152. // TODO: copy_from_user
  153. auto* name = (const char __user*)arg2;
  154. current_thread->name.assign(name, 15);
  155. break;
  156. }
  157. case PR_GET_NAME: {
  158. auto* name = (char __user*)arg2;
  159. // TODO: copy_to_user
  160. strncpy(name, current_thread->name.c_str(), 16);
  161. name[15] = 0;
  162. break;
  163. }
  164. default:
  165. return -EINVAL;
  166. }
  167. return 0;
  168. }
  169. int kernel::syscall::do_arch_prctl(int option, uintptr_t arg2) {
  170. switch (option) {
  171. case PR_SET_NAME: {
  172. // TODO: copy_from_user
  173. auto* name = (const char __user*)arg2;
  174. current_thread->name.assign(name, 15);
  175. break;
  176. }
  177. case PR_GET_NAME: {
  178. auto* name = (char __user*)arg2;
  179. // TODO: copy_to_user
  180. strncpy(name, current_thread->name.c_str(), 16);
  181. name[15] = 0;
  182. break;
  183. }
  184. default:
  185. return -EINVAL;
  186. }
  187. return 0;
  188. }
  189. int kernel::syscall::do_umask(mode_t mask) {
  190. mode_t old = current_process->umask;
  191. current_process->umask = mask;
  192. return old;
  193. }
  194. int kernel::syscall::do_kill(pid_t pid, int sig) {
  195. auto [pproc, found] = procs->try_find(pid);
  196. if (!found)
  197. return -ESRCH;
  198. if (!kernel::signal_list::check_valid(sig))
  199. return -EINVAL;
  200. if (pproc->is_system())
  201. return 0;
  202. // TODO: check permission
  203. procs->send_signal(pid, sig);
  204. return 0;
  205. }
  206. int kernel::syscall::do_tkill(pid_t tid, int sig) {
  207. NOT_IMPLEMENTED;
  208. return -EINVAL;
  209. auto [pproc, found] = procs->try_find(tid);
  210. if (!found)
  211. return -ESRCH;
  212. if (!kernel::signal_list::check_valid(sig))
  213. return -EINVAL;
  214. if (pproc->is_system())
  215. return 0;
  216. // TODO: check permission
  217. procs->send_signal(tid, sig);
  218. return 0;
  219. }
  220. int kernel::syscall::do_rt_sigprocmask(int how, const sigmask_type __user* set,
  221. sigmask_type __user* oldset,
  222. size_t sigsetsize) {
  223. if (sigsetsize != sizeof(sigmask_type))
  224. return -EINVAL;
  225. sigmask_type sigs = current_thread->signals.get_mask();
  226. // TODO: use copy_to_user
  227. if (oldset)
  228. memcpy(oldset, &sigs, sizeof(sigmask_type));
  229. if (!set)
  230. return 0;
  231. // TODO: use copy_from_user
  232. switch (how) {
  233. case SIG_BLOCK:
  234. current_thread->signals.mask(*set);
  235. break;
  236. case SIG_UNBLOCK:
  237. current_thread->signals.unmask(*set);
  238. break;
  239. case SIG_SETMASK:
  240. current_thread->signals.set_mask(*set);
  241. break;
  242. }
  243. return 0;
  244. }
  245. int kernel::syscall::do_rt_sigaction(int signum, const sigaction __user* act,
  246. sigaction __user* oldact,
  247. size_t sigsetsize) {
  248. if (sigsetsize != sizeof(sigmask_type))
  249. return -EINVAL;
  250. if (!kernel::signal_list::check_valid(signum) || signum == SIGKILL ||
  251. signum == SIGSTOP)
  252. return -EINVAL;
  253. // TODO: use copy_to_user
  254. if (oldact)
  255. current_thread->signals.get_handler(signum, *oldact);
  256. if (!act)
  257. return 0;
  258. // TODO: use copy_from_user
  259. current_thread->signals.set_handler(signum, *act);
  260. return 0;
  261. }
  262. int kernel::syscall::do_newuname(new_utsname __user* buf) {
  263. if (!buf)
  264. return -EFAULT;
  265. // TODO: use copy_to_user
  266. memcpy(buf, sys_utsname, sizeof(new_utsname));
  267. return 0;
  268. }
  269. pid_t kernel::syscall::do_getpgid(pid_t pid) {
  270. if (pid == 0)
  271. return current_process->pgid;
  272. auto [pproc, found] = procs->try_find(pid);
  273. if (!found)
  274. return -ESRCH;
  275. return pproc->pgid;
  276. }
  277. pid_t kernel::syscall::do_getpid() {
  278. return current_process->pid;
  279. }
  280. pid_t kernel::syscall::do_getppid() {
  281. return current_process->ppid;
  282. }
  283. uid_t kernel::syscall::do_getuid() {
  284. return 0; // all users are root for now
  285. }
  286. uid_t kernel::syscall::do_geteuid() {
  287. return 0; // all users are root for now
  288. }
  289. gid_t kernel::syscall::do_getgid() {
  290. return 0; // all users are root for now
  291. }
  292. pid_t kernel::syscall::do_gettid() {
  293. return current_thread->tid();
  294. }
  295. uintptr_t kernel::syscall::do_brk(uintptr_t addr) {
  296. return current_process->mms.set_brk(addr);
  297. }