2018-10-16 09:01:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
#include "TSS.h"
|
|
|
|
#include "i386.h"
|
2018-10-30 12:59:29 +00:00
|
|
|
#include "TTY.h"
|
2018-11-08 10:37:01 +00:00
|
|
|
#include "Syscall.h"
|
2019-01-23 04:13:17 +00:00
|
|
|
#include <Kernel/VirtualFileSystem.h>
|
|
|
|
#include <Kernel/UnixTypes.h>
|
2018-12-03 00:18:54 +00:00
|
|
|
#include <AK/InlineLinkedList.h>
|
2018-12-03 23:27:16 +00:00
|
|
|
#include <AK/AKString.h>
|
2018-12-03 00:18:54 +00:00
|
|
|
#include <AK/Vector.h>
|
2019-01-13 04:03:17 +00:00
|
|
|
#include <AK/WeakPtr.h>
|
2019-01-31 06:02:40 +00:00
|
|
|
#include <AK/Weakable.h>
|
2019-01-14 13:21:51 +00:00
|
|
|
#include <AK/Lock.h>
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2018-11-07 10:37:54 +00:00
|
|
|
class FileDescriptor;
|
2018-11-01 22:04:34 +00:00
|
|
|
class PageDirectory;
|
2018-11-01 12:21:02 +00:00
|
|
|
class Region;
|
2018-11-08 20:20:09 +00:00
|
|
|
class VMObject;
|
2018-10-18 11:05:00 +00:00
|
|
|
class Zone;
|
2019-01-16 15:03:50 +00:00
|
|
|
class WSWindow;
|
2019-01-24 22:40:12 +00:00
|
|
|
class GraphicsBitmap;
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2018-11-07 21:15:02 +00:00
|
|
|
#define COOL_GLOBALS
|
|
|
|
#ifdef COOL_GLOBALS
|
|
|
|
struct CoolGlobals {
|
|
|
|
pid_t current_pid;
|
|
|
|
};
|
|
|
|
extern CoolGlobals* g_cool_globals;
|
|
|
|
#endif
|
|
|
|
|
2019-02-04 13:06:38 +00:00
|
|
|
enum class ShouldUnblockProcess { No = 0, Yes };
|
|
|
|
|
2018-11-06 09:46:40 +00:00
|
|
|
struct SignalActionData {
|
|
|
|
LinearAddress handler_or_sigaction;
|
|
|
|
dword mask { 0 };
|
|
|
|
int flags { 0 };
|
|
|
|
LinearAddress restorer;
|
|
|
|
};
|
|
|
|
|
2019-01-09 01:29:11 +00:00
|
|
|
struct DisplayInfo {
|
|
|
|
unsigned width;
|
|
|
|
unsigned height;
|
|
|
|
unsigned bpp;
|
|
|
|
unsigned pitch;
|
|
|
|
};
|
|
|
|
|
2019-01-31 06:02:40 +00:00
|
|
|
class Process : public InlineLinkedListNode<Process>, public Weakable<Process> {
|
2018-11-01 12:15:46 +00:00
|
|
|
friend class InlineLinkedListNode<Process>;
|
2019-01-16 15:03:50 +00:00
|
|
|
friend class WSWindowManager; // FIXME: Make a better API for allocate_region().
|
2019-01-12 23:27:25 +00:00
|
|
|
friend class GraphicsBitmap; // FIXME: Make a better API for allocate_region().
|
2019-02-05 05:43:33 +00:00
|
|
|
friend class Font; //FIXME: This is beyond gross.
|
2018-10-16 09:01:38 +00:00
|
|
|
public:
|
2018-12-24 22:10:48 +00:00
|
|
|
static Process* create_kernel_process(String&& name, void (*entry)());
|
2018-11-06 12:33:06 +00:00
|
|
|
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);
|
2018-11-01 12:15:46 +00:00
|
|
|
~Process();
|
2018-10-23 10:44:46 +00:00
|
|
|
|
2019-02-03 11:33:11 +00:00
|
|
|
static Vector<pid_t> all_pids();
|
2019-01-31 16:31:23 +00:00
|
|
|
static Vector<Process*> all_processes();
|
2019-02-06 17:45:21 +00:00
|
|
|
static void finalize_dying_processes();
|
2018-10-16 09:01:38 +00:00
|
|
|
|
|
|
|
enum State {
|
|
|
|
Invalid = 0,
|
2018-11-07 17:30:59 +00:00
|
|
|
Runnable,
|
|
|
|
Running,
|
2018-11-07 22:21:32 +00:00
|
|
|
Skip1SchedulerPass,
|
|
|
|
Skip0SchedulerPasses,
|
2019-02-06 16:28:14 +00:00
|
|
|
Dying,
|
2018-11-07 17:30:59 +00:00
|
|
|
Dead,
|
|
|
|
BeingInspected,
|
2019-02-06 17:45:21 +00:00
|
|
|
BlockedLurking,
|
2018-11-07 17:30:59 +00:00
|
|
|
BlockedSleep,
|
|
|
|
BlockedWait,
|
|
|
|
BlockedRead,
|
2018-11-12 00:28:46 +00:00
|
|
|
BlockedWrite,
|
2018-11-10 01:43:33 +00:00
|
|
|
BlockedSignal,
|
2019-01-15 22:12:20 +00:00
|
|
|
BlockedSelect,
|
2019-02-14 16:18:35 +00:00
|
|
|
BlockedConnect,
|
2018-10-16 09:01:38 +00:00
|
|
|
};
|
|
|
|
|
2019-02-07 11:21:17 +00:00
|
|
|
enum Priority {
|
|
|
|
LowPriority,
|
|
|
|
NormalPriority,
|
|
|
|
HighPriority,
|
|
|
|
};
|
|
|
|
|
2018-10-16 09:01:38 +00:00
|
|
|
enum RingLevel {
|
|
|
|
Ring0 = 0,
|
|
|
|
Ring3 = 3,
|
|
|
|
};
|
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
bool is_ring0() const { return m_ring == Ring0; }
|
|
|
|
bool is_ring3() const { return m_ring == Ring3; }
|
2018-10-17 22:12:52 +00:00
|
|
|
|
2018-11-07 20:19:47 +00:00
|
|
|
bool is_blocked() const
|
|
|
|
{
|
2019-02-05 11:55:19 +00:00
|
|
|
return m_state == BlockedSleep || m_state == BlockedWait || m_state == BlockedRead || m_state == BlockedWrite || m_state == BlockedSignal || m_state == BlockedSelect;
|
2018-11-07 20:19:47 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 00:25:31 +00:00
|
|
|
PageDirectory& page_directory() { return *m_page_directory; }
|
|
|
|
const PageDirectory& page_directory() const { return *m_page_directory; }
|
|
|
|
|
2018-11-07 20:19:47 +00:00
|
|
|
bool in_kernel() const { return (m_tss.cs & 0x03) == 0; }
|
|
|
|
|
2018-11-07 20:38:18 +00:00
|
|
|
static Process* from_pid(pid_t);
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2019-02-07 11:21:17 +00:00
|
|
|
void set_priority(Priority p) { m_priority = p; }
|
|
|
|
Priority priority() const { return m_priority; }
|
|
|
|
|
2018-10-16 09:01:38 +00:00
|
|
|
const String& name() const { return m_name; }
|
|
|
|
pid_t pid() const { return m_pid; }
|
2018-11-02 11:56:51 +00:00
|
|
|
pid_t sid() const { return m_sid; }
|
|
|
|
pid_t pgid() const { return m_pgid; }
|
2018-11-16 23:11:08 +00:00
|
|
|
dword ticks() const { return m_ticks; }
|
2019-01-31 16:31:23 +00:00
|
|
|
word selector() const { return m_far_ptr.selector; }
|
2018-10-16 09:01:38 +00:00
|
|
|
TSS32& tss() { return m_tss; }
|
|
|
|
State state() const { return m_state; }
|
2018-10-23 10:44:46 +00:00
|
|
|
uid_t uid() const { return m_uid; }
|
2018-11-05 14:04:19 +00:00
|
|
|
gid_t gid() const { return m_gid; }
|
|
|
|
uid_t euid() const { return m_euid; }
|
|
|
|
gid_t egid() const { return m_egid; }
|
2018-11-06 12:33:06 +00:00
|
|
|
pid_t ppid() const { return m_ppid; }
|
2018-10-25 08:15:13 +00:00
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
const FarPtr& far_ptr() const { return m_far_ptr; }
|
2018-10-16 09:06:35 +00:00
|
|
|
|
2018-11-07 10:37:54 +00:00
|
|
|
FileDescriptor* file_descriptor(int fd);
|
|
|
|
const FileDescriptor* file_descriptor(int fd) const;
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2018-11-01 12:15:46 +00:00
|
|
|
void block(Process::State);
|
2018-10-16 09:01:38 +00:00
|
|
|
void unblock();
|
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
void set_wakeup_time(dword t) { m_wakeup_time = t; }
|
|
|
|
dword wakeup_time() const { return m_wakeup_time; }
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2018-11-08 15:09:05 +00:00
|
|
|
template<typename Callback> static void for_each(Callback);
|
|
|
|
template<typename Callback> static void for_each_in_pgrp(pid_t, Callback);
|
|
|
|
template<typename Callback> static void for_each_in_state(State, Callback);
|
2019-02-06 16:28:14 +00:00
|
|
|
template<typename Callback> static void for_each_living(Callback);
|
2018-11-11 14:36:40 +00:00
|
|
|
template<typename Callback> void for_each_child(Callback);
|
2018-11-02 13:06:48 +00:00
|
|
|
|
2019-02-07 10:12:23 +00:00
|
|
|
bool tick();
|
2019-01-31 16:31:23 +00:00
|
|
|
void set_ticks_left(dword t) { m_ticks_left = t; }
|
2019-02-07 10:12:23 +00:00
|
|
|
dword ticks_left() const { return m_ticks_left; }
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
void set_selector(word s) { m_far_ptr.selector = s; }
|
2018-11-01 13:21:02 +00:00
|
|
|
void set_state(State s) { m_state = s; }
|
2019-01-30 17:26:19 +00:00
|
|
|
void die();
|
2019-02-06 17:45:21 +00:00
|
|
|
void finalize();
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2018-11-02 11:56:51 +00:00
|
|
|
pid_t sys$setsid();
|
|
|
|
pid_t sys$getsid(pid_t);
|
|
|
|
int sys$setpgid(pid_t pid, pid_t pgid);
|
|
|
|
pid_t sys$getpgrp();
|
|
|
|
pid_t sys$getpgid(pid_t);
|
2018-10-16 09:01:38 +00:00
|
|
|
uid_t sys$getuid();
|
2018-10-22 11:55:11 +00:00
|
|
|
gid_t sys$getgid();
|
2018-11-05 14:04:19 +00:00
|
|
|
uid_t sys$geteuid();
|
|
|
|
gid_t sys$getegid();
|
2018-10-22 11:55:11 +00:00
|
|
|
pid_t sys$getpid();
|
2018-11-06 12:33:06 +00:00
|
|
|
pid_t sys$getppid();
|
2018-11-06 12:40:23 +00:00
|
|
|
mode_t sys$umask(mode_t);
|
2019-01-21 23:58:13 +00:00
|
|
|
int sys$open(const char* path, int options, mode_t mode = 0);
|
2018-10-16 09:01:38 +00:00
|
|
|
int sys$close(int fd);
|
2018-10-30 14:33:37 +00:00
|
|
|
ssize_t sys$read(int fd, void* outbuf, size_t nread);
|
|
|
|
ssize_t sys$write(int fd, const void*, size_t);
|
2019-01-23 05:53:01 +00:00
|
|
|
int sys$fstat(int fd, stat*);
|
|
|
|
int sys$lstat(const char*, stat*);
|
|
|
|
int sys$stat(const char*, stat*);
|
2018-10-31 16:50:43 +00:00
|
|
|
int sys$lseek(int fd, off_t, int whence);
|
2018-10-16 09:01:38 +00:00
|
|
|
int sys$kill(pid_t pid, int sig);
|
|
|
|
int sys$geterror() { return m_error; }
|
2019-02-15 11:30:48 +00:00
|
|
|
[[noreturn]] void sys$exit(int status);
|
|
|
|
[[noreturn]] void sys$sigreturn();
|
2018-10-26 23:24:22 +00:00
|
|
|
pid_t sys$waitpid(pid_t, int* wstatus, int options);
|
2018-11-08 10:37:01 +00:00
|
|
|
void* sys$mmap(const Syscall::SC_mmap_params*);
|
2018-10-24 07:48:24 +00:00
|
|
|
int sys$munmap(void*, size_t size);
|
2018-10-28 08:57:22 +00:00
|
|
|
int sys$set_mmap_name(void*, size_t, const char*);
|
2019-01-15 22:12:20 +00:00
|
|
|
int sys$select(const Syscall::SC_select_params*);
|
2019-01-23 06:27:41 +00:00
|
|
|
int sys$poll(pollfd*, int nfds, int timeout);
|
2018-12-02 22:34:50 +00:00
|
|
|
ssize_t sys$get_dir_entries(int fd, void*, size_t);
|
2018-10-24 12:28:22 +00:00
|
|
|
int sys$getcwd(char*, size_t);
|
2018-10-26 12:24:11 +00:00
|
|
|
int sys$chdir(const char*);
|
2018-10-25 11:53:49 +00:00
|
|
|
int sys$sleep(unsigned seconds);
|
2019-02-03 15:11:28 +00:00
|
|
|
int sys$usleep(useconds_t usec);
|
2018-10-25 15:29:49 +00:00
|
|
|
int sys$gettimeofday(timeval*);
|
2018-10-26 07:54:29 +00:00
|
|
|
int sys$gethostname(char* name, size_t length);
|
2018-10-26 09:16:56 +00:00
|
|
|
int sys$get_arguments(int* argc, char*** argv);
|
2018-10-31 16:50:43 +00:00
|
|
|
int sys$get_environment(char*** environ);
|
2018-10-26 12:56:21 +00:00
|
|
|
int sys$uname(utsname*);
|
2018-10-28 13:11:51 +00:00
|
|
|
int sys$readlink(const char*, char*, size_t);
|
2018-10-30 21:03:02 +00:00
|
|
|
int sys$ttyname_r(int fd, char*, size_t);
|
2019-01-15 05:30:19 +00:00
|
|
|
int sys$ptsname_r(int fd, char*, size_t);
|
2018-11-02 19:41:58 +00:00
|
|
|
pid_t sys$fork(RegisterDump&);
|
2018-11-03 00:49:40 +00:00
|
|
|
int sys$execve(const char* filename, const char** argv, const char** envp);
|
2018-11-05 17:16:00 +00:00
|
|
|
int sys$isatty(int fd);
|
2018-11-05 18:01:22 +00:00
|
|
|
int sys$getdtablesize();
|
|
|
|
int sys$dup(int oldfd);
|
|
|
|
int sys$dup2(int oldfd, int newfd);
|
2019-01-23 05:53:01 +00:00
|
|
|
int sys$sigaction(int signum, const sigaction* act, sigaction* old_act);
|
|
|
|
int sys$sigprocmask(int how, const sigset_t* set, sigset_t* old_set);
|
|
|
|
int sys$sigpending(sigset_t*);
|
2018-11-07 00:38:51 +00:00
|
|
|
int sys$getgroups(int size, gid_t*);
|
|
|
|
int sys$setgroups(size_t, const gid_t*);
|
2018-11-10 23:20:53 +00:00
|
|
|
int sys$pipe(int* pipefd);
|
|
|
|
int sys$killpg(int pgrp, int sig);
|
|
|
|
int sys$setgid(gid_t);
|
|
|
|
int sys$setuid(uid_t);
|
|
|
|
unsigned sys$alarm(unsigned seconds);
|
|
|
|
int sys$access(const char* pathname, int mode);
|
2018-11-11 09:38:33 +00:00
|
|
|
int sys$fcntl(int fd, int cmd, dword extra_arg);
|
2018-11-16 12:11:21 +00:00
|
|
|
int sys$ioctl(int fd, unsigned request, unsigned arg);
|
2018-11-18 13:57:41 +00:00
|
|
|
int sys$mkdir(const char* pathname, mode_t mode);
|
2019-01-23 05:53:01 +00:00
|
|
|
clock_t sys$times(tms*);
|
|
|
|
int sys$utime(const char* pathname, const struct utimbuf*);
|
2019-01-22 06:03:44 +00:00
|
|
|
int sys$unlink(const char* pathname);
|
2019-01-28 03:16:01 +00:00
|
|
|
int sys$rmdir(const char* pathname);
|
2019-01-25 01:09:29 +00:00
|
|
|
int sys$read_tsc(dword* lsw, dword* msw);
|
2019-01-29 03:55:08 +00:00
|
|
|
int sys$chmod(const char* pathname, mode_t);
|
2019-02-14 13:17:38 +00:00
|
|
|
int sys$socket(int domain, int type, int protocol);
|
|
|
|
int sys$bind(int sockfd, const sockaddr* addr, socklen_t);
|
|
|
|
int sys$listen(int sockfd, int backlog);
|
2019-02-14 14:17:30 +00:00
|
|
|
int sys$accept(int sockfd, sockaddr*, socklen_t*);
|
2019-02-14 13:17:38 +00:00
|
|
|
int sys$connect(int sockfd, const sockaddr*, socklen_t);
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2019-02-14 16:18:35 +00:00
|
|
|
bool wait_for_connect(Socket&, int& error);
|
|
|
|
|
2018-10-16 09:01:38 +00:00
|
|
|
static void initialize();
|
|
|
|
|
2019-02-15 11:30:48 +00:00
|
|
|
[[noreturn]] void crash();
|
|
|
|
[[nodiscard]] static int reap(Process&);
|
2018-10-17 22:26:30 +00:00
|
|
|
|
2018-10-30 14:33:37 +00:00
|
|
|
const TTY* tty() const { return m_tty; }
|
2019-01-15 07:49:24 +00:00
|
|
|
void set_tty(TTY* tty) { m_tty = tty; }
|
2018-10-30 14:33:37 +00:00
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
size_t region_count() const { return m_regions.size(); }
|
2018-10-27 12:56:52 +00:00
|
|
|
const Vector<RetainPtr<Region>>& regions() const { return m_regions; }
|
2019-01-31 16:31:23 +00:00
|
|
|
void dump_regions();
|
2018-10-18 12:53:00 +00:00
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
void did_schedule() { ++m_times_scheduled; }
|
|
|
|
dword times_scheduled() const { return m_times_scheduled; }
|
2018-10-22 09:15:16 +00:00
|
|
|
|
2018-12-03 00:12:26 +00:00
|
|
|
dword m_ticks_in_user { 0 };
|
|
|
|
dword m_ticks_in_kernel { 0 };
|
|
|
|
|
|
|
|
dword m_ticks_in_user_for_dead_children { 0 };
|
|
|
|
dword m_ticks_in_kernel_for_dead_children { 0 };
|
|
|
|
|
2018-11-28 22:30:06 +00:00
|
|
|
pid_t waitee_pid() const { return m_waitee_pid; }
|
2018-10-23 22:20:34 +00:00
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
dword frame_ptr() const { return m_tss.ebp; }
|
|
|
|
dword stack_ptr() const { return m_tss.esp; }
|
|
|
|
dword stack_top() const { return m_tss.ss == 0x10 ? m_stack_top0 : m_stack_top3; }
|
2018-10-26 20:32:35 +00:00
|
|
|
|
2019-01-01 01:20:01 +00:00
|
|
|
bool validate_read_from_kernel(LinearAddress) const;
|
2018-10-26 22:14:24 +00:00
|
|
|
|
2018-11-16 15:10:59 +00:00
|
|
|
bool validate_read(const void*, size_t) const;
|
2018-11-16 14:41:48 +00:00
|
|
|
bool validate_write(void*, size_t) const;
|
2019-02-07 23:10:01 +00:00
|
|
|
bool validate_read_str(const char* str);
|
2018-11-16 15:23:39 +00:00
|
|
|
template<typename T> bool validate_read_typed(T* value, size_t count = 1) { return validate_read(value, sizeof(T) * count); }
|
|
|
|
template<typename T> bool validate_write_typed(T* value, size_t count = 1) { return validate_write(value, sizeof(T) * count); }
|
2018-11-16 14:41:48 +00:00
|
|
|
|
2019-01-16 16:20:58 +00:00
|
|
|
Inode* cwd_inode();
|
2019-01-16 11:57:07 +00:00
|
|
|
Inode* executable_inode() { return m_executable.ptr(); }
|
2018-10-28 11:20:25 +00:00
|
|
|
|
2018-11-01 12:39:28 +00:00
|
|
|
size_t number_of_open_file_descriptors() const;
|
2018-11-01 13:00:28 +00:00
|
|
|
size_t max_open_file_descriptors() const { return m_max_open_file_descriptors; }
|
2018-11-01 12:39:28 +00:00
|
|
|
|
2018-11-07 17:30:59 +00:00
|
|
|
void send_signal(byte signal, Process* sender);
|
2019-02-04 13:06:38 +00:00
|
|
|
|
|
|
|
ShouldUnblockProcess dispatch_one_pending_signal();
|
|
|
|
ShouldUnblockProcess dispatch_signal(byte signal);
|
2018-11-07 17:30:59 +00:00
|
|
|
bool has_unmasked_pending_signals() const;
|
|
|
|
void terminate_due_to_signal(byte signal);
|
2018-11-02 13:06:48 +00:00
|
|
|
|
2019-02-03 17:53:18 +00:00
|
|
|
size_t amount_virtual() const;
|
|
|
|
size_t amount_resident() const;
|
|
|
|
size_t amount_shared() const;
|
2019-02-05 08:27:27 +00:00
|
|
|
size_t amount_in_bitmaps() const;
|
2019-02-03 17:53:18 +00:00
|
|
|
|
2018-11-02 19:41:58 +00:00
|
|
|
Process* fork(RegisterDump&);
|
2018-11-03 09:20:23 +00:00
|
|
|
int exec(const String& path, Vector<String>&& arguments, Vector<String>&& environment);
|
2018-11-02 19:41:58 +00:00
|
|
|
|
2018-11-07 00:38:51 +00:00
|
|
|
bool is_root() const { return m_euid == 0; }
|
|
|
|
|
2019-01-16 16:20:58 +00:00
|
|
|
bool wakeup_requested() { return m_wakeup_requested; }
|
|
|
|
void request_wakeup() { m_wakeup_requested = true; }
|
|
|
|
|
2019-01-25 06:52:44 +00:00
|
|
|
FPUState& fpu_state() { return m_fpu_state; }
|
|
|
|
bool has_used_fpu() const { return m_has_used_fpu; }
|
|
|
|
void set_has_used_fpu(bool b) { m_has_used_fpu = b; }
|
|
|
|
|
2019-02-13 16:54:30 +00:00
|
|
|
int gui_client_id() const { return (int)this; }
|
|
|
|
|
2019-02-16 08:57:42 +00:00
|
|
|
Region* allocate_region_with_vmo(LinearAddress, size_t, RetainPtr<VMObject>&&, size_t offset_in_vmo, String&& name, bool is_readable, bool is_writable);
|
|
|
|
Region* allocate_file_backed_region(LinearAddress, size_t, RetainPtr<Inode>&&, String&& name, bool is_readable, bool is_writable);
|
|
|
|
|
2018-10-16 09:01:38 +00:00
|
|
|
private:
|
2018-10-18 11:05:00 +00:00
|
|
|
friend class MemoryManager;
|
2018-11-07 21:15:02 +00:00
|
|
|
friend class Scheduler;
|
2018-11-08 13:35:30 +00:00
|
|
|
friend class Region;
|
2018-10-18 11:05:00 +00:00
|
|
|
|
2019-01-16 11:57:07 +00:00
|
|
|
Process(String&& name, uid_t, gid_t, pid_t ppid, RingLevel, RetainPtr<Inode>&& cwd = nullptr, RetainPtr<Inode>&& executable = nullptr, TTY* = nullptr, Process* fork_parent = nullptr);
|
2018-10-25 09:15:17 +00:00
|
|
|
|
2018-11-09 16:59:14 +00:00
|
|
|
int do_exec(const String& path, Vector<String>&& arguments, Vector<String>&& environment);
|
2018-11-06 09:46:40 +00:00
|
|
|
void push_value_on_stack(dword);
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2018-11-12 00:28:46 +00:00
|
|
|
int alloc_fd();
|
2019-02-04 13:06:38 +00:00
|
|
|
void set_default_signal_dispositions();
|
2018-11-12 00:28:46 +00:00
|
|
|
|
2019-01-22 04:01:00 +00:00
|
|
|
RetainPtr<PageDirectory> m_page_directory;
|
2018-11-01 08:01:51 +00:00
|
|
|
|
2018-11-01 12:15:46 +00:00
|
|
|
Process* m_prev { nullptr };
|
|
|
|
Process* m_next { nullptr };
|
2018-10-16 09:01:38 +00:00
|
|
|
|
|
|
|
String m_name;
|
|
|
|
void (*m_entry)() { nullptr };
|
|
|
|
pid_t m_pid { 0 };
|
|
|
|
uid_t m_uid { 0 };
|
2018-10-22 11:55:11 +00:00
|
|
|
gid_t m_gid { 0 };
|
2018-11-05 14:04:19 +00:00
|
|
|
uid_t m_euid { 0 };
|
|
|
|
gid_t m_egid { 0 };
|
2018-11-02 11:56:51 +00:00
|
|
|
pid_t m_sid { 0 };
|
|
|
|
pid_t m_pgid { 0 };
|
2018-11-16 23:11:08 +00:00
|
|
|
dword m_ticks { 0 };
|
2019-01-31 16:31:23 +00:00
|
|
|
dword m_ticks_left { 0 };
|
|
|
|
dword m_stack_top0 { 0 };
|
|
|
|
dword m_stack_top3 { 0 };
|
|
|
|
FarPtr m_far_ptr;
|
2018-10-16 09:01:38 +00:00
|
|
|
State m_state { Invalid };
|
2019-02-07 11:21:17 +00:00
|
|
|
Priority m_priority { NormalPriority };
|
2019-01-31 16:31:23 +00:00
|
|
|
dword m_wakeup_time { 0 };
|
2018-10-16 09:01:38 +00:00
|
|
|
TSS32 m_tss;
|
2018-11-07 20:19:47 +00:00
|
|
|
TSS32 m_tss_to_resume_kernel;
|
2019-01-25 06:52:44 +00:00
|
|
|
FPUState m_fpu_state;
|
2018-11-13 00:36:31 +00:00
|
|
|
struct FileDescriptorAndFlags {
|
|
|
|
operator bool() const { return !!descriptor; }
|
|
|
|
void clear() { descriptor = nullptr; flags = 0; }
|
2019-01-30 17:26:19 +00:00
|
|
|
void set(RetainPtr<FileDescriptor>&& d, dword f = 0) { descriptor = move(d); flags = f; }
|
2018-11-13 00:36:31 +00:00
|
|
|
RetainPtr<FileDescriptor> descriptor;
|
|
|
|
dword flags { 0 };
|
|
|
|
};
|
|
|
|
Vector<FileDescriptorAndFlags> m_fds;
|
2018-10-16 09:01:38 +00:00
|
|
|
RingLevel m_ring { Ring0 };
|
|
|
|
int m_error { 0 };
|
2019-01-31 16:31:23 +00:00
|
|
|
void* m_kernel_stack { nullptr };
|
|
|
|
dword m_times_scheduled { 0 };
|
2018-11-28 22:30:06 +00:00
|
|
|
pid_t m_waitee_pid { -1 };
|
2018-11-12 00:28:46 +00:00
|
|
|
int m_blocked_fd { -1 };
|
2019-01-15 22:12:20 +00:00
|
|
|
Vector<int> m_select_read_fds;
|
|
|
|
Vector<int> m_select_write_fds;
|
2019-02-01 02:50:06 +00:00
|
|
|
timeval m_select_timeout;
|
|
|
|
bool m_select_has_timeout { false };
|
2018-11-01 12:39:28 +00:00
|
|
|
size_t m_max_open_file_descriptors { 16 };
|
2018-11-06 09:46:40 +00:00
|
|
|
SignalActionData m_signal_action_data[32];
|
2018-11-07 17:30:59 +00:00
|
|
|
dword m_pending_signals { 0 };
|
2018-11-10 22:29:07 +00:00
|
|
|
dword m_signal_mask { 0xffffffff };
|
2019-02-14 16:18:35 +00:00
|
|
|
RetainPtr<Socket> m_blocked_connecting_socket;
|
2018-11-07 17:30:59 +00:00
|
|
|
|
|
|
|
byte m_termination_status { 0 };
|
|
|
|
byte m_termination_signal { 0 };
|
2018-10-18 11:05:00 +00:00
|
|
|
|
2019-01-16 11:57:07 +00:00
|
|
|
RetainPtr<Inode> m_cwd;
|
|
|
|
RetainPtr<Inode> m_executable;
|
2018-10-24 12:28:22 +00:00
|
|
|
|
2018-10-30 12:59:29 +00:00
|
|
|
TTY* m_tty { nullptr };
|
|
|
|
|
2018-11-19 01:17:20 +00:00
|
|
|
Region* allocate_region(LinearAddress, size_t, String&& name, bool is_readable = true, bool is_writable = true, bool commit = true);
|
2018-11-03 10:28:23 +00:00
|
|
|
bool deallocate_region(Region& region);
|
2018-10-24 07:48:24 +00:00
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
Region* region_from_range(LinearAddress, size_t);
|
2018-10-18 12:53:00 +00:00
|
|
|
|
2018-10-27 12:56:52 +00:00
|
|
|
Vector<RetainPtr<Region>> m_regions;
|
2018-10-18 11:05:00 +00:00
|
|
|
|
2018-10-18 12:53:00 +00:00
|
|
|
// FIXME: Implement some kind of ASLR?
|
2019-01-31 16:31:23 +00:00
|
|
|
LinearAddress m_next_region;
|
2018-10-24 12:28:22 +00:00
|
|
|
|
2018-11-07 20:19:47 +00:00
|
|
|
LinearAddress m_return_to_ring3_from_signal_trampoline;
|
|
|
|
LinearAddress m_return_to_ring0_from_signal_trampoline;
|
2018-11-06 09:46:40 +00:00
|
|
|
|
2018-11-06 12:33:06 +00:00
|
|
|
pid_t m_ppid { 0 };
|
2018-11-06 12:40:23 +00:00
|
|
|
mode_t m_umask { 022 };
|
2018-10-26 09:16:56 +00:00
|
|
|
|
2018-11-07 20:19:47 +00:00
|
|
|
bool m_was_interrupted_while_blocked { false };
|
|
|
|
|
2018-11-01 00:04:02 +00:00
|
|
|
static void notify_waiters(pid_t waitee, int exit_status, int signal);
|
2018-10-31 00:06:57 +00:00
|
|
|
|
2019-01-21 01:58:19 +00:00
|
|
|
Vector<String> m_initial_arguments;
|
|
|
|
Vector<String> m_initial_environment;
|
2018-11-07 00:38:51 +00:00
|
|
|
HashTable<gid_t> m_gids;
|
2018-11-07 20:19:47 +00:00
|
|
|
|
|
|
|
Region* m_stack_region { nullptr };
|
|
|
|
Region* m_signal_stack_user_region { nullptr };
|
|
|
|
Region* m_signal_stack_kernel_region { nullptr };
|
2019-01-09 01:29:11 +00:00
|
|
|
|
|
|
|
RetainPtr<Region> m_display_framebuffer_region;
|
2019-01-13 00:59:38 +00:00
|
|
|
|
2019-01-16 16:20:58 +00:00
|
|
|
dword m_wakeup_requested { false };
|
2019-01-25 06:52:44 +00:00
|
|
|
bool m_has_used_fpu { false };
|
2018-10-16 09:01:38 +00:00
|
|
|
};
|
|
|
|
|
2018-11-10 15:50:42 +00:00
|
|
|
extern Process* current;
|
|
|
|
|
|
|
|
class ProcessInspectionHandle {
|
2018-11-01 13:21:02 +00:00
|
|
|
public:
|
2018-11-10 15:50:42 +00:00
|
|
|
ProcessInspectionHandle(Process& process)
|
2018-11-01 13:21:02 +00:00
|
|
|
: m_process(process)
|
|
|
|
, m_original_state(process.state())
|
|
|
|
{
|
2018-11-10 15:50:42 +00:00
|
|
|
if (&process != current)
|
|
|
|
m_process.set_state(Process::BeingInspected);
|
2018-11-01 13:21:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-10 15:50:42 +00:00
|
|
|
~ProcessInspectionHandle()
|
2018-11-01 13:21:02 +00:00
|
|
|
{
|
|
|
|
m_process.set_state(m_original_state);
|
|
|
|
}
|
2018-11-10 15:50:42 +00:00
|
|
|
|
2019-02-03 11:33:11 +00:00
|
|
|
Process& process() { return m_process; }
|
|
|
|
|
|
|
|
static OwnPtr<ProcessInspectionHandle> from_pid(pid_t pid)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
auto* process = Process::from_pid(pid);
|
|
|
|
if (process)
|
|
|
|
return make<ProcessInspectionHandle>(*process);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-10 15:50:42 +00:00
|
|
|
Process* operator->() { return &m_process; }
|
|
|
|
Process& operator*() { return m_process; }
|
2018-11-01 13:21:02 +00:00
|
|
|
private:
|
|
|
|
Process& m_process;
|
|
|
|
Process::State m_original_state { Process::Invalid };
|
|
|
|
};
|
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
static inline const char* to_string(Process::State state)
|
2018-11-05 12:48:07 +00:00
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case Process::Invalid: return "Invalid";
|
|
|
|
case Process::Runnable: return "Runnable";
|
|
|
|
case Process::Running: return "Running";
|
2019-02-06 16:28:14 +00:00
|
|
|
case Process::Dying: return "Dying";
|
2018-11-07 17:30:59 +00:00
|
|
|
case Process::Dead: return "Dead";
|
2018-11-07 22:21:32 +00:00
|
|
|
case Process::Skip1SchedulerPass: return "Skip1";
|
|
|
|
case Process::Skip0SchedulerPasses: return "Skip0";
|
2018-11-05 12:48:07 +00:00
|
|
|
case Process::BlockedSleep: return "Sleep";
|
|
|
|
case Process::BlockedWait: return "Wait";
|
|
|
|
case Process::BlockedRead: return "Read";
|
2018-11-12 00:28:46 +00:00
|
|
|
case Process::BlockedWrite: return "Write";
|
2018-11-10 01:43:33 +00:00
|
|
|
case Process::BlockedSignal: return "Signal";
|
2019-01-15 22:12:20 +00:00
|
|
|
case Process::BlockedSelect: return "Select";
|
2019-02-06 17:45:21 +00:00
|
|
|
case Process::BlockedLurking: return "Lurking";
|
2019-02-14 16:18:35 +00:00
|
|
|
case Process::BlockedConnect: return "Connect";
|
2018-11-05 12:48:07 +00:00
|
|
|
case Process::BeingInspected: return "Inspect";
|
|
|
|
}
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-07 11:21:17 +00:00
|
|
|
static inline const char* to_string(Process::Priority state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case Process::LowPriority: return "Low";
|
|
|
|
case Process::NormalPriority: return "Normal";
|
|
|
|
case Process::HighPriority: return "High";
|
|
|
|
}
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-01 12:15:46 +00:00
|
|
|
extern void block(Process::State);
|
2018-11-16 23:11:08 +00:00
|
|
|
extern void sleep(dword ticks);
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2018-11-07 21:15:02 +00:00
|
|
|
extern InlineLinkedList<Process>* g_processes;
|
2018-11-08 15:09:05 +00:00
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
inline void Process::for_each(Callback callback)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
for (auto* process = g_processes->head(); process;) {
|
|
|
|
auto* next_process = process->next();
|
|
|
|
if (!callback(*process))
|
|
|
|
break;
|
|
|
|
process = next_process;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 14:36:40 +00:00
|
|
|
template<typename Callback>
|
|
|
|
inline void Process::for_each_child(Callback callback)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
pid_t my_pid = pid();
|
|
|
|
for (auto* process = g_processes->head(); process;) {
|
|
|
|
auto* next_process = process->next();
|
|
|
|
if (process->ppid() == my_pid) {
|
|
|
|
if (!callback(*process))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
process = next_process;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 15:09:05 +00:00
|
|
|
template<typename Callback>
|
|
|
|
inline void Process::for_each_in_pgrp(pid_t pgid, Callback callback)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
for (auto* process = g_processes->head(); process;) {
|
|
|
|
auto* next_process = process->next();
|
|
|
|
if (process->pgid() == pgid) {
|
|
|
|
if (!callback(*process))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
process = next_process;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
inline void Process::for_each_in_state(State state, Callback callback)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
for (auto* process = g_processes->head(); process;) {
|
|
|
|
auto* next_process = process->next();
|
|
|
|
if (process->state() == state)
|
|
|
|
callback(*process);
|
|
|
|
process = next_process;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
2019-02-06 16:28:14 +00:00
|
|
|
inline void Process::for_each_living(Callback callback)
|
2018-11-08 15:09:05 +00:00
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
for (auto* process = g_processes->head(); process;) {
|
|
|
|
auto* next_process = process->next();
|
2019-02-06 16:28:14 +00:00
|
|
|
if (process->state() != Process::Dead && process->state() != Process::Dying)
|
2018-11-08 15:09:05 +00:00
|
|
|
callback(*process);
|
|
|
|
process = next_process;
|
|
|
|
}
|
|
|
|
}
|