Serenity Operating System
at hosted 683 lines 25 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#pragma once 28 29#include <AK/FixedArray.h> 30#include <AK/HashMap.h> 31#include <AK/InlineLinkedList.h> 32#include <AK/NonnullOwnPtrVector.h> 33#include <AK/String.h> 34#include <AK/WeakPtr.h> 35#include <Kernel/FileSystem/InodeMetadata.h> 36#include <Kernel/Forward.h> 37#include <Kernel/Lock.h> 38#include <Kernel/Syscall.h> 39#include <Kernel/Thread.h> 40#include <Kernel/UnixTypes.h> 41#include <Kernel/VM/RangeAllocator.h> 42#include <LibC/signal_numbers.h> 43 44class ELFLoader; 45 46namespace Kernel { 47 48timeval kgettimeofday(); 49void kgettimeofday(timeval&); 50 51extern VirtualAddress g_return_to_ring3_from_signal_trampoline; 52 53#define ENUMERATE_PLEDGE_PROMISES \ 54 __ENUMERATE_PLEDGE_PROMISE(stdio) \ 55 __ENUMERATE_PLEDGE_PROMISE(rpath) \ 56 __ENUMERATE_PLEDGE_PROMISE(wpath) \ 57 __ENUMERATE_PLEDGE_PROMISE(cpath) \ 58 __ENUMERATE_PLEDGE_PROMISE(dpath) \ 59 __ENUMERATE_PLEDGE_PROMISE(inet) \ 60 __ENUMERATE_PLEDGE_PROMISE(id) \ 61 __ENUMERATE_PLEDGE_PROMISE(proc) \ 62 __ENUMERATE_PLEDGE_PROMISE(exec) \ 63 __ENUMERATE_PLEDGE_PROMISE(unix) \ 64 __ENUMERATE_PLEDGE_PROMISE(fattr) \ 65 __ENUMERATE_PLEDGE_PROMISE(tty) \ 66 __ENUMERATE_PLEDGE_PROMISE(chown) \ 67 __ENUMERATE_PLEDGE_PROMISE(chroot) \ 68 __ENUMERATE_PLEDGE_PROMISE(thread) \ 69 __ENUMERATE_PLEDGE_PROMISE(video) \ 70 __ENUMERATE_PLEDGE_PROMISE(accept) \ 71 __ENUMERATE_PLEDGE_PROMISE(shared_buffer) 72 73enum class Pledge : u32 { 74#define __ENUMERATE_PLEDGE_PROMISE(x) x, 75 ENUMERATE_PLEDGE_PROMISES 76#undef __ENUMERATE_PLEDGE_PROMISE 77}; 78 79enum class VeilState { 80 None, 81 Dropped, 82 Locked, 83}; 84 85struct UnveiledPath { 86 enum Access { 87 Read = 1, 88 Write = 2, 89 Execute = 4, 90 CreateOrRemove = 8, 91 }; 92 93 String path; 94 unsigned permissions { 0 }; 95}; 96 97class Process : public InlineLinkedListNode<Process> { 98 friend class InlineLinkedListNode<Process>; 99 friend class Thread; 100 101public: 102 static Process* current; 103 104 static Process* create_kernel_process(Thread*& first_thread, String&& name, void (*entry)()); 105 static Process* create_user_process(Thread*& first_thread, const String& path, uid_t, gid_t, pid_t ppid, int& error, Vector<String>&& arguments = Vector<String>(), Vector<String>&& environment = Vector<String>(), TTY* = nullptr); 106 ~Process(); 107 108 static Vector<pid_t> all_pids(); 109 static Vector<Process*> all_processes(); 110 111 bool is_profiling() const { return m_profiling; } 112 void set_profiling(bool profiling) { m_profiling = profiling; } 113 114 enum RingLevel : u8 { 115 Ring0 = 0, 116 Ring3 = 3, 117 }; 118 119 KBuffer backtrace(ProcessInspectionHandle&) const; 120 121 bool is_dead() const { return m_dead; } 122 123 bool is_ring0() const { return m_ring == Ring0; } 124 bool is_ring3() const { return m_ring == Ring3; } 125 126 PageDirectory& page_directory() { return *m_page_directory; } 127 const PageDirectory& page_directory() const { return *m_page_directory; } 128 129 static Process* from_pid(pid_t); 130 131 static void update_info_page_timestamp(const timeval&); 132 133 const String& name() const { return m_name; } 134 pid_t pid() const { return m_pid; } 135 pid_t sid() const { return m_sid; } 136 pid_t pgid() const { return m_pgid; } 137 uid_t uid() const { return m_uid; } 138 gid_t gid() const { return m_gid; } 139 const FixedArray<gid_t>& extra_gids() const { return m_extra_gids; } 140 uid_t euid() const { return m_euid; } 141 gid_t egid() const { return m_egid; } 142 pid_t ppid() const { return m_ppid; } 143 144 pid_t exec_tid() const { return m_exec_tid; } 145 146 mode_t umask() const { return m_umask; } 147 148 bool in_group(gid_t) const; 149 150 RefPtr<FileDescription> file_description(int fd) const; 151 int fd_flags(int fd) const; 152 153 template<typename Callback> 154 static void for_each(Callback); 155 template<typename Callback> 156 static void for_each_in_pgrp(pid_t, Callback); 157 template<typename Callback> 158 void for_each_child(Callback); 159 template<typename Callback> 160 void for_each_thread(Callback) const; 161 162 void die(); 163 void finalize(); 164 165 int sys$yield(); 166 int sys$sync(); 167 int sys$beep(); 168 int sys$get_process_name(char* buffer, int buffer_size); 169 int sys$watch_file(const char* path, size_t path_length); 170 int sys$dbgputch(u8); 171 int sys$dbgputstr(const u8*, int length); 172 int sys$dump_backtrace(); 173 int sys$gettid(); 174 int sys$donate(int tid); 175 int sys$ftruncate(int fd, off_t); 176 pid_t sys$setsid(); 177 pid_t sys$getsid(pid_t); 178 int sys$setpgid(pid_t pid, pid_t pgid); 179 pid_t sys$getpgrp(); 180 pid_t sys$getpgid(pid_t); 181 uid_t sys$getuid(); 182 gid_t sys$getgid(); 183 uid_t sys$geteuid(); 184 gid_t sys$getegid(); 185 pid_t sys$getpid(); 186 pid_t sys$getppid(); 187 mode_t sys$umask(mode_t); 188 int sys$open(const Syscall::SC_open_params*); 189 int sys$close(int fd); 190 ssize_t sys$read(int fd, u8*, ssize_t); 191 ssize_t sys$write(int fd, const u8*, ssize_t); 192 ssize_t sys$writev(int fd, const struct iovec* iov, int iov_count); 193 int sys$fstat(int fd, stat*); 194 int sys$stat(const Syscall::SC_stat_params*); 195 int sys$lseek(int fd, off_t, int whence); 196 int sys$kill(pid_t pid, int sig); 197 [[noreturn]] void sys$exit(int status); 198 int sys$sigreturn(RegisterState& registers); 199 pid_t sys$waitid(const Syscall::SC_waitid_params*); 200 void* sys$mmap(const Syscall::SC_mmap_params*); 201 int sys$munmap(void*, size_t size); 202 int sys$set_mmap_name(const Syscall::SC_set_mmap_name_params*); 203 int sys$mprotect(void*, size_t, int prot); 204 int sys$madvise(void*, size_t, int advice); 205 int sys$purge(int mode); 206 int sys$select(const Syscall::SC_select_params*); 207 int sys$poll(pollfd*, int nfds, int timeout); 208 ssize_t sys$get_dir_entries(int fd, void*, ssize_t); 209 int sys$getcwd(char*, ssize_t); 210 int sys$chdir(const char*, size_t); 211 int sys$fchdir(int fd); 212 int sys$sleep(unsigned seconds); 213 int sys$usleep(useconds_t usec); 214 int sys$gettimeofday(timeval*); 215 int sys$clock_gettime(clockid_t, timespec*); 216 int sys$clock_settime(clockid_t, timespec*); 217 int sys$clock_nanosleep(const Syscall::SC_clock_nanosleep_params*); 218 int sys$gethostname(char*, ssize_t); 219 int sys$uname(utsname*); 220 int sys$readlink(const Syscall::SC_readlink_params*); 221 int sys$ttyname_r(int fd, char*, ssize_t); 222 int sys$ptsname_r(int fd, char*, ssize_t); 223 pid_t sys$fork(RegisterState&); 224 int sys$execve(const Syscall::SC_execve_params*); 225 int sys$getdtablesize(); 226 int sys$dup(int oldfd); 227 int sys$dup2(int oldfd, int newfd); 228 int sys$sigaction(int signum, const sigaction* act, sigaction* old_act); 229 int sys$sigprocmask(int how, const sigset_t* set, sigset_t* old_set); 230 int sys$sigpending(sigset_t*); 231 int sys$getgroups(ssize_t, gid_t*); 232 int sys$setgroups(ssize_t, const gid_t*); 233 int sys$pipe(int pipefd[2], int flags); 234 int sys$killpg(int pgrp, int sig); 235 int sys$setgid(gid_t); 236 int sys$setuid(uid_t); 237 unsigned sys$alarm(unsigned seconds); 238 int sys$access(const char* pathname, size_t path_length, int mode); 239 int sys$fcntl(int fd, int cmd, u32 extra_arg); 240 int sys$ioctl(int fd, unsigned request, unsigned arg); 241 int sys$mkdir(const char* pathname, size_t path_length, mode_t mode); 242 clock_t sys$times(tms*); 243 int sys$utime(const char* pathname, size_t path_length, const struct utimbuf*); 244 int sys$link(const Syscall::SC_link_params*); 245 int sys$unlink(const char* pathname, size_t path_length); 246 int sys$symlink(const Syscall::SC_symlink_params*); 247 int sys$rmdir(const char* pathname, size_t path_length); 248 int sys$mount(const Syscall::SC_mount_params*); 249 int sys$umount(const char* mountpoint, size_t mountpoint_length); 250 int sys$chmod(const char* pathname, size_t path_length, mode_t); 251 int sys$fchmod(int fd, mode_t); 252 int sys$chown(const Syscall::SC_chown_params*); 253 int sys$fchown(int fd, uid_t, gid_t); 254 int sys$socket(int domain, int type, int protocol); 255 int sys$bind(int sockfd, const sockaddr* addr, socklen_t); 256 int sys$listen(int sockfd, int backlog); 257 int sys$accept(int sockfd, sockaddr*, socklen_t*); 258 int sys$connect(int sockfd, const sockaddr*, socklen_t); 259 int sys$shutdown(int sockfd, int how); 260 ssize_t sys$sendto(const Syscall::SC_sendto_params*); 261 ssize_t sys$recvfrom(const Syscall::SC_recvfrom_params*); 262 int sys$getsockopt(const Syscall::SC_getsockopt_params*); 263 int sys$setsockopt(const Syscall::SC_setsockopt_params*); 264 int sys$getsockname(const Syscall::SC_getsockname_params*); 265 int sys$getpeername(const Syscall::SC_getpeername_params*); 266 int sys$sched_setparam(pid_t pid, const struct sched_param* param); 267 int sys$sched_getparam(pid_t pid, struct sched_param* param); 268 int sys$create_thread(void* (*)(void*), void* argument, const Syscall::SC_create_thread_params*); 269 void sys$exit_thread(void*); 270 int sys$join_thread(int tid, void** exit_value); 271 int sys$detach_thread(int tid); 272 int sys$set_thread_name(int tid, const char* buffer, size_t buffer_size); 273 int sys$get_thread_name(int tid, char* buffer, size_t buffer_size); 274 int sys$rename(const Syscall::SC_rename_params*); 275 int sys$mknod(const Syscall::SC_mknod_params*); 276 int sys$shbuf_create(int, void** buffer); 277 int sys$shbuf_allow_pid(int, pid_t peer_pid); 278 int sys$shbuf_allow_all(int); 279 void* sys$shbuf_get(int shbuf_id, size_t* size); 280 int sys$shbuf_release(int shbuf_id); 281 int sys$shbuf_seal(int shbuf_id); 282 int sys$shbuf_set_volatile(int shbuf_id, bool); 283 int sys$halt(); 284 int sys$reboot(); 285 int sys$set_process_icon(int icon_id); 286 int sys$realpath(const Syscall::SC_realpath_params*); 287 ssize_t sys$getrandom(void*, size_t, unsigned int); 288 int sys$setkeymap(const Syscall::SC_setkeymap_params*); 289 int sys$module_load(const char* path, size_t path_length); 290 int sys$module_unload(const char* name, size_t name_length); 291 int sys$profiling_enable(pid_t); 292 int sys$profiling_disable(pid_t); 293 void* sys$get_kernel_info_page(); 294 int sys$futex(const Syscall::SC_futex_params*); 295 int sys$set_thread_boost(int tid, int amount); 296 int sys$set_process_boost(pid_t, int amount); 297 int sys$chroot(const char* path, size_t path_length, int mount_flags); 298 int sys$pledge(const Syscall::SC_pledge_params*); 299 int sys$unveil(const Syscall::SC_unveil_params*); 300 int sys$perf_event(int type, FlatPtr arg1, FlatPtr arg2); 301 int sys$get_stack_bounds(FlatPtr* stack_base, size_t* stack_size); 302 int sys$ptrace(const Syscall::SC_ptrace_params*); 303 304 template<bool sockname, typename Params> 305 int get_sock_or_peer_name(const Params&); 306 307 static void initialize(); 308 309 [[noreturn]] void crash(int signal, u32 eip); 310 [[nodiscard]] static siginfo_t reap(Process&); 311 312 const TTY* tty() const { return m_tty; } 313 void set_tty(TTY*); 314 315 size_t region_count() const { return m_regions.size(); } 316 const NonnullOwnPtrVector<Region>& regions() const { return m_regions; } 317 void dump_regions(); 318 319 u32 m_ticks_in_user { 0 }; 320 u32 m_ticks_in_kernel { 0 }; 321 322 u32 m_ticks_in_user_for_dead_children { 0 }; 323 u32 m_ticks_in_kernel_for_dead_children { 0 }; 324 325 bool validate_read_from_kernel(VirtualAddress, size_t) const; 326 327 bool validate_read(const void*, size_t) const; 328 bool validate_write(void*, size_t) const; 329 template<typename T> 330 bool validate_read_typed(T* value, size_t count = 1) { return validate_read(value, sizeof(T) * count); } 331 template<typename T> 332 bool validate_read_and_copy_typed(T* dest, const T* src) 333 { 334 bool validated = validate_read_typed(src); 335 if (validated) { 336 copy_from_user(dest, src); 337 } 338 return validated; 339 } 340 template<typename T> 341 bool validate_write_typed(T* value, size_t count = 1) { return validate_write(value, sizeof(T) * count); } 342 template<typename DataType, typename SizeType> 343 bool validate(const Syscall::MutableBufferArgument<DataType, SizeType>&); 344 template<typename DataType, typename SizeType> 345 bool validate(const Syscall::ImmutableBufferArgument<DataType, SizeType>&); 346 347 String validate_and_copy_string_from_user(const char*, size_t) const; 348 String validate_and_copy_string_from_user(const Syscall::StringArgument&) const; 349 350 Custody& current_directory(); 351 Custody* executable() { return m_executable.ptr(); } 352 353 int number_of_open_file_descriptors() const; 354 int max_open_file_descriptors() const { return m_max_open_file_descriptors; } 355 356 size_t amount_clean_inode() const; 357 size_t amount_dirty_private() const; 358 size_t amount_virtual() const; 359 size_t amount_resident() const; 360 size_t amount_shared() const; 361 size_t amount_purgeable_volatile() const; 362 size_t amount_purgeable_nonvolatile() const; 363 364 int exec(String path, Vector<String> arguments, Vector<String> environment, int recusion_depth = 0); 365 366 bool is_superuser() const { return m_euid == 0; } 367 368 Region* allocate_region_with_vmobject(VirtualAddress, size_t, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, const String& name, int prot); 369 Region* allocate_region(VirtualAddress, size_t, const String& name, int prot = PROT_READ | PROT_WRITE, bool commit = true); 370 Region* allocate_region_with_vmobject(const Range&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, const String& name, int prot); 371 Region* allocate_region(const Range&, const String& name, int prot = PROT_READ | PROT_WRITE, bool commit = true); 372 bool deallocate_region(Region& region); 373 374 Region& allocate_split_region(const Region& source_region, const Range&, size_t offset_in_vmobject); 375 Vector<Region*, 2> split_region_around_range(const Region& source_region, const Range&); 376 377 bool is_being_inspected() const { return m_inspector_count; } 378 379 void terminate_due_to_signal(u8 signal); 380 void send_signal(u8, Process* sender); 381 382 u16 thread_count() const { return m_thread_count; } 383 384 Thread& any_thread(); 385 386 Lock& big_lock() { return m_big_lock; } 387 388 struct ELFBundle { 389 OwnPtr<Region> region; 390 OwnPtr<ELFLoader> elf_loader; 391 }; 392 OwnPtr<ELFBundle> elf_bundle() const; 393 394 int icon_id() const { return m_icon_id; } 395 396 u32 priority_boost() const { return m_priority_boost; } 397 398 Custody& root_directory(); 399 Custody& root_directory_relative_to_global_root(); 400 void set_root_directory(const Custody&); 401 402 bool has_promises() const { return m_promises; } 403 bool has_promised(Pledge pledge) const { return m_promises & (1u << (u32)pledge); } 404 405 VeilState veil_state() const { return m_veil_state; } 406 const Vector<UnveiledPath>& unveiled_paths() const { return m_unveiled_paths; } 407 408 void increment_inspector_count(Badge<ProcessInspectionHandle>) { ++m_inspector_count; } 409 void decrement_inspector_count(Badge<ProcessInspectionHandle>) { --m_inspector_count; } 410 411private: 412 friend class MemoryManager; 413 friend class Scheduler; 414 friend class Region; 415 416 Process(Thread*& first_thread, const String& name, uid_t, gid_t, pid_t ppid, RingLevel, RefPtr<Custody> cwd = nullptr, RefPtr<Custody> executable = nullptr, TTY* = nullptr, Process* fork_parent = nullptr); 417 static pid_t allocate_pid(); 418 419 Range allocate_range(VirtualAddress, size_t, size_t alignment = PAGE_SIZE); 420 421 Region& add_region(NonnullOwnPtr<Region>); 422 423 void kill_threads_except_self(); 424 void kill_all_threads(); 425 426 int do_exec(NonnullRefPtr<FileDescription> main_program_description, Vector<String> arguments, Vector<String> environment, RefPtr<FileDescription> interpreter_description); 427 ssize_t do_write(FileDescription&, const u8*, int data_size); 428 429 KResultOr<NonnullRefPtr<FileDescription>> find_elf_interpreter_for_executable(const String& path, char (&first_page)[PAGE_SIZE], int nread, size_t file_size); 430 431 int alloc_fd(int first_candidate_fd = 0); 432 void disown_all_shared_buffers(); 433 434 KResult do_kill(Process&, int signal); 435 KResult do_killpg(pid_t pgrp, int signal); 436 437 KResultOr<siginfo_t> do_waitid(idtype_t idtype, int id, int options); 438 439 KResultOr<String> get_syscall_path_argument(const char* user_path, size_t path_length) const; 440 KResultOr<String> get_syscall_path_argument(const Syscall::StringArgument&) const; 441 442 bool has_tracee_thread(int tracer_pid) const; 443 444 RefPtr<PageDirectory> m_page_directory; 445 446 Process* m_prev { nullptr }; 447 Process* m_next { nullptr }; 448 449 String m_name; 450 451 pid_t m_pid { 0 }; 452 uid_t m_uid { 0 }; 453 gid_t m_gid { 0 }; 454 uid_t m_euid { 0 }; 455 gid_t m_egid { 0 }; 456 pid_t m_sid { 0 }; 457 pid_t m_pgid { 0 }; 458 459 pid_t m_exec_tid { 0 }; 460 461 static const int m_max_open_file_descriptors { FD_SETSIZE }; 462 463 struct FileDescriptionAndFlags { 464 operator bool() const { return !!description; } 465 void clear(); 466 void set(NonnullRefPtr<FileDescription>&& d, u32 f = 0); 467 RefPtr<FileDescription> description; 468 u32 flags { 0 }; 469 }; 470 Vector<FileDescriptionAndFlags> m_fds; 471 472 RingLevel m_ring { Ring0 }; 473 u8 m_termination_status { 0 }; 474 u8 m_termination_signal { 0 }; 475 u16 m_thread_count { 0 }; 476 477 bool m_dead { false }; 478 bool m_profiling { false }; 479 480 RefPtr<Custody> m_executable; 481 RefPtr<Custody> m_cwd; 482 RefPtr<Custody> m_root_directory; 483 RefPtr<Custody> m_root_directory_relative_to_global_root; 484 485 RefPtr<TTY> m_tty; 486 487 Region* region_from_range(const Range&); 488 Region* region_containing(const Range&); 489 490 NonnullOwnPtrVector<Region> m_regions; 491 struct RegionLookupCache { 492 Range range; 493 WeakPtr<Region> region; 494 }; 495 RegionLookupCache m_region_lookup_cache; 496 497 pid_t m_ppid { 0 }; 498 mode_t m_umask { 022 }; 499 500 FixedArray<gid_t> m_extra_gids; 501 502 WeakPtr<Region> m_master_tls_region; 503 size_t m_master_tls_size { 0 }; 504 size_t m_master_tls_alignment { 0 }; 505 506 Lock m_big_lock { "Process" }; 507 508 u64 m_alarm_deadline { 0 }; 509 510 int m_icon_id { -1 }; 511 512 u32 m_priority_boost { 0 }; 513 514 u32 m_promises { 0 }; 515 u32 m_execpromises { 0 }; 516 517 VeilState m_veil_state { VeilState::None }; 518 Vector<UnveiledPath> m_unveiled_paths; 519 520 WaitQueue& futex_queue(i32*); 521 HashMap<u32, OwnPtr<WaitQueue>> m_futex_queues; 522 523 OwnPtr<PerformanceEventBuffer> m_perf_event_buffer; 524 525 u32 m_inspector_count { 0 }; 526 527 // This member is used in the implementation of ptrace's PT_TRACEME flag. 528 // If it is set to true, the process will stop at the next execve syscall 529 // and wait for a tracer to attach. 530 bool m_wait_for_tracer_at_next_execve { false }; 531}; 532 533class ProcessInspectionHandle { 534public: 535 ProcessInspectionHandle(Process& process) 536 : m_process(process) 537 { 538 if (&process != Process::current) { 539 InterruptDisabler disabler; 540 m_process.increment_inspector_count({}); 541 } 542 } 543 ~ProcessInspectionHandle() 544 { 545 if (&m_process != Process::current) { 546 InterruptDisabler disabler; 547 m_process.decrement_inspector_count({}); 548 } 549 } 550 551 Process& process() { return m_process; } 552 553 static OwnPtr<ProcessInspectionHandle> from_pid(pid_t pid) 554 { 555 InterruptDisabler disabler; 556 auto* process = Process::from_pid(pid); 557 if (process) 558 return make<ProcessInspectionHandle>(*process); 559 return nullptr; 560 } 561 562 Process* operator->() { return &m_process; } 563 Process& operator*() { return m_process; } 564 565private: 566 Process& m_process; 567}; 568 569extern InlineLinkedList<Process>* g_processes; 570 571template<typename Callback> 572inline void Process::for_each(Callback callback) 573{ 574 ASSERT_INTERRUPTS_DISABLED(); 575 for (auto* process = g_processes->head(); process;) { 576 auto* next_process = process->next(); 577 if (callback(*process) == IterationDecision::Break) 578 break; 579 process = next_process; 580 } 581} 582 583template<typename Callback> 584inline void Process::for_each_child(Callback callback) 585{ 586 ASSERT_INTERRUPTS_DISABLED(); 587 pid_t my_pid = pid(); 588 for (auto* process = g_processes->head(); process;) { 589 auto* next_process = process->next(); 590 if (process->ppid() == my_pid || process->has_tracee_thread(m_pid)) { 591 if (callback(*process) == IterationDecision::Break) 592 break; 593 } 594 process = next_process; 595 } 596} 597 598template<typename Callback> 599inline void Process::for_each_thread(Callback callback) const 600{ 601 InterruptDisabler disabler; 602 pid_t my_pid = pid(); 603 604 if (my_pid == 0) { 605 // NOTE: Special case the colonel process, since its main thread is not in the global thread table. 606 callback(*g_colonel); 607 return; 608 } 609 610 Thread::for_each([callback, my_pid](Thread& thread) -> IterationDecision { 611 if (thread.pid() == my_pid) 612 return callback(thread); 613 614 return IterationDecision::Continue; 615 }); 616} 617 618template<typename Callback> 619inline void Process::for_each_in_pgrp(pid_t pgid, Callback callback) 620{ 621 ASSERT_INTERRUPTS_DISABLED(); 622 for (auto* process = g_processes->head(); process;) { 623 auto* next_process = process->next(); 624 if (!process->is_dead() && process->pgid() == pgid) { 625 if (callback(*process) == IterationDecision::Break) 626 break; 627 } 628 process = next_process; 629 } 630} 631 632inline bool InodeMetadata::may_read(const Process& process) const 633{ 634 return may_read(process.euid(), process.egid(), process.extra_gids()); 635} 636 637inline bool InodeMetadata::may_write(const Process& process) const 638{ 639 return may_write(process.euid(), process.egid(), process.extra_gids()); 640} 641 642inline bool InodeMetadata::may_execute(const Process& process) const 643{ 644 return may_execute(process.euid(), process.egid(), process.extra_gids()); 645} 646 647inline int Thread::pid() const 648{ 649 return m_process.pid(); 650} 651 652inline const LogStream& operator<<(const LogStream& stream, const Process& process) 653{ 654 return stream << process.name() << '(' << process.pid() << ')'; 655} 656 657inline u32 Thread::effective_priority() const 658{ 659 return m_priority + m_process.priority_boost() + m_priority_boost + m_extra_priority; 660} 661 662#define REQUIRE_NO_PROMISES \ 663 do { \ 664 if (Process::current->has_promises()) { \ 665 dbg() << "Has made a promise"; \ 666 cli(); \ 667 Process::current->crash(SIGABRT, 0); \ 668 ASSERT_NOT_REACHED(); \ 669 } \ 670 } while (0) 671 672#define REQUIRE_PROMISE(promise) \ 673 do { \ 674 if (Process::current->has_promises() \ 675 && !Process::current->has_promised(Pledge::promise)) { \ 676 dbg() << "Has not pledged " << #promise; \ 677 cli(); \ 678 Process::current->crash(SIGABRT, 0); \ 679 ASSERT_NOT_REACHED(); \ 680 } \ 681 } while (0) 682 683}