Serenity Operating System
at portability 671 lines 24 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 mode_t umask() const { return m_umask; } 145 146 bool in_group(gid_t) const; 147 148 RefPtr<FileDescription> file_description(int fd) const; 149 int fd_flags(int fd) const; 150 151 template<typename Callback> 152 static void for_each(Callback); 153 template<typename Callback> 154 static void for_each_in_pgrp(pid_t, Callback); 155 template<typename Callback> 156 void for_each_child(Callback); 157 template<typename Callback> 158 void for_each_thread(Callback) const; 159 160 void die(); 161 void finalize(); 162 163 int sys$yield(); 164 int sys$sync(); 165 int sys$beep(); 166 int sys$get_process_name(char* buffer, int buffer_size); 167 int sys$watch_file(const char* path, size_t path_length); 168 int sys$dbgputch(u8); 169 int sys$dbgputstr(const u8*, int length); 170 int sys$dump_backtrace(); 171 int sys$gettid(); 172 int sys$donate(int tid); 173 int sys$ftruncate(int fd, off_t); 174 pid_t sys$setsid(); 175 pid_t sys$getsid(pid_t); 176 int sys$setpgid(pid_t pid, pid_t pgid); 177 pid_t sys$getpgrp(); 178 pid_t sys$getpgid(pid_t); 179 uid_t sys$getuid(); 180 gid_t sys$getgid(); 181 uid_t sys$geteuid(); 182 gid_t sys$getegid(); 183 pid_t sys$getpid(); 184 pid_t sys$getppid(); 185 mode_t sys$umask(mode_t); 186 int sys$open(const Syscall::SC_open_params*); 187 int sys$close(int fd); 188 ssize_t sys$read(int fd, u8*, ssize_t); 189 ssize_t sys$write(int fd, const u8*, ssize_t); 190 ssize_t sys$writev(int fd, const struct iovec* iov, int iov_count); 191 int sys$fstat(int fd, stat*); 192 int sys$stat(const Syscall::SC_stat_params*); 193 int sys$lseek(int fd, off_t, int whence); 194 int sys$kill(pid_t pid, int sig); 195 [[noreturn]] void sys$exit(int status); 196 int sys$sigreturn(RegisterState& registers); 197 pid_t sys$waitid(const Syscall::SC_waitid_params*); 198 void* sys$mmap(const Syscall::SC_mmap_params*); 199 int sys$munmap(void*, size_t size); 200 int sys$set_mmap_name(const Syscall::SC_set_mmap_name_params*); 201 int sys$mprotect(void*, size_t, int prot); 202 int sys$madvise(void*, size_t, int advice); 203 int sys$purge(int mode); 204 int sys$select(const Syscall::SC_select_params*); 205 int sys$poll(pollfd*, int nfds, int timeout); 206 ssize_t sys$get_dir_entries(int fd, void*, ssize_t); 207 int sys$getcwd(char*, ssize_t); 208 int sys$chdir(const char*, size_t); 209 int sys$fchdir(int fd); 210 int sys$sleep(unsigned seconds); 211 int sys$usleep(useconds_t usec); 212 int sys$gettimeofday(timeval*); 213 int sys$clock_gettime(clockid_t, timespec*); 214 int sys$clock_nanosleep(const Syscall::SC_clock_nanosleep_params*); 215 int sys$gethostname(char*, ssize_t); 216 int sys$uname(utsname*); 217 int sys$readlink(const Syscall::SC_readlink_params*); 218 int sys$ttyname_r(int fd, char*, ssize_t); 219 int sys$ptsname_r(int fd, char*, ssize_t); 220 pid_t sys$fork(RegisterState&); 221 int sys$execve(const Syscall::SC_execve_params*); 222 int sys$getdtablesize(); 223 int sys$dup(int oldfd); 224 int sys$dup2(int oldfd, int newfd); 225 int sys$sigaction(int signum, const sigaction* act, sigaction* old_act); 226 int sys$sigprocmask(int how, const sigset_t* set, sigset_t* old_set); 227 int sys$sigpending(sigset_t*); 228 int sys$getgroups(ssize_t, gid_t*); 229 int sys$setgroups(ssize_t, const gid_t*); 230 int sys$pipe(int pipefd[2], int flags); 231 int sys$killpg(int pgrp, int sig); 232 int sys$setgid(gid_t); 233 int sys$setuid(uid_t); 234 unsigned sys$alarm(unsigned seconds); 235 int sys$access(const char* pathname, size_t path_length, int mode); 236 int sys$fcntl(int fd, int cmd, u32 extra_arg); 237 int sys$ioctl(int fd, unsigned request, unsigned arg); 238 int sys$mkdir(const char* pathname, size_t path_length, mode_t mode); 239 clock_t sys$times(tms*); 240 int sys$utime(const char* pathname, size_t path_length, const struct utimbuf*); 241 int sys$link(const Syscall::SC_link_params*); 242 int sys$unlink(const char* pathname, size_t path_length); 243 int sys$symlink(const Syscall::SC_symlink_params*); 244 int sys$rmdir(const char* pathname, size_t path_length); 245 int sys$mount(const Syscall::SC_mount_params*); 246 int sys$umount(const char* mountpoint, size_t mountpoint_length); 247 int sys$chmod(const char* pathname, size_t path_length, mode_t); 248 int sys$fchmod(int fd, mode_t); 249 int sys$chown(const Syscall::SC_chown_params*); 250 int sys$fchown(int fd, uid_t, gid_t); 251 int sys$socket(int domain, int type, int protocol); 252 int sys$bind(int sockfd, const sockaddr* addr, socklen_t); 253 int sys$listen(int sockfd, int backlog); 254 int sys$accept(int sockfd, sockaddr*, socklen_t*); 255 int sys$connect(int sockfd, const sockaddr*, socklen_t); 256 int sys$shutdown(int sockfd, int how); 257 ssize_t sys$sendto(const Syscall::SC_sendto_params*); 258 ssize_t sys$recvfrom(const Syscall::SC_recvfrom_params*); 259 int sys$getsockopt(const Syscall::SC_getsockopt_params*); 260 int sys$setsockopt(const Syscall::SC_setsockopt_params*); 261 int sys$getsockname(const Syscall::SC_getsockname_params*); 262 int sys$getpeername(const Syscall::SC_getpeername_params*); 263 int sys$sched_setparam(pid_t pid, const struct sched_param* param); 264 int sys$sched_getparam(pid_t pid, struct sched_param* param); 265 int sys$create_thread(void* (*)(void*), void* argument, const Syscall::SC_create_thread_params*); 266 void sys$exit_thread(void*); 267 int sys$join_thread(int tid, void** exit_value); 268 int sys$detach_thread(int tid); 269 int sys$set_thread_name(int tid, const char* buffer, size_t buffer_size); 270 int sys$get_thread_name(int tid, char* buffer, size_t buffer_size); 271 int sys$rename(const Syscall::SC_rename_params*); 272 int sys$systrace(pid_t); 273 int sys$mknod(const Syscall::SC_mknod_params*); 274 int sys$create_shared_buffer(int, void** buffer); 275 int sys$share_buffer_with(int, pid_t peer_pid); 276 int sys$share_buffer_globally(int); 277 void* sys$get_shared_buffer(int shared_buffer_id); 278 int sys$release_shared_buffer(int shared_buffer_id); 279 int sys$seal_shared_buffer(int shared_buffer_id); 280 int sys$get_shared_buffer_size(int shared_buffer_id); 281 int sys$set_shared_buffer_volatile(int shared_buffer_id, bool); 282 int sys$halt(); 283 int sys$reboot(); 284 int sys$set_process_icon(int icon_id); 285 int sys$realpath(const Syscall::SC_realpath_params*); 286 ssize_t sys$getrandom(void*, size_t, unsigned int); 287 int sys$setkeymap(const Syscall::SC_setkeymap_params*); 288 int sys$module_load(const char* path, size_t path_length); 289 int sys$module_unload(const char* name, size_t name_length); 290 int sys$profiling_enable(pid_t); 291 int sys$profiling_disable(pid_t); 292 void* sys$get_kernel_info_page(); 293 int sys$futex(const Syscall::SC_futex_params*); 294 int sys$set_thread_boost(int tid, int amount); 295 int sys$set_process_boost(pid_t, int amount); 296 int sys$chroot(const char* path, size_t path_length, int mount_flags); 297 int sys$pledge(const Syscall::SC_pledge_params*); 298 int sys$unveil(const Syscall::SC_unveil_params*); 299 int sys$perf_event(int type, uintptr_t arg1, uintptr_t arg2); 300 301 template<bool sockname, typename Params> 302 int get_sock_or_peer_name(const Params&); 303 304 static void initialize(); 305 306 [[noreturn]] void crash(int signal, u32 eip); 307 [[nodiscard]] static siginfo_t reap(Process&); 308 309 const TTY* tty() const { return m_tty; } 310 void set_tty(TTY*); 311 312 size_t region_count() const { return m_regions.size(); } 313 const NonnullOwnPtrVector<Region>& regions() const { return m_regions; } 314 void dump_regions(); 315 316 ProcessTracer* tracer() { return m_tracer.ptr(); } 317 ProcessTracer& ensure_tracer(); 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, bool user_accessible = true); 369 Region* allocate_file_backed_region(VirtualAddress, size_t, NonnullRefPtr<Inode>, const String& name, int prot); 370 Region* allocate_region(VirtualAddress, size_t, const String& name, int prot = PROT_READ | PROT_WRITE, bool commit = true); 371 Region* allocate_region_with_vmobject(const Range&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, const String& name, int prot, bool user_accessible = true); 372 Region* allocate_region(const Range&, const String& name, int prot = PROT_READ | PROT_WRITE, bool commit = true); 373 bool deallocate_region(Region& region); 374 375 Region& allocate_split_region(const Region& source_region, const Range&, size_t offset_in_vmobject); 376 Vector<Region*, 2> split_region_around_range(const Region& source_region, const Range&); 377 378 bool is_being_inspected() const { return m_inspector_count; } 379 380 void terminate_due_to_signal(u8 signal); 381 void send_signal(u8, Process* sender); 382 383 u16 thread_count() const { return m_thread_count; } 384 385 Thread& any_thread(); 386 387 Lock& big_lock() { return m_big_lock; } 388 389 const ELFLoader* elf_loader() const { return m_elf_loader.ptr(); } 390 391 int icon_id() const { return m_icon_id; } 392 393 u32 priority_boost() const { return m_priority_boost; } 394 395 Custody& root_directory(); 396 Custody& root_directory_relative_to_global_root(); 397 void set_root_directory(const Custody&); 398 399 bool has_promises() const { return m_promises; } 400 bool has_promised(Pledge pledge) const { return m_promises & (1u << (u32)pledge); } 401 402 VeilState veil_state() const { return m_veil_state; } 403 const Vector<UnveiledPath>& unveiled_paths() const { return m_unveiled_paths; } 404 405 void increment_inspector_count(Badge<ProcessInspectionHandle>) { ++m_inspector_count; } 406 void decrement_inspector_count(Badge<ProcessInspectionHandle>) { --m_inspector_count; } 407 408private: 409 friend class MemoryManager; 410 friend class Scheduler; 411 friend class Region; 412 413 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); 414 static pid_t allocate_pid(); 415 416 Range allocate_range(VirtualAddress, size_t, size_t alignment = PAGE_SIZE); 417 418 Region& add_region(NonnullOwnPtr<Region>); 419 420 int do_exec(NonnullRefPtr<FileDescription> main_program_description, Vector<String> arguments, Vector<String> environment, RefPtr<FileDescription> interpreter_description); 421 ssize_t do_write(FileDescription&, const u8*, int data_size); 422 423 KResultOr<NonnullRefPtr<FileDescription>> find_elf_interpreter_for_executable(const String& path, char (&first_page)[PAGE_SIZE], int nread, size_t file_size); 424 425 int alloc_fd(int first_candidate_fd = 0); 426 void disown_all_shared_buffers(); 427 428 KResult do_kill(Process&, int signal); 429 KResult do_killpg(pid_t pgrp, int signal); 430 431 KResultOr<siginfo_t> do_waitid(idtype_t idtype, int id, int options); 432 433 KResultOr<String> get_syscall_path_argument(const char* user_path, size_t path_length) const; 434 KResultOr<String> get_syscall_path_argument(const Syscall::StringArgument&) const; 435 436 RefPtr<PageDirectory> m_page_directory; 437 438 Process* m_prev { nullptr }; 439 Process* m_next { nullptr }; 440 441 String m_name; 442 443 pid_t m_pid { 0 }; 444 uid_t m_uid { 0 }; 445 gid_t m_gid { 0 }; 446 uid_t m_euid { 0 }; 447 gid_t m_egid { 0 }; 448 pid_t m_sid { 0 }; 449 pid_t m_pgid { 0 }; 450 451 static const int m_max_open_file_descriptors { FD_SETSIZE }; 452 453 struct FileDescriptionAndFlags { 454 operator bool() const { return !!description; } 455 void clear(); 456 void set(NonnullRefPtr<FileDescription>&& d, u32 f = 0); 457 RefPtr<FileDescription> description; 458 u32 flags { 0 }; 459 }; 460 Vector<FileDescriptionAndFlags> m_fds; 461 462 RingLevel m_ring { Ring0 }; 463 u8 m_termination_status { 0 }; 464 u8 m_termination_signal { 0 }; 465 u16 m_thread_count { 0 }; 466 467 bool m_dead { false }; 468 bool m_profiling { false }; 469 470 RefPtr<Custody> m_executable; 471 RefPtr<Custody> m_cwd; 472 RefPtr<Custody> m_root_directory; 473 RefPtr<Custody> m_root_directory_relative_to_global_root; 474 475 RefPtr<TTY> m_tty; 476 477 Region* region_from_range(const Range&); 478 Region* region_containing(const Range&); 479 480 NonnullOwnPtrVector<Region> m_regions; 481 struct RegionLookupCache { 482 Range range; 483 WeakPtr<Region> region; 484 }; 485 RegionLookupCache m_region_lookup_cache; 486 487 pid_t m_ppid { 0 }; 488 mode_t m_umask { 022 }; 489 490 FixedArray<gid_t> m_extra_gids; 491 492 RefPtr<ProcessTracer> m_tracer; 493 OwnPtr<ELFLoader> m_elf_loader; 494 495 Region* m_master_tls_region { nullptr }; 496 size_t m_master_tls_size { 0 }; 497 size_t m_master_tls_alignment { 0 }; 498 499 Lock m_big_lock { "Process" }; 500 501 u64 m_alarm_deadline { 0 }; 502 503 int m_icon_id { -1 }; 504 505 u32 m_priority_boost { 0 }; 506 507 u32 m_promises { 0 }; 508 u32 m_execpromises { 0 }; 509 510 VeilState m_veil_state { VeilState::None }; 511 Vector<UnveiledPath> m_unveiled_paths; 512 513 WaitQueue& futex_queue(i32*); 514 HashMap<u32, OwnPtr<WaitQueue>> m_futex_queues; 515 516 OwnPtr<PerformanceEventBuffer> m_perf_event_buffer; 517 518 u32 m_inspector_count { 0 }; 519}; 520 521class ProcessInspectionHandle { 522public: 523 ProcessInspectionHandle(Process& process) 524 : m_process(process) 525 { 526 if (&process != Process::current) { 527 InterruptDisabler disabler; 528 m_process.increment_inspector_count({}); 529 } 530 } 531 ~ProcessInspectionHandle() 532 { 533 if (&m_process != Process::current) { 534 InterruptDisabler disabler; 535 m_process.decrement_inspector_count({}); 536 } 537 } 538 539 Process& process() { return m_process; } 540 541 static OwnPtr<ProcessInspectionHandle> from_pid(pid_t pid) 542 { 543 InterruptDisabler disabler; 544 auto* process = Process::from_pid(pid); 545 if (process) 546 return make<ProcessInspectionHandle>(*process); 547 return nullptr; 548 } 549 550 Process* operator->() { return &m_process; } 551 Process& operator*() { return m_process; } 552 553private: 554 Process& m_process; 555}; 556 557extern InlineLinkedList<Process>* g_processes; 558 559template<typename Callback> 560inline void Process::for_each(Callback callback) 561{ 562 ASSERT_INTERRUPTS_DISABLED(); 563 for (auto* process = g_processes->head(); process;) { 564 auto* next_process = process->next(); 565 if (callback(*process) == IterationDecision::Break) 566 break; 567 process = next_process; 568 } 569} 570 571template<typename Callback> 572inline void Process::for_each_child(Callback callback) 573{ 574 ASSERT_INTERRUPTS_DISABLED(); 575 pid_t my_pid = pid(); 576 for (auto* process = g_processes->head(); process;) { 577 auto* next_process = process->next(); 578 if (process->ppid() == my_pid) { 579 if (callback(*process) == IterationDecision::Break) 580 break; 581 } 582 process = next_process; 583 } 584} 585 586template<typename Callback> 587inline void Process::for_each_thread(Callback callback) const 588{ 589 InterruptDisabler disabler; 590 pid_t my_pid = pid(); 591 592 if (my_pid == 0) { 593 // NOTE: Special case the colonel process, since its main thread is not in the global thread table. 594 callback(*g_colonel); 595 return; 596 } 597 598 Thread::for_each([callback, my_pid](Thread& thread) -> IterationDecision { 599 if (thread.pid() == my_pid) 600 return callback(thread); 601 602 return IterationDecision::Continue; 603 }); 604} 605 606template<typename Callback> 607inline void Process::for_each_in_pgrp(pid_t pgid, Callback callback) 608{ 609 ASSERT_INTERRUPTS_DISABLED(); 610 for (auto* process = g_processes->head(); process;) { 611 auto* next_process = process->next(); 612 if (!process->is_dead() && process->pgid() == pgid) { 613 if (callback(*process) == IterationDecision::Break) 614 break; 615 } 616 process = next_process; 617 } 618} 619 620inline bool InodeMetadata::may_read(const Process& process) const 621{ 622 return may_read(process.euid(), process.egid(), process.extra_gids()); 623} 624 625inline bool InodeMetadata::may_write(const Process& process) const 626{ 627 return may_write(process.euid(), process.egid(), process.extra_gids()); 628} 629 630inline bool InodeMetadata::may_execute(const Process& process) const 631{ 632 return may_execute(process.euid(), process.egid(), process.extra_gids()); 633} 634 635inline int Thread::pid() const 636{ 637 return m_process.pid(); 638} 639 640inline const LogStream& operator<<(const LogStream& stream, const Process& process) 641{ 642 return stream << process.name() << '(' << process.pid() << ')'; 643} 644 645inline u32 Thread::effective_priority() const 646{ 647 return m_priority + m_process.priority_boost() + m_priority_boost + m_extra_priority; 648} 649 650#define REQUIRE_NO_PROMISES \ 651 do { \ 652 if (Process::current->has_promises()) { \ 653 dbg() << "Has made a promise"; \ 654 cli(); \ 655 Process::current->crash(SIGABRT, 0); \ 656 ASSERT_NOT_REACHED(); \ 657 } \ 658 } while (0) 659 660#define REQUIRE_PROMISE(promise) \ 661 do { \ 662 if (Process::current->has_promises() \ 663 && !Process::current->has_promised(Pledge::promise)) { \ 664 dbg() << "Has not pledged " << #promise; \ 665 cli(); \ 666 Process::current->crash(SIGABRT, 0); \ 667 ASSERT_NOT_REACHED(); \ 668 } \ 669 } while (0) 670 671}