Process.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. #pragma once
  2. #include <AK/InlineLinkedList.h>
  3. #include <AK/NonnullRefPtrVector.h>
  4. #include <AK/String.h>
  5. #include <AK/Types.h>
  6. #include <AK/Vector.h>
  7. #include <AK/WeakPtr.h>
  8. #include <AK/Weakable.h>
  9. #include <Kernel/FileSystem/VirtualFileSystem.h>
  10. #include <Kernel/Lock.h>
  11. #include <Kernel/Syscall.h>
  12. #include <Kernel/TTY/TTY.h>
  13. #include <Kernel/Thread.h>
  14. #include <Kernel/UnixTypes.h>
  15. #include <Kernel/VM/RangeAllocator.h>
  16. #include <LibC/signal_numbers.h>
  17. class ELFLoader;
  18. class FileDescription;
  19. class KBuffer;
  20. class PageDirectory;
  21. class Region;
  22. class VMObject;
  23. class ProcessTracer;
  24. class SharedBuffer;
  25. timeval kgettimeofday();
  26. void kgettimeofday(timeval&);
  27. extern VirtualAddress g_return_to_ring3_from_signal_trampoline;
  28. extern VirtualAddress g_return_to_ring0_from_signal_trampoline;
  29. class Process : public InlineLinkedListNode<Process>
  30. , public Weakable<Process> {
  31. friend class InlineLinkedListNode<Process>;
  32. friend class Thread;
  33. public:
  34. static Process* create_kernel_process(String&& name, void (*entry)());
  35. static Process* create_user_process(const String& path, uid_t, gid_t, pid_t ppid, int& error, Vector<String>&& arguments = Vector<String>(), Vector<String>&& environment = Vector<String>(), TTY* = nullptr);
  36. ~Process();
  37. static Vector<pid_t> all_pids();
  38. static Vector<Process*> all_processes();
  39. enum Priority : u8 {
  40. IdlePriority,
  41. FirstPriority = IdlePriority,
  42. LowPriority,
  43. NormalPriority,
  44. HighPriority,
  45. LastPriority = HighPriority,
  46. };
  47. enum RingLevel : u8 {
  48. Ring0 = 0,
  49. Ring3 = 3,
  50. };
  51. KBuffer backtrace(ProcessInspectionHandle&) const;
  52. bool is_dead() const { return m_dead; }
  53. Thread::State state() const { return main_thread().state(); }
  54. Thread& main_thread() { return *m_main_thread; }
  55. const Thread& main_thread() const { return *m_main_thread; }
  56. bool is_ring0() const { return m_ring == Ring0; }
  57. bool is_ring3() const { return m_ring == Ring3; }
  58. PageDirectory& page_directory() { return *m_page_directory; }
  59. const PageDirectory& page_directory() const { return *m_page_directory; }
  60. static Process* from_pid(pid_t);
  61. void set_priority(Priority p) { m_priority = p; }
  62. Priority priority() const { return m_priority; }
  63. const String& name() const { return m_name; }
  64. pid_t pid() const { return m_pid; }
  65. pid_t sid() const { return m_sid; }
  66. pid_t pgid() const { return m_pgid; }
  67. uid_t uid() const { return m_uid; }
  68. gid_t gid() const { return m_gid; }
  69. const HashTable<gid_t>& gids() const { return m_gids; }
  70. uid_t euid() const { return m_euid; }
  71. gid_t egid() const { return m_egid; }
  72. pid_t ppid() const { return m_ppid; }
  73. mode_t umask() const { return m_umask; }
  74. bool in_group(gid_t) const;
  75. FileDescription* file_description(int fd);
  76. const FileDescription* file_description(int fd) const;
  77. template<typename Callback>
  78. static void for_each(Callback);
  79. template<typename Callback>
  80. static void for_each_in_pgrp(pid_t, Callback);
  81. template<typename Callback>
  82. void for_each_child(Callback);
  83. template<typename Callback>
  84. void for_each_thread(Callback) const;
  85. void die();
  86. void finalize();
  87. int sys$get_process_name(char* buffer, int buffer_size);
  88. int sys$watch_file(const char* path, int path_length);
  89. int sys$dbgputch(u8);
  90. int sys$dbgputstr(const u8*, int length);
  91. int sys$dump_backtrace();
  92. int sys$gettid();
  93. int sys$donate(int tid);
  94. int sys$shm_open(const char* name, int flags, mode_t);
  95. int sys$shm_unlink(const char* name);
  96. int sys$ftruncate(int fd, off_t);
  97. pid_t sys$setsid();
  98. pid_t sys$getsid(pid_t);
  99. int sys$setpgid(pid_t pid, pid_t pgid);
  100. pid_t sys$getpgrp();
  101. pid_t sys$getpgid(pid_t);
  102. uid_t sys$getuid();
  103. gid_t sys$getgid();
  104. uid_t sys$geteuid();
  105. gid_t sys$getegid();
  106. pid_t sys$getpid();
  107. pid_t sys$getppid();
  108. mode_t sys$umask(mode_t);
  109. int sys$open(const Syscall::SC_open_params*);
  110. int sys$close(int fd);
  111. ssize_t sys$read(int fd, u8*, ssize_t);
  112. ssize_t sys$write(int fd, const u8*, ssize_t);
  113. ssize_t sys$writev(int fd, const struct iovec* iov, int iov_count);
  114. int sys$fstat(int fd, stat*);
  115. int sys$lstat(const char*, stat*);
  116. int sys$stat(const char*, stat*);
  117. int sys$lseek(int fd, off_t, int whence);
  118. int sys$kill(pid_t pid, int sig);
  119. [[noreturn]] void sys$exit(int status);
  120. int sys$sigreturn(RegisterDump& registers);
  121. pid_t sys$waitpid(pid_t, int* wstatus, int options);
  122. void* sys$mmap(const Syscall::SC_mmap_params*);
  123. int sys$munmap(void*, size_t size);
  124. int sys$set_mmap_name(void*, size_t, const char*);
  125. int sys$mprotect(void*, size_t, int prot);
  126. int sys$select(const Syscall::SC_select_params*);
  127. int sys$poll(pollfd*, int nfds, int timeout);
  128. ssize_t sys$get_dir_entries(int fd, void*, ssize_t);
  129. int sys$getcwd(char*, ssize_t);
  130. int sys$chdir(const char*);
  131. int sys$fchdir(int fd);
  132. int sys$sleep(unsigned seconds);
  133. int sys$usleep(useconds_t usec);
  134. int sys$gettimeofday(timeval*);
  135. int sys$gethostname(char*, ssize_t);
  136. int sys$uname(utsname*);
  137. int sys$readlink(const char*, char*, ssize_t);
  138. int sys$ttyname_r(int fd, char*, ssize_t);
  139. int sys$ptsname_r(int fd, char*, ssize_t);
  140. pid_t sys$fork(RegisterDump&);
  141. int sys$execve(const char* filename, const char** argv, const char** envp);
  142. int sys$isatty(int fd);
  143. int sys$getdtablesize();
  144. int sys$dup(int oldfd);
  145. int sys$dup2(int oldfd, int newfd);
  146. int sys$sigaction(int signum, const sigaction* act, sigaction* old_act);
  147. int sys$sigprocmask(int how, const sigset_t* set, sigset_t* old_set);
  148. int sys$sigpending(sigset_t*);
  149. int sys$getgroups(ssize_t, gid_t*);
  150. int sys$setgroups(ssize_t, const gid_t*);
  151. int sys$pipe(int pipefd[2], int flags);
  152. int sys$killpg(int pgrp, int sig);
  153. int sys$setgid(gid_t);
  154. int sys$setuid(uid_t);
  155. unsigned sys$alarm(unsigned seconds);
  156. int sys$access(const char* pathname, int mode);
  157. int sys$fcntl(int fd, int cmd, u32 extra_arg);
  158. int sys$ioctl(int fd, unsigned request, unsigned arg);
  159. int sys$mkdir(const char* pathname, mode_t mode);
  160. clock_t sys$times(tms*);
  161. int sys$utime(const char* pathname, const struct utimbuf*);
  162. int sys$link(const char* old_path, const char* new_path);
  163. int sys$unlink(const char* pathname);
  164. int sys$symlink(const char* target, const char* linkpath);
  165. int sys$rmdir(const char* pathname);
  166. int sys$mount(const char* device, const char* mountpoint, const char* fstype);
  167. int sys$umount(const char* mountpoint);
  168. int sys$read_tsc(u32* lsw, u32* msw);
  169. int sys$chmod(const char* pathname, mode_t);
  170. int sys$fchmod(int fd, mode_t);
  171. int sys$chown(const char* pathname, uid_t, gid_t);
  172. int sys$fchown(int fd, uid_t, gid_t);
  173. int sys$socket(int domain, int type, int protocol);
  174. int sys$bind(int sockfd, const sockaddr* addr, socklen_t);
  175. int sys$listen(int sockfd, int backlog);
  176. int sys$accept(int sockfd, sockaddr*, socklen_t*);
  177. int sys$connect(int sockfd, const sockaddr*, socklen_t);
  178. ssize_t sys$sendto(const Syscall::SC_sendto_params*);
  179. ssize_t sys$recvfrom(const Syscall::SC_recvfrom_params*);
  180. int sys$getsockopt(const Syscall::SC_getsockopt_params*);
  181. int sys$setsockopt(const Syscall::SC_setsockopt_params*);
  182. int sys$getsockname(int sockfd, sockaddr* addr, socklen_t* addrlen);
  183. int sys$getpeername(int sockfd, sockaddr* addr, socklen_t* addrlen);
  184. int sys$sched_setparam(pid_t pid, const struct sched_param* param);
  185. int sys$sched_getparam(pid_t pid, struct sched_param* param);
  186. int sys$restore_signal_mask(u32 mask);
  187. int sys$create_thread(int (*)(void*), void*);
  188. void sys$exit_thread(int code);
  189. int sys$rename(const char* oldpath, const char* newpath);
  190. int sys$systrace(pid_t);
  191. int sys$mknod(const char* pathname, mode_t, dev_t);
  192. int sys$create_shared_buffer(int, void** buffer);
  193. int sys$share_buffer_with(int, pid_t peer_pid);
  194. int sys$share_buffer_globally(int);
  195. void* sys$get_shared_buffer(int shared_buffer_id);
  196. int sys$release_shared_buffer(int shared_buffer_id);
  197. int sys$seal_shared_buffer(int shared_buffer_id);
  198. int sys$get_shared_buffer_size(int shared_buffer_id);
  199. int sys$halt();
  200. int sys$reboot();
  201. int sys$set_process_icon(int icon_id);
  202. int sys$realpath(const char* pathname, char*, size_t);
  203. static void initialize();
  204. [[noreturn]] void crash(int signal, u32 eip);
  205. [[nodiscard]] static int reap(Process&);
  206. const TTY* tty() const { return m_tty; }
  207. void set_tty(TTY* tty) { m_tty = tty; }
  208. size_t region_count() const { return m_regions.size(); }
  209. const NonnullOwnPtrVector<Region>& regions() const { return m_regions; }
  210. void dump_regions();
  211. ProcessTracer* tracer() { return m_tracer.ptr(); }
  212. ProcessTracer& ensure_tracer();
  213. u32 m_ticks_in_user { 0 };
  214. u32 m_ticks_in_kernel { 0 };
  215. u32 m_ticks_in_user_for_dead_children { 0 };
  216. u32 m_ticks_in_kernel_for_dead_children { 0 };
  217. bool validate_read_from_kernel(VirtualAddress) const;
  218. bool validate_read(const void*, ssize_t) const;
  219. bool validate_write(void*, ssize_t) const;
  220. bool validate_read_str(const char* str);
  221. template<typename T>
  222. bool validate_read_typed(T* value, size_t count = 1) { return validate_read(value, sizeof(T) * count); }
  223. template<typename T>
  224. bool validate_write_typed(T* value, size_t count = 1) { return validate_write(value, sizeof(T) * count); }
  225. Custody& current_directory();
  226. Custody* executable() { return m_executable.ptr(); }
  227. int number_of_open_file_descriptors() const;
  228. int max_open_file_descriptors() const { return m_max_open_file_descriptors; }
  229. size_t amount_virtual() const;
  230. size_t amount_resident() const;
  231. size_t amount_shared() const;
  232. Process* fork(RegisterDump&);
  233. int exec(String path, Vector<String> arguments, Vector<String> environment);
  234. bool is_superuser() const { return m_euid == 0; }
  235. Region* allocate_region_with_vmo(VirtualAddress, size_t, NonnullRefPtr<VMObject>, size_t offset_in_vmo, const String& name, int prot);
  236. Region* allocate_file_backed_region(VirtualAddress, size_t, NonnullRefPtr<Inode>, const String& name, int prot);
  237. Region* allocate_region(VirtualAddress, size_t, const String& name, int prot = PROT_READ | PROT_WRITE, bool commit = true);
  238. bool deallocate_region(Region& region);
  239. Region& allocate_split_region(const Region& source_region, const Range&);
  240. void set_being_inspected(bool b) { m_being_inspected = b; }
  241. bool is_being_inspected() const { return m_being_inspected; }
  242. void terminate_due_to_signal(u8 signal);
  243. void send_signal(u8, Process* sender);
  244. int thread_count() const;
  245. Lock& big_lock() { return m_big_lock; }
  246. unsigned syscall_count() const { return m_syscall_count; }
  247. void did_syscall() { ++m_syscall_count; }
  248. const ELFLoader* elf_loader() const { return m_elf_loader.ptr(); }
  249. int icon_id() const { return m_icon_id; }
  250. private:
  251. friend class MemoryManager;
  252. friend class Scheduler;
  253. friend class Region;
  254. Process(String&& name, uid_t, gid_t, pid_t ppid, RingLevel, RefPtr<Custody> cwd = nullptr, RefPtr<Custody> executable = nullptr, TTY* = nullptr, Process* fork_parent = nullptr);
  255. Range allocate_range(VirtualAddress, size_t);
  256. int do_exec(String path, Vector<String> arguments, Vector<String> environment);
  257. ssize_t do_write(FileDescription&, const u8*, int data_size);
  258. int alloc_fd(int first_candidate_fd = 0);
  259. void disown_all_shared_buffers();
  260. KResultOr<String> find_shebang_interpreter_for_executable(const String& executable_path);
  261. Thread* m_main_thread { nullptr };
  262. RefPtr<PageDirectory> m_page_directory;
  263. Process* m_prev { nullptr };
  264. Process* m_next { nullptr };
  265. String m_name;
  266. pid_t m_pid { 0 };
  267. uid_t m_uid { 0 };
  268. gid_t m_gid { 0 };
  269. uid_t m_euid { 0 };
  270. gid_t m_egid { 0 };
  271. pid_t m_sid { 0 };
  272. pid_t m_pgid { 0 };
  273. static const int m_max_open_file_descriptors { FD_SETSIZE };
  274. struct FileDescriptionAndFlags {
  275. operator bool() const { return !!description; }
  276. void clear();
  277. void set(NonnullRefPtr<FileDescription>&& d, u32 f = 0);
  278. RefPtr<FileDescription> description;
  279. u32 flags { 0 };
  280. };
  281. Vector<FileDescriptionAndFlags> m_fds;
  282. RingLevel m_ring { Ring0 };
  283. Priority m_priority { NormalPriority };
  284. u8 m_termination_status { 0 };
  285. u8 m_termination_signal { 0 };
  286. bool m_being_inspected { false };
  287. bool m_dead { false };
  288. RefPtr<Custody> m_executable;
  289. RefPtr<Custody> m_cwd;
  290. TTY* m_tty { nullptr };
  291. Region* region_from_range(const Range&);
  292. Region* region_containing(const Range&);
  293. NonnullOwnPtrVector<Region> m_regions;
  294. pid_t m_ppid { 0 };
  295. mode_t m_umask { 022 };
  296. static void notify_waiters(pid_t waitee, int exit_status, int signal);
  297. HashTable<gid_t> m_gids;
  298. int m_next_tid { 0 };
  299. unsigned m_syscall_count { 0 };
  300. RefPtr<ProcessTracer> m_tracer;
  301. OwnPtr<ELFLoader> m_elf_loader;
  302. Region* m_master_tls_region { nullptr };
  303. size_t m_master_tls_size { 0 };
  304. size_t m_master_tls_alignment { 0 };
  305. Lock m_big_lock { "Process" };
  306. u64 m_alarm_deadline { 0 };
  307. int m_icon_id { -1 };
  308. };
  309. class ProcessInspectionHandle {
  310. public:
  311. ProcessInspectionHandle(Process& process)
  312. : m_process(process)
  313. {
  314. if (&process != &current->process()) {
  315. ASSERT(!m_process.is_being_inspected());
  316. m_process.set_being_inspected(true);
  317. }
  318. }
  319. ~ProcessInspectionHandle()
  320. {
  321. m_process.set_being_inspected(false);
  322. }
  323. Process& process() { return m_process; }
  324. static OwnPtr<ProcessInspectionHandle> from_pid(pid_t pid)
  325. {
  326. InterruptDisabler disabler;
  327. auto* process = Process::from_pid(pid);
  328. if (process)
  329. return make<ProcessInspectionHandle>(*process);
  330. return nullptr;
  331. }
  332. Process* operator->() { return &m_process; }
  333. Process& operator*() { return m_process; }
  334. private:
  335. Process& m_process;
  336. };
  337. extern const char* to_string(Process::Priority);
  338. extern InlineLinkedList<Process>* g_processes;
  339. template<typename Callback>
  340. inline void Process::for_each(Callback callback)
  341. {
  342. ASSERT_INTERRUPTS_DISABLED();
  343. for (auto* process = g_processes->head(); process;) {
  344. auto* next_process = process->next();
  345. if (callback(*process) == IterationDecision::Break)
  346. break;
  347. process = next_process;
  348. }
  349. }
  350. template<typename Callback>
  351. inline void Process::for_each_child(Callback callback)
  352. {
  353. ASSERT_INTERRUPTS_DISABLED();
  354. pid_t my_pid = pid();
  355. for (auto* process = g_processes->head(); process;) {
  356. auto* next_process = process->next();
  357. if (process->ppid() == my_pid) {
  358. if (callback(*process) == IterationDecision::Break)
  359. break;
  360. }
  361. process = next_process;
  362. }
  363. }
  364. template<typename Callback>
  365. inline void Process::for_each_thread(Callback callback) const
  366. {
  367. InterruptDisabler disabler;
  368. pid_t my_pid = pid();
  369. Thread::for_each([callback, my_pid](Thread& thread) -> IterationDecision {
  370. if (thread.pid() == my_pid)
  371. return callback(thread);
  372. return IterationDecision::Continue;
  373. });
  374. }
  375. template<typename Callback>
  376. inline void Process::for_each_in_pgrp(pid_t pgid, Callback callback)
  377. {
  378. ASSERT_INTERRUPTS_DISABLED();
  379. for (auto* process = g_processes->head(); process;) {
  380. auto* next_process = process->next();
  381. if (process->pgid() == pgid) {
  382. if (callback(*process) == IterationDecision::Break)
  383. break;
  384. }
  385. process = next_process;
  386. }
  387. }
  388. inline bool InodeMetadata::may_read(Process& process) const
  389. {
  390. return may_read(process.euid(), process.gids());
  391. }
  392. inline bool InodeMetadata::may_write(Process& process) const
  393. {
  394. return may_write(process.euid(), process.gids());
  395. }
  396. inline bool InodeMetadata::may_execute(Process& process) const
  397. {
  398. return may_execute(process.euid(), process.gids());
  399. }
  400. inline int Thread::pid() const
  401. {
  402. return m_process.pid();
  403. }
  404. inline const LogStream& operator<<(const LogStream& stream, const Process& process)
  405. {
  406. return stream << process.name() << '(' << process.pid() << ')';
  407. }