2019-08-25 19:17:05 +03:00
# include <AK/FileSystemPath.h>
2019-06-07 12:43:58 +03:00
# include <AK/StdLibExtras.h>
2019-02-17 02:13:47 +03:00
# include <AK/StringBuilder.h>
2019-05-18 03:00:01 +03:00
# include <AK/Time.h>
2019-06-07 12:43:58 +03:00
# include <AK/Types.h>
2019-06-07 21:02:01 +03:00
# include <Kernel/Arch/i386/CPU.h>
2019-07-09 15:23:12 +03:00
# include <Kernel/Arch/i386/PIT.h>
2019-11-10 00:18:16 +03:00
# include <Kernel/Console.h>
2019-11-23 10:48:07 +03:00
# include <Kernel/Devices/KeyboardDevice.h>
2019-11-28 22:59:11 +03:00
# include <Kernel/Devices/NullDevice.h>
2019-12-27 00:31:26 +03:00
# include <Kernel/Devices/PCSpeaker.h>
2019-10-13 17:41:55 +03:00
# include <Kernel/Devices/RandomDevice.h>
2019-05-30 19:58:59 +03:00
# include <Kernel/FileSystem/Custody.h>
2019-08-15 19:13:56 +03:00
# include <Kernel/FileSystem/DevPtsFS.h>
2019-08-29 21:57:02 +03:00
# include <Kernel/FileSystem/Ext2FileSystem.h>
2019-06-07 12:43:58 +03:00
# include <Kernel/FileSystem/FIFO.h>
# include <Kernel/FileSystem/FileDescription.h>
2019-07-22 21:01:11 +03:00
# include <Kernel/FileSystem/InodeWatcher.h>
2019-08-29 21:57:02 +03:00
# include <Kernel/FileSystem/ProcFS.h>
2019-07-09 15:50:01 +03:00
# include <Kernel/FileSystem/SharedMemory.h>
2019-08-29 21:57:02 +03:00
# include <Kernel/FileSystem/TmpFS.h>
2019-06-07 12:43:58 +03:00
# include <Kernel/FileSystem/VirtualFileSystem.h>
2019-10-20 17:24:42 +03:00
# include <Kernel/Heap/kmalloc.h>
2019-07-19 10:58:12 +03:00
# include <Kernel/IO.h>
2019-08-07 22:52:43 +03:00
# include <Kernel/KBufferBuilder.h>
2019-06-07 20:29:34 +03:00
# include <Kernel/KSyms.h>
2019-12-15 23:29:26 +03:00
# include <Kernel/KernelInfoPage.h>
2019-11-28 22:59:11 +03:00
# include <Kernel/Module.h>
2019-06-02 10:50:18 +03:00
# include <Kernel/Multiboot.h>
2019-06-07 12:43:58 +03:00
# include <Kernel/Net/Socket.h>
2019-06-07 20:29:34 +03:00
# include <Kernel/Process.h>
2019-06-07 12:43:58 +03:00
# include <Kernel/ProcessTracer.h>
2019-12-11 22:36:56 +03:00
# include <Kernel/Profiling.h>
2019-06-07 20:29:34 +03:00
# include <Kernel/RTC.h>
# include <Kernel/Scheduler.h>
2019-07-16 16:03:39 +03:00
# include <Kernel/SharedBuffer.h>
2019-07-19 10:58:12 +03:00
# include <Kernel/StdLib.h>
2019-06-07 20:29:34 +03:00
# include <Kernel/Syscall.h>
2019-06-07 12:43:58 +03:00
# include <Kernel/TTY/MasterPTY.h>
2019-10-20 19:11:40 +03:00
# include <Kernel/Thread.h>
2019-08-07 19:06:17 +03:00
# include <Kernel/VM/InodeVMObject.h>
2019-12-09 21:12:38 +03:00
# include <Kernel/VM/PurgeableVMObject.h>
2019-06-07 12:43:58 +03:00
# include <LibC/errno_numbers.h>
# include <LibC/signal_numbers.h>
2019-11-06 15:42:38 +03:00
# include <LibELF/ELFLoader.h>
# include <LibELF/exec_elf.h>
2018-10-16 12:01:38 +03:00
2019-05-19 11:24:28 +03:00
//#define DEBUG_POLL_SELECT
2018-10-23 11:12:50 +03:00
//#define DEBUG_IO
2019-03-27 17:07:12 +03:00
//#define TASK_DEBUG
//#define FORK_DEBUG
2019-07-15 21:38:41 +03:00
//#define SIGNAL_DEBUG
2019-02-17 02:13:47 +03:00
//#define SHARED_BUFFER_DEBUG
2018-11-05 15:48:07 +03:00
2019-07-19 18:01:16 +03:00
static void create_signal_trampolines ( ) ;
2019-12-15 23:29:26 +03:00
static void create_kernel_info_page ( ) ;
2019-07-19 18:01:16 +03:00
2018-10-16 12:01:38 +03:00
static pid_t next_pid ;
2018-11-08 00:15:02 +03:00
InlineLinkedList < Process > * g_processes ;
2018-10-26 10:54:29 +03:00
static String * s_hostname ;
2019-02-07 12:29:26 +03:00
static Lock * s_hostname_lock ;
2019-12-16 00:21:28 +03:00
static VirtualAddress s_info_page_address_for_userspace ;
static VirtualAddress s_info_page_address_for_kernel ;
2019-07-19 18:01:16 +03:00
VirtualAddress g_return_to_ring3_from_signal_trampoline ;
VirtualAddress g_return_to_ring0_from_signal_trampoline ;
2019-11-28 22:59:11 +03:00
HashMap < String , OwnPtr < Module > > * g_modules ;
2018-10-26 15:56:21 +03:00
2019-12-22 13:51:24 +03:00
pid_t Process : : allocate_pid ( )
{
InterruptDisabler disabler ;
return next_pid + + ;
}
2018-11-01 15:15:46 +03:00
void Process : : initialize ( )
2018-10-16 12:01:38 +03:00
{
2019-11-28 22:59:11 +03:00
g_modules = new HashMap < String , OwnPtr < Module > > ;
2018-10-16 12:01:38 +03:00
next_pid = 0 ;
2018-11-08 00:15:02 +03:00
g_processes = new InlineLinkedList < Process > ;
2018-11-17 02:23:39 +03:00
s_hostname = new String ( " courage " ) ;
2019-02-07 12:29:26 +03:00
s_hostname_lock = new Lock ;
2019-07-19 18:01:16 +03:00
create_signal_trampolines ( ) ;
2019-12-15 23:29:26 +03:00
create_kernel_info_page ( ) ;
}
void Process : : update_info_page_timestamp ( const timeval & tv )
{
2019-12-16 00:21:28 +03:00
auto * info_page = ( KernelInfoPage * ) s_info_page_address_for_kernel . as_ptr ( ) ;
2019-12-15 23:29:26 +03:00
info_page - > serial + + ;
const_cast < timeval & > ( info_page - > now ) = tv ;
2018-11-02 16:06:48 +03:00
}
2019-02-03 14:33:11 +03:00
Vector < pid_t > Process : : all_pids ( )
{
Vector < pid_t > pids ;
2019-02-06 20:45:21 +03:00
InterruptDisabler disabler ;
2019-12-09 19:48:58 +03:00
pids . ensure_capacity ( ( int ) g_processes - > size_slow ( ) ) ;
2019-08-08 15:40:13 +03:00
for ( auto & process : * g_processes )
pids . append ( process . pid ( ) ) ;
2019-02-03 14:33:11 +03:00
return pids ;
}
2019-01-31 19:31:23 +03:00
Vector < Process * > Process : : all_processes ( )
2018-10-23 13:44:46 +03:00
{
2018-11-01 15:15:46 +03:00
Vector < Process * > processes ;
2019-02-06 20:45:21 +03:00
InterruptDisabler disabler ;
2019-12-09 19:48:58 +03:00
processes . ensure_capacity ( ( int ) g_processes - > size_slow ( ) ) ;
2019-08-08 15:40:13 +03:00
for ( auto & process : * g_processes )
processes . append ( & process ) ;
2018-11-01 15:15:46 +03:00
return processes ;
2018-10-23 13:44:46 +03:00
}
2019-02-27 14:32:53 +03:00
bool Process : : in_group ( gid_t gid ) const
{
return m_gids . contains ( gid ) ;
}
2019-06-07 13:56:50 +03:00
Range Process : : allocate_range ( VirtualAddress vaddr , size_t size )
2018-10-18 14:05:00 +03:00
{
2019-06-07 13:56:50 +03:00
vaddr . mask ( PAGE_MASK ) ;
2019-01-13 02:27:25 +03:00
size = PAGE_ROUND_UP ( size ) ;
2019-06-07 13:56:50 +03:00
if ( vaddr . is_null ( ) )
2019-05-20 05:46:29 +03:00
return page_directory ( ) . range_allocator ( ) . allocate_anywhere ( size ) ;
2019-06-07 13:56:50 +03:00
return page_directory ( ) . range_allocator ( ) . allocate_specific ( vaddr , size ) ;
2019-05-17 05:39:22 +03:00
}
2019-05-17 04:40:15 +03:00
2019-05-30 17:14:37 +03:00
static unsigned prot_to_region_access_flags ( int prot )
{
unsigned access = 0 ;
if ( prot & PROT_READ )
access | = Region : : Access : : Read ;
if ( prot & PROT_WRITE )
access | = Region : : Access : : Write ;
if ( prot & PROT_EXEC )
access | = Region : : Access : : Execute ;
return access ;
}
2019-12-19 21:13:44 +03:00
Region & Process : : allocate_split_region ( const Region & source_region , const Range & range , size_t offset_in_vmobject )
2019-08-29 21:57:02 +03:00
{
2019-12-19 21:13:44 +03:00
m_regions . append ( Region : : create_user_accessible ( range , source_region . vmobject ( ) , offset_in_vmobject , source_region . name ( ) , source_region . access ( ) ) ) ;
2019-08-29 21:57:02 +03:00
return m_regions . last ( ) ;
}
2019-06-07 21:58:12 +03:00
Region * Process : : allocate_region ( VirtualAddress vaddr , size_t size , const String & name , int prot , bool commit )
2019-05-17 05:39:22 +03:00
{
2019-06-07 13:56:50 +03:00
auto range = allocate_range ( vaddr , size ) ;
2019-05-17 05:39:22 +03:00
if ( ! range . is_valid ( ) )
return nullptr ;
2019-07-19 17:09:34 +03:00
m_regions . append ( Region : : create_user_accessible ( range , name , prot_to_region_access_flags ( prot ) ) ) ;
2019-11-03 22:48:35 +03:00
m_regions . last ( ) . map ( page_directory ( ) ) ;
2019-01-22 07:01:00 +03:00
if ( commit )
2019-06-27 14:34:28 +03:00
m_regions . last ( ) . commit ( ) ;
return & m_regions . last ( ) ;
2018-10-18 14:05:00 +03:00
}
2019-07-19 17:09:34 +03:00
Region * Process : : allocate_file_backed_region ( VirtualAddress vaddr , size_t size , NonnullRefPtr < Inode > inode , const String & name , int prot )
2018-11-08 14:59:16 +03:00
{
2019-06-07 13:56:50 +03:00
auto range = allocate_range ( vaddr , size ) ;
2019-05-17 05:39:22 +03:00
if ( ! range . is_valid ( ) )
return nullptr ;
2019-07-19 17:09:34 +03:00
m_regions . append ( Region : : create_user_accessible ( range , inode , name , prot_to_region_access_flags ( prot ) ) ) ;
2019-11-03 22:48:35 +03:00
m_regions . last ( ) . map ( page_directory ( ) ) ;
2019-06-27 14:34:28 +03:00
return & m_regions . last ( ) ;
2018-11-08 14:59:16 +03:00
}
2019-12-19 21:13:44 +03:00
Region * Process : : allocate_region_with_vmobject ( VirtualAddress vaddr , size_t size , NonnullRefPtr < VMObject > vmobject , size_t offset_in_vmobject , const String & name , int prot )
2018-11-08 23:20:09 +03:00
{
2019-06-07 13:56:50 +03:00
auto range = allocate_range ( vaddr , size ) ;
2019-05-17 05:39:22 +03:00
if ( ! range . is_valid ( ) )
return nullptr ;
2019-12-19 21:13:44 +03:00
offset_in_vmobject & = PAGE_MASK ;
m_regions . append ( Region : : create_user_accessible ( range , move ( vmobject ) , offset_in_vmobject , name , prot_to_region_access_flags ( prot ) ) ) ;
2019-11-03 22:48:35 +03:00
m_regions . last ( ) . map ( page_directory ( ) ) ;
2019-06-27 14:34:28 +03:00
return & m_regions . last ( ) ;
2018-11-08 23:20:09 +03:00
}
2018-11-03 13:28:23 +03:00
bool Process : : deallocate_region ( Region & region )
2018-10-24 10:48:24 +03:00
{
2018-10-28 11:36:21 +03:00
InterruptDisabler disabler ;
2019-02-26 00:06:55 +03:00
for ( int i = 0 ; i < m_regions . size ( ) ; + + i ) {
2019-06-27 14:34:28 +03:00
if ( & m_regions [ i ] = = & region ) {
2018-10-24 10:48:24 +03:00
m_regions . remove ( i ) ;
return true ;
}
}
return false ;
}
2019-08-29 21:57:02 +03:00
Region * Process : : region_from_range ( const Range & range )
{
size_t size = PAGE_ROUND_UP ( range . size ( ) ) ;
for ( auto & region : m_regions ) {
if ( region . vaddr ( ) = = range . base ( ) & & region . size ( ) = = size )
return & region ;
}
return nullptr ;
}
Region * Process : : region_containing ( const Range & range )
2018-10-24 10:48:24 +03:00
{
for ( auto & region : m_regions ) {
2019-08-29 21:57:02 +03:00
if ( region . contains ( range ) )
2019-06-27 14:34:28 +03:00
return & region ;
2018-10-24 10:48:24 +03:00
}
return nullptr ;
}
2018-11-01 15:15:46 +03:00
int Process : : sys $ set_mmap_name ( void * addr , size_t size , const char * name )
2018-10-28 11:57:22 +03:00
{
2018-11-16 18:23:39 +03:00
if ( ! validate_read_str ( name ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2019-08-29 21:57:02 +03:00
auto * region = region_from_range ( { VirtualAddress ( ( u32 ) addr ) , size } ) ;
2018-10-28 11:57:22 +03:00
if ( ! region )
return - EINVAL ;
2019-11-24 14:24:16 +03:00
if ( ! region - > is_mmap ( ) )
return - EPERM ;
2019-01-24 20:09:46 +03:00
region - > set_name ( String ( name ) ) ;
2018-10-28 11:57:22 +03:00
return 0 ;
}
2019-12-25 23:50:13 +03:00
static bool validate_mmap_prot ( int prot , bool map_stack )
{
bool readable = prot & PROT_READ ;
bool writable = prot & PROT_WRITE ;
bool executable = prot & PROT_EXEC ;
if ( writable & & executable )
return false ;
if ( map_stack ) {
if ( executable )
return false ;
if ( ! readable | | ! writable )
return false ;
}
return true ;
}
2018-11-08 13:37:01 +03:00
void * Process : : sys $ mmap ( const Syscall : : SC_mmap_params * params )
2018-10-24 10:48:24 +03:00
{
2018-11-16 18:10:59 +03:00
if ( ! validate_read ( params , sizeof ( Syscall : : SC_mmap_params ) ) )
return ( void * ) - EFAULT ;
2019-11-10 17:32:59 +03:00
auto & [ addr , size , prot , flags , fd , offset , name ] = * params ;
if ( name & & ! validate_read_str ( name ) )
2019-05-19 16:54:56 +03:00
return ( void * ) - EFAULT ;
2019-12-25 23:50:13 +03:00
2018-11-08 14:59:16 +03:00
if ( size = = 0 )
return ( void * ) - EINVAL ;
2019-07-03 22:17:35 +03:00
if ( ( u32 ) addr & ~ PAGE_MASK )
2018-11-08 14:59:16 +03:00
return ( void * ) - EINVAL ;
2019-12-25 23:50:13 +03:00
bool map_shared = flags & MAP_SHARED ;
bool map_anonymous = flags & MAP_ANONYMOUS ;
bool map_purgeable = flags & MAP_PURGEABLE ;
bool map_private = flags & MAP_PRIVATE ;
bool map_stack = flags & MAP_STACK ;
2019-12-29 08:54:10 +03:00
bool map_fixed = flags & MAP_FIXED ;
2019-12-25 23:50:13 +03:00
if ( map_shared & & map_private )
2019-10-01 20:31:55 +03:00
return ( void * ) - EINVAL ;
2019-11-17 14:11:43 +03:00
2019-12-25 23:50:13 +03:00
if ( ! map_shared & & ! map_private )
2019-12-25 13:54:16 +03:00
return ( void * ) - EINVAL ;
2019-12-25 23:50:13 +03:00
if ( ! validate_mmap_prot ( prot , map_stack ) )
2019-11-17 14:11:43 +03:00
return ( void * ) - EINVAL ;
2019-12-25 23:50:13 +03:00
if ( map_stack & & ( ! map_private | | ! map_anonymous ) )
2019-11-17 14:11:43 +03:00
return ( void * ) - EINVAL ;
2019-12-25 23:50:13 +03:00
Region * region = nullptr ;
if ( map_purgeable ) {
2019-12-09 21:12:38 +03:00
auto vmobject = PurgeableVMObject : : create_with_size ( size ) ;
2019-12-25 23:50:13 +03:00
region = allocate_region_with_vmobject ( VirtualAddress ( ( u32 ) addr ) , size , vmobject , 0 , name ? name : " mmap (purgeable) " , prot ) ;
2019-12-29 08:54:10 +03:00
if ( ! region & & ( ! map_fixed & & addr ! = 0 ) )
region = allocate_region_with_vmobject ( { } , size , vmobject , 0 , name ? name : " mmap (purgeable) " , prot ) ;
2019-12-25 23:50:13 +03:00
} else if ( map_anonymous ) {
region = allocate_region ( VirtualAddress ( ( u32 ) addr ) , size , name ? name : " mmap " , prot , false ) ;
2019-12-29 08:54:10 +03:00
if ( ! region & & ( ! map_fixed & & addr ! = 0 ) )
region = allocate_region ( { } , size , name ? name : " mmap " , prot , false ) ;
2019-12-25 23:50:13 +03:00
} else {
if ( offset < 0 )
return ( void * ) - EINVAL ;
if ( static_cast < size_t > ( offset ) & ~ PAGE_MASK )
return ( void * ) - EINVAL ;
auto * description = file_description ( fd ) ;
if ( ! description )
return ( void * ) - EBADF ;
auto region_or_error = description - > mmap ( * this , VirtualAddress ( ( u32 ) addr ) , static_cast < size_t > ( offset ) , size , prot ) ;
2019-12-29 08:54:10 +03:00
if ( region_or_error . is_error ( ) ) {
// Fail if MAP_FIXED or address is 0, retry otherwise
if ( map_fixed | | addr = = 0 )
return ( void * ) ( int ) region_or_error . error ( ) ;
region_or_error = description - > mmap ( * this , { } , static_cast < size_t > ( offset ) , size , prot ) ;
}
2019-12-25 23:50:13 +03:00
if ( region_or_error . is_error ( ) )
return ( void * ) ( int ) region_or_error . error ( ) ;
region = region_or_error . value ( ) ;
2019-12-09 21:12:38 +03:00
}
2019-12-25 23:50:13 +03:00
if ( ! region )
return ( void * ) - ENOMEM ;
region - > set_mmap ( true ) ;
if ( map_shared )
2019-02-17 02:13:47 +03:00
region - > set_shared ( true ) ;
2019-12-25 23:50:13 +03:00
if ( map_stack )
region - > set_stack ( true ) ;
2019-05-19 16:54:56 +03:00
if ( name )
region - > set_name ( name ) ;
2019-06-07 13:56:50 +03:00
return region - > vaddr ( ) . as_ptr ( ) ;
2018-10-24 10:48:24 +03:00
}
2018-11-01 15:15:46 +03:00
int Process : : sys $ munmap ( void * addr , size_t size )
2018-10-24 10:48:24 +03:00
{
2019-08-29 21:57:02 +03:00
Range range_to_unmap { VirtualAddress ( ( u32 ) addr ) , size } ;
if ( auto * whole_region = region_from_range ( range_to_unmap ) ) {
2019-11-24 14:24:16 +03:00
if ( ! whole_region - > is_mmap ( ) )
return - EPERM ;
2019-08-29 21:57:02 +03:00
bool success = deallocate_region ( * whole_region ) ;
ASSERT ( success ) ;
return 0 ;
}
if ( auto * old_region = region_containing ( range_to_unmap ) ) {
2019-11-24 14:24:16 +03:00
if ( ! old_region - > is_mmap ( ) )
return - EPERM ;
2019-08-29 21:57:02 +03:00
Range old_region_range = old_region - > range ( ) ;
auto remaining_ranges_after_unmap = old_region_range . carve ( range_to_unmap ) ;
ASSERT ( ! remaining_ranges_after_unmap . is_empty ( ) ) ;
auto make_replacement_region = [ & ] ( const Range & new_range ) - > Region & {
ASSERT ( new_range . base ( ) > = old_region_range . base ( ) ) ;
2019-09-27 21:17:41 +03:00
ASSERT ( new_range . end ( ) < = old_region_range . end ( ) ) ;
2019-10-01 12:38:59 +03:00
size_t new_range_offset_in_vmobject = old_region - > offset_in_vmobject ( ) + ( new_range . base ( ) . get ( ) - old_region_range . base ( ) . get ( ) ) ;
return allocate_split_region ( * old_region , new_range , new_range_offset_in_vmobject ) ;
2019-08-29 21:57:02 +03:00
} ;
Vector < Region * , 2 > new_regions ;
for ( auto & new_range : remaining_ranges_after_unmap ) {
new_regions . unchecked_append ( & make_replacement_region ( new_range ) ) ;
}
2019-09-27 21:17:41 +03:00
// We manually unmap the old region here, specifying that we *don't* want the VM deallocated.
2019-11-03 22:37:03 +03:00
old_region - > unmap ( Region : : ShouldDeallocateVirtualMemoryRange : : No ) ;
2019-08-29 21:57:02 +03:00
deallocate_region ( * old_region ) ;
2019-09-27 21:17:41 +03:00
// Instead we give back the unwanted VM manually.
page_directory ( ) . range_allocator ( ) . deallocate ( range_to_unmap ) ;
// And finally we map the new region(s).
2019-08-29 21:57:02 +03:00
for ( auto * new_region : new_regions ) {
2019-11-03 22:48:35 +03:00
new_region - > map ( page_directory ( ) ) ;
2019-08-29 21:57:02 +03:00
}
return 0 ;
}
// FIXME: We should also support munmap() across multiple regions. (#175)
return - EINVAL ;
2018-10-24 10:48:24 +03:00
}
2019-08-12 20:33:24 +03:00
int Process : : sys $ mprotect ( void * addr , size_t size , int prot )
{
2019-08-29 21:57:02 +03:00
auto * region = region_from_range ( { VirtualAddress ( ( u32 ) addr ) , size } ) ;
2019-08-12 20:33:24 +03:00
if ( ! region )
return - EINVAL ;
2019-11-24 14:24:16 +03:00
if ( ! region - > is_mmap ( ) )
return - EPERM ;
2019-12-25 23:50:13 +03:00
if ( ! validate_mmap_prot ( prot , region - > is_stack ( ) ) )
2019-12-25 13:54:16 +03:00
return - EINVAL ;
2019-12-02 21:14:16 +03:00
region - > set_readable ( prot & PROT_READ ) ;
2019-08-12 20:33:24 +03:00
region - > set_writable ( prot & PROT_WRITE ) ;
2019-12-25 13:54:16 +03:00
region - > set_executable ( prot & PROT_EXEC ) ;
2019-11-03 22:59:54 +03:00
region - > remap ( ) ;
2019-08-12 20:33:24 +03:00
return 0 ;
}
2019-12-09 21:12:38 +03:00
int Process : : sys $ madvise ( void * address , size_t size , int advice )
{
auto * region = region_from_range ( { VirtualAddress ( ( u32 ) address ) , size } ) ;
if ( ! region )
return - EINVAL ;
if ( ! region - > is_mmap ( ) )
return - EPERM ;
if ( ( advice & MADV_SET_VOLATILE ) & & ( advice & MADV_SET_NONVOLATILE ) )
return - EINVAL ;
if ( advice & MADV_SET_VOLATILE ) {
if ( ! region - > vmobject ( ) . is_purgeable ( ) )
return - EPERM ;
auto & vmobject = static_cast < PurgeableVMObject & > ( region - > vmobject ( ) ) ;
vmobject . set_volatile ( true ) ;
return 0 ;
}
if ( advice & MADV_SET_NONVOLATILE ) {
if ( ! region - > vmobject ( ) . is_purgeable ( ) )
return - EPERM ;
auto & vmobject = static_cast < PurgeableVMObject & > ( region - > vmobject ( ) ) ;
2019-12-18 22:48:58 +03:00
if ( ! vmobject . is_volatile ( ) )
return 0 ;
2019-12-09 21:12:38 +03:00
vmobject . set_volatile ( false ) ;
bool was_purged = vmobject . was_purged ( ) ;
vmobject . set_was_purged ( false ) ;
return was_purged ? 1 : 0 ;
}
2019-12-18 22:48:24 +03:00
if ( advice & MADV_GET_VOLATILE ) {
if ( ! region - > vmobject ( ) . is_purgeable ( ) )
return - EPERM ;
auto & vmobject = static_cast < PurgeableVMObject & > ( region - > vmobject ( ) ) ;
return vmobject . is_volatile ( ) ? 0 : 1 ;
}
2019-12-09 21:12:38 +03:00
return - EINVAL ;
}
2019-12-29 15:16:53 +03:00
int Process : : sys $ purge ( int mode )
2019-12-09 21:12:38 +03:00
{
int purged_page_count = 0 ;
2019-12-29 15:16:53 +03:00
if ( mode & PURGE_ALL_VOLATILE ) {
NonnullRefPtrVector < PurgeableVMObject > vmobjects ;
{
InterruptDisabler disabler ;
MM . for_each_vmobject ( [ & ] ( auto & vmobject ) {
if ( vmobject . is_purgeable ( ) )
vmobjects . append ( static_cast < PurgeableVMObject & > ( vmobject ) ) ;
return IterationDecision : : Continue ;
} ) ;
}
for ( auto & vmobject : vmobjects ) {
purged_page_count + = vmobject . purge ( ) ;
}
}
if ( mode & PURGE_ALL_CLEAN_INODE ) {
NonnullRefPtrVector < InodeVMObject > vmobjects ;
{
InterruptDisabler disabler ;
MM . for_each_vmobject ( [ & ] ( auto & vmobject ) {
if ( vmobject . is_inode ( ) )
vmobjects . append ( static_cast < InodeVMObject & > ( vmobject ) ) ;
return IterationDecision : : Continue ;
} ) ;
}
for ( auto & vmobject : vmobjects ) {
purged_page_count + = vmobject . release_all_clean_pages ( ) ;
}
2019-12-09 21:12:38 +03:00
}
return purged_page_count ;
}
2019-02-25 23:19:57 +03:00
int Process : : sys $ gethostname ( char * buffer , ssize_t size )
2018-10-26 10:54:29 +03:00
{
2019-02-25 23:19:57 +03:00
if ( size < 0 )
return - EINVAL ;
2018-11-16 18:10:59 +03:00
if ( ! validate_write ( buffer , size ) )
return - EFAULT ;
2019-02-07 12:29:26 +03:00
LOCKER ( * s_hostname_lock ) ;
2019-12-09 19:45:40 +03:00
if ( ( size_t ) size < ( s_hostname - > length ( ) + 1 ) )
2018-10-26 10:54:29 +03:00
return - ENAMETOOLONG ;
2019-02-07 12:29:26 +03:00
strcpy ( buffer , s_hostname - > characters ( ) ) ;
2018-10-26 12:16:56 +03:00
return 0 ;
2018-10-26 10:54:29 +03:00
}
2019-12-19 21:07:41 +03:00
pid_t Process : : sys $ fork ( RegisterDump & regs )
2018-11-02 22:41:58 +03:00
{
2019-12-22 13:35:02 +03:00
Thread * child_first_thread = nullptr ;
auto * child = new Process ( child_first_thread , m_name , m_uid , m_gid , m_pid , m_ring , m_cwd , m_executable , m_tty , this ) ;
2018-11-11 01:29:07 +03:00
2018-11-02 22:41:58 +03:00
# ifdef FORK_DEBUG
dbgprintf ( " fork: child=%p \n " , child ) ;
# endif
for ( auto & region : m_regions ) {
# ifdef FORK_DEBUG
2019-09-27 14:57:35 +03:00
dbg ( ) < < " fork: cloning Region{ " < < & region < < " } ' " < < region . name ( ) < < " ' @ " < < region . vaddr ( ) ;
2018-11-02 22:41:58 +03:00
# endif
2019-09-27 15:19:07 +03:00
child - > m_regions . append ( region . clone ( ) ) ;
2019-11-03 22:48:35 +03:00
child - > m_regions . last ( ) . map ( child - > page_directory ( ) ) ;
2019-09-07 16:50:44 +03:00
if ( & region = = m_master_tls_region )
2019-09-27 15:19:07 +03:00
child - > m_master_tls_region = & child - > m_regions . last ( ) ;
2018-11-02 22:41:58 +03:00
}
2019-01-21 03:49:30 +03:00
for ( auto gid : m_gids )
child - > m_gids . set ( gid ) ;
2019-12-22 13:35:02 +03:00
auto & child_tss = child_first_thread - > m_tss ;
2019-03-24 00:03:17 +03:00
child_tss . eax = 0 ; // fork() returns 0 in the child :^)
child_tss . ebx = regs . ebx ;
child_tss . ecx = regs . ecx ;
child_tss . edx = regs . edx ;
child_tss . ebp = regs . ebp ;
child_tss . esp = regs . esp_if_crossRing ;
child_tss . esi = regs . esi ;
child_tss . edi = regs . edi ;
child_tss . eflags = regs . eflags ;
child_tss . eip = regs . eip ;
child_tss . cs = regs . cs ;
child_tss . ds = regs . ds ;
child_tss . es = regs . es ;
child_tss . fs = regs . fs ;
child_tss . gs = regs . gs ;
child_tss . ss = regs . ss_if_crossRing ;
2019-01-25 09:52:44 +03:00
2018-11-02 22:41:58 +03:00
# ifdef FORK_DEBUG
2019-03-24 00:03:17 +03:00
dbgprintf ( " fork: child will begin executing at %w:%x with stack %w:%x, kstack %w:%x \n " , child_tss . cs , child_tss . eip , child_tss . ss , child_tss . esp , child_tss . ss0 , child_tss . esp0 ) ;
2018-11-02 22:41:58 +03:00
# endif
2018-11-09 03:25:31 +03:00
{
InterruptDisabler disabler ;
g_processes - > prepend ( child ) ;
}
2018-11-02 22:41:58 +03:00
# ifdef TASK_DEBUG
2019-03-24 00:03:17 +03:00
kprintf ( " Process %u (%s) forked from %u @ %p \n " , child - > pid ( ) , child - > name ( ) . characters ( ) , m_pid , child_tss . eip ) ;
2018-11-02 22:41:58 +03:00
# endif
2019-03-24 00:03:17 +03:00
2019-12-22 13:35:02 +03:00
child_first_thread - > set_state ( Thread : : State : : Skip1SchedulerPass ) ;
2018-11-02 22:41:58 +03:00
return child - > pid ( ) ;
}
2019-02-17 12:18:25 +03:00
int Process : : do_exec ( String path , Vector < String > arguments , Vector < String > environment )
2018-11-03 03:49:40 +03:00
{
2019-02-03 05:56:08 +03:00
ASSERT ( is_ring3 ( ) ) ;
2019-04-25 14:52:07 +03:00
dbgprintf ( " %s(%d) do_exec(%s): thread_count() = %d \n " , m_name . characters ( ) , m_pid , path . characters ( ) , thread_count ( ) ) ;
2019-03-24 00:03:17 +03:00
// FIXME(Thread): Kill any threads the moment we commit to the exec().
2019-04-23 23:17:01 +03:00
if ( thread_count ( ) ! = 1 ) {
dbgprintf ( " Gonna die because I have many threads! These are the threads: \n " ) ;
2019-06-07 12:43:58 +03:00
for_each_thread ( [ ] ( Thread & thread ) {
2019-04-23 23:17:01 +03:00
dbgprintf ( " Thread{%p}: TID=%d, PID=%d \n " , & thread , thread . tid ( ) , thread . pid ( ) ) ;
return IterationDecision : : Continue ;
} ) ;
ASSERT ( thread_count ( ) = = 1 ) ;
ASSERT_NOT_REACHED ( ) ;
}
2019-10-20 19:11:40 +03:00
size_t total_blob_size = 0 ;
for ( auto & a : arguments )
total_blob_size + = a . length ( ) + 1 ;
for ( auto & e : environment )
total_blob_size + = e . length ( ) + 1 ;
size_t total_meta_size = sizeof ( char * ) * ( arguments . size ( ) + 1 ) + sizeof ( char * ) * ( environment . size ( ) + 1 ) ;
// FIXME: How much stack space does process startup need?
if ( ( total_blob_size + total_meta_size ) > = Thread : : default_userspace_stack_size )
return - E2BIG ;
2018-11-03 03:49:40 +03:00
auto parts = path . split ( ' / ' ) ;
2018-12-21 04:10:45 +03:00
if ( parts . is_empty ( ) )
2018-11-03 03:49:40 +03:00
return - ENOENT ;
2019-06-02 13:19:21 +03:00
auto result = VFS : : the ( ) . open ( path , 0 , 0 , current_directory ( ) ) ;
2019-03-07 00:14:31 +03:00
if ( result . is_error ( ) )
return result . error ( ) ;
2019-06-13 23:03:04 +03:00
auto description = result . value ( ) ;
auto metadata = description - > metadata ( ) ;
2018-11-03 03:49:40 +03:00
2019-05-31 08:02:43 +03:00
if ( ! metadata . may_execute ( m_euid , m_gids ) )
2018-11-03 03:49:40 +03:00
return - EACCES ;
2019-05-31 08:02:43 +03:00
if ( ! metadata . size )
2018-11-08 23:20:09 +03:00
return - ENOTIMPL ;
2019-07-03 22:17:35 +03:00
u32 entry_eip = 0 ;
2018-12-31 16:58:03 +03:00
// FIXME: Is there a race here?
auto old_page_directory = move ( m_page_directory ) ;
2019-08-06 12:19:16 +03:00
m_page_directory = PageDirectory : : create_for_userspace ( * this ) ;
2018-11-09 03:25:31 +03:00
# ifdef MM_DEBUG
2018-12-31 16:58:03 +03:00
dbgprintf ( " Process %u exec: PD=%x created \n " , pid ( ) , m_page_directory . ptr ( ) ) ;
2018-11-09 03:25:31 +03:00
# endif
ProcessPagingScope paging_scope ( * this ) ;
2018-11-08 23:20:09 +03:00
2019-08-07 19:06:17 +03:00
ASSERT ( description - > inode ( ) ) ;
2019-12-19 21:13:44 +03:00
auto vmobject = InodeVMObject : : create_with_inode ( * description - > inode ( ) ) ;
auto * region = allocate_region_with_vmobject ( VirtualAddress ( ) , metadata . size , vmobject , 0 , description - > absolute_path ( ) , PROT_READ ) ;
2019-05-17 05:39:22 +03:00
ASSERT ( region ) ;
2019-01-22 07:01:00 +03:00
2019-09-27 15:19:07 +03:00
// NOTE: We yank this out of 'm_regions' since we're about to manipulate the vector
// and we don't want it getting lost.
auto executable_region = m_regions . take_last ( ) ;
Region * master_tls_region { nullptr } ;
2019-09-07 16:50:44 +03:00
size_t master_tls_size = 0 ;
size_t master_tls_alignment = 0 ;
2019-05-16 18:18:25 +03:00
OwnPtr < ELFLoader > loader ;
2018-11-03 03:49:40 +03:00
{
// Okay, here comes the sleight of hand, pay close attention..
auto old_regions = move ( m_regions ) ;
2019-09-27 15:19:07 +03:00
m_regions . append ( move ( executable_region ) ) ;
2019-06-07 13:56:50 +03:00
loader = make < ELFLoader > ( region - > vaddr ( ) . as_ptr ( ) ) ;
2019-10-20 17:24:42 +03:00
loader - > map_section_hook = [ & ] ( VirtualAddress vaddr , size_t size , size_t alignment , size_t offset_in_image , bool is_readable , bool is_writable , bool is_executable , const String & name ) - > u8 * {
2018-11-08 23:20:09 +03:00
ASSERT ( size ) ;
2018-11-09 12:03:21 +03:00
ASSERT ( alignment = = PAGE_SIZE ) ;
2019-05-30 17:14:37 +03:00
int prot = 0 ;
if ( is_readable )
prot | = PROT_READ ;
if ( is_writable )
prot | = PROT_WRITE ;
if ( is_executable )
prot | = PROT_EXEC ;
2019-12-19 21:13:44 +03:00
if ( ! allocate_region_with_vmobject ( vaddr , size , vmobject , offset_in_image , String ( name ) , prot ) )
2019-10-20 17:24:42 +03:00
return nullptr ;
2019-06-07 13:56:50 +03:00
return vaddr . as_ptr ( ) ;
2018-11-08 23:20:09 +03:00
} ;
2019-10-20 17:24:42 +03:00
loader - > alloc_section_hook = [ & ] ( VirtualAddress vaddr , size_t size , size_t alignment , bool is_readable , bool is_writable , const String & name ) - > u8 * {
2018-11-03 13:28:23 +03:00
ASSERT ( size ) ;
2018-11-09 12:03:21 +03:00
ASSERT ( alignment = = PAGE_SIZE ) ;
2019-05-30 17:14:37 +03:00
int prot = 0 ;
if ( is_readable )
prot | = PROT_READ ;
if ( is_writable )
prot | = PROT_WRITE ;
2019-10-20 17:24:42 +03:00
if ( ! allocate_region ( vaddr , size , String ( name ) , prot ) )
return nullptr ;
2019-06-07 13:56:50 +03:00
return vaddr . as_ptr ( ) ;
2018-11-03 03:49:40 +03:00
} ;
2019-09-07 16:50:44 +03:00
loader - > tls_section_hook = [ & ] ( size_t size , size_t alignment ) {
ASSERT ( size ) ;
master_tls_region = allocate_region ( { } , size , String ( ) , PROT_READ | PROT_WRITE ) ;
master_tls_size = size ;
master_tls_alignment = alignment ;
return master_tls_region - > vaddr ( ) . as_ptr ( ) ;
} ;
2019-05-16 18:18:25 +03:00
bool success = loader - > load ( ) ;
if ( ! success | | ! loader - > entry ( ) . get ( ) ) {
2018-12-31 16:58:03 +03:00
m_page_directory = move ( old_page_directory ) ;
2019-01-01 04:09:43 +03:00
// FIXME: RAII this somehow instead.
2019-03-24 00:03:17 +03:00
ASSERT ( & current - > process ( ) = = this ) ;
2018-11-03 04:04:36 +03:00
MM . enter_process_paging_scope ( * this ) ;
2019-09-27 15:19:07 +03:00
executable_region = m_regions . take_first ( ) ;
2018-11-03 03:49:40 +03:00
m_regions = move ( old_regions ) ;
2019-03-27 06:01:15 +03:00
kprintf ( " do_exec: Failure loading %s \n " , path . characters ( ) ) ;
2018-11-03 03:49:40 +03:00
return - ENOEXEC ;
}
2019-05-31 08:02:43 +03:00
// NOTE: At this point, we've committed to the new executable.
2019-05-16 18:18:25 +03:00
entry_eip = loader - > entry ( ) . get ( ) ;
2018-11-03 03:49:40 +03:00
}
2019-12-15 22:11:57 +03:00
region - > set_user_accessible ( false ) ;
region - > remap ( ) ;
2019-05-16 18:18:25 +03:00
m_elf_loader = move ( loader ) ;
2019-06-13 23:03:04 +03:00
m_executable = description - > custody ( ) ;
2019-05-31 08:02:43 +03:00
2019-09-07 16:50:44 +03:00
// Copy of the master TLS region that we will clone for new threads
2019-09-27 15:19:07 +03:00
m_master_tls_region = master_tls_region ;
2019-09-07 16:50:44 +03:00
2019-05-31 08:02:43 +03:00
if ( metadata . is_setuid ( ) )
m_euid = metadata . uid ;
if ( metadata . is_setgid ( ) )
m_egid = metadata . gid ;
2019-05-16 18:18:25 +03:00
2019-03-24 00:03:17 +03:00
current - > set_default_signal_dispositions ( ) ;
current - > m_signal_mask = 0 ;
current - > m_pending_signals = 0 ;
2018-11-11 01:29:07 +03:00
2019-02-26 00:06:55 +03:00
for ( int i = 0 ; i < m_fds . size ( ) ; + + i ) {
2018-11-13 03:36:31 +03:00
auto & daf = m_fds [ i ] ;
2019-06-13 23:03:04 +03:00
if ( daf . description & & daf . flags & FD_CLOEXEC ) {
daf . description - > close ( ) ;
2019-06-07 12:43:58 +03:00
daf = { } ;
2018-11-13 03:36:31 +03:00
}
}
2019-12-22 13:35:02 +03:00
// FIXME: Should we just make a new Thread here instead?
Thread * new_main_thread = nullptr ;
if ( & current - > process ( ) = = this ) {
new_main_thread = current ;
} else {
for_each_thread ( [ & ] ( auto & thread ) {
new_main_thread = & thread ;
return IterationDecision : : Break ;
} ) ;
}
ASSERT ( new_main_thread ) ;
2019-12-19 01:03:23 +03:00
// NOTE: We create the new stack before disabling interrupts since it will zero-fault
// and we don't want to deal with faults after this point.
2019-12-22 13:35:02 +03:00
u32 new_userspace_esp = new_main_thread - > make_userspace_stack_for_main_thread ( move ( arguments ) , move ( environment ) ) ;
2019-12-19 01:03:23 +03:00
2018-11-17 02:52:29 +03:00
// We cli() manually here because we don't want to get interrupted between do_exec() and Schedule::yield().
// The reason is that the task redirection we've set up above will be clobbered by the timer IRQ.
// If we used an InterruptDisabler that sti()'d on exit, we might timer tick'd too soon in exec().
2019-03-24 00:03:17 +03:00
if ( & current - > process ( ) = = this )
2019-02-06 19:28:14 +03:00
cli ( ) ;
2018-11-17 02:52:29 +03:00
2019-12-19 01:03:23 +03:00
// NOTE: Be careful to not trigger any page faults below!
2019-12-22 13:35:02 +03:00
Scheduler : : prepare_to_modify_tss ( * new_main_thread ) ;
2018-11-03 03:49:40 +03:00
2019-01-20 00:53:05 +03:00
m_name = parts . take_last ( ) ;
2019-12-22 13:35:02 +03:00
new_main_thread - > set_name ( m_name ) ;
auto & tss = new_main_thread - > m_tss ;
2018-11-03 03:49:40 +03:00
2019-12-22 13:35:02 +03:00
u32 old_esp0 = tss . esp0 ;
2019-03-24 00:03:17 +03:00
2019-09-07 16:50:44 +03:00
m_master_tls_size = master_tls_size ;
m_master_tls_alignment = master_tls_alignment ;
2019-12-22 13:35:02 +03:00
new_main_thread - > make_thread_specific_region ( { } ) ;
memset ( & tss , 0 , sizeof ( TSS32 ) ) ;
2020-01-01 19:26:25 +03:00
tss . iomapbase = sizeof ( TSS32 ) ;
2019-12-22 13:35:02 +03:00
tss . eflags = 0x0202 ;
tss . eip = entry_eip ;
tss . cs = 0x1b ;
tss . ds = 0x23 ;
tss . es = 0x23 ;
tss . fs = 0x23 ;
tss . gs = thread_specific_selector ( ) | 3 ;
tss . ss = 0x23 ;
tss . cr3 = page_directory ( ) . cr3 ( ) ;
tss . esp = new_userspace_esp ;
tss . ss0 = 0x10 ;
tss . esp0 = old_esp0 ;
tss . ss2 = m_pid ;
2018-11-03 03:49:40 +03:00
# ifdef TASK_DEBUG
2019-12-22 13:35:02 +03:00
kprintf ( " Process %u (%s) exec'd %s @ %p \n " , pid ( ) , name ( ) . characters ( ) , path . characters ( ) , tss . eip ) ;
2018-11-03 03:49:40 +03:00
# endif
2019-12-22 13:35:02 +03:00
new_main_thread - > set_state ( Thread : : State : : Skip1SchedulerPass ) ;
2019-07-17 15:15:13 +03:00
big_lock ( ) . unlock_if_locked ( ) ;
2018-11-09 19:59:14 +03:00
return 0 ;
}
2019-10-27 12:42:07 +03:00
KResultOr < Vector < String > > Process : : find_shebang_interpreter_for_executable ( const String & executable_path )
2019-09-15 12:47:21 +03:00
{
// FIXME: It's a bit sad that we'll open the executable twice (in case there's no shebang)
// Maybe we can find a way to plumb this opened FileDescription to the rest of the
// exec implementation..
auto result = VFS : : the ( ) . open ( executable_path , 0 , 0 , current_directory ( ) ) ;
if ( result . is_error ( ) )
return result . error ( ) ;
auto description = result . value ( ) ;
auto metadata = description - > metadata ( ) ;
if ( ! metadata . may_execute ( m_euid , m_gids ) )
return KResult ( - EACCES ) ;
if ( metadata . size < 3 )
return KResult ( - ENOEXEC ) ;
char first_page [ PAGE_SIZE ] ;
int nread = description - > read ( ( u8 * ) & first_page , sizeof ( first_page ) ) ;
2019-10-27 12:42:07 +03:00
int word_start = 2 ;
int word_length = 0 ;
2019-09-15 12:47:21 +03:00
if ( nread > 2 & & first_page [ 0 ] = = ' # ' & & first_page [ 1 ] = = ' ! ' ) {
2019-10-27 12:42:07 +03:00
Vector < String > interpreter_words ;
2019-09-15 12:47:21 +03:00
for ( int i = 2 ; i < nread ; + + i ) {
if ( first_page [ i ] = = ' \n ' ) {
break ;
}
2019-10-27 12:42:07 +03:00
if ( first_page [ i ] ! = ' ' ) {
+ + word_length ;
}
if ( first_page [ i ] = = ' ' ) {
if ( word_length > 0 ) {
interpreter_words . append ( String ( & first_page [ word_start ] , word_length ) ) ;
}
word_length = 0 ;
word_start = i + 1 ;
}
2019-09-15 12:47:21 +03:00
}
2019-10-27 12:42:07 +03:00
if ( word_length > 0 )
interpreter_words . append ( String ( & first_page [ word_start ] , word_length ) ) ;
if ( ! interpreter_words . is_empty ( ) )
return interpreter_words ;
2019-09-15 12:47:21 +03:00
}
return KResult ( - ENOEXEC ) ;
}
2019-02-17 12:18:25 +03:00
int Process : : exec ( String path , Vector < String > arguments , Vector < String > environment )
2018-11-09 19:59:14 +03:00
{
2019-09-15 12:47:21 +03:00
auto result = find_shebang_interpreter_for_executable ( path ) ;
2019-10-27 12:42:07 +03:00
if ( ! result . is_error ( ) ) {
Vector < String > new_arguments ( result . value ( ) ) ;
new_arguments . append ( path ) ;
arguments . remove ( 0 ) ;
new_arguments . append ( move ( arguments ) ) ;
return exec ( result . value ( ) . first ( ) , move ( new_arguments ) , move ( environment ) ) ;
}
2019-09-15 12:47:21 +03:00
2018-11-09 19:59:14 +03:00
// The bulk of exec() is done by do_exec(), which ensures that all locals
// are cleaned up by the time we yield-teleport below.
2019-02-17 12:18:25 +03:00
int rc = do_exec ( move ( path ) , move ( arguments ) , move ( environment ) ) ;
2018-11-09 19:59:14 +03:00
if ( rc < 0 )
return rc ;
2018-11-08 01:13:38 +03:00
2019-03-24 00:03:17 +03:00
if ( & current - > process ( ) = = this ) {
2018-11-09 03:25:31 +03:00
Scheduler : : yield ( ) ;
2018-11-09 00:24:02 +03:00
ASSERT_NOT_REACHED ( ) ;
2018-11-08 01:13:38 +03:00
}
2018-11-03 03:49:40 +03:00
return 0 ;
}
2018-11-03 12:20:23 +03:00
int Process : : sys $ execve ( const char * filename , const char * * argv , const char * * envp )
{
2019-02-17 12:18:25 +03:00
// NOTE: Be extremely careful with allocating any kernel memory in exec().
// On success, the kernel stack will be lost.
2018-11-16 18:23:39 +03:00
if ( ! validate_read_str ( filename ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2019-04-26 04:16:26 +03:00
if ( ! * filename )
return - ENOENT ;
2018-11-03 12:20:23 +03:00
if ( argv ) {
2018-11-16 18:23:39 +03:00
if ( ! validate_read_typed ( argv ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2018-11-03 12:20:23 +03:00
for ( size_t i = 0 ; argv [ i ] ; + + i ) {
2018-11-16 18:23:39 +03:00
if ( ! validate_read_str ( argv [ i ] ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2018-11-03 12:20:23 +03:00
}
}
if ( envp ) {
2018-11-16 18:23:39 +03:00
if ( ! validate_read_typed ( envp ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2018-11-03 12:20:23 +03:00
for ( size_t i = 0 ; envp [ i ] ; + + i ) {
2018-11-16 18:23:39 +03:00
if ( ! validate_read_str ( envp [ i ] ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2018-11-03 12:20:23 +03:00
}
}
String path ( filename ) ;
Vector < String > arguments ;
2019-02-17 12:18:25 +03:00
Vector < String > environment ;
{
auto parts = path . split ( ' / ' ) ;
if ( argv ) {
for ( size_t i = 0 ; argv [ i ] ; + + i ) {
arguments . append ( argv [ i ] ) ;
}
} else {
arguments . append ( parts . last ( ) ) ;
2018-11-03 12:20:23 +03:00
}
2019-02-17 12:18:25 +03:00
if ( envp ) {
for ( size_t i = 0 ; envp [ i ] ; + + i )
environment . append ( envp [ i ] ) ;
}
2018-11-03 12:20:23 +03:00
}
2019-02-17 12:18:25 +03:00
int rc = exec ( move ( path ) , move ( arguments ) , move ( environment ) ) ;
2018-11-08 01:13:38 +03:00
ASSERT ( rc < 0 ) ; // We should never continue after a successful exec!
2018-11-03 12:49:13 +03:00
return rc ;
2018-11-03 12:20:23 +03:00
}
2019-12-22 13:35:02 +03:00
Process * Process : : create_user_process ( Thread * & first_thread , const String & path , uid_t uid , gid_t gid , pid_t parent_pid , int & error , Vector < String > & & arguments , Vector < String > & & environment , TTY * tty )
2018-10-22 16:42:39 +03:00
{
2018-11-03 12:49:13 +03:00
// FIXME: Don't split() the path twice (sys$spawn also does it...)
2018-10-22 16:42:39 +03:00
auto parts = path . split ( ' / ' ) ;
2018-12-21 04:10:45 +03:00
if ( arguments . is_empty ( ) ) {
2018-11-03 12:49:13 +03:00
arguments . append ( parts . last ( ) ) ;
2018-10-25 13:06:00 +03:00
}
2019-06-21 19:37:47 +03:00
RefPtr < Custody > cwd ;
2018-10-26 15:24:11 +03:00
{
InterruptDisabler disabler ;
2018-11-07 23:38:18 +03:00
if ( auto * parent = Process : : from_pid ( parent_pid ) )
2019-07-11 16:38:47 +03:00
cwd = parent - > m_cwd ;
2018-10-27 17:43:03 +03:00
}
2019-01-16 14:57:07 +03:00
2018-11-03 12:49:13 +03:00
if ( ! cwd )
2019-05-30 19:58:59 +03:00
cwd = VFS : : the ( ) . root_custody ( ) ;
2018-10-27 17:43:03 +03:00
2019-12-22 13:35:02 +03:00
auto * process = new Process ( first_thread , parts . take_last ( ) , uid , gid , parent_pid , Ring3 , move ( cwd ) , nullptr , tty ) ;
2018-10-26 12:16:56 +03:00
2018-11-03 12:49:13 +03:00
error = process - > exec ( path , move ( arguments ) , move ( environment ) ) ;
2018-12-26 23:04:27 +03:00
if ( error ! = 0 ) {
delete process ;
2018-10-22 16:42:39 +03:00
return nullptr ;
2018-12-26 23:04:27 +03:00
}
2018-11-01 13:30:48 +03:00
2018-11-09 03:25:31 +03:00
{
InterruptDisabler disabler ;
g_processes - > prepend ( process ) ;
}
2018-10-24 01:20:34 +03:00
# ifdef TASK_DEBUG
2019-12-22 13:35:02 +03:00
kprintf ( " Process %u (%s) spawned @ %p \n " , process - > pid ( ) , process - > name ( ) . characters ( ) , first_thread - > tss ( ) . eip ) ;
2018-10-24 01:20:34 +03:00
# endif
2018-10-25 13:06:00 +03:00
error = 0 ;
2018-11-03 12:49:13 +03:00
return process ;
2018-10-22 16:42:39 +03:00
}
2019-12-22 13:35:02 +03:00
Process * Process : : create_kernel_process ( Thread * & first_thread , String & & name , void ( * e ) ( ) )
2018-10-25 12:15:17 +03:00
{
2019-12-22 13:35:02 +03:00
auto * process = new Process ( first_thread , move ( name ) , ( uid_t ) 0 , ( gid_t ) 0 , ( pid_t ) 0 , Ring0 ) ;
first_thread - > tss ( ) . eip = ( u32 ) e ;
2018-10-25 12:15:17 +03:00
2018-11-01 15:15:46 +03:00
if ( process - > pid ( ) ! = 0 ) {
2019-03-24 00:03:17 +03:00
InterruptDisabler disabler ;
g_processes - > prepend ( process ) ;
2018-10-25 12:15:17 +03:00
# ifdef TASK_DEBUG
2019-12-22 13:35:02 +03:00
kprintf ( " Kernel process %u (%s) spawned @ %p \n " , process - > pid ( ) , process - > name ( ) . characters ( ) , first_thread - > tss ( ) . eip ) ;
2018-10-25 12:15:17 +03:00
# endif
}
2019-12-22 13:35:02 +03:00
first_thread - > set_state ( Thread : : State : : Runnable ) ;
2018-11-01 15:15:46 +03:00
return process ;
2018-10-25 12:15:17 +03:00
}
2019-12-22 13:35:02 +03:00
Process : : Process ( Thread * & first_thread , const String & name , uid_t uid , gid_t gid , pid_t ppid , RingLevel ring , RefPtr < Custody > cwd , RefPtr < Custody > executable , TTY * tty , Process * fork_parent )
2018-10-22 16:42:39 +03:00
: m_name ( move ( name ) )
2019-12-22 13:51:24 +03:00
, m_pid ( allocate_pid ( ) )
2018-10-22 16:42:39 +03:00
, m_uid ( uid )
, m_gid ( gid )
2018-11-05 17:04:19 +03:00
, m_euid ( uid )
, m_egid ( gid )
2018-10-25 12:15:17 +03:00
, m_ring ( ring )
2019-05-30 21:23:50 +03:00
, m_executable ( move ( executable ) )
, m_cwd ( move ( cwd ) )
2018-10-30 15:59:29 +03:00
, m_tty ( tty )
2018-11-06 15:33:06 +03:00
, m_ppid ( ppid )
2018-10-22 16:42:39 +03:00
{
2019-03-24 00:03:17 +03:00
dbgprintf ( " Process: New process PID=%u with name=%s \n " , m_pid , m_name . characters ( ) ) ;
2019-08-06 12:19:16 +03:00
m_page_directory = PageDirectory : : create_for_userspace ( * this , fork_parent ? & fork_parent - > page_directory ( ) . range_allocator ( ) : nullptr ) ;
2019-03-24 00:03:17 +03:00
# ifdef MM_DEBUG
dbgprintf ( " Process %u ctor: PD=%x created \n " , pid ( ) , m_page_directory . ptr ( ) ) ;
# endif
2019-02-04 16:06:38 +03:00
2019-03-24 00:03:17 +03:00
// NOTE: fork() doesn't clone all threads; the thread that called fork() becomes the main thread in the new process.
if ( fork_parent )
2019-12-22 13:35:02 +03:00
first_thread = current - > clone ( * this ) ;
2019-03-24 00:03:17 +03:00
else
2019-12-22 13:35:02 +03:00
first_thread = new Thread ( * this ) ;
2019-01-25 09:52:44 +03:00
2018-11-07 03:38:51 +03:00
m_gids . set ( m_gid ) ;
2018-11-02 22:41:58 +03:00
if ( fork_parent ) {
m_sid = fork_parent - > m_sid ;
m_pgid = fork_parent - > m_pgid ;
} else {
2018-11-02 14:56:51 +03:00
// FIXME: Use a ProcessHandle? Presumably we're executing *IN* the parent right now though..
InterruptDisabler disabler ;
2018-11-07 23:38:18 +03:00
if ( auto * parent = Process : : from_pid ( m_ppid ) ) {
2018-11-02 14:56:51 +03:00
m_sid = parent - > m_sid ;
m_pgid = parent - > m_pgid ;
}
}
2018-11-02 22:41:58 +03:00
if ( fork_parent ) {
2018-11-13 03:36:31 +03:00
m_fds . resize ( fork_parent - > m_fds . size ( ) ) ;
2019-02-26 00:06:55 +03:00
for ( int i = 0 ; i < fork_parent - > m_fds . size ( ) ; + + i ) {
2019-06-13 23:03:04 +03:00
if ( ! fork_parent - > m_fds [ i ] . description )
2018-11-02 22:41:58 +03:00
continue ;
# ifdef FORK_DEBUG
2019-06-13 23:03:04 +03:00
dbgprintf ( " fork: cloning fd %u... (%p) istty? %u \n " , i , fork_parent - > m_fds [ i ] . description . ptr ( ) , fork_parent - > m_fds [ i ] . description - > is_tty ( ) ) ;
2018-11-02 22:41:58 +03:00
# endif
2019-08-11 16:07:47 +03:00
m_fds [ i ] = fork_parent - > m_fds [ i ] ;
2018-11-02 22:41:58 +03:00
}
} else {
2018-11-13 03:36:31 +03:00
m_fds . resize ( m_max_open_file_descriptors ) ;
2019-02-12 13:25:25 +03:00
auto & device_to_use_as_tty = tty ? ( CharacterDevice & ) * tty : NullDevice : : the ( ) ;
2019-03-07 00:14:31 +03:00
m_fds [ 0 ] . set ( * device_to_use_as_tty . open ( O_RDONLY ) . value ( ) ) ;
m_fds [ 1 ] . set ( * device_to_use_as_tty . open ( O_WRONLY ) . value ( ) ) ;
m_fds [ 2 ] . set ( * device_to_use_as_tty . open ( O_WRONLY ) . value ( ) ) ;
2018-10-16 12:01:38 +03:00
}
2019-02-22 04:39:13 +03:00
if ( fork_parent ) {
m_sid = fork_parent - > m_sid ;
m_pgid = fork_parent - > m_pgid ;
m_umask = fork_parent - > m_umask ;
}
2018-10-16 12:01:38 +03:00
}
2018-11-01 15:15:46 +03:00
Process : : ~ Process ( )
2018-10-16 12:01:38 +03:00
{
2019-12-22 13:35:02 +03:00
dbgprintf ( " ~Process{%p} name=%s pid=%d, m_fds=%d, m_thread_count=%u \n " , this , m_name . characters ( ) , pid ( ) , m_fds . size ( ) , m_thread_count ) ;
ASSERT ( thread_count ( ) = = 0 ) ;
2018-10-18 15:53:00 +03:00
}
2019-01-31 19:31:23 +03:00
void Process : : dump_regions ( )
2018-10-18 15:53:00 +03:00
{
2018-11-01 15:15:46 +03:00
kprintf ( " Process %s(%u) regions: \n " , name ( ) . characters ( ) , pid ( ) ) ;
2019-08-12 20:37:28 +03:00
kprintf ( " BEGIN END SIZE ACCESS NAME \n " ) ;
2018-10-18 15:53:00 +03:00
for ( auto & region : m_regions ) {
2019-12-09 21:12:38 +03:00
kprintf ( " %08x -- %08x %08x %c%c%c%c%c%c %s \n " ,
2019-06-27 14:34:28 +03:00
region . vaddr ( ) . get ( ) ,
region . vaddr ( ) . offset ( region . size ( ) - 1 ) . get ( ) ,
region . size ( ) ,
2019-08-12 20:37:28 +03:00
region . is_readable ( ) ? ' R ' : ' ' ,
region . is_writable ( ) ? ' W ' : ' ' ,
region . is_executable ( ) ? ' X ' : ' ' ,
2019-11-17 14:11:43 +03:00
region . is_shared ( ) ? ' S ' : ' ' ,
region . is_stack ( ) ? ' T ' : ' ' ,
2019-12-09 21:12:38 +03:00
region . vmobject ( ) . is_purgeable ( ) ? ' P ' : ' ' ,
2019-06-27 14:34:28 +03:00
region . name ( ) . characters ( ) ) ;
2018-10-18 15:53:00 +03:00
}
2018-10-16 12:01:38 +03:00
}
2018-11-01 15:15:46 +03:00
void Process : : sys $ exit ( int status )
2018-10-22 12:43:55 +03:00
{
cli ( ) ;
2018-10-24 01:20:34 +03:00
# ifdef TASK_DEBUG
2018-10-22 12:43:55 +03:00
kprintf ( " sys$exit: %s(%u) exit with status %d \n " , name ( ) . characters ( ) , pid ( ) , status ) ;
2018-10-24 01:20:34 +03:00
# endif
2018-10-22 12:43:55 +03:00
2019-05-16 14:41:16 +03:00
dump_backtrace ( ) ;
2019-05-06 22:48:48 +03:00
2018-11-07 20:30:59 +03:00
m_termination_status = status ;
m_termination_signal = 0 ;
2019-02-06 20:45:21 +03:00
die ( ) ;
Kernel: Unwind kernel stacks before dying
While executing in the kernel, a thread can acquire various resources
that need cleanup, such as locks and references to RefCounted objects.
This cleanup normally happens on the exit path, such as in destructors
for various RAII guards. But we weren't calling those exit paths when
killing threads that have been executing in the kernel, such as threads
blocked on reading or sleeping, thus causing leaks.
This commit changes how killing threads works. Now, instead of killing
a thread directly, one is supposed to call thread->set_should_die(),
which will unblock it and make it unwind the stack if it is blocked
in the kernel. Then, just before returning to the userspace, the thread
will automatically die.
2019-11-14 18:46:01 +03:00
current - > die_if_needed ( ) ;
2018-11-08 00:15:02 +03:00
ASSERT_NOT_REACHED ( ) ;
2018-10-22 12:43:55 +03:00
}
2019-09-17 16:41:42 +03:00
void signal_trampoline_dummy ( void )
{
// The trampoline preserves the current eax, pushes the signal code and
// then calls the signal handler. We do this because, when interrupting a
// blocking syscall, that syscall may return some special error code in eax;
// This error code would likely be overwritten by the signal handler, so it's
// neccessary to preserve it here.
asm (
" .intel_syntax noprefix \n "
" asm_signal_trampoline: \n "
" push ebp \n "
" mov ebp, esp \n "
" push eax \n " // we have to store eax 'cause it might be the return value from a syscall
" sub esp, 4 \n " // align the stack to 16 bytes
" mov eax, [ebp+12] \n " // push the signal code
" push eax \n "
" call [ebp+8] \n " // call the signal handler
" add esp, 8 \n "
" mov eax, %P0 \n "
" int 0x82 \n " // sigreturn syscall
" asm_signal_trampoline_end: \n "
" .att_syntax " : : " i " ( Syscall : : SC_sigreturn ) ) ;
}
2019-09-04 16:14:54 +03:00
extern " C " void asm_signal_trampoline ( void ) ;
extern " C " void asm_signal_trampoline_end ( void ) ;
2019-07-19 18:01:16 +03:00
void create_signal_trampolines ( )
2019-03-05 14:50:55 +03:00
{
2019-07-19 18:01:16 +03:00
InterruptDisabler disabler ;
// NOTE: We leak this region.
2019-12-26 00:41:34 +03:00
auto * trampoline_region = MM . allocate_user_accessible_kernel_region ( PAGE_SIZE , " Signal trampolines " , Region : : Access : : Read | Region : : Access : : Write | Region : : Access : : Execute ) . leak_ptr ( ) ;
2019-07-19 18:01:16 +03:00
g_return_to_ring3_from_signal_trampoline = trampoline_region - > vaddr ( ) ;
2019-09-04 16:14:54 +03:00
u8 * trampoline = ( u8 * ) asm_signal_trampoline ;
u8 * trampoline_end = ( u8 * ) asm_signal_trampoline_end ;
size_t trampoline_size = trampoline_end - trampoline ;
2019-07-19 18:01:16 +03:00
u8 * code_ptr = ( u8 * ) trampoline_region - > vaddr ( ) . as_ptr ( ) ;
2019-09-04 16:14:54 +03:00
memcpy ( code_ptr , trampoline , trampoline_size ) ;
2019-07-19 18:01:16 +03:00
trampoline_region - > set_writable ( false ) ;
2019-11-03 22:59:54 +03:00
trampoline_region - > remap ( ) ;
2019-03-05 14:50:55 +03:00
}
2019-12-15 23:29:26 +03:00
void create_kernel_info_page ( )
{
2019-12-26 00:41:34 +03:00
auto * info_page_region_for_userspace = MM . allocate_user_accessible_kernel_region ( PAGE_SIZE , " Kernel info page " , Region : : Access : : Read ) . leak_ptr ( ) ;
auto * info_page_region_for_kernel = MM . allocate_kernel_region_with_vmobject ( info_page_region_for_userspace - > vmobject ( ) , PAGE_SIZE , " Kernel info page " , Region : : Access : : Read | Region : : Access : : Write ) . leak_ptr ( ) ;
2019-12-16 00:21:28 +03:00
s_info_page_address_for_userspace = info_page_region_for_userspace - > vaddr ( ) ;
s_info_page_address_for_kernel = info_page_region_for_kernel - > vaddr ( ) ;
memset ( s_info_page_address_for_kernel . as_ptr ( ) , 0 , PAGE_SIZE ) ;
2019-12-15 23:29:26 +03:00
}
2019-07-03 22:17:35 +03:00
int Process : : sys $ restore_signal_mask ( u32 mask )
2019-03-05 12:34:08 +03:00
{
2019-03-24 00:03:17 +03:00
current - > m_signal_mask = mask ;
2019-03-05 12:34:08 +03:00
return 0 ;
}
2019-09-04 16:14:54 +03:00
int Process : : sys $ sigreturn ( RegisterDump & registers )
2018-11-07 23:19:47 +03:00
{
2019-09-04 16:14:54 +03:00
//Here, we restore the state pushed by dispatch signal and asm_signal_trampoline.
u32 * stack_ptr = ( u32 * ) registers . esp_if_crossRing ;
u32 smuggled_eax = * stack_ptr ;
//pop the stored eax, ebp, return address, handler and signal code
stack_ptr + = 5 ;
current - > m_signal_mask = * stack_ptr ;
stack_ptr + + ;
2019-10-04 17:31:34 +03:00
//pop edi, esi, ebp, esp, ebx, edx, ecx and eax
memcpy ( & registers . edi , stack_ptr , 8 * sizeof ( u32 ) ) ;
stack_ptr + = 8 ;
registers . eip = * stack_ptr ;
stack_ptr + + ;
2019-09-04 16:14:54 +03:00
registers . eflags = * stack_ptr ;
stack_ptr + + ;
registers . esp_if_crossRing = registers . esp ;
return smuggled_eax ;
2018-11-06 12:46:40 +03:00
}
2019-07-03 22:17:35 +03:00
void Process : : crash ( int signal , u32 eip )
2018-10-18 01:26:30 +03:00
{
2018-10-25 11:33:10 +03:00
ASSERT_INTERRUPTS_DISABLED ( ) ;
2019-03-24 00:03:17 +03:00
ASSERT ( ! is_dead ( ) ) ;
Kernel: Unwind kernel stacks before dying
While executing in the kernel, a thread can acquire various resources
that need cleanup, such as locks and references to RefCounted objects.
This cleanup normally happens on the exit path, such as in destructors
for various RAII guards. But we weren't calling those exit paths when
killing threads that have been executing in the kernel, such as threads
blocked on reading or sleeping, thus causing leaks.
This commit changes how killing threads works. Now, instead of killing
a thread directly, one is supposed to call thread->set_should_die(),
which will unblock it and make it unwind the stack if it is blocked
in the kernel. Then, just before returning to the userspace, the thread
will automatically die.
2019-11-14 18:46:01 +03:00
ASSERT ( & current - > process ( ) = = this ) ;
2019-03-14 01:14:30 +03:00
2019-06-19 19:50:02 +03:00
if ( m_elf_loader & & ksyms_ready )
dbgprintf ( " \033 [31;1m%p %s \033 [0m \n " , eip , m_elf_loader - > symbolicate ( eip ) . characters ( ) ) ;
2019-05-16 20:49:48 +03:00
dump_backtrace ( ) ;
2019-05-26 03:08:51 +03:00
m_termination_signal = signal ;
2019-01-31 19:31:23 +03:00
dump_regions ( ) ;
2019-02-08 11:45:53 +03:00
ASSERT ( is_ring3 ( ) ) ;
2019-01-30 20:26:19 +03:00
die ( ) ;
Kernel: Unwind kernel stacks before dying
While executing in the kernel, a thread can acquire various resources
that need cleanup, such as locks and references to RefCounted objects.
This cleanup normally happens on the exit path, such as in destructors
for various RAII guards. But we weren't calling those exit paths when
killing threads that have been executing in the kernel, such as threads
blocked on reading or sleeping, thus causing leaks.
This commit changes how killing threads works. Now, instead of killing
a thread directly, one is supposed to call thread->set_should_die(),
which will unblock it and make it unwind the stack if it is blocked
in the kernel. Then, just before returning to the userspace, the thread
will automatically die.
2019-11-14 18:46:01 +03:00
// We can not return from here, as there is nowhere
// to unwind to, so die right away.
current - > die_if_needed ( ) ;
2018-11-08 00:15:02 +03:00
ASSERT_NOT_REACHED ( ) ;
2018-10-18 01:26:30 +03:00
}
2018-11-07 23:38:18 +03:00
Process * Process : : from_pid ( pid_t pid )
2018-10-16 12:01:38 +03:00
{
2018-10-26 16:11:29 +03:00
ASSERT_INTERRUPTS_DISABLED ( ) ;
2019-08-08 15:40:13 +03:00
for ( auto & process : * g_processes ) {
if ( process . pid ( ) = = pid )
return & process ;
2018-10-16 12:01:38 +03:00
}
return nullptr ;
}
2019-06-07 10:36:51 +03:00
FileDescription * Process : : file_description ( int fd )
2018-10-16 12:01:38 +03:00
{
if ( fd < 0 )
return nullptr ;
2019-02-26 00:06:55 +03:00
if ( fd < m_fds . size ( ) )
2019-06-13 23:03:04 +03:00
return m_fds [ fd ] . description . ptr ( ) ;
2018-11-07 13:37:54 +03:00
return nullptr ;
}
2019-06-07 10:36:51 +03:00
const FileDescription * Process : : file_description ( int fd ) const
2018-11-07 13:37:54 +03:00
{
if ( fd < 0 )
return nullptr ;
2019-02-26 00:06:55 +03:00
if ( fd < m_fds . size ( ) )
2019-06-13 23:03:04 +03:00
return m_fds [ fd ] . description . ptr ( ) ;
2018-10-16 12:01:38 +03:00
return nullptr ;
}
2019-09-28 23:00:38 +03:00
int Process : : fd_flags ( int fd ) const
{
if ( fd < 0 )
return - 1 ;
if ( fd < m_fds . size ( ) )
return m_fds [ fd ] . flags ;
return - 1 ;
}
2019-02-25 23:19:57 +03:00
ssize_t Process : : sys $ get_dir_entries ( int fd , void * buffer , ssize_t size )
2018-10-24 13:43:52 +03:00
{
2019-02-25 23:19:57 +03:00
if ( size < 0 )
return - EINVAL ;
2018-11-16 18:10:59 +03:00
if ( ! validate_write ( buffer , size ) )
return - EFAULT ;
2019-06-13 23:03:04 +03:00
auto * description = file_description ( fd ) ;
if ( ! description )
2018-10-31 00:03:02 +03:00
return - EBADF ;
2019-07-03 22:17:35 +03:00
return description - > get_dir_entries ( ( u8 * ) buffer , size ) ;
2018-10-24 13:43:52 +03:00
}
2018-11-01 15:15:46 +03:00
int Process : : sys $ lseek ( int fd , off_t offset , int whence )
2018-10-16 12:01:38 +03:00
{
2019-06-13 23:03:04 +03:00
auto * description = file_description ( fd ) ;
if ( ! description )
2018-10-31 19:50:43 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
return description - > seek ( offset , whence ) ;
2018-10-16 12:01:38 +03:00
}
2019-02-25 23:19:57 +03:00
int Process : : sys $ ttyname_r ( int fd , char * buffer , ssize_t size )
2018-10-31 00:03:02 +03:00
{
2019-02-25 23:19:57 +03:00
if ( size < 0 )
return - EINVAL ;
2018-11-16 18:10:59 +03:00
if ( ! validate_write ( buffer , size ) )
return - EFAULT ;
2019-06-13 23:03:04 +03:00
auto * description = file_description ( fd ) ;
if ( ! description )
2018-10-31 00:03:02 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
if ( ! description - > is_tty ( ) )
2018-10-31 00:03:02 +03:00
return - ENOTTY ;
2019-06-13 23:03:04 +03:00
auto tty_name = description - > tty ( ) - > tty_name ( ) ;
2019-12-09 19:45:40 +03:00
if ( ( size_t ) size < tty_name . length ( ) + 1 )
2018-10-31 00:03:02 +03:00
return - ERANGE ;
2019-10-18 15:13:43 +03:00
memcpy ( buffer , tty_name . characters_without_null_termination ( ) , tty_name . length ( ) ) ;
buffer [ tty_name . length ( ) ] = ' \0 ' ;
2018-10-31 00:03:02 +03:00
return 0 ;
}
2019-02-25 23:19:57 +03:00
int Process : : sys $ ptsname_r ( int fd , char * buffer , ssize_t size )
2019-01-15 08:30:19 +03:00
{
2019-02-25 23:19:57 +03:00
if ( size < 0 )
return - EINVAL ;
2019-01-15 08:30:19 +03:00
if ( ! validate_write ( buffer , size ) )
return - EFAULT ;
2019-06-13 23:03:04 +03:00
auto * description = file_description ( fd ) ;
if ( ! description )
2019-01-15 08:30:19 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
auto * master_pty = description - > master_pty ( ) ;
2019-01-15 08:30:19 +03:00
if ( ! master_pty )
return - ENOTTY ;
auto pts_name = master_pty - > pts_name ( ) ;
2019-12-09 19:45:40 +03:00
if ( ( size_t ) size < pts_name . length ( ) + 1 )
2019-01-15 08:30:19 +03:00
return - ERANGE ;
strcpy ( buffer , pts_name . characters ( ) ) ;
return 0 ;
}
2019-05-10 04:19:25 +03:00
ssize_t Process : : sys $ writev ( int fd , const struct iovec * iov , int iov_count )
2018-10-30 17:33:37 +03:00
{
2019-05-10 04:19:25 +03:00
if ( iov_count < 0 )
2019-02-25 23:19:57 +03:00
return - EINVAL ;
2019-05-10 04:19:25 +03:00
if ( ! validate_read_typed ( iov , iov_count ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2019-05-10 04:19:25 +03:00
// FIXME: Return EINVAL if sum of iovecs is greater than INT_MAX
2019-06-13 23:03:04 +03:00
auto * description = file_description ( fd ) ;
if ( ! description )
2018-10-30 17:33:37 +03:00
return - EBADF ;
2019-05-10 04:19:25 +03:00
int nwritten = 0 ;
for ( int i = 0 ; i < iov_count ; + + i ) {
2019-07-03 22:17:35 +03:00
int rc = do_write ( * description , ( const u8 * ) iov [ i ] . iov_base , iov [ i ] . iov_len ) ;
2019-05-10 04:19:25 +03:00
if ( rc < 0 ) {
if ( nwritten = = 0 )
return rc ;
return nwritten ;
}
nwritten + = rc ;
}
return nwritten ;
}
2019-07-03 22:17:35 +03:00
ssize_t Process : : do_write ( FileDescription & description , const u8 * data , int data_size )
2019-05-10 04:19:25 +03:00
{
2018-11-12 03:28:46 +03:00
ssize_t nwritten = 0 ;
2019-06-13 23:03:04 +03:00
if ( ! description . is_blocking ( ) ) {
if ( ! description . can_write ( ) )
2019-05-19 11:25:05 +03:00
return - EAGAIN ;
}
2019-05-10 04:19:25 +03:00
2019-06-13 23:03:04 +03:00
if ( description . should_append ( ) ) {
2019-05-26 02:18:04 +03:00
# ifdef IO_DEBUG
dbgprintf ( " seeking to end (O_APPEND) \n " ) ;
# endif
2019-06-13 23:03:04 +03:00
description . seek ( 0 , SEEK_END ) ;
2019-05-26 02:18:04 +03:00
}
2019-05-10 04:19:25 +03:00
while ( nwritten < data_size ) {
2018-11-12 03:28:46 +03:00
# ifdef IO_DEBUG
2019-05-10 04:19:25 +03:00
dbgprintf ( " while %u < %u \n " , nwritten , size ) ;
2018-11-12 03:28:46 +03:00
# endif
2019-06-13 23:03:04 +03:00
if ( ! description . can_write ( ) ) {
2018-11-12 03:28:46 +03:00
# ifdef IO_DEBUG
2019-05-10 04:19:25 +03:00
dbgprintf ( " block write on %d \n " , fd ) ;
2018-11-12 03:28:46 +03:00
# endif
2019-07-20 12:05:52 +03:00
if ( current - > block < Thread : : WriteBlocker > ( description ) = = Thread : : BlockResult : : InterruptedBySignal ) {
if ( nwritten = = 0 )
return - EINTR ;
}
2019-05-10 04:19:25 +03:00
}
2019-06-13 23:03:04 +03:00
ssize_t rc = description . write ( data + nwritten , data_size - nwritten ) ;
2018-11-12 03:28:46 +03:00
# ifdef IO_DEBUG
2019-05-10 04:19:25 +03:00
dbgprintf ( " -> write returned %d \n " , rc ) ;
2018-11-12 03:28:46 +03:00
# endif
2019-05-10 04:19:25 +03:00
if ( rc < 0 ) {
// FIXME: Support returning partial nwritten with errno.
ASSERT ( nwritten = = 0 ) ;
return rc ;
2018-11-12 03:28:46 +03:00
}
2019-05-10 04:19:25 +03:00
if ( rc = = 0 )
break ;
nwritten + = rc ;
2018-11-12 03:28:46 +03:00
}
2019-05-10 04:19:25 +03:00
return nwritten ;
}
2019-07-03 22:17:35 +03:00
ssize_t Process : : sys $ write ( int fd , const u8 * data , ssize_t size )
2019-05-10 04:19:25 +03:00
{
if ( size < 0 )
return - EINVAL ;
if ( size = = 0 )
return 0 ;
if ( ! validate_read ( data , size ) )
return - EFAULT ;
# ifdef DEBUG_IO
dbgprintf ( " %s(%u): sys$write(%d, %p, %u) \n " , name ( ) . characters ( ) , pid ( ) , fd , data , size ) ;
# endif
2019-06-13 23:03:04 +03:00
auto * description = file_description ( fd ) ;
if ( ! description )
2019-05-10 04:19:25 +03:00
return - EBADF ;
2019-11-19 15:33:08 +03:00
return do_write ( * description , data , size ) ;
2018-10-30 17:33:37 +03:00
}
2019-07-03 22:17:35 +03:00
ssize_t Process : : sys $ read ( int fd , u8 * buffer , ssize_t size )
2018-10-16 12:01:38 +03:00
{
2019-02-25 23:19:57 +03:00
if ( size < 0 )
return - EINVAL ;
2019-04-27 17:17:24 +03:00
if ( size = = 0 )
return 0 ;
2019-02-25 23:19:57 +03:00
if ( ! validate_write ( buffer , size ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2018-10-23 11:12:50 +03:00
# ifdef DEBUG_IO
2019-02-25 23:19:57 +03:00
dbgprintf ( " %s(%u) sys$read(%d, %p, %u) \n " , name ( ) . characters ( ) , pid ( ) , fd , buffer , size ) ;
2018-10-23 11:12:50 +03:00
# endif
2019-06-13 23:03:04 +03:00
auto * description = file_description ( fd ) ;
if ( ! description )
2018-10-30 17:33:37 +03:00
return - EBADF ;
2019-10-24 16:46:31 +03:00
if ( description - > is_directory ( ) )
return - EISDIR ;
2019-06-13 23:03:04 +03:00
if ( description - > is_blocking ( ) ) {
if ( ! description - > can_read ( ) ) {
2019-07-20 12:05:52 +03:00
if ( current - > block < Thread : : ReadBlocker > ( * description ) = = Thread : : BlockResult : : InterruptedBySignal )
2018-11-07 23:19:47 +03:00
return - EINTR ;
2018-10-25 14:07:59 +03:00
}
}
2019-06-13 23:03:04 +03:00
return description - > read ( buffer , size ) ;
2018-10-16 12:01:38 +03:00
}
2018-11-01 15:15:46 +03:00
int Process : : sys $ close ( int fd )
2018-10-16 12:01:38 +03:00
{
2019-06-13 23:03:04 +03:00
auto * description = file_description ( fd ) ;
2019-12-02 11:33:37 +03:00
# ifdef DEBUG_IO
dbgprintf ( " %s(%u) sys$close(%d) %p \n " , name ( ) . characters ( ) , pid ( ) , fd , description ) ;
# endif
2019-06-13 23:03:04 +03:00
if ( ! description )
2018-10-31 00:03:02 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
int rc = description - > close ( ) ;
2019-06-07 12:43:58 +03:00
m_fds [ fd ] = { } ;
2018-11-01 16:00:28 +03:00
return rc ;
2018-10-16 12:01:38 +03:00
}
2019-01-23 08:53:01 +03:00
int Process : : sys $ utime ( const char * pathname , const utimbuf * buf )
2018-12-19 23:14:55 +03:00
{
if ( ! validate_read_str ( pathname ) )
return - EFAULT ;
if ( buf & & ! validate_read_typed ( buf ) )
return - EFAULT ;
2019-01-23 08:53:01 +03:00
time_t atime ;
time_t mtime ;
2018-12-19 23:14:55 +03:00
if ( buf ) {
atime = buf - > actime ;
mtime = buf - > modtime ;
} else {
2019-03-25 04:06:57 +03:00
struct timeval now ;
kgettimeofday ( now ) ;
mtime = now . tv_sec ;
atime = now . tv_sec ;
2018-12-19 23:14:55 +03:00
}
2019-05-30 21:23:50 +03:00
return VFS : : the ( ) . utime ( StringView ( pathname ) , current_directory ( ) , atime , mtime ) ;
2018-12-19 23:14:55 +03:00
}
2018-11-11 02:20:53 +03:00
int Process : : sys $ access ( const char * pathname , int mode )
{
2018-11-16 18:23:39 +03:00
if ( ! validate_read_str ( pathname ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2019-05-30 21:23:50 +03:00
return VFS : : the ( ) . access ( StringView ( pathname ) , mode , current_directory ( ) ) ;
2018-11-11 02:20:53 +03:00
}
2019-07-03 22:17:35 +03:00
int Process : : sys $ fcntl ( int fd , int cmd , u32 arg )
2018-11-11 12:38:33 +03:00
{
2019-06-07 12:43:58 +03:00
( void ) cmd ;
( void ) arg ;
2018-11-11 17:36:40 +03:00
dbgprintf ( " sys$fcntl: fd=%d, cmd=%d, arg=%u \n " , fd , cmd , arg ) ;
2019-06-13 23:03:04 +03:00
auto * description = file_description ( fd ) ;
if ( ! description )
2018-11-11 12:38:33 +03:00
return - EBADF ;
2019-06-07 10:36:51 +03:00
// NOTE: The FD flags are not shared between FileDescription objects.
2018-11-13 03:36:31 +03:00
// This means that dup() doesn't copy the FD_CLOEXEC flag!
2018-11-11 17:36:40 +03:00
switch ( cmd ) {
2018-11-17 00:14:40 +03:00
case F_DUPFD : {
int arg_fd = ( int ) arg ;
if ( arg_fd < 0 )
return - EINVAL ;
2019-04-06 15:54:32 +03:00
int new_fd = alloc_fd ( arg_fd ) ;
if ( new_fd < 0 )
return new_fd ;
2019-06-13 23:03:04 +03:00
m_fds [ new_fd ] . set ( * description ) ;
2018-11-17 00:14:40 +03:00
break ;
}
2018-11-11 17:36:40 +03:00
case F_GETFD :
2018-11-13 03:36:31 +03:00
return m_fds [ fd ] . flags ;
2018-11-11 17:36:40 +03:00
case F_SETFD :
2018-11-13 03:36:31 +03:00
m_fds [ fd ] . flags = arg ;
break ;
2018-11-11 17:36:40 +03:00
case F_GETFL :
2019-06-13 23:03:04 +03:00
return description - > file_flags ( ) ;
2018-11-11 17:36:40 +03:00
case F_SETFL :
2019-06-13 23:03:04 +03:00
description - > set_file_flags ( arg ) ;
2018-11-13 03:36:31 +03:00
break ;
2018-11-11 17:36:40 +03:00
default :
ASSERT_NOT_REACHED ( ) ;
}
return 0 ;
2018-11-11 12:38:33 +03:00
}
2019-01-23 08:53:01 +03:00
int Process : : sys $ fstat ( int fd , stat * statbuf )
2018-11-11 02:20:53 +03:00
{
2018-11-16 18:23:39 +03:00
if ( ! validate_write_typed ( statbuf ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2019-06-13 23:03:04 +03:00
auto * description = file_description ( fd ) ;
if ( ! description )
2018-11-11 02:20:53 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
return description - > fstat ( * statbuf ) ;
2018-11-11 02:20:53 +03:00
}
2019-01-23 08:53:01 +03:00
int Process : : sys $ lstat ( const char * path , stat * statbuf )
2018-10-24 14:19:36 +03:00
{
2018-11-16 18:23:39 +03:00
if ( ! validate_write_typed ( statbuf ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2019-08-02 20:23:23 +03:00
auto metadata_or_error = VFS : : the ( ) . lookup_metadata ( StringView ( path ) , current_directory ( ) , O_NOFOLLOW_NOERROR ) ;
if ( metadata_or_error . is_error ( ) )
return metadata_or_error . error ( ) ;
return metadata_or_error . value ( ) . stat ( * statbuf ) ;
2018-10-24 14:19:36 +03:00
}
2019-01-23 08:53:01 +03:00
int Process : : sys $ stat ( const char * path , stat * statbuf )
2018-10-31 12:14:56 +03:00
{
2018-11-16 18:23:39 +03:00
if ( ! validate_write_typed ( statbuf ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2019-08-02 20:23:23 +03:00
auto metadata_or_error = VFS : : the ( ) . lookup_metadata ( StringView ( path ) , current_directory ( ) ) ;
if ( metadata_or_error . is_error ( ) )
return metadata_or_error . error ( ) ;
return metadata_or_error . value ( ) . stat ( * statbuf ) ;
2018-10-31 12:14:56 +03:00
}
2019-02-25 23:19:57 +03:00
int Process : : sys $ readlink ( const char * path , char * buffer , ssize_t size )
2018-10-28 16:11:51 +03:00
{
2019-02-25 23:19:57 +03:00
if ( size < 0 )
return - EINVAL ;
2018-11-16 18:23:39 +03:00
if ( ! validate_read_str ( path ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
if ( ! validate_write ( buffer , size ) )
return - EFAULT ;
2018-10-28 16:11:51 +03:00
2019-05-30 21:23:50 +03:00
auto result = VFS : : the ( ) . open ( path , O_RDONLY | O_NOFOLLOW_NOERROR , 0 , current_directory ( ) ) ;
2019-03-07 00:14:31 +03:00
if ( result . is_error ( ) )
return result . error ( ) ;
2019-06-13 23:03:04 +03:00
auto description = result . value ( ) ;
2018-10-28 16:11:51 +03:00
2019-06-13 23:03:04 +03:00
if ( ! description - > metadata ( ) . is_symlink ( ) )
2018-10-28 16:11:51 +03:00
return - EINVAL ;
2019-06-13 23:03:04 +03:00
auto contents = description - > read_entire_file ( ) ;
2018-10-28 16:11:51 +03:00
if ( ! contents )
return - EIO ; // FIXME: Get a more detailed error from VFS.
2019-09-30 09:57:01 +03:00
memcpy ( buffer , contents . data ( ) , min ( size , ( ssize_t ) contents . size ( ) ) ) ;
2018-10-28 16:11:51 +03:00
if ( contents . size ( ) + 1 < size )
buffer [ contents . size ( ) ] = ' \0 ' ;
return 0 ;
}
2018-11-01 15:15:46 +03:00
int Process : : sys $ chdir ( const char * path )
2018-10-24 15:28:22 +03:00
{
2018-11-16 18:23:39 +03:00
if ( ! validate_read_str ( path ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2019-05-30 21:23:50 +03:00
auto directory_or_error = VFS : : the ( ) . open_directory ( StringView ( path ) , current_directory ( ) ) ;
2019-03-02 01:54:07 +03:00
if ( directory_or_error . is_error ( ) )
return directory_or_error . error ( ) ;
2019-05-30 21:23:50 +03:00
m_cwd = * directory_or_error . value ( ) ;
2018-10-24 15:28:22 +03:00
return 0 ;
}
2019-09-12 02:18:25 +03:00
int Process : : sys $ fchdir ( int fd )
{
auto * description = file_description ( fd ) ;
if ( ! description )
return - EBADF ;
if ( ! description - > is_directory ( ) )
2019-10-20 17:24:42 +03:00
return - ENOTDIR ;
2019-09-12 02:18:25 +03:00
2019-09-13 15:35:18 +03:00
if ( ! description - > metadata ( ) . may_execute ( * this ) )
return - EACCES ;
2019-09-12 02:18:25 +03:00
m_cwd = description - > custody ( ) ;
return 0 ;
}
2019-02-25 23:19:57 +03:00
int Process : : sys $ getcwd ( char * buffer , ssize_t size )
2018-10-26 15:24:11 +03:00
{
2019-02-25 23:19:57 +03:00
if ( size < 0 )
return - EINVAL ;
2018-11-16 18:10:59 +03:00
if ( ! validate_write ( buffer , size ) )
return - EFAULT ;
2019-05-30 21:23:50 +03:00
auto path = current_directory ( ) . absolute_path ( ) ;
2019-12-09 19:45:40 +03:00
if ( ( size_t ) size < path . length ( ) + 1 )
2018-10-30 02:06:31 +03:00
return - ERANGE ;
strcpy ( buffer , path . characters ( ) ) ;
2018-11-11 17:36:40 +03:00
return 0 ;
2018-10-26 15:24:11 +03:00
}
2019-03-07 00:30:13 +03:00
int Process : : number_of_open_file_descriptors ( ) const
2018-11-01 15:39:28 +03:00
{
2019-03-07 00:30:13 +03:00
int count = 0 ;
2019-06-13 23:03:04 +03:00
for ( auto & description : m_fds ) {
if ( description )
2018-11-01 15:39:28 +03:00
+ + count ;
}
return count ;
}
2019-07-08 21:01:49 +03:00
int Process : : sys $ open ( const Syscall : : SC_open_params * params )
2018-10-16 12:01:38 +03:00
{
2019-07-08 21:01:49 +03:00
if ( ! validate_read_typed ( params ) )
return - EFAULT ;
2019-11-10 17:32:59 +03:00
auto & [ path , path_length , options , mode ] = * params ;
2019-11-11 11:38:44 +03:00
if ( ! path_length )
return - EINVAL ;
2019-11-10 15:47:02 +03:00
if ( ! validate_read ( path , path_length ) )
return - EFAULT ;
2019-12-02 11:33:37 +03:00
int fd = alloc_fd ( ) ;
2018-10-23 11:12:50 +03:00
# ifdef DEBUG_IO
2019-12-02 11:33:37 +03:00
dbgprintf ( " %s(%u) sys$open( \" %s \" ) -> %d \n " , name ( ) . characters ( ) , pid ( ) , path , fd ) ;
2018-10-23 11:12:50 +03:00
# endif
2019-11-10 15:47:02 +03:00
if ( fd < 0 )
return fd ;
auto result = VFS : : the ( ) . open ( path , options , mode & ~ umask ( ) , current_directory ( ) ) ;
if ( result . is_error ( ) )
return result . error ( ) ;
auto description = result . value ( ) ;
if ( options & O_DIRECTORY & & ! description - > is_directory ( ) )
return - ENOTDIR ; // FIXME: This should be handled by VFS::open.
description - > set_file_flags ( options ) ;
u32 fd_flags = ( options & O_CLOEXEC ) ? FD_CLOEXEC : 0 ;
m_fds [ fd ] . set ( move ( description ) , fd_flags ) ;
return fd ;
}
int Process : : sys $ openat ( const Syscall : : SC_openat_params * params )
{
if ( ! validate_read_typed ( params ) )
return - EFAULT ;
2019-11-10 17:32:59 +03:00
auto & [ dirfd , path , path_length , options , mode ] = * params ;
2019-07-08 21:01:49 +03:00
if ( ! validate_read ( path , path_length ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2019-11-10 15:47:02 +03:00
# ifdef DEBUG_IO
dbgprintf ( " %s(%u) sys$openat(%d, \" %s \" ) \n " , dirfd , name ( ) . characters ( ) , pid ( ) , path ) ;
# endif
2019-04-06 15:54:32 +03:00
int fd = alloc_fd ( ) ;
if ( fd < 0 )
return fd ;
2019-11-10 15:47:02 +03:00
RefPtr < Custody > base ;
if ( dirfd = = AT_FDCWD ) {
base = current_directory ( ) ;
} else {
auto * base_description = file_description ( dirfd ) ;
if ( ! base_description )
return - EBADF ;
if ( ! base_description - > is_directory ( ) )
return - ENOTDIR ;
if ( ! base_description - > custody ( ) )
return - EINVAL ;
base = base_description - > custody ( ) ;
}
auto result = VFS : : the ( ) . open ( path , options , mode & ~ umask ( ) , * base ) ;
2019-03-07 00:14:31 +03:00
if ( result . is_error ( ) )
return result . error ( ) ;
2019-06-13 23:03:04 +03:00
auto description = result . value ( ) ;
if ( options & O_DIRECTORY & & ! description - > is_directory ( ) )
2018-10-28 16:11:51 +03:00
return - ENOTDIR ; // FIXME: This should be handled by VFS::open.
2019-06-13 23:03:04 +03:00
description - > set_file_flags ( options ) ;
2019-07-03 22:17:35 +03:00
u32 fd_flags = ( options & O_CLOEXEC ) ? FD_CLOEXEC : 0 ;
2019-06-13 23:03:04 +03:00
m_fds [ fd ] . set ( move ( description ) , fd_flags ) ;
2018-10-26 15:30:13 +03:00
return fd ;
2018-10-16 12:01:38 +03:00
}
2019-04-06 15:54:32 +03:00
int Process : : alloc_fd ( int first_candidate_fd )
2018-11-12 03:28:46 +03:00
{
2019-04-06 15:54:32 +03:00
int fd = - EMFILE ;
for ( int i = first_candidate_fd ; i < ( int ) m_max_open_file_descriptors ; + + i ) {
2018-11-13 03:36:31 +03:00
if ( ! m_fds [ i ] ) {
2018-11-12 03:28:46 +03:00
fd = i ;
break ;
}
}
return fd ;
}
2019-08-05 15:29:05 +03:00
int Process : : sys $ pipe ( int pipefd [ 2 ] , int flags )
2018-11-11 02:20:53 +03:00
{
2018-11-16 18:23:39 +03:00
if ( ! validate_write_typed ( pipefd ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2018-11-12 03:28:46 +03:00
if ( number_of_open_file_descriptors ( ) + 2 > max_open_file_descriptors ( ) )
return - EMFILE ;
2019-08-05 15:29:05 +03:00
// Reject flags other than O_CLOEXEC.
if ( ( flags & O_CLOEXEC ) ! = flags )
return - EINVAL ;
u32 fd_flags = ( flags & O_CLOEXEC ) ? FD_CLOEXEC : 0 ;
2019-04-25 14:52:07 +03:00
auto fifo = FIFO : : create ( m_uid ) ;
2018-11-12 03:28:46 +03:00
int reader_fd = alloc_fd ( ) ;
2019-08-05 15:29:05 +03:00
m_fds [ reader_fd ] . set ( fifo - > open_direction ( FIFO : : Direction : : Reader ) , fd_flags ) ;
2018-11-12 03:28:46 +03:00
pipefd [ 0 ] = reader_fd ;
int writer_fd = alloc_fd ( ) ;
2019-08-05 15:29:05 +03:00
m_fds [ writer_fd ] . set ( fifo - > open_direction ( FIFO : : Direction : : Writer ) , fd_flags ) ;
2018-11-12 03:28:46 +03:00
pipefd [ 1 ] = writer_fd ;
return 0 ;
2018-11-11 02:20:53 +03:00
}
int Process : : sys $ killpg ( int pgrp , int signum )
{
if ( signum < 1 | | signum > = 32 )
return - EINVAL ;
2019-11-14 19:16:30 +03:00
if ( pgrp < 0 )
return - EINVAL ;
InterruptDisabler disabler ;
return do_killpg ( pgrp , signum ) ;
2018-11-11 02:20:53 +03:00
}
2019-02-22 01:35:07 +03:00
int Process : : sys $ setuid ( uid_t uid )
2018-11-11 02:20:53 +03:00
{
2019-02-22 01:35:07 +03:00
if ( uid ! = m_uid & & ! is_superuser ( ) )
return - EPERM ;
m_uid = uid ;
m_euid = uid ;
return 0 ;
2018-11-11 02:20:53 +03:00
}
2019-02-22 01:35:07 +03:00
int Process : : sys $ setgid ( gid_t gid )
2018-11-11 02:20:53 +03:00
{
2019-02-22 01:35:07 +03:00
if ( gid ! = m_gid & & ! is_superuser ( ) )
return - EPERM ;
m_gid = gid ;
m_egid = gid ;
return 0 ;
2018-11-11 02:20:53 +03:00
}
unsigned Process : : sys $ alarm ( unsigned seconds )
{
2019-06-07 12:30:07 +03:00
unsigned previous_alarm_remaining = 0 ;
if ( m_alarm_deadline & & m_alarm_deadline > g_uptime ) {
previous_alarm_remaining = ( m_alarm_deadline - g_uptime ) / TICKS_PER_SECOND ;
}
if ( ! seconds ) {
m_alarm_deadline = 0 ;
return previous_alarm_remaining ;
}
m_alarm_deadline = g_uptime + seconds * TICKS_PER_SECOND ;
return previous_alarm_remaining ;
2018-11-11 02:20:53 +03:00
}
2018-11-01 15:15:46 +03:00
int Process : : sys $ uname ( utsname * buf )
2018-10-26 15:56:21 +03:00
{
2018-11-16 18:23:39 +03:00
if ( ! validate_write_typed ( buf ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2019-12-29 21:08:02 +03:00
strcpy ( buf - > sysname , " SerenityOS " ) ;
2018-10-26 15:56:21 +03:00
strcpy ( buf - > release , " 1.0-dev " ) ;
strcpy ( buf - > version , " FIXME " ) ;
2019-11-17 21:39:12 +03:00
strcpy ( buf - > machine , " i686 " ) ;
2019-02-07 12:29:26 +03:00
LOCKER ( * s_hostname_lock ) ;
strncpy ( buf - > nodename , s_hostname - > characters ( ) , sizeof ( utsname : : nodename ) ) ;
2018-10-26 15:56:21 +03:00
return 0 ;
}
2019-11-14 19:16:30 +03:00
KResult Process : : do_kill ( Process & process , int signal )
{
// FIXME: Allow sending SIGCONT to everyone in the process group.
// FIXME: Should setuid processes have some special treatment here?
if ( ! is_superuser ( ) & & m_euid ! = process . m_uid & & m_uid ! = process . m_uid )
return KResult ( - EPERM ) ;
if ( process . is_ring0 ( ) & & signal = = SIGKILL ) {
kprintf ( " %s(%u) attempted to send SIGKILL to ring 0 process %s(%u) \n " , name ( ) . characters ( ) , m_pid , process . name ( ) . characters ( ) , process . pid ( ) ) ;
return KResult ( - EPERM ) ;
}
process . send_signal ( signal , this ) ;
return KSuccess ;
}
KResult Process : : do_killpg ( pid_t pgrp , int signal )
{
ASSERT ( pgrp > = 0 ) ;
// Send the signal to all processes in the given group.
if ( pgrp = = 0 ) {
// Send the signal to our own pgrp.
pgrp = pgid ( ) ;
}
bool group_was_empty = true ;
bool any_succeeded = false ;
KResult error = KSuccess ;
Process : : for_each_in_pgrp ( pgrp , [ & ] ( auto & process ) {
group_was_empty = false ;
KResult res = do_kill ( process , signal ) ;
if ( res . is_success ( ) )
any_succeeded = true ;
else
error = res ;
return IterationDecision : : Continue ;
} ) ;
if ( group_was_empty )
return KResult ( - ESRCH ) ;
if ( any_succeeded )
return KSuccess ;
return error ;
}
2018-11-02 16:06:48 +03:00
int Process : : sys $ kill ( pid_t pid , int signal )
2018-10-16 12:01:38 +03:00
{
2019-02-28 13:45:45 +03:00
if ( signal < 0 | | signal > = 32 )
return - EINVAL ;
2019-11-14 19:16:30 +03:00
if ( pid < = 0 ) {
return do_killpg ( - pid , signal ) ;
2018-10-16 12:01:38 +03:00
}
if ( pid = = - 1 ) {
// FIXME: Send to all processes.
ASSERT ( pid ! = - 1 ) ;
}
2019-02-28 11:44:48 +03:00
if ( pid = = m_pid ) {
2019-07-25 22:02:19 +03:00
// FIXME: If we ignore this signal anyway, we don't need to block here, right?
2019-03-24 00:03:17 +03:00
current - > send_signal ( signal , this ) ;
2019-07-20 12:05:52 +03:00
( void ) current - > block < Thread : : SemiPermanentBlocker > ( Thread : : SemiPermanentBlocker : : Reason : : Signal ) ;
2019-02-28 11:44:48 +03:00
return 0 ;
}
2019-02-28 13:45:45 +03:00
InterruptDisabler disabler ;
auto * peer = Process : : from_pid ( pid ) ;
2018-10-31 03:06:57 +03:00
if ( ! peer )
return - ESRCH ;
2019-11-14 19:16:30 +03:00
return do_kill ( * peer , signal ) ;
2018-10-16 12:01:38 +03:00
}
2019-02-03 18:11:28 +03:00
int Process : : sys $ usleep ( useconds_t usec )
{
if ( ! usec )
return 0 ;
2019-07-18 18:26:11 +03:00
u64 wakeup_time = current - > sleep ( usec / 1000 ) ;
2019-11-06 23:01:44 +03:00
if ( wakeup_time > g_uptime )
return - EINTR ;
2019-02-03 18:11:28 +03:00
return 0 ;
}
2018-11-01 15:15:46 +03:00
int Process : : sys $ sleep ( unsigned seconds )
2018-10-25 14:53:49 +03:00
{
if ( ! seconds )
return 0 ;
2019-07-18 18:26:11 +03:00
u64 wakeup_time = current - > sleep ( seconds * TICKS_PER_SECOND ) ;
if ( wakeup_time > g_uptime ) {
u32 ticks_left_until_original_wakeup_time = wakeup_time - g_uptime ;
2018-11-07 23:19:47 +03:00
return ticks_left_until_original_wakeup_time / TICKS_PER_SECOND ;
}
2018-10-25 14:53:49 +03:00
return 0 ;
}
2019-06-06 18:46:41 +03:00
timeval kgettimeofday ( )
2019-03-13 15:13:23 +03:00
{
2019-12-16 00:21:28 +03:00
return const_cast < const timeval & > ( ( ( KernelInfoPage * ) s_info_page_address_for_kernel . as_ptr ( ) ) - > now ) ;
2019-06-06 18:46:41 +03:00
}
void kgettimeofday ( timeval & tv )
{
tv = kgettimeofday ( ) ;
2019-03-13 15:13:23 +03:00
}
2018-11-01 15:15:46 +03:00
int Process : : sys $ gettimeofday ( timeval * tv )
2018-10-25 18:29:49 +03:00
{
2018-11-16 18:23:39 +03:00
if ( ! validate_write_typed ( tv ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2019-12-15 23:29:26 +03:00
* tv = kgettimeofday ( ) ;
2018-10-25 18:29:49 +03:00
return 0 ;
}
2018-11-01 15:15:46 +03:00
uid_t Process : : sys $ getuid ( )
2018-10-16 12:01:38 +03:00
{
return m_uid ;
}
2018-11-01 15:15:46 +03:00
gid_t Process : : sys $ getgid ( )
2018-10-22 14:55:11 +03:00
{
return m_gid ;
}
2018-11-05 17:04:19 +03:00
uid_t Process : : sys $ geteuid ( )
{
return m_euid ;
}
gid_t Process : : sys $ getegid ( )
{
return m_egid ;
}
2018-11-01 15:15:46 +03:00
pid_t Process : : sys $ getpid ( )
2018-10-22 14:55:11 +03:00
{
return m_pid ;
}
2018-11-06 15:33:06 +03:00
pid_t Process : : sys $ getppid ( )
{
return m_ppid ;
}
2018-11-06 15:40:23 +03:00
mode_t Process : : sys $ umask ( mode_t mask )
{
auto old_mask = m_umask ;
2019-02-22 04:39:13 +03:00
m_umask = mask & 0777 ;
2018-11-06 15:40:23 +03:00
return old_mask ;
}
2018-11-29 00:01:24 +03:00
int Process : : reap ( Process & process )
2018-11-08 01:59:49 +03:00
{
2019-03-27 16:38:32 +03:00
int exit_status ;
{
InterruptDisabler disabler ;
exit_status = ( process . m_termination_status < < 8 ) | process . m_termination_signal ;
2018-12-03 03:12:26 +03:00
2019-03-27 16:38:32 +03:00
if ( process . ppid ( ) ) {
auto * parent = Process : : from_pid ( process . ppid ( ) ) ;
if ( parent ) {
parent - > m_ticks_in_user_for_dead_children + = process . m_ticks_in_user + process . m_ticks_in_user_for_dead_children ;
parent - > m_ticks_in_kernel_for_dead_children + = process . m_ticks_in_kernel + process . m_ticks_in_kernel_for_dead_children ;
}
2019-01-30 21:35:38 +03:00
}
2018-12-03 03:12:26 +03:00
2019-12-22 13:35:02 +03:00
dbgprintf ( " reap: %s(%u) \n " , process . name ( ) . characters ( ) , process . pid ( ) ) ;
2019-03-27 16:38:32 +03:00
ASSERT ( process . is_dead ( ) ) ;
g_processes - > remove ( & process ) ;
}
2018-11-09 03:25:31 +03:00
delete & process ;
2018-11-29 00:01:24 +03:00
return exit_status ;
2018-11-08 01:59:49 +03:00
}
2018-11-01 15:15:46 +03:00
pid_t Process : : sys $ waitpid ( pid_t waitee , int * wstatus , int options )
2018-10-24 01:20:34 +03:00
{
2018-11-17 02:11:08 +03:00
dbgprintf ( " sys$waitpid(%d, %p, %d) \n " , waitee , wstatus , options ) ;
2019-07-14 12:35:49 +03:00
if ( ! options ) {
// FIXME: This can't be right.. can it? Figure out how this should actually work.
options = WEXITED ;
}
2018-10-27 02:24:22 +03:00
if ( wstatus )
2018-11-16 18:23:39 +03:00
if ( ! validate_write_typed ( wstatus ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2018-10-27 02:24:22 +03:00
2018-11-29 00:01:24 +03:00
int dummy_wstatus ;
int & exit_status = wstatus ? * wstatus : dummy_wstatus ;
2018-11-08 01:59:49 +03:00
{
InterruptDisabler disabler ;
2018-11-11 17:36:40 +03:00
if ( waitee ! = - 1 & & ! Process : : from_pid ( waitee ) )
2018-11-08 01:59:49 +03:00
return - ECHILD ;
}
2018-11-17 02:11:08 +03:00
if ( options & WNOHANG ) {
2019-07-14 12:35:49 +03:00
// FIXME: Figure out what WNOHANG should do with stopped children.
2018-11-17 02:11:08 +03:00
if ( waitee = = - 1 ) {
pid_t reaped_pid = 0 ;
InterruptDisabler disabler ;
2019-06-07 12:43:58 +03:00
for_each_child ( [ & reaped_pid , & exit_status ] ( Process & process ) {
2019-03-24 00:03:17 +03:00
if ( process . is_dead ( ) ) {
2018-11-17 02:11:08 +03:00
reaped_pid = process . pid ( ) ;
2018-11-29 00:01:24 +03:00
exit_status = reap ( process ) ;
2018-11-17 02:11:08 +03:00
}
2019-07-14 12:35:49 +03:00
return IterationDecision : : Continue ;
2018-11-17 02:11:08 +03:00
} ) ;
return reaped_pid ;
} else {
2019-02-04 16:06:38 +03:00
ASSERT ( waitee > 0 ) ; // FIXME: Implement other PID specs.
2019-02-27 02:02:01 +03:00
InterruptDisabler disabler ;
2018-11-17 02:11:08 +03:00
auto * waitee_process = Process : : from_pid ( waitee ) ;
if ( ! waitee_process )
return - ECHILD ;
2019-03-24 00:03:17 +03:00
if ( waitee_process - > is_dead ( ) ) {
2018-11-29 00:01:24 +03:00
exit_status = reap ( * waitee_process ) ;
2018-11-17 02:11:08 +03:00
return waitee ;
}
return 0 ;
}
}
2019-07-18 19:05:19 +03:00
pid_t waitee_pid = waitee ;
2019-07-20 12:05:52 +03:00
if ( current - > block < Thread : : WaitBlocker > ( options , waitee_pid ) = = Thread : : BlockResult : : InterruptedBySignal )
2018-11-07 23:19:47 +03:00
return - EINTR ;
2019-07-14 12:35:49 +03:00
InterruptDisabler disabler ;
// NOTE: If waitee was -1, m_waitee_pid will have been filled in by the scheduler.
2019-07-18 19:05:19 +03:00
Process * waitee_process = Process : : from_pid ( waitee_pid ) ;
2019-09-08 14:54:48 +03:00
if ( ! waitee_process )
return - ECHILD ;
2018-11-09 03:25:31 +03:00
ASSERT ( waitee_process ) ;
2019-07-14 12:35:49 +03:00
if ( waitee_process - > is_dead ( ) ) {
exit_status = reap ( * waitee_process ) ;
} else {
2019-12-22 13:35:02 +03:00
ASSERT ( waitee_process - > any_thread ( ) . state ( ) = = Thread : : State : : Stopped ) ;
2019-07-14 12:35:49 +03:00
exit_status = 0x7f ;
}
2019-07-18 19:05:19 +03:00
return waitee_pid ;
2018-10-16 12:01:38 +03:00
}
2019-06-07 21:02:01 +03:00
enum class KernelMemoryCheckResult {
2019-02-11 08:03:30 +03:00
NotInsideKernelMemory ,
AccessGranted ,
AccessDenied
} ;
2019-06-07 13:56:50 +03:00
static KernelMemoryCheckResult check_kernel_memory_access ( VirtualAddress vaddr , bool is_write )
2019-01-27 12:17:56 +03:00
{
2019-06-02 10:50:18 +03:00
auto & sections = multiboot_info_ptr - > u . elf_sec ;
2019-05-18 04:06:34 +03:00
2019-06-02 10:50:18 +03:00
auto * kernel_program_headers = ( Elf32_Phdr * ) ( sections . addr ) ;
for ( unsigned i = 0 ; i < sections . num ; + + i ) {
2019-02-06 19:28:14 +03:00
auto & segment = kernel_program_headers [ i ] ;
2019-02-11 08:03:30 +03:00
if ( segment . p_type ! = PT_LOAD | | ! segment . p_vaddr | | ! segment . p_memsz )
continue ;
2019-06-07 13:56:50 +03:00
if ( vaddr . get ( ) < segment . p_vaddr | | vaddr . get ( ) > ( segment . p_vaddr + segment . p_memsz ) )
2019-02-06 19:28:14 +03:00
continue ;
if ( is_write & & ! ( kernel_program_headers [ i ] . p_flags & PF_W ) )
2019-02-11 08:03:30 +03:00
return KernelMemoryCheckResult : : AccessDenied ;
2019-02-06 19:28:14 +03:00
if ( ! is_write & & ! ( kernel_program_headers [ i ] . p_flags & PF_R ) )
2019-02-11 08:03:30 +03:00
return KernelMemoryCheckResult : : AccessDenied ;
return KernelMemoryCheckResult : : AccessGranted ;
2019-02-06 19:28:14 +03:00
}
2019-02-11 08:03:30 +03:00
return KernelMemoryCheckResult : : NotInsideKernelMemory ;
2019-01-27 12:17:56 +03:00
}
2019-12-24 03:01:23 +03:00
bool Process : : validate_read_from_kernel ( VirtualAddress vaddr , ssize_t size ) const
2018-10-27 01:14:24 +03:00
{
2019-06-07 13:56:50 +03:00
if ( vaddr . is_null ( ) )
2019-04-16 00:48:31 +03:00
return false ;
2018-11-01 14:45:51 +03:00
// We check extra carefully here since the first 4MB of the address space is identity-mapped.
// This code allows access outside of the known used address ranges to get caught.
2019-06-07 13:56:50 +03:00
auto kmc_result = check_kernel_memory_access ( vaddr , false ) ;
2019-02-11 08:03:30 +03:00
if ( kmc_result = = KernelMemoryCheckResult : : AccessGranted )
2018-10-27 01:14:24 +03:00
return true ;
2019-02-11 08:03:30 +03:00
if ( kmc_result = = KernelMemoryCheckResult : : AccessDenied )
return false ;
2019-06-07 13:56:50 +03:00
if ( is_kmalloc_address ( vaddr . as_ptr ( ) ) )
2018-10-27 01:14:24 +03:00
return true ;
2019-12-24 03:01:23 +03:00
return validate_read ( vaddr . as_ptr ( ) , size ) ;
2018-10-27 01:14:24 +03:00
}
2019-02-08 02:10:01 +03:00
bool Process : : validate_read_str ( const char * str )
{
if ( ! validate_read ( str , 1 ) )
return false ;
return validate_read ( str , strlen ( str ) + 1 ) ;
}
2019-02-25 23:19:57 +03:00
bool Process : : validate_read ( const void * address , ssize_t size ) const
2018-11-16 17:41:48 +03:00
{
2019-02-25 23:19:57 +03:00
ASSERT ( size > = 0 ) ;
2019-07-03 22:17:35 +03:00
VirtualAddress first_address ( ( u32 ) address ) ;
2019-06-07 13:56:50 +03:00
VirtualAddress last_address = first_address . offset ( size - 1 ) ;
2019-12-31 20:23:17 +03:00
if ( last_address < first_address )
return false ;
2019-01-31 19:31:23 +03:00
if ( is_ring0 ( ) ) {
2019-02-11 08:03:30 +03:00
auto kmc_result = check_kernel_memory_access ( first_address , false ) ;
if ( kmc_result = = KernelMemoryCheckResult : : AccessGranted )
2019-01-27 12:17:56 +03:00
return true ;
2019-02-11 08:03:30 +03:00
if ( kmc_result = = KernelMemoryCheckResult : : AccessDenied )
return false ;
2019-01-27 12:17:56 +03:00
if ( is_kmalloc_address ( address ) )
return true ;
}
2019-01-25 03:39:15 +03:00
ASSERT ( size ) ;
if ( ! size )
return false ;
if ( first_address . page_base ( ) ! = last_address . page_base ( ) ) {
if ( ! MM . validate_user_read ( * this , last_address ) )
2018-11-16 17:41:48 +03:00
return false ;
}
2019-01-25 03:39:15 +03:00
return MM . validate_user_read ( * this , first_address ) ;
2018-11-16 17:41:48 +03:00
}
2019-02-25 23:19:57 +03:00
bool Process : : validate_write ( void * address , ssize_t size ) const
2018-11-16 17:41:48 +03:00
{
2019-02-25 23:19:57 +03:00
ASSERT ( size > = 0 ) ;
2019-07-03 22:17:35 +03:00
VirtualAddress first_address ( ( u32 ) address ) ;
2019-06-07 13:56:50 +03:00
VirtualAddress last_address = first_address . offset ( size - 1 ) ;
2019-12-31 20:23:17 +03:00
if ( last_address < first_address )
return false ;
2019-01-31 19:31:23 +03:00
if ( is_ring0 ( ) ) {
2019-01-27 12:17:56 +03:00
if ( is_kmalloc_address ( address ) )
return true ;
2019-02-11 08:03:30 +03:00
auto kmc_result = check_kernel_memory_access ( first_address , true ) ;
if ( kmc_result = = KernelMemoryCheckResult : : AccessGranted )
2019-02-06 19:28:14 +03:00
return true ;
2019-02-11 08:03:30 +03:00
if ( kmc_result = = KernelMemoryCheckResult : : AccessDenied )
return false ;
2019-01-27 12:17:56 +03:00
}
2019-01-25 03:39:15 +03:00
if ( ! size )
return false ;
if ( first_address . page_base ( ) ! = last_address . page_base ( ) ) {
if ( ! MM . validate_user_write ( * this , last_address ) )
2018-11-16 17:41:48 +03:00
return false ;
}
2019-12-31 20:17:27 +03:00
return MM . validate_user_write ( * this , first_address ) ;
2018-11-16 17:41:48 +03:00
}
2018-11-02 14:56:51 +03:00
pid_t Process : : sys $ getsid ( pid_t pid )
{
if ( pid = = 0 )
return m_sid ;
InterruptDisabler disabler ;
2018-11-07 23:38:18 +03:00
auto * process = Process : : from_pid ( pid ) ;
2018-11-02 14:56:51 +03:00
if ( ! process )
return - ESRCH ;
if ( m_sid ! = process - > m_sid )
return - EPERM ;
return process - > m_sid ;
}
pid_t Process : : sys $ setsid ( )
{
InterruptDisabler disabler ;
bool found_process_with_same_pgid_as_my_pid = false ;
2019-06-07 12:43:58 +03:00
Process : : for_each_in_pgrp ( pid ( ) , [ & ] ( auto & ) {
2018-11-07 23:38:18 +03:00
found_process_with_same_pgid_as_my_pid = true ;
2019-08-22 22:12:55 +03:00
return IterationDecision : : Break ;
2018-11-02 14:56:51 +03:00
} ) ;
if ( found_process_with_same_pgid_as_my_pid )
return - EPERM ;
m_sid = m_pid ;
m_pgid = m_pid ;
return m_sid ;
}
pid_t Process : : sys $ getpgid ( pid_t pid )
{
if ( pid = = 0 )
return m_pgid ;
InterruptDisabler disabler ; // FIXME: Use a ProcessHandle
2018-11-07 23:38:18 +03:00
auto * process = Process : : from_pid ( pid ) ;
2018-11-02 14:56:51 +03:00
if ( ! process )
return - ESRCH ;
return process - > m_pgid ;
}
pid_t Process : : sys $ getpgrp ( )
{
return m_pgid ;
}
static pid_t get_sid_from_pgid ( pid_t pgid )
{
InterruptDisabler disabler ;
2018-11-07 23:38:18 +03:00
auto * group_leader = Process : : from_pid ( pgid ) ;
2018-11-02 14:56:51 +03:00
if ( ! group_leader )
return - 1 ;
return group_leader - > sid ( ) ;
}
int Process : : sys $ setpgid ( pid_t specified_pid , pid_t specified_pgid )
{
InterruptDisabler disabler ; // FIXME: Use a ProcessHandle
pid_t pid = specified_pid ? specified_pid : m_pid ;
if ( specified_pgid < 0 )
return - EINVAL ;
2018-11-07 23:38:18 +03:00
auto * process = Process : : from_pid ( pid ) ;
2018-11-02 14:56:51 +03:00
if ( ! process )
return - ESRCH ;
pid_t new_pgid = specified_pgid ? specified_pgid : process - > m_pid ;
pid_t current_sid = get_sid_from_pgid ( process - > m_pgid ) ;
pid_t new_sid = get_sid_from_pgid ( new_pgid ) ;
if ( current_sid ! = new_sid ) {
// Can't move a process between sessions.
return - EPERM ;
}
// FIXME: There are more EPERM conditions to check for here..
process - > m_pgid = new_pgid ;
return 0 ;
}
2018-11-02 15:14:25 +03:00
2018-11-16 15:11:21 +03:00
int Process : : sys $ ioctl ( int fd , unsigned request , unsigned arg )
2018-11-02 15:14:25 +03:00
{
2019-06-13 23:03:04 +03:00
auto * description = file_description ( fd ) ;
if ( ! description )
2018-11-02 15:14:25 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
return description - > file ( ) . ioctl ( * description , request , arg ) ;
2018-11-02 15:14:25 +03:00
}
2018-11-05 21:01:22 +03:00
int Process : : sys $ getdtablesize ( )
{
return m_max_open_file_descriptors ;
}
int Process : : sys $ dup ( int old_fd )
{
2019-06-13 23:03:04 +03:00
auto * description = file_description ( old_fd ) ;
if ( ! description )
2018-11-05 21:01:22 +03:00
return - EBADF ;
2019-04-06 15:54:32 +03:00
int new_fd = alloc_fd ( 0 ) ;
if ( new_fd < 0 )
return new_fd ;
2019-06-13 23:03:04 +03:00
m_fds [ new_fd ] . set ( * description ) ;
2018-11-05 21:01:22 +03:00
return new_fd ;
}
int Process : : sys $ dup2 ( int old_fd , int new_fd )
{
2019-06-13 23:03:04 +03:00
auto * description = file_description ( old_fd ) ;
if ( ! description )
2018-11-05 21:01:22 +03:00
return - EBADF ;
2019-04-06 15:54:32 +03:00
if ( new_fd < 0 | | new_fd > = m_max_open_file_descriptors )
return - EINVAL ;
2019-06-13 23:03:04 +03:00
m_fds [ new_fd ] . set ( * description ) ;
2018-11-05 21:01:22 +03:00
return new_fd ;
}
2018-11-06 12:46:40 +03:00
2019-01-23 08:53:01 +03:00
int Process : : sys $ sigprocmask ( int how , const sigset_t * set , sigset_t * old_set )
2018-11-11 01:29:07 +03:00
{
2018-11-11 17:36:40 +03:00
if ( old_set ) {
2019-02-21 23:33:52 +03:00
if ( ! validate_write_typed ( old_set ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2019-03-24 00:03:17 +03:00
* old_set = current - > m_signal_mask ;
2018-11-11 17:36:40 +03:00
}
if ( set ) {
2018-11-16 18:23:39 +03:00
if ( ! validate_read_typed ( set ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2018-11-11 17:36:40 +03:00
switch ( how ) {
case SIG_BLOCK :
2019-03-24 00:03:17 +03:00
current - > m_signal_mask & = ~ ( * set ) ;
2018-11-11 17:36:40 +03:00
break ;
case SIG_UNBLOCK :
2019-03-24 00:03:17 +03:00
current - > m_signal_mask | = * set ;
2018-11-11 17:36:40 +03:00
break ;
case SIG_SETMASK :
2019-03-24 00:03:17 +03:00
current - > m_signal_mask = * set ;
2018-11-11 17:36:40 +03:00
break ;
default :
return - EINVAL ;
}
2018-11-11 01:29:07 +03:00
}
return 0 ;
}
2019-01-23 08:53:01 +03:00
int Process : : sys $ sigpending ( sigset_t * set )
2018-11-11 01:29:07 +03:00
{
2019-02-21 23:33:52 +03:00
if ( ! validate_write_typed ( set ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2019-03-24 00:03:17 +03:00
* set = current - > m_pending_signals ;
2018-11-11 01:29:07 +03:00
return 0 ;
}
2019-01-23 08:53:01 +03:00
int Process : : sys $ sigaction ( int signum , const sigaction * act , sigaction * old_act )
2018-11-06 12:46:40 +03:00
{
2018-12-25 00:22:19 +03:00
if ( signum < 1 | | signum > = 32 | | signum = = SIGKILL | | signum = = SIGSTOP )
2018-11-06 12:46:40 +03:00
return - EINVAL ;
2018-11-16 18:23:39 +03:00
if ( ! validate_read_typed ( act ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2019-02-07 13:24:09 +03:00
InterruptDisabler disabler ; // FIXME: This should use a narrower lock. Maybe a way to ignore signals temporarily?
2019-03-24 00:03:17 +03:00
auto & action = current - > m_signal_action_data [ signum ] ;
2018-11-07 12:48:44 +03:00
if ( old_act ) {
2018-11-16 18:23:39 +03:00
if ( ! validate_write_typed ( old_act ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2018-11-07 12:48:44 +03:00
old_act - > sa_flags = action . flags ;
old_act - > sa_sigaction = ( decltype ( old_act - > sa_sigaction ) ) action . handler_or_sigaction . get ( ) ;
}
2018-11-06 12:46:40 +03:00
action . flags = act - > sa_flags ;
2019-07-03 22:17:35 +03:00
action . handler_or_sigaction = VirtualAddress ( ( u32 ) act - > sa_sigaction ) ;
2018-11-06 12:46:40 +03:00
return 0 ;
}
2018-11-07 03:38:51 +03:00
2019-02-26 00:06:55 +03:00
int Process : : sys $ getgroups ( ssize_t count , gid_t * gids )
2018-11-07 03:38:51 +03:00
{
if ( count < 0 )
return - EINVAL ;
if ( ! count )
return m_gids . size ( ) ;
2018-11-09 12:03:21 +03:00
if ( count ! = ( int ) m_gids . size ( ) )
2018-11-07 03:38:51 +03:00
return - EINVAL ;
2018-11-16 18:23:39 +03:00
if ( ! validate_write_typed ( gids , m_gids . size ( ) ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2018-11-07 03:38:51 +03:00
size_t i = 0 ;
for ( auto gid : m_gids )
gids [ i + + ] = gid ;
return 0 ;
}
2019-02-26 00:06:55 +03:00
int Process : : sys $ setgroups ( ssize_t count , const gid_t * gids )
2018-11-07 03:38:51 +03:00
{
2019-02-26 00:06:55 +03:00
if ( count < 0 )
return - EINVAL ;
2019-02-22 01:35:07 +03:00
if ( ! is_superuser ( ) )
2018-11-07 03:38:51 +03:00
return - EPERM ;
2018-11-16 18:23:39 +03:00
if ( ! validate_read ( gids , count ) )
2018-11-16 18:10:59 +03:00
return - EFAULT ;
2018-11-07 03:38:51 +03:00
m_gids . clear ( ) ;
m_gids . set ( m_gid ) ;
2019-02-26 00:06:55 +03:00
for ( int i = 0 ; i < count ; + + i )
2018-11-07 03:38:51 +03:00
m_gids . set ( gids [ i ] ) ;
return 0 ;
}
2018-11-18 16:57:41 +03:00
int Process : : sys $ mkdir ( const char * pathname , mode_t mode )
{
if ( ! validate_read_str ( pathname ) )
return - EFAULT ;
2019-02-01 17:24:42 +03:00
size_t pathname_length = strlen ( pathname ) ;
if ( pathname_length = = 0 )
return - EINVAL ;
if ( pathname_length > = 255 )
2018-11-18 16:57:41 +03:00
return - ENAMETOOLONG ;
2019-05-30 21:23:50 +03:00
return VFS : : the ( ) . mkdir ( StringView ( pathname , pathname_length ) , mode & ~ umask ( ) , current_directory ( ) ) ;
2018-11-18 16:57:41 +03:00
}
2018-12-03 03:12:26 +03:00
2019-08-25 19:17:05 +03:00
int Process : : sys $ realpath ( const char * pathname , char * buffer , size_t size )
{
if ( ! validate_read_str ( pathname ) )
return - EFAULT ;
size_t pathname_length = strlen ( pathname ) ;
if ( pathname_length = = 0 )
return - EINVAL ;
if ( pathname_length > = size )
return - ENAMETOOLONG ;
if ( ! validate_write ( buffer , size ) )
return - EFAULT ;
auto custody_or_error = VFS : : the ( ) . resolve_path ( pathname , current_directory ( ) ) ;
if ( custody_or_error . is_error ( ) )
return custody_or_error . error ( ) ;
auto & custody = custody_or_error . value ( ) ;
// FIXME: Once resolve_path is fixed to deal with .. and . , remove the use of FileSystemPath::canonical_path.
FileSystemPath canonical_path ( custody - > absolute_path ( ) ) ;
if ( ! canonical_path . is_valid ( ) ) {
2019-08-29 22:01:33 +03:00
dbg ( ) < < " FileSystemPath failed to canonicalize " < < custody - > absolute_path ( ) ;
ASSERT_NOT_REACHED ( ) ;
2019-08-25 19:17:05 +03:00
}
strncpy ( buffer , canonical_path . string ( ) . characters ( ) , size ) ;
return 0 ;
} ;
2019-01-23 08:53:01 +03:00
clock_t Process : : sys $ times ( tms * times )
2018-12-03 03:12:26 +03:00
{
if ( ! validate_write_typed ( times ) )
return - EFAULT ;
times - > tms_utime = m_ticks_in_user ;
times - > tms_stime = m_ticks_in_kernel ;
times - > tms_cutime = m_ticks_in_user_for_dead_children ;
times - > tms_cstime = m_ticks_in_kernel_for_dead_children ;
2019-05-17 21:19:29 +03:00
return g_uptime & 0x7fffffff ;
2018-12-03 03:12:26 +03:00
}
2019-01-09 04:29:11 +03:00
2019-01-16 01:12:20 +03:00
int Process : : sys $ select ( const Syscall : : SC_select_params * params )
{
2019-06-06 18:46:41 +03:00
// FIXME: Return -EINVAL if timeout is invalid.
2019-01-16 01:12:20 +03:00
if ( ! validate_read_typed ( params ) )
return - EFAULT ;
2019-11-10 17:32:59 +03:00
auto & [ nfds , readfds , writefds , exceptfds , timeout ] = * params ;
if ( writefds & & ! validate_write_typed ( writefds ) )
2019-01-16 01:12:20 +03:00
return - EFAULT ;
2019-11-10 17:32:59 +03:00
if ( readfds & & ! validate_write_typed ( readfds ) )
2019-01-16 01:12:20 +03:00
return - EFAULT ;
2019-11-10 17:32:59 +03:00
if ( exceptfds & & ! validate_write_typed ( exceptfds ) )
2019-01-16 01:12:20 +03:00
return - EFAULT ;
2019-11-10 17:32:59 +03:00
if ( timeout & & ! validate_read_typed ( timeout ) )
2019-01-16 01:12:20 +03:00
return - EFAULT ;
2019-11-10 17:32:59 +03:00
if ( nfds < 0 )
2019-06-06 18:46:41 +03:00
return - EINVAL ;
2019-01-16 01:12:20 +03:00
2019-11-10 17:32:59 +03:00
timeval computed_timeout ;
2019-07-18 18:39:49 +03:00
bool select_has_timeout = false ;
2019-11-10 17:32:59 +03:00
if ( timeout & & ( timeout - > tv_sec | | timeout - > tv_usec ) ) {
timeval_add ( kgettimeofday ( ) , * timeout , computed_timeout ) ;
2019-07-18 18:39:49 +03:00
select_has_timeout = true ;
2019-02-01 05:50:06 +03:00
}
2019-01-16 01:12:20 +03:00
2019-07-19 10:04:12 +03:00
Thread : : SelectBlocker : : FDVector rfds ;
Thread : : SelectBlocker : : FDVector wfds ;
Thread : : SelectBlocker : : FDVector efds ;
2019-07-18 18:39:49 +03:00
2019-06-07 12:43:58 +03:00
auto transfer_fds = [ & ] ( auto * fds , auto & vector ) - > int {
2019-05-18 03:00:54 +03:00
vector . clear_with_capacity ( ) ;
2019-06-06 18:46:41 +03:00
if ( ! fds )
2019-01-30 21:01:31 +03:00
return 0 ;
2019-06-06 18:46:41 +03:00
for ( int fd = 0 ; fd < params - > nfds ; + + fd ) {
if ( FD_ISSET ( fd , fds ) ) {
2019-12-02 11:33:37 +03:00
if ( ! file_description ( fd ) ) {
dbg ( ) < < * current < < " sys$select: Bad fd number " < < fd ;
2019-01-30 21:01:31 +03:00
return - EBADF ;
2019-12-02 11:33:37 +03:00
}
2019-06-06 18:46:41 +03:00
vector . append ( fd ) ;
2019-01-30 21:01:31 +03:00
}
2019-01-16 01:12:20 +03:00
}
2019-01-30 21:01:31 +03:00
return 0 ;
2019-01-16 01:12:20 +03:00
} ;
2019-11-10 17:32:59 +03:00
if ( int error = transfer_fds ( writefds , wfds ) )
2019-01-30 21:01:31 +03:00
return error ;
2019-11-10 17:32:59 +03:00
if ( int error = transfer_fds ( readfds , rfds ) )
2019-01-30 21:01:31 +03:00
return error ;
2019-11-10 17:32:59 +03:00
if ( int error = transfer_fds ( exceptfds , efds ) )
2019-02-27 02:02:01 +03:00
return error ;
2019-01-16 01:12:20 +03:00
2019-05-19 11:24:28 +03:00
# if defined(DEBUG_IO) || defined(DEBUG_POLL_SELECT)
2019-11-10 17:32:59 +03:00
dbgprintf ( " %s<%u> selecting on (read:%u, write:%u), timeout=%p \n " , name ( ) . characters ( ) , pid ( ) , rfds . size ( ) , wfds . size ( ) , timeout ) ;
2019-01-16 19:20:58 +03:00
# endif
2019-11-10 17:32:59 +03:00
if ( ! timeout | | select_has_timeout ) {
if ( current - > block < Thread : : SelectBlocker > ( computed_timeout , select_has_timeout , rfds , wfds , efds ) = = Thread : : BlockResult : : InterruptedBySignal )
2019-07-21 14:31:20 +03:00
return - EINTR ;
}
2019-01-16 01:12:20 +03:00
2019-06-06 18:46:41 +03:00
int marked_fd_count = 0 ;
2019-06-07 12:43:58 +03:00
auto mark_fds = [ & ] ( auto * fds , auto & vector , auto should_mark ) {
2019-06-06 18:46:41 +03:00
if ( ! fds )
return ;
FD_ZERO ( fds ) ;
for ( int fd : vector ) {
2019-06-13 23:03:04 +03:00
if ( auto * description = file_description ( fd ) ; description & & should_mark ( * description ) ) {
2019-06-06 18:46:41 +03:00
FD_SET ( fd , fds ) ;
+ + marked_fd_count ;
2019-01-16 01:12:20 +03:00
}
}
2019-06-06 18:46:41 +03:00
} ;
2019-11-10 17:32:59 +03:00
mark_fds ( readfds , rfds , [ ] ( auto & description ) { return description . can_read ( ) ; } ) ;
mark_fds ( writefds , wfds , [ ] ( auto & description ) { return description . can_write ( ) ; } ) ;
// FIXME: We should also mark exceptfds as appropriate.
2019-07-20 12:05:52 +03:00
2019-06-06 18:46:41 +03:00
return marked_fd_count ;
2019-01-16 01:12:20 +03:00
}
2019-01-16 19:20:58 +03:00
2019-01-23 09:27:41 +03:00
int Process : : sys $ poll ( pollfd * fds , int nfds , int timeout )
{
if ( ! validate_read_typed ( fds ) )
return - EFAULT ;
2019-01-23 10:03:31 +03:00
2019-07-19 10:04:12 +03:00
Thread : : SelectBlocker : : FDVector rfds ;
Thread : : SelectBlocker : : FDVector wfds ;
2019-07-18 18:39:49 +03:00
2019-01-23 10:03:31 +03:00
for ( int i = 0 ; i < nfds ; + + i ) {
if ( fds [ i ] . events & POLLIN )
2019-07-18 18:39:49 +03:00
rfds . append ( fds [ i ] . fd ) ;
2019-01-23 10:03:31 +03:00
if ( fds [ i ] . events & POLLOUT )
2019-07-18 18:39:49 +03:00
wfds . append ( fds [ i ] . fd ) ;
2019-01-23 10:03:31 +03:00
}
2019-07-18 18:39:49 +03:00
timeval actual_timeout ;
bool has_timeout = false ;
2019-05-18 04:59:48 +03:00
if ( timeout > = 0 ) {
// poll is in ms, we want s/us.
struct timeval tvtimeout ;
tvtimeout . tv_sec = 0 ;
while ( timeout > = 1000 ) {
tvtimeout . tv_sec + = 1 ;
timeout - = 1000 ;
}
tvtimeout . tv_usec = timeout * 1000 ;
2019-07-18 18:39:49 +03:00
timeval_add ( kgettimeofday ( ) , tvtimeout , actual_timeout ) ;
has_timeout = true ;
2019-05-18 04:59:48 +03:00
}
2019-05-19 11:24:28 +03:00
# if defined(DEBUG_IO) || defined(DEBUG_POLL_SELECT)
2019-07-18 18:39:49 +03:00
dbgprintf ( " %s<%u> polling on (read:%u, write:%u), timeout=%d \n " , name ( ) . characters ( ) , pid ( ) , rfds . size ( ) , wfds . size ( ) , timeout ) ;
2019-05-19 11:24:28 +03:00
# endif
2019-08-02 16:18:47 +03:00
if ( has_timeout | | timeout < 0 ) {
2019-07-21 14:31:20 +03:00
if ( current - > block < Thread : : SelectBlocker > ( actual_timeout , has_timeout , rfds , wfds , Thread : : SelectBlocker : : FDVector ( ) ) = = Thread : : BlockResult : : InterruptedBySignal )
return - EINTR ;
}
2019-01-23 10:03:31 +03:00
int fds_with_revents = 0 ;
for ( int i = 0 ; i < nfds ; + + i ) {
2019-06-13 23:03:04 +03:00
auto * description = file_description ( fds [ i ] . fd ) ;
if ( ! description ) {
2019-01-23 10:03:31 +03:00
fds [ i ] . revents = POLLNVAL ;
continue ;
}
fds [ i ] . revents = 0 ;
2019-06-13 23:03:04 +03:00
if ( fds [ i ] . events & POLLIN & & description - > can_read ( ) )
2019-01-23 10:03:31 +03:00
fds [ i ] . revents | = POLLIN ;
2019-06-13 23:03:04 +03:00
if ( fds [ i ] . events & POLLOUT & & description - > can_write ( ) )
2019-01-23 10:03:31 +03:00
fds [ i ] . revents | = POLLOUT ;
if ( fds [ i ] . revents )
+ + fds_with_revents ;
}
return fds_with_revents ;
2019-01-23 09:27:41 +03:00
}
2019-05-30 21:23:50 +03:00
Custody & Process : : current_directory ( )
2019-01-16 19:20:58 +03:00
{
2019-05-30 21:23:50 +03:00
if ( ! m_cwd )
m_cwd = VFS : : the ( ) . root_custody ( ) ;
return * m_cwd ;
2019-01-16 19:20:58 +03:00
}
2019-01-22 09:03:44 +03:00
2019-02-21 15:26:40 +03:00
int Process : : sys $ link ( const char * old_path , const char * new_path )
{
if ( ! validate_read_str ( old_path ) )
return - EFAULT ;
if ( ! validate_read_str ( new_path ) )
return - EFAULT ;
2019-05-30 21:23:50 +03:00
return VFS : : the ( ) . link ( StringView ( old_path ) , StringView ( new_path ) , current_directory ( ) ) ;
2019-02-21 15:26:40 +03:00
}
2019-01-22 09:03:44 +03:00
int Process : : sys $ unlink ( const char * pathname )
{
if ( ! validate_read_str ( pathname ) )
return - EFAULT ;
2019-05-30 21:23:50 +03:00
return VFS : : the ( ) . unlink ( StringView ( pathname ) , current_directory ( ) ) ;
2019-01-22 09:03:44 +03:00
}
2019-01-25 04:09:29 +03:00
2019-03-02 03:50:34 +03:00
int Process : : sys $ symlink ( const char * target , const char * linkpath )
{
if ( ! validate_read_str ( target ) )
return - EFAULT ;
if ( ! validate_read_str ( linkpath ) )
return - EFAULT ;
2019-05-30 21:23:50 +03:00
return VFS : : the ( ) . symlink ( StringView ( target ) , StringView ( linkpath ) , current_directory ( ) ) ;
2019-03-02 03:50:34 +03:00
}
2019-01-28 06:16:01 +03:00
int Process : : sys $ rmdir ( const char * pathname )
{
if ( ! validate_read_str ( pathname ) )
return - EFAULT ;
2019-05-30 21:23:50 +03:00
return VFS : : the ( ) . rmdir ( StringView ( pathname ) , current_directory ( ) ) ;
2019-01-28 06:16:01 +03:00
}
2019-07-03 22:17:35 +03:00
int Process : : sys $ read_tsc ( u32 * lsw , u32 * msw )
2019-01-25 04:09:29 +03:00
{
if ( ! validate_write_typed ( lsw ) )
return - EFAULT ;
if ( ! validate_write_typed ( msw ) )
return - EFAULT ;
read_tsc ( * lsw , * msw ) ;
2020-01-01 20:18:02 +03:00
if ( ! is_superuser ( ) )
* lsw & = ~ 0xfff ;
2019-01-25 04:09:29 +03:00
return 0 ;
}
2019-01-29 06:55:08 +03:00
int Process : : sys $ chmod ( const char * pathname , mode_t mode )
{
if ( ! validate_read_str ( pathname ) )
return - EFAULT ;
2019-05-30 21:23:50 +03:00
return VFS : : the ( ) . chmod ( StringView ( pathname ) , mode , current_directory ( ) ) ;
2019-01-29 06:55:08 +03:00
}
2019-01-30 20:26:19 +03:00
2019-03-01 12:39:19 +03:00
int Process : : sys $ fchmod ( int fd , mode_t mode )
{
2019-06-13 23:03:04 +03:00
auto * description = file_description ( fd ) ;
if ( ! description )
2019-03-01 12:39:19 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
return description - > fchmod ( mode ) ;
2019-03-01 12:39:19 +03:00
}
2019-06-01 21:31:36 +03:00
int Process : : sys $ fchown ( int fd , uid_t uid , gid_t gid )
{
2019-06-13 23:03:04 +03:00
auto * description = file_description ( fd ) ;
if ( ! description )
2019-06-01 21:31:36 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
return description - > chown ( uid , gid ) ;
2019-06-01 21:31:36 +03:00
}
2019-02-27 14:32:53 +03:00
int Process : : sys $ chown ( const char * pathname , uid_t uid , gid_t gid )
{
if ( ! validate_read_str ( pathname ) )
return - EFAULT ;
2019-05-30 21:23:50 +03:00
return VFS : : the ( ) . chown ( StringView ( pathname ) , uid , gid , current_directory ( ) ) ;
2019-02-27 14:32:53 +03:00
}
2019-02-06 20:45:21 +03:00
void Process : : finalize ( )
2019-01-30 20:26:19 +03:00
{
2019-02-06 20:45:21 +03:00
ASSERT ( current = = g_finalizer ) ;
2019-03-24 00:03:17 +03:00
dbgprintf ( " Finalizing Process %s(%u) \n " , m_name . characters ( ) , m_pid ) ;
2019-02-06 19:28:14 +03:00
2019-01-30 20:26:19 +03:00
m_fds . clear ( ) ;
2019-02-04 12:21:15 +03:00
m_tty = nullptr ;
2019-05-31 08:19:54 +03:00
m_executable = nullptr ;
m_cwd = nullptr ;
2019-05-31 09:03:58 +03:00
m_elf_loader = nullptr ;
2019-02-16 14:13:43 +03:00
disown_all_shared_buffers ( ) ;
2019-02-06 20:45:21 +03:00
{
InterruptDisabler disabler ;
if ( auto * parent_process = Process : : from_pid ( m_ppid ) ) {
2019-03-24 00:03:17 +03:00
// FIXME(Thread): What should we do here? Should we look at all threads' signal actions?
2019-12-22 13:35:02 +03:00
if ( parent_process - > thread_count ( ) & & parent_process - > any_thread ( ) . m_signal_action_data [ SIGCHLD ] . flags & SA_NOCLDWAIT ) {
2019-03-01 17:47:07 +03:00
// NOTE: If the parent doesn't care about this process, let it go.
m_ppid = 0 ;
} else {
parent_process - > send_signal ( SIGCHLD , this ) ;
}
2019-02-06 20:45:21 +03:00
}
2019-02-04 15:30:03 +03:00
}
2019-03-24 03:20:35 +03:00
m_dead = true ;
2019-01-30 20:26:19 +03:00
}
2019-02-03 20:53:18 +03:00
2019-02-06 20:45:21 +03:00
void Process : : die ( )
{
2019-11-10 10:49:23 +03:00
// Let go of the TTY, otherwise a slave PTY may keep the master PTY from
// getting an EOF when the last process using the slave PTY dies.
// If the master PTY owner relies on an EOF to know when to wait() on a
// slave owner, we have to allow the PTY pair to be torn down.
m_tty = nullptr ;
2019-04-22 19:44:45 +03:00
if ( m_tracer )
m_tracer - > set_dead ( ) ;
2019-03-24 00:03:17 +03:00
{
Kernel: Unwind kernel stacks before dying
While executing in the kernel, a thread can acquire various resources
that need cleanup, such as locks and references to RefCounted objects.
This cleanup normally happens on the exit path, such as in destructors
for various RAII guards. But we weren't calling those exit paths when
killing threads that have been executing in the kernel, such as threads
blocked on reading or sleeping, thus causing leaks.
This commit changes how killing threads works. Now, instead of killing
a thread directly, one is supposed to call thread->set_should_die(),
which will unblock it and make it unwind the stack if it is blocked
in the kernel. Then, just before returning to the userspace, the thread
will automatically die.
2019-11-14 18:46:01 +03:00
// Tell the threads to unwind and die.
2019-03-24 00:03:17 +03:00
InterruptDisabler disabler ;
2019-06-07 12:43:58 +03:00
for_each_thread ( [ ] ( Thread & thread ) {
2019-12-22 13:35:02 +03:00
kprintf ( " Mark PID %u TID %u for death \n " , thread . pid ( ) , thread . tid ( ) ) ;
Kernel: Unwind kernel stacks before dying
While executing in the kernel, a thread can acquire various resources
that need cleanup, such as locks and references to RefCounted objects.
This cleanup normally happens on the exit path, such as in destructors
for various RAII guards. But we weren't calling those exit paths when
killing threads that have been executing in the kernel, such as threads
blocked on reading or sleeping, thus causing leaks.
This commit changes how killing threads works. Now, instead of killing
a thread directly, one is supposed to call thread->set_should_die(),
which will unblock it and make it unwind the stack if it is blocked
in the kernel. Then, just before returning to the userspace, the thread
will automatically die.
2019-11-14 18:46:01 +03:00
thread . set_should_die ( ) ;
2019-03-24 00:03:17 +03:00
return IterationDecision : : Continue ;
} ) ;
}
2019-02-06 20:45:21 +03:00
}
2019-12-29 14:28:32 +03:00
size_t Process : : amount_dirty_private ( ) const
{
2019-12-29 14:45:58 +03:00
// FIXME: This gets a bit more complicated for Regions sharing the same underlying VMObject.
// The main issue I'm thinking of is when the VMObject has physical pages that none of the Regions are mapping.
// That's probably a situation that needs to be looked at in general.
2019-12-29 14:28:32 +03:00
size_t amount = 0 ;
for ( auto & region : m_regions ) {
if ( ! region . is_shared ( ) )
amount + = region . amount_dirty ( ) ;
}
return amount ;
}
2019-12-29 14:45:58 +03:00
size_t Process : : amount_clean_inode ( ) const
{
HashTable < const InodeVMObject * > vmobjects ;
for ( auto & region : m_regions ) {
if ( region . vmobject ( ) . is_inode ( ) )
vmobjects . set ( & static_cast < const InodeVMObject & > ( region . vmobject ( ) ) ) ;
}
size_t amount = 0 ;
for ( auto & vmobject : vmobjects )
amount + = vmobject - > amount_clean ( ) ;
return amount ;
}
2019-02-03 20:53:18 +03:00
size_t Process : : amount_virtual ( ) const
{
size_t amount = 0 ;
for ( auto & region : m_regions ) {
2019-06-27 14:34:28 +03:00
amount + = region . size ( ) ;
2019-02-03 20:53:18 +03:00
}
return amount ;
}
size_t Process : : amount_resident ( ) const
{
// FIXME: This will double count if multiple regions use the same physical page.
size_t amount = 0 ;
for ( auto & region : m_regions ) {
2019-06-27 14:34:28 +03:00
amount + = region . amount_resident ( ) ;
2019-02-03 20:53:18 +03:00
}
return amount ;
}
size_t Process : : amount_shared ( ) const
{
// FIXME: This will double count if multiple regions use the same physical page.
2019-06-21 19:40:24 +03:00
// FIXME: It doesn't work at the moment, since it relies on PhysicalPage ref counts,
2019-06-21 16:29:31 +03:00
// and each PhysicalPage is only reffed by its VMObject. This needs to be refactored
2019-06-21 19:40:24 +03:00
// so that every Region contributes +1 ref to each of its PhysicalPages.
2019-02-03 20:53:18 +03:00
size_t amount = 0 ;
for ( auto & region : m_regions ) {
2019-06-27 14:34:28 +03:00
amount + = region . amount_shared ( ) ;
2019-02-03 20:53:18 +03:00
}
return amount ;
}
2019-02-06 20:45:21 +03:00
2019-12-09 21:12:38 +03:00
size_t Process : : amount_purgeable_volatile ( ) const
{
size_t amount = 0 ;
for ( auto & region : m_regions ) {
if ( region . vmobject ( ) . is_purgeable ( ) & & static_cast < const PurgeableVMObject & > ( region . vmobject ( ) ) . is_volatile ( ) )
amount + = region . amount_resident ( ) ;
}
return amount ;
}
size_t Process : : amount_purgeable_nonvolatile ( ) const
{
size_t amount = 0 ;
for ( auto & region : m_regions ) {
if ( region . vmobject ( ) . is_purgeable ( ) & & ! static_cast < const PurgeableVMObject & > ( region . vmobject ( ) ) . is_volatile ( ) )
amount + = region . amount_resident ( ) ;
}
return amount ;
}
2019-02-14 16:17:38 +03:00
int Process : : sys $ socket ( int domain , int type , int protocol )
{
2019-12-31 03:42:34 +03:00
if ( ( type & SOCK_TYPE_MASK ) = = SOCK_RAW & & ! is_superuser ( ) )
return - EACCES ;
2019-04-06 15:54:32 +03:00
int fd = alloc_fd ( ) ;
if ( fd < 0 )
return fd ;
2019-03-07 00:14:31 +03:00
auto result = Socket : : create ( domain , type , protocol ) ;
if ( result . is_error ( ) )
return result . error ( ) ;
2019-06-13 23:03:04 +03:00
auto description = FileDescription : : create ( * result . value ( ) ) ;
2019-02-14 17:17:30 +03:00
unsigned flags = 0 ;
if ( type & SOCK_CLOEXEC )
2019-02-17 12:41:37 +03:00
flags | = FD_CLOEXEC ;
2019-02-14 17:17:30 +03:00
if ( type & SOCK_NONBLOCK )
2019-06-13 23:03:04 +03:00
description - > set_blocking ( false ) ;
m_fds [ fd ] . set ( move ( description ) , flags ) ;
2019-02-14 16:17:38 +03:00
return fd ;
}
2019-02-14 16:38:30 +03:00
int Process : : sys $ bind ( int sockfd , const sockaddr * address , socklen_t address_length )
2019-02-14 16:17:38 +03:00
{
2019-02-14 16:38:30 +03:00
if ( ! validate_read ( address , address_length ) )
return - EFAULT ;
2019-06-13 23:03:04 +03:00
auto * description = file_description ( sockfd ) ;
if ( ! description )
2019-02-14 16:38:30 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
if ( ! description - > is_socket ( ) )
2019-02-14 16:38:30 +03:00
return - ENOTSOCK ;
2019-06-13 23:03:04 +03:00
auto & socket = * description - > socket ( ) ;
2019-03-07 00:14:31 +03:00
return socket . bind ( address , address_length ) ;
2019-02-14 16:17:38 +03:00
}
int Process : : sys $ listen ( int sockfd , int backlog )
{
2019-06-13 23:03:04 +03:00
auto * description = file_description ( sockfd ) ;
if ( ! description )
2019-02-14 17:17:30 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
if ( ! description - > is_socket ( ) )
2019-02-14 17:17:30 +03:00
return - ENOTSOCK ;
2019-06-13 23:03:04 +03:00
auto & socket = * description - > socket ( ) ;
2019-11-27 18:01:22 +03:00
if ( socket . is_connected ( ) )
return - EINVAL ;
2019-08-11 16:38:20 +03:00
return socket . listen ( backlog ) ;
2019-02-14 16:17:38 +03:00
}
2019-02-15 13:43:43 +03:00
int Process : : sys $ accept ( int accepting_socket_fd , sockaddr * address , socklen_t * address_size )
2019-02-14 16:17:38 +03:00
{
2019-02-14 17:17:30 +03:00
if ( ! validate_write_typed ( address_size ) )
return - EFAULT ;
if ( ! validate_write ( address , * address_size ) )
return - EFAULT ;
2019-04-06 15:54:32 +03:00
int accepted_socket_fd = alloc_fd ( ) ;
if ( accepted_socket_fd < 0 )
return accepted_socket_fd ;
2019-06-13 23:03:04 +03:00
auto * accepting_socket_description = file_description ( accepting_socket_fd ) ;
if ( ! accepting_socket_description )
2019-02-14 17:17:30 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
if ( ! accepting_socket_description - > is_socket ( ) )
2019-02-14 17:17:30 +03:00
return - ENOTSOCK ;
2019-06-13 23:03:04 +03:00
auto & socket = * accepting_socket_description - > socket ( ) ;
2019-02-14 17:17:30 +03:00
if ( ! socket . can_accept ( ) ) {
2019-07-18 11:39:00 +03:00
if ( accepting_socket_description - > is_blocking ( ) ) {
2019-07-20 12:05:52 +03:00
if ( current - > block < Thread : : AcceptBlocker > ( * accepting_socket_description ) = = Thread : : BlockResult : : InterruptedBySignal )
2019-07-18 11:39:00 +03:00
return - EINTR ;
} else {
return - EAGAIN ;
}
2019-02-14 17:17:30 +03:00
}
2019-02-15 13:43:43 +03:00
auto accepted_socket = socket . accept ( ) ;
ASSERT ( accepted_socket ) ;
2019-08-10 05:25:30 +03:00
bool success = accepted_socket - > get_peer_address ( address , address_size ) ;
2019-02-14 17:40:04 +03:00
ASSERT ( success ) ;
2019-08-11 16:38:20 +03:00
auto accepted_socket_description = FileDescription : : create ( * accepted_socket ) ;
2019-02-15 13:43:43 +03:00
// NOTE: The accepted socket inherits fd flags from the accepting socket.
// I'm not sure if this matches other systems but it makes sense to me.
2019-06-13 23:03:04 +03:00
accepted_socket_description - > set_blocking ( accepting_socket_description - > is_blocking ( ) ) ;
m_fds [ accepted_socket_fd ] . set ( move ( accepted_socket_description ) , m_fds [ accepting_socket_fd ] . flags ) ;
2019-10-08 22:41:46 +03:00
// NOTE: Moving this state to Completed is what causes connect() to unblock on the client side.
accepted_socket - > set_setup_state ( Socket : : SetupState : : Completed ) ;
2019-02-15 13:43:43 +03:00
return accepted_socket_fd ;
2019-02-14 16:17:38 +03:00
}
2019-02-14 17:55:19 +03:00
int Process : : sys $ connect ( int sockfd , const sockaddr * address , socklen_t address_size )
2019-02-14 16:17:38 +03:00
{
2019-02-14 17:55:19 +03:00
if ( ! validate_read ( address , address_size ) )
return - EFAULT ;
2019-04-06 15:54:32 +03:00
int fd = alloc_fd ( ) ;
if ( fd < 0 )
return fd ;
2019-06-13 23:03:04 +03:00
auto * description = file_description ( sockfd ) ;
if ( ! description )
2019-02-14 17:55:19 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
if ( ! description - > is_socket ( ) )
2019-02-14 17:55:19 +03:00
return - ENOTSOCK ;
2019-08-11 16:38:20 +03:00
2019-06-13 23:03:04 +03:00
auto & socket = * description - > socket ( ) ;
2019-08-11 16:38:20 +03:00
return socket . connect ( * description , address , address_size , description - > is_blocking ( ) ? ShouldBlock : : Yes : ShouldBlock : : No ) ;
2019-02-14 19:18:35 +03:00
}
2019-03-12 17:51:42 +03:00
ssize_t Process : : sys $ sendto ( const Syscall : : SC_sendto_params * params )
{
if ( ! validate_read_typed ( params ) )
return - EFAULT ;
2019-11-10 17:32:59 +03:00
auto & [ sockfd , data , data_length , flags , addr , addr_length ] = * params ;
2019-03-12 17:51:42 +03:00
if ( ! validate_read ( data , data_length ) )
return - EFAULT ;
2019-03-14 01:14:30 +03:00
if ( addr & & ! validate_read ( addr , addr_length ) )
2019-03-12 17:51:42 +03:00
return - EFAULT ;
2019-06-13 23:03:04 +03:00
auto * description = file_description ( sockfd ) ;
if ( ! description )
2019-03-12 17:51:42 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
if ( ! description - > is_socket ( ) )
2019-03-12 17:51:42 +03:00
return - ENOTSOCK ;
2019-06-13 23:03:04 +03:00
auto & socket = * description - > socket ( ) ;
return socket . sendto ( * description , data , data_length , flags , addr , addr_length ) ;
2019-03-12 17:51:42 +03:00
}
2019-03-12 19:27:07 +03:00
ssize_t Process : : sys $ recvfrom ( const Syscall : : SC_recvfrom_params * params )
{
if ( ! validate_read_typed ( params ) )
return - EFAULT ;
2019-11-10 17:32:59 +03:00
auto & [ sockfd , buffer , buffer_length , flags , addr , addr_length ] = * params ;
2019-03-12 19:27:07 +03:00
if ( ! validate_write ( buffer , buffer_length ) )
return - EFAULT ;
2019-03-14 01:14:30 +03:00
if ( addr_length ) {
2019-05-04 04:27:50 +03:00
if ( ! validate_write_typed ( addr_length ) )
2019-03-14 01:14:30 +03:00
return - EFAULT ;
2019-05-04 04:27:50 +03:00
if ( ! validate_write ( addr , * addr_length ) )
2019-03-14 01:14:30 +03:00
return - EFAULT ;
} else if ( addr ) {
2019-06-07 12:43:58 +03:00
return - EINVAL ;
2019-03-14 01:14:30 +03:00
}
2019-06-13 23:03:04 +03:00
auto * description = file_description ( sockfd ) ;
if ( ! description )
2019-03-12 19:27:07 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
if ( ! description - > is_socket ( ) )
2019-03-12 19:27:07 +03:00
return - ENOTSOCK ;
2019-06-13 23:03:04 +03:00
auto & socket = * description - > socket ( ) ;
2019-05-20 04:44:45 +03:00
2019-06-13 23:03:04 +03:00
bool original_blocking = description - > is_blocking ( ) ;
2019-05-20 04:44:45 +03:00
if ( flags & MSG_DONTWAIT )
2019-06-13 23:03:04 +03:00
description - > set_blocking ( false ) ;
2019-05-20 04:44:45 +03:00
2019-06-13 23:03:04 +03:00
auto nrecv = socket . recvfrom ( * description , buffer , buffer_length , flags , addr , addr_length ) ;
2019-05-20 04:44:45 +03:00
if ( flags & MSG_DONTWAIT )
2019-06-13 23:03:04 +03:00
description - > set_blocking ( original_blocking ) ;
2019-05-20 04:44:45 +03:00
return nrecv ;
2019-03-12 19:27:07 +03:00
}
2019-05-19 20:55:27 +03:00
int Process : : sys $ getsockname ( int sockfd , sockaddr * addr , socklen_t * addrlen )
{
if ( ! validate_read_typed ( addrlen ) )
return - EFAULT ;
if ( * addrlen < = 0 )
return - EINVAL ;
if ( ! validate_write ( addr , * addrlen ) )
return - EFAULT ;
2019-06-13 23:03:04 +03:00
auto * description = file_description ( sockfd ) ;
if ( ! description )
2019-05-19 20:55:27 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
if ( ! description - > is_socket ( ) )
2019-05-19 20:55:27 +03:00
return - ENOTSOCK ;
2019-06-13 23:03:04 +03:00
auto & socket = * description - > socket ( ) ;
2019-05-20 21:33:03 +03:00
if ( ! socket . get_local_address ( addr , addrlen ) )
return - EINVAL ; // FIXME: Should this be another error? I'm not sure.
return 0 ;
}
int Process : : sys $ getpeername ( int sockfd , sockaddr * addr , socklen_t * addrlen )
{
if ( ! validate_read_typed ( addrlen ) )
return - EFAULT ;
if ( * addrlen < = 0 )
return - EINVAL ;
if ( ! validate_write ( addr , * addrlen ) )
return - EFAULT ;
2019-06-13 23:03:04 +03:00
auto * description = file_description ( sockfd ) ;
if ( ! description )
2019-05-20 21:33:03 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
if ( ! description - > is_socket ( ) )
2019-05-20 21:33:03 +03:00
return - ENOTSOCK ;
2019-06-13 23:03:04 +03:00
auto & socket = * description - > socket ( ) ;
2019-05-20 21:33:03 +03:00
2019-08-10 06:17:00 +03:00
if ( socket . setup_state ( ) ! = Socket : : SetupState : : Completed )
2019-05-23 16:39:52 +03:00
return - ENOTCONN ;
2019-05-20 21:33:03 +03:00
if ( ! socket . get_peer_address ( addr , addrlen ) )
2019-05-19 20:55:27 +03:00
return - EINVAL ; // FIXME: Should this be another error? I'm not sure.
return 0 ;
}
2019-05-30 00:20:51 +03:00
int Process : : sys $ sched_setparam ( pid_t pid , const struct sched_param * param )
{
if ( ! validate_read_typed ( param ) )
return - EFAULT ;
InterruptDisabler disabler ;
auto * peer = this ;
if ( pid ! = 0 )
peer = Process : : from_pid ( pid ) ;
if ( ! peer )
return - ESRCH ;
if ( ! is_superuser ( ) & & m_euid ! = peer - > m_uid & & m_uid ! = peer - > m_uid )
return - EPERM ;
2019-12-30 20:46:17 +03:00
if ( param - > sched_priority < THREAD_PRIORITY_MIN | | param - > sched_priority > THREAD_PRIORITY_MAX )
2019-05-30 00:20:51 +03:00
return - EINVAL ;
2019-12-30 20:46:17 +03:00
peer - > any_thread ( ) . set_priority ( ( u32 ) param - > sched_priority ) ;
2019-05-30 00:20:51 +03:00
return 0 ;
}
int Process : : sys $ sched_getparam ( pid_t pid , struct sched_param * param )
{
if ( ! validate_read_typed ( param ) )
return - EFAULT ;
InterruptDisabler disabler ;
auto * peer = this ;
if ( pid ! = 0 )
peer = Process : : from_pid ( pid ) ;
if ( ! peer )
return - ESRCH ;
if ( ! is_superuser ( ) & & m_euid ! = peer - > m_uid & & m_uid ! = peer - > m_uid )
return - EPERM ;
2019-12-22 13:35:02 +03:00
param - > sched_priority = ( int ) peer - > any_thread ( ) . priority ( ) ;
2019-05-30 00:20:51 +03:00
return 0 ;
}
2019-03-13 15:13:23 +03:00
int Process : : sys $ getsockopt ( const Syscall : : SC_getsockopt_params * params )
{
if ( ! validate_read_typed ( params ) )
return - EFAULT ;
2019-11-10 17:32:59 +03:00
auto & [ sockfd , level , option , value , value_size ] = * params ;
2019-03-13 15:13:23 +03:00
if ( ! validate_write_typed ( value_size ) )
return - EFAULT ;
if ( ! validate_write ( value , * value_size ) )
return - EFAULT ;
2019-06-13 23:03:04 +03:00
auto * description = file_description ( sockfd ) ;
if ( ! description )
2019-03-13 15:13:23 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
if ( ! description - > is_socket ( ) )
2019-03-13 15:13:23 +03:00
return - ENOTSOCK ;
2019-06-13 23:03:04 +03:00
auto & socket = * description - > socket ( ) ;
2019-12-06 20:38:36 +03:00
return socket . getsockopt ( * description , level , option , value , value_size ) ;
2019-03-13 15:13:23 +03:00
}
int Process : : sys $ setsockopt ( const Syscall : : SC_setsockopt_params * params )
{
if ( ! validate_read_typed ( params ) )
return - EFAULT ;
2019-11-10 17:32:59 +03:00
auto & [ sockfd , level , option , value , value_size ] = * params ;
2019-03-13 15:13:23 +03:00
if ( ! validate_read ( value , value_size ) )
return - EFAULT ;
2019-06-13 23:03:04 +03:00
auto * description = file_description ( sockfd ) ;
if ( ! description )
2019-03-13 15:13:23 +03:00
return - EBADF ;
2019-06-13 23:03:04 +03:00
if ( ! description - > is_socket ( ) )
2019-03-13 15:13:23 +03:00
return - ENOTSOCK ;
2019-06-13 23:03:04 +03:00
auto & socket = * description - > socket ( ) ;
2019-03-13 15:13:23 +03:00
return socket . setsockopt ( level , option , value , value_size ) ;
}
2019-02-16 14:13:43 +03:00
void Process : : disown_all_shared_buffers ( )
{
LOCKER ( shared_buffers ( ) . lock ( ) ) ;
2019-04-20 15:02:19 +03:00
Vector < SharedBuffer * , 32 > buffers_to_disown ;
2019-02-20 23:31:52 +03:00
for ( auto & it : shared_buffers ( ) . resource ( ) )
buffers_to_disown . append ( it . value . ptr ( ) ) ;
for ( auto * shared_buffer : buffers_to_disown )
shared_buffer - > disown ( m_pid ) ;
2019-02-16 14:13:43 +03:00
}
2019-07-18 10:52:22 +03:00
int Process : : sys $ create_shared_buffer ( int size , void * * buffer )
2019-02-16 14:13:43 +03:00
{
2019-03-08 14:22:55 +03:00
if ( ! size | | size < 0 )
2019-02-20 23:31:52 +03:00
return - EINVAL ;
size = PAGE_ROUND_UP ( size ) ;
2019-02-16 14:13:43 +03:00
if ( ! validate_write_typed ( buffer ) )
return - EFAULT ;
2019-07-15 21:38:41 +03:00
2019-02-16 14:13:43 +03:00
LOCKER ( shared_buffers ( ) . lock ( ) ) ;
2019-07-16 16:03:39 +03:00
static int s_next_shared_buffer_id ;
2019-02-16 14:13:43 +03:00
int shared_buffer_id = + + s_next_shared_buffer_id ;
2019-07-15 21:38:41 +03:00
auto shared_buffer = make < SharedBuffer > ( shared_buffer_id , size ) ;
shared_buffer - > share_with ( m_pid ) ;
2019-07-19 18:46:21 +03:00
* buffer = shared_buffer - > ref_for_process_and_get_address ( * this ) ;
2019-06-22 17:22:34 +03:00
ASSERT ( ( int ) shared_buffer - > size ( ) > = size ) ;
2019-02-17 02:13:47 +03:00
# ifdef SHARED_BUFFER_DEBUG
2019-12-19 21:13:44 +03:00
kprintf ( " %s(%u): Created shared buffer %d @ %p (%u bytes, vmobject is %u) \n " , name ( ) . characters ( ) , pid ( ) , shared_buffer_id , * buffer , size , shared_buffer - > size ( ) ) ;
2019-02-17 02:13:47 +03:00
# endif
2019-02-16 14:13:43 +03:00
shared_buffers ( ) . resource ( ) . set ( shared_buffer_id , move ( shared_buffer ) ) ;
2019-07-15 21:38:41 +03:00
2019-02-16 14:13:43 +03:00
return shared_buffer_id ;
}
2019-07-18 10:52:22 +03:00
int Process : : sys $ share_buffer_with ( int shared_buffer_id , pid_t peer_pid )
{
if ( ! peer_pid | | peer_pid < 0 | | peer_pid = = m_pid )
return - EINVAL ;
LOCKER ( shared_buffers ( ) . lock ( ) ) ;
auto it = shared_buffers ( ) . resource ( ) . find ( shared_buffer_id ) ;
if ( it = = shared_buffers ( ) . resource ( ) . end ( ) )
return - EINVAL ;
auto & shared_buffer = * ( * it ) . value ;
2019-07-28 08:11:14 +03:00
if ( ! shared_buffer . is_shared_with ( m_pid ) )
return - EPERM ;
2019-07-18 10:52:22 +03:00
{
InterruptDisabler disabler ;
auto * peer = Process : : from_pid ( peer_pid ) ;
if ( ! peer )
return - ESRCH ;
}
shared_buffer . share_with ( peer_pid ) ;
return 0 ;
}
2019-07-29 08:26:01 +03:00
int Process : : sys $ share_buffer_globally ( int shared_buffer_id )
{
LOCKER ( shared_buffers ( ) . lock ( ) ) ;
auto it = shared_buffers ( ) . resource ( ) . find ( shared_buffer_id ) ;
if ( it = = shared_buffers ( ) . resource ( ) . end ( ) )
return - EINVAL ;
auto & shared_buffer = * ( * it ) . value ;
if ( ! shared_buffer . is_shared_with ( m_pid ) )
return - EPERM ;
shared_buffer . share_globally ( ) ;
return 0 ;
}
2019-02-16 14:13:43 +03:00
int Process : : sys $ release_shared_buffer ( int shared_buffer_id )
{
LOCKER ( shared_buffers ( ) . lock ( ) ) ;
auto it = shared_buffers ( ) . resource ( ) . find ( shared_buffer_id ) ;
if ( it = = shared_buffers ( ) . resource ( ) . end ( ) )
return - EINVAL ;
auto & shared_buffer = * ( * it ) . value ;
2019-07-28 08:11:14 +03:00
if ( ! shared_buffer . is_shared_with ( m_pid ) )
return - EPERM ;
2019-02-17 02:13:47 +03:00
# ifdef SHARED_BUFFER_DEBUG
2019-03-08 14:22:55 +03:00
kprintf ( " %s(%u): Releasing shared buffer %d, buffer count: %u \n " , name ( ) . characters ( ) , pid ( ) , shared_buffer_id , shared_buffers ( ) . resource ( ) . size ( ) ) ;
2019-02-17 02:13:47 +03:00
# endif
2019-07-19 18:46:21 +03:00
shared_buffer . deref_for_process ( * this ) ;
2019-02-16 14:13:43 +03:00
return 0 ;
}
void * Process : : sys $ get_shared_buffer ( int shared_buffer_id )
{
LOCKER ( shared_buffers ( ) . lock ( ) ) ;
auto it = shared_buffers ( ) . resource ( ) . find ( shared_buffer_id ) ;
if ( it = = shared_buffers ( ) . resource ( ) . end ( ) )
return ( void * ) - EINVAL ;
auto & shared_buffer = * ( * it ) . value ;
2019-07-15 21:38:41 +03:00
if ( ! shared_buffer . is_shared_with ( m_pid ) )
2019-07-28 08:11:14 +03:00
return ( void * ) - EPERM ;
2019-02-17 02:13:47 +03:00
# ifdef SHARED_BUFFER_DEBUG
2019-03-08 14:22:55 +03:00
kprintf ( " %s(%u): Retaining shared buffer %d, buffer count: %u \n " , name ( ) . characters ( ) , pid ( ) , shared_buffer_id , shared_buffers ( ) . resource ( ) . size ( ) ) ;
2019-02-17 02:13:47 +03:00
# endif
2019-07-19 18:46:21 +03:00
return shared_buffer . ref_for_process_and_get_address ( * this ) ;
2019-02-16 14:13:43 +03:00
}
2019-03-05 12:34:08 +03:00
2019-03-08 14:22:55 +03:00
int Process : : sys $ seal_shared_buffer ( int shared_buffer_id )
{
LOCKER ( shared_buffers ( ) . lock ( ) ) ;
auto it = shared_buffers ( ) . resource ( ) . find ( shared_buffer_id ) ;
if ( it = = shared_buffers ( ) . resource ( ) . end ( ) )
return - EINVAL ;
auto & shared_buffer = * ( * it ) . value ;
2019-07-15 21:38:41 +03:00
if ( ! shared_buffer . is_shared_with ( m_pid ) )
2019-07-28 08:11:14 +03:00
return - EPERM ;
2019-03-08 14:22:55 +03:00
# ifdef SHARED_BUFFER_DEBUG
kprintf ( " %s(%u): Sealing shared buffer %d \n " , name ( ) . characters ( ) , pid ( ) , shared_buffer_id ) ;
# endif
shared_buffer . seal ( ) ;
return 0 ;
}
int Process : : sys $ get_shared_buffer_size ( int shared_buffer_id )
{
LOCKER ( shared_buffers ( ) . lock ( ) ) ;
auto it = shared_buffers ( ) . resource ( ) . find ( shared_buffer_id ) ;
if ( it = = shared_buffers ( ) . resource ( ) . end ( ) )
return - EINVAL ;
auto & shared_buffer = * ( * it ) . value ;
2019-07-15 21:38:41 +03:00
if ( ! shared_buffer . is_shared_with ( m_pid ) )
2019-07-28 08:11:14 +03:00
return - EPERM ;
2019-03-08 14:22:55 +03:00
# ifdef SHARED_BUFFER_DEBUG
kprintf ( " %s(%u): Get shared buffer %d size: %u \n " , name ( ) . characters ( ) , pid ( ) , shared_buffer_id , shared_buffers ( ) . resource ( ) . size ( ) ) ;
# endif
return shared_buffer . size ( ) ;
}
2019-12-09 22:06:47 +03:00
int Process : : sys $ set_shared_buffer_volatile ( int shared_buffer_id , bool state )
{
LOCKER ( shared_buffers ( ) . lock ( ) ) ;
auto it = shared_buffers ( ) . resource ( ) . find ( shared_buffer_id ) ;
if ( it = = shared_buffers ( ) . resource ( ) . end ( ) )
return - EINVAL ;
auto & shared_buffer = * ( * it ) . value ;
if ( ! shared_buffer . is_shared_with ( m_pid ) )
return - EPERM ;
# ifdef SHARED_BUFFER_DEBUG
kprintf ( " %s(%u): Set shared buffer %d volatile: %u \n " , name ( ) . characters ( ) , pid ( ) , shared_buffer_id , state ) ;
# endif
if ( ! state ) {
bool was_purged = shared_buffer . vmobject ( ) . was_purged ( ) ;
shared_buffer . vmobject ( ) . set_volatile ( state ) ;
shared_buffer . vmobject ( ) . set_was_purged ( false ) ;
return was_purged ? 1 : 0 ;
}
shared_buffer . vmobject ( ) . set_volatile ( true ) ;
return 0 ;
}
2019-07-03 22:17:35 +03:00
void Process : : terminate_due_to_signal ( u8 signal )
2019-03-24 00:03:17 +03:00
{
ASSERT_INTERRUPTS_DISABLED ( ) ;
ASSERT ( signal < 32 ) ;
dbgprintf ( " terminate_due_to_signal %s(%u) <- %u \n " , name ( ) . characters ( ) , pid ( ) , signal ) ;
m_termination_status = 0 ;
m_termination_signal = signal ;
die ( ) ;
}
2019-07-03 22:17:35 +03:00
void Process : : send_signal ( u8 signal , Process * sender )
2019-03-24 00:03:17 +03:00
{
// FIXME(Thread): Find the appropriate thread to deliver the signal to.
2019-12-22 13:35:02 +03:00
any_thread ( ) . send_signal ( signal , sender ) ;
2019-03-24 00:03:17 +03:00
}
2019-03-24 00:59:08 +03:00
2019-11-18 06:08:10 +03:00
int Process : : sys $ create_thread ( void * ( * entry ) ( void * ) , void * argument , const Syscall : : SC_create_thread_params * params )
2019-03-24 00:59:08 +03:00
{
if ( ! validate_read ( ( const void * ) entry , sizeof ( void * ) ) )
return - EFAULT ;
2019-11-18 06:08:10 +03:00
if ( ! validate_read_typed ( params ) )
return - EFAULT ;
u32 user_stack_address = reinterpret_cast < u32 > ( params - > m_stack_location ) + params - > m_stack_size ;
if ( ! MM . validate_user_stack ( * this , VirtualAddress ( user_stack_address - 4 ) ) )
2019-11-17 19:28:17 +03:00
return - EFAULT ;
2019-11-18 06:08:10 +03:00
// FIXME: return EAGAIN if Thread::all_threads().size() is greater than PTHREAD_THREADS_MAX
2019-12-30 20:46:17 +03:00
int requested_thread_priority = params - > m_schedule_priority ;
if ( requested_thread_priority < THREAD_PRIORITY_MIN | | requested_thread_priority > THREAD_PRIORITY_MAX )
2019-11-18 06:08:10 +03:00
return - EINVAL ;
bool is_thread_joinable = ( 0 = = params - > m_detach_state ) ;
// FIXME: Do something with guard pages?
2019-03-24 00:59:08 +03:00
auto * thread = new Thread ( * this ) ;
2019-11-18 06:08:10 +03:00
2019-12-07 22:45:26 +03:00
// We know this thread is not the main_thread,
// So give it a unique name until the user calls $set_thread_name on it
// length + 4 to give space for our extra junk at the end
StringBuilder builder ( m_name . length ( ) + 4 ) ;
builder . append ( m_name ) ;
builder . appendf ( " [%d] " , thread - > tid ( ) ) ;
thread - > set_name ( builder . to_string ( ) ) ;
2019-11-18 06:08:10 +03:00
thread - > set_priority ( requested_thread_priority ) ;
thread - > set_joinable ( is_thread_joinable ) ;
2019-03-24 00:59:08 +03:00
auto & tss = thread - > tss ( ) ;
2019-07-03 22:17:35 +03:00
tss . eip = ( u32 ) entry ;
2019-03-24 00:59:08 +03:00
tss . eflags = 0x0202 ;
tss . cr3 = page_directory ( ) . cr3 ( ) ;
2019-11-18 06:08:10 +03:00
tss . esp = user_stack_address ;
2019-11-17 19:28:17 +03:00
// NOTE: The stack needs to be 16-byte aligned.
thread - > push_value_on_stack ( ( u32 ) argument ) ;
thread - > push_value_on_stack ( 0 ) ;
2019-09-07 16:50:44 +03:00
thread - > make_thread_specific_region ( { } ) ;
2019-03-24 00:59:08 +03:00
thread - > set_state ( Thread : : State : : Runnable ) ;
2019-07-14 11:17:58 +03:00
return thread - > tid ( ) ;
2019-03-24 00:59:08 +03:00
}
2019-03-25 15:03:49 +03:00
2019-11-14 22:58:23 +03:00
void Process : : sys $ exit_thread ( void * exit_value )
2019-04-29 16:17:20 +03:00
{
2019-04-29 16:56:25 +03:00
cli ( ) ;
2019-12-22 13:35:02 +03:00
current - > m_exit_value = exit_value ;
Kernel: Unwind kernel stacks before dying
While executing in the kernel, a thread can acquire various resources
that need cleanup, such as locks and references to RefCounted objects.
This cleanup normally happens on the exit path, such as in destructors
for various RAII guards. But we weren't calling those exit paths when
killing threads that have been executing in the kernel, such as threads
blocked on reading or sleeping, thus causing leaks.
This commit changes how killing threads works. Now, instead of killing
a thread directly, one is supposed to call thread->set_should_die(),
which will unblock it and make it unwind the stack if it is blocked
in the kernel. Then, just before returning to the userspace, the thread
will automatically die.
2019-11-14 18:46:01 +03:00
current - > set_should_die ( ) ;
2019-04-29 16:56:25 +03:00
big_lock ( ) . unlock_if_locked ( ) ;
Kernel: Unwind kernel stacks before dying
While executing in the kernel, a thread can acquire various resources
that need cleanup, such as locks and references to RefCounted objects.
This cleanup normally happens on the exit path, such as in destructors
for various RAII guards. But we weren't calling those exit paths when
killing threads that have been executing in the kernel, such as threads
blocked on reading or sleeping, thus causing leaks.
This commit changes how killing threads works. Now, instead of killing
a thread directly, one is supposed to call thread->set_should_die(),
which will unblock it and make it unwind the stack if it is blocked
in the kernel. Then, just before returning to the userspace, the thread
will automatically die.
2019-11-14 18:46:01 +03:00
current - > die_if_needed ( ) ;
2019-04-29 16:17:20 +03:00
ASSERT_NOT_REACHED ( ) ;
}
2019-12-07 16:47:00 +03:00
int Process : : sys $ detach_thread ( int tid )
{
Thread * thread = nullptr ;
for_each_thread ( [ & ] ( auto & child_thread ) {
if ( child_thread . tid ( ) = = tid ) {
thread = & child_thread ;
return IterationDecision : : Break ;
}
return IterationDecision : : Continue ;
} ) ;
if ( ! thread )
return - ESRCH ;
if ( ! thread - > is_joinable ( ) )
return - EINVAL ;
thread - > set_joinable ( false ) ;
return 0 ;
}
2019-11-14 22:58:23 +03:00
int Process : : sys $ join_thread ( int tid , void * * exit_value )
{
if ( exit_value & & ! validate_write_typed ( exit_value ) )
return - EFAULT ;
Thread * thread = nullptr ;
for_each_thread ( [ & ] ( auto & child_thread ) {
if ( child_thread . tid ( ) = = tid ) {
thread = & child_thread ;
return IterationDecision : : Break ;
}
return IterationDecision : : Continue ;
} ) ;
if ( ! thread )
return - ESRCH ;
if ( thread = = current )
return - EDEADLK ;
if ( thread - > m_joinee = = current )
return - EDEADLK ;
ASSERT ( thread - > m_joiner ! = current ) ;
if ( thread - > m_joiner )
return - EINVAL ;
2019-11-18 06:08:10 +03:00
if ( ! thread - > is_joinable ( ) )
return - EINVAL ;
2019-11-14 22:58:23 +03:00
2019-11-14 23:04:34 +03:00
void * joinee_exit_value = nullptr ;
2019-11-14 22:58:23 +03:00
// FIXME: pthread_join() should not be interruptable. Enforce this somehow?
2019-11-14 23:04:34 +03:00
auto result = current - > block < Thread : : JoinBlocker > ( * thread , joinee_exit_value ) ;
2019-11-14 22:58:23 +03:00
( void ) result ;
// NOTE: 'thread' is very possibly deleted at this point. Clear it just to be safe.
thread = nullptr ;
if ( exit_value )
2019-11-14 23:04:34 +03:00
* exit_value = joinee_exit_value ;
2019-11-14 22:58:23 +03:00
return 0 ;
}
2019-12-07 22:45:26 +03:00
int Process : : sys $ set_thread_name ( int tid , const char * buffer , int buffer_size )
{
2019-12-09 19:45:40 +03:00
if ( buffer_size < 0 )
return - EINVAL ;
2019-12-07 22:45:26 +03:00
if ( ! validate_read ( buffer , buffer_size ) )
return - EFAULT ;
const size_t max_thread_name_size = 64 ;
2019-12-09 19:45:40 +03:00
if ( strnlen ( buffer , ( size_t ) buffer_size ) > max_thread_name_size )
2019-12-07 22:45:26 +03:00
return - EINVAL ;
Thread * thread = nullptr ;
for_each_thread ( [ & ] ( auto & child_thread ) {
if ( child_thread . tid ( ) = = tid ) {
thread = & child_thread ;
return IterationDecision : : Break ;
}
return IterationDecision : : Continue ;
} ) ;
if ( ! thread )
return - ESRCH ;
2019-12-09 19:45:40 +03:00
thread - > set_name ( { buffer , ( size_t ) buffer_size } ) ;
2019-12-07 22:45:26 +03:00
return 0 ;
}
int Process : : sys $ get_thread_name ( int tid , char * buffer , int buffer_size )
{
if ( buffer_size < = 0 )
return - EINVAL ;
if ( ! validate_write ( buffer , buffer_size ) )
return - EFAULT ;
Thread * thread = nullptr ;
for_each_thread ( [ & ] ( auto & child_thread ) {
if ( child_thread . tid ( ) = = tid ) {
thread = & child_thread ;
return IterationDecision : : Break ;
}
return IterationDecision : : Continue ;
} ) ;
if ( ! thread )
return - ESRCH ;
2019-12-09 19:45:40 +03:00
if ( thread - > name ( ) . length ( ) > = ( size_t ) buffer_size )
2019-12-07 22:45:26 +03:00
return - ENAMETOOLONG ;
strncpy ( buffer , thread - > name ( ) . characters ( ) , buffer_size ) ;
return 0 ;
}
2019-03-25 15:03:49 +03:00
int Process : : sys $ gettid ( )
{
return current - > tid ( ) ;
}
int Process : : sys $ donate ( int tid )
{
if ( tid < 0 )
return - EINVAL ;
InterruptDisabler disabler ;
Thread * beneficiary = nullptr ;
2019-06-07 12:43:58 +03:00
for_each_thread ( [ & ] ( Thread & thread ) {
2019-03-25 15:03:49 +03:00
if ( thread . tid ( ) = = tid ) {
beneficiary = & thread ;
2019-06-07 18:13:23 +03:00
return IterationDecision : : Break ;
2019-03-25 15:03:49 +03:00
}
return IterationDecision : : Continue ;
} ) ;
if ( ! beneficiary )
return - ENOTHREAD ;
Scheduler : : donate_to ( beneficiary , " sys$donate " ) ;
return 0 ;
}
2019-04-08 00:35:26 +03:00
int Process : : sys $ rename ( const char * oldpath , const char * newpath )
{
if ( ! validate_read_str ( oldpath ) )
return - EFAULT ;
if ( ! validate_read_str ( newpath ) )
return - EFAULT ;
2019-05-30 21:23:50 +03:00
return VFS : : the ( ) . rename ( StringView ( oldpath ) , StringView ( newpath ) , current_directory ( ) ) ;
2019-04-08 00:35:26 +03:00
}
2019-04-09 00:44:12 +03:00
int Process : : sys $ shm_open ( const char * name , int flags , mode_t mode )
{
if ( ! validate_read_str ( name ) )
return - EFAULT ;
2019-04-09 03:37:05 +03:00
int fd = alloc_fd ( ) ;
if ( fd < 0 )
return fd ;
auto shm_or_error = SharedMemory : : open ( String ( name ) , flags , mode ) ;
if ( shm_or_error . is_error ( ) )
return shm_or_error . error ( ) ;
2019-08-11 10:32:21 +03:00
auto description = FileDescription : : create ( shm_or_error . value ( ) ) ;
2019-06-13 23:03:04 +03:00
m_fds [ fd ] . set ( move ( description ) , FD_CLOEXEC ) ;
2019-04-09 03:37:05 +03:00
return fd ;
2019-04-09 00:44:12 +03:00
}
int Process : : sys $ shm_unlink ( const char * name )
{
if ( ! validate_read_str ( name ) )
return - EFAULT ;
2019-04-09 03:37:05 +03:00
return SharedMemory : : unlink ( String ( name ) ) ;
2019-04-09 00:44:12 +03:00
}
2019-04-09 02:10:00 +03:00
int Process : : sys $ ftruncate ( int fd , off_t length )
{
2019-06-13 23:03:04 +03:00
auto * description = file_description ( fd ) ;
if ( ! description )
2019-04-09 02:10:00 +03:00
return - EBADF ;
// FIXME: Check that fd is writable, otherwise EINVAL.
2019-06-13 23:03:04 +03:00
return description - > truncate ( length ) ;
2019-04-09 02:10:00 +03:00
}
2019-04-22 19:44:45 +03:00
2019-07-22 21:01:11 +03:00
int Process : : sys $ watch_file ( const char * path , int path_length )
{
2019-12-09 19:45:40 +03:00
if ( path_length < 0 )
return - EINVAL ;
2019-07-22 21:01:11 +03:00
if ( ! validate_read ( path , path_length ) )
return - EFAULT ;
2019-12-09 19:45:40 +03:00
auto custody_or_error = VFS : : the ( ) . resolve_path ( { path , ( size_t ) path_length } , current_directory ( ) ) ;
2019-07-22 21:01:11 +03:00
if ( custody_or_error . is_error ( ) )
return custody_or_error . error ( ) ;
auto & custody = custody_or_error . value ( ) ;
auto & inode = custody - > inode ( ) ;
2019-12-15 21:33:39 +03:00
if ( ! inode . fs ( ) . supports_watchers ( ) )
return - ENOTSUP ;
2019-07-22 21:01:11 +03:00
int fd = alloc_fd ( ) ;
if ( fd < 0 )
return fd ;
m_fds [ fd ] . set ( FileDescription : : create ( * InodeWatcher : : create ( inode ) ) ) ;
return fd ;
}
2019-04-22 19:44:45 +03:00
int Process : : sys $ systrace ( pid_t pid )
{
InterruptDisabler disabler ;
auto * peer = Process : : from_pid ( pid ) ;
if ( ! peer )
return - ESRCH ;
if ( peer - > uid ( ) ! = m_euid )
return - EACCES ;
int fd = alloc_fd ( ) ;
if ( fd < 0 )
return fd ;
2019-06-13 23:03:04 +03:00
auto description = FileDescription : : create ( peer - > ensure_tracer ( ) ) ;
m_fds [ fd ] . set ( move ( description ) , 0 ) ;
2019-04-22 19:44:45 +03:00
return fd ;
}
2019-07-19 14:08:26 +03:00
int Process : : sys $ halt ( )
{
if ( ! is_superuser ( ) )
return - EPERM ;
dbgprintf ( " acquiring FS locks... \n " ) ;
FS : : lock_all ( ) ;
dbgprintf ( " syncing mounted filesystems... \n " ) ;
FS : : sync ( ) ;
dbgprintf ( " attempting system shutdown... \n " ) ;
IO : : out16 ( 0x604 , 0x2000 ) ;
return ESUCCESS ;
}
2019-07-19 10:58:12 +03:00
int Process : : sys $ reboot ( )
{
if ( ! is_superuser ( ) )
return - EPERM ;
dbgprintf ( " acquiring FS locks... \n " ) ;
FS : : lock_all ( ) ;
dbgprintf ( " syncing mounted filesystems... \n " ) ;
FS : : sync ( ) ;
dbgprintf ( " attempting reboot via KB Controller... \n " ) ;
IO : : out8 ( 0x64 , 0xFE ) ;
return ESUCCESS ;
}
2019-08-15 19:13:56 +03:00
int Process : : sys $ mount ( const char * device_path , const char * mountpoint , const char * fstype )
2019-08-02 16:18:47 +03:00
{
2019-08-02 20:03:50 +03:00
if ( ! is_superuser ( ) )
return - EPERM ;
2019-08-15 19:13:56 +03:00
if ( ! validate_read_str ( device_path ) | | ! validate_read_str ( mountpoint ) | | ! validate_read_str ( fstype ) )
2019-08-02 16:18:47 +03:00
return - EFAULT ;
2019-08-15 19:13:56 +03:00
dbg ( ) < < " mount " < < fstype < < " : device " < < device_path < < " @ " < < mountpoint ;
2019-08-02 20:03:50 +03:00
auto custody_or_error = VFS : : the ( ) . resolve_path ( mountpoint , current_directory ( ) ) ;
if ( custody_or_error . is_error ( ) )
return custody_or_error . error ( ) ;
auto & mountpoint_custody = custody_or_error . value ( ) ;
2019-08-15 19:13:56 +03:00
RefPtr < FS > fs { nullptr } ;
2019-08-02 16:18:47 +03:00
2019-08-15 19:13:56 +03:00
if ( strcmp ( fstype , " ext2 " ) = = 0 | | strcmp ( fstype , " Ext2FS " ) = = 0 ) {
auto metadata_or_error = VFS : : the ( ) . lookup_metadata ( device_path , current_directory ( ) ) ;
if ( metadata_or_error . is_error ( ) )
return metadata_or_error . error ( ) ;
2019-08-02 16:18:47 +03:00
2019-08-15 19:13:56 +03:00
auto major = metadata_or_error . value ( ) . major_device ;
auto minor = metadata_or_error . value ( ) . minor_device ;
2019-08-02 16:18:47 +03:00
2019-08-18 14:48:15 +03:00
auto * device = Device : : get_device ( major , minor ) ;
2019-08-15 19:13:56 +03:00
if ( ! device ) {
dbg ( ) < < " mount: device ( " < < major < < " , " < < minor < < " ) not found " ;
return - ENODEV ;
}
if ( ! device - > is_disk_device ( ) ) {
dbg ( ) < < " mount: device ( " < < major < < " , " < < minor < < " ) is not a DiskDevice " ;
return - ENODEV ;
}
2019-08-02 20:30:30 +03:00
2019-08-15 19:13:56 +03:00
auto & disk_device = static_cast < DiskDevice & > ( * device ) ;
2019-08-02 20:30:30 +03:00
2019-08-15 19:13:56 +03:00
dbg ( ) < < " mount: attempting to mount device ( " < < major < < " , " < < minor < < " ) on " < < mountpoint ;
fs = Ext2FS : : create ( disk_device ) ;
} else if ( strcmp ( fstype , " proc " ) = = 0 | | strcmp ( fstype , " ProcFS " ) = = 0 )
fs = ProcFS : : create ( ) ;
else if ( strcmp ( fstype , " devpts " ) = = 0 | | strcmp ( fstype , " DevPtsFS " ) = = 0 )
fs = DevPtsFS : : create ( ) ;
else if ( strcmp ( fstype , " tmp " ) = = 0 | | strcmp ( fstype , " TmpFS " ) = = 0 )
fs = TmpFS : : create ( ) ;
else
return - ENODEV ;
2019-08-02 16:18:47 +03:00
2019-08-15 19:13:56 +03:00
if ( ! fs - > initialize ( ) ) {
dbg ( ) < < " mount: failed to initialize " < < fstype < < " filesystem on " < < device_path ;
2019-08-02 20:30:30 +03:00
return - ENODEV ;
2019-08-02 16:18:47 +03:00
}
2019-08-15 19:13:56 +03:00
auto result = VFS : : the ( ) . mount ( fs . release_nonnull ( ) , mountpoint_custody ) ;
2019-08-02 20:23:23 +03:00
dbg ( ) < < " mount: successfully mounted " < < device_path < < " on " < < mountpoint ;
2019-08-02 20:03:50 +03:00
return result ;
2019-08-02 16:18:47 +03:00
}
2019-08-11 16:56:39 +03:00
int Process : : sys $ umount ( const char * mountpoint )
{
if ( ! is_superuser ( ) )
return - EPERM ;
if ( ! validate_read_str ( mountpoint ) )
return - EFAULT ;
auto metadata_or_error = VFS : : the ( ) . lookup_metadata ( mountpoint , current_directory ( ) ) ;
if ( metadata_or_error . is_error ( ) )
return metadata_or_error . error ( ) ;
2019-08-17 15:24:50 +03:00
auto guest_inode_id = metadata_or_error . value ( ) . inode ;
return VFS : : the ( ) . unmount ( guest_inode_id ) ;
2019-08-11 16:56:39 +03:00
}
2019-04-22 19:44:45 +03:00
ProcessTracer & Process : : ensure_tracer ( )
{
if ( ! m_tracer )
m_tracer = ProcessTracer : : create ( m_pid ) ;
return * m_tracer ;
}
2019-04-29 05:55:54 +03:00
2019-06-07 10:36:51 +03:00
void Process : : FileDescriptionAndFlags : : clear ( )
2019-04-29 05:55:54 +03:00
{
2019-06-13 23:03:04 +03:00
description = nullptr ;
2019-04-29 05:55:54 +03:00
flags = 0 ;
}
2019-07-03 22:17:35 +03:00
void Process : : FileDescriptionAndFlags : : set ( NonnullRefPtr < FileDescription > & & d , u32 f )
2019-04-29 05:55:54 +03:00
{
2019-06-13 23:03:04 +03:00
description = move ( d ) ;
2019-04-29 05:55:54 +03:00
flags = f ;
}
2019-05-03 23:59:58 +03:00
int Process : : sys $ mknod ( const char * pathname , mode_t mode , dev_t dev )
{
if ( ! validate_read_str ( pathname ) )
return - EFAULT ;
2019-05-30 21:23:50 +03:00
return VFS : : the ( ) . mknod ( StringView ( pathname ) , mode , dev , current_directory ( ) ) ;
2019-05-03 23:59:58 +03:00
}
2019-07-21 10:59:17 +03:00
int Process : : sys $ dump_backtrace ( )
{
dump_backtrace ( ) ;
return 0 ;
}
2019-07-21 20:45:31 +03:00
int Process : : sys $ dbgputch ( u8 ch )
{
IO : : out8 ( 0xe9 , ch ) ;
return 0 ;
}
2019-07-21 22:43:37 +03:00
int Process : : sys $ dbgputstr ( const u8 * characters , int length )
{
2019-08-09 20:44:14 +03:00
if ( ! length )
return 0 ;
2019-07-21 22:43:37 +03:00
if ( ! validate_read ( characters , length ) )
return - EFAULT ;
for ( int i = 0 ; i < length ; + + i )
IO : : out8 ( 0xe9 , characters [ i ] ) ;
return 0 ;
}
2019-07-25 22:02:19 +03:00
2019-08-07 22:52:43 +03:00
KBuffer Process : : backtrace ( ProcessInspectionHandle & handle ) const
2019-07-25 22:02:19 +03:00
{
2019-08-07 22:52:43 +03:00
KBufferBuilder builder ;
2019-07-25 22:02:19 +03:00
for_each_thread ( [ & ] ( Thread & thread ) {
2019-12-07 22:45:26 +03:00
builder . appendf ( " Thread %d (%s): \n " , thread . tid ( ) , thread . name ( ) . characters ( ) ) ;
2019-07-25 22:02:19 +03:00
builder . append ( thread . backtrace ( handle ) ) ;
return IterationDecision : : Continue ;
} ) ;
2019-08-07 22:52:43 +03:00
return builder . build ( ) ;
2019-07-25 22:02:19 +03:00
}
2019-07-29 08:26:01 +03:00
int Process : : sys $ set_process_icon ( int icon_id )
{
LOCKER ( shared_buffers ( ) . lock ( ) ) ;
auto it = shared_buffers ( ) . resource ( ) . find ( icon_id ) ;
if ( it = = shared_buffers ( ) . resource ( ) . end ( ) )
return - EINVAL ;
auto & shared_buffer = * ( * it ) . value ;
if ( ! shared_buffer . is_shared_with ( m_pid ) )
return - EPERM ;
m_icon_id = icon_id ;
return 0 ;
}
2019-08-15 21:55:10 +03:00
int Process : : sys $ get_process_name ( char * buffer , int buffer_size )
{
if ( buffer_size < = 0 )
return - EINVAL ;
if ( ! validate_write ( buffer , buffer_size ) )
return - EFAULT ;
2019-12-09 19:45:40 +03:00
if ( m_name . length ( ) > = ( size_t ) buffer_size )
2019-08-15 21:55:10 +03:00
return - ENAMETOOLONG ;
2019-12-09 19:45:40 +03:00
strncpy ( buffer , m_name . characters ( ) , ( size_t ) buffer_size ) ;
2019-08-15 21:55:10 +03:00
return 0 ;
}
2019-10-13 17:41:55 +03:00
// We don't use the flag yet, but we could use it for distinguishing
// random source like Linux, unlike the OpenBSD equivalent. However, if we
// do, we should be able of the caveats that Linux has dealt with.
int Process : : sys $ getrandom ( void * buffer , size_t buffer_size , unsigned int flags __attribute__ ( ( unused ) ) )
{
if ( buffer_size < = 0 )
return - EINVAL ;
if ( ! validate_write ( buffer , buffer_size ) )
return - EFAULT ;
2019-10-13 18:55:58 +03:00
// We prefer to get whole words of entropy.
// If the length is unaligned, we can work with bytes instead.
// Mask out the bottom two bits for words.
size_t words_len = buffer_size & ~ 3 ;
if ( words_len ) {
uint32_t * words = ( uint32_t * ) buffer ;
for ( size_t i = 0 ; i < words_len / 4 ; i + + )
words [ i ] = RandomDevice : : random_value ( ) ;
}
// The remaining non-whole word bytes we can fill in.
size_t bytes_len = buffer_size & 3 ;
if ( bytes_len ) {
uint8_t * bytes = ( uint8_t * ) buffer + words_len ;
// Get a whole word of entropy to use.
uint32_t word = RandomDevice : : random_value ( ) ;
for ( size_t i = 0 ; i < bytes_len ; i + + )
bytes [ i ] = ( ( uint8_t * ) & word ) [ i ] ;
}
2019-10-13 17:41:55 +03:00
return 0 ;
}
2019-11-02 21:34:06 +03:00
2019-12-31 15:37:38 +03:00
int Process : : sys $ setkeymap ( const Syscall : : SC_setkeymap_params * params )
2019-11-23 10:48:07 +03:00
{
if ( ! is_superuser ( ) )
return - EPERM ;
2019-12-31 15:37:38 +03:00
if ( ! validate_read_typed ( params ) )
return - EFAULT ;
if ( ! validate_read ( params - > map , 0x80 ) )
return - EFAULT ;
if ( ! validate_read ( params - > shift_map , 0x80 ) )
2019-11-23 10:48:07 +03:00
return - EFAULT ;
2019-12-31 15:37:38 +03:00
if ( ! validate_read ( params - > alt_map , 0x80 ) )
2019-11-23 10:48:07 +03:00
return - EFAULT ;
2019-12-31 15:37:38 +03:00
if ( ! validate_read ( params - > altgr_map , 0x80 ) )
2019-11-23 10:48:07 +03:00
return - EFAULT ;
2019-12-31 15:37:38 +03:00
KeyboardDevice : : the ( ) . set_maps ( params - > map , params - > shift_map , params - > alt_map , params - > altgr_map ) ;
2019-11-23 10:48:07 +03:00
return 0 ;
}
2019-11-02 21:34:06 +03:00
int Process : : sys $ clock_gettime ( clockid_t clock_id , timespec * ts )
{
if ( ! validate_write_typed ( ts ) )
return - EFAULT ;
switch ( clock_id ) {
case CLOCK_MONOTONIC :
ts - > tv_sec = g_uptime / TICKS_PER_SECOND ;
ts - > tv_nsec = ( g_uptime % TICKS_PER_SECOND ) * 1000000 ;
break ;
default :
return - EINVAL ;
}
return 0 ;
}
int Process : : sys $ clock_nanosleep ( const Syscall : : SC_clock_nanosleep_params * params )
{
if ( ! validate_read_typed ( params ) )
return - EFAULT ;
2019-11-10 17:32:59 +03:00
auto & [ clock_id , flags , requested_sleep , remaining_sleep ] = * params ;
if ( requested_sleep & & ! validate_read_typed ( requested_sleep ) )
2019-11-02 21:34:06 +03:00
return - EFAULT ;
2019-11-10 17:32:59 +03:00
if ( remaining_sleep & & ! validate_write_typed ( remaining_sleep ) )
2019-11-02 21:34:06 +03:00
return - EFAULT ;
bool is_absolute = flags & TIMER_ABSTIME ;
switch ( clock_id ) {
case CLOCK_MONOTONIC : {
u64 wakeup_time ;
if ( is_absolute ) {
u64 time_to_wake = ( requested_sleep - > tv_sec * 1000 + requested_sleep - > tv_nsec / 1000000 ) ;
wakeup_time = current - > sleep_until ( time_to_wake ) ;
} else {
u32 ticks_to_sleep = ( requested_sleep - > tv_sec * 1000 + requested_sleep - > tv_nsec / 1000000 ) ;
if ( ! ticks_to_sleep )
return 0 ;
wakeup_time = current - > sleep ( ticks_to_sleep ) ;
}
if ( wakeup_time > g_uptime ) {
u32 ticks_left = wakeup_time - g_uptime ;
if ( ! is_absolute & & remaining_sleep ) {
remaining_sleep - > tv_sec = ticks_left / TICKS_PER_SECOND ;
ticks_left - = remaining_sleep - > tv_sec * TICKS_PER_SECOND ;
remaining_sleep - > tv_nsec = ticks_left * 1000000 ;
}
return - EINTR ;
}
return 0 ;
}
default :
return - EINVAL ;
}
}
2019-11-10 00:18:16 +03:00
int Process : : sys $ sync ( )
{
VFS : : the ( ) . sync ( ) ;
return 0 ;
}
int Process : : sys $ putch ( char ch )
{
Console : : the ( ) . put_char ( ch ) ;
return 0 ;
}
int Process : : sys $ yield ( )
{
2019-11-16 14:18:59 +03:00
current - > yield_without_holding_big_lock ( ) ;
return 0 ;
2019-11-10 00:18:16 +03:00
}
int Process : : sys $ beep ( )
{
2019-12-27 00:31:26 +03:00
PCSpeaker : : tone_on ( 440 ) ;
u64 wakeup_time = current - > sleep ( 100 ) ;
PCSpeaker : : tone_off ( ) ;
if ( wakeup_time > g_uptime )
return - EINTR ;
2019-11-10 00:18:16 +03:00
return 0 ;
}
2019-11-28 22:59:11 +03:00
int Process : : sys $ module_load ( const char * path , size_t path_length )
{
if ( ! is_superuser ( ) )
return - EPERM ;
if ( ! validate_read ( path , path_length ) )
return - EFAULT ;
auto description_or_error = VFS : : the ( ) . open ( path , 0 , 0 , current_directory ( ) ) ;
if ( description_or_error . is_error ( ) )
return description_or_error . error ( ) ;
auto & description = description_or_error . value ( ) ;
auto payload = description - > read_entire_file ( ) ;
auto storage = KBuffer : : create_with_size ( payload . size ( ) ) ;
memcpy ( storage . data ( ) , payload . data ( ) , payload . size ( ) ) ;
payload . clear ( ) ;
// FIXME: ELFImage should really be taking a size argument as well...
auto elf_image = make < ELFImage > ( storage . data ( ) ) ;
if ( ! elf_image - > parse ( ) )
return - ENOEXEC ;
HashMap < String , u8 * > section_storage_by_name ;
auto module = make < Module > ( ) ;
elf_image - > for_each_section_of_type ( SHT_PROGBITS , [ & ] ( const ELFImage : : Section & section ) {
auto section_storage = KBuffer : : copy ( section . raw_data ( ) , section . size ( ) ) ;
section_storage_by_name . set ( section . name ( ) , section_storage . data ( ) ) ;
module - > sections . append ( move ( section_storage ) ) ;
return IterationDecision : : Continue ;
} ) ;
2019-12-24 06:09:18 +03:00
bool missing_symbols = false ;
2019-11-28 22:59:11 +03:00
elf_image - > for_each_section_of_type ( SHT_PROGBITS , [ & ] ( const ELFImage : : Section & section ) {
auto * section_storage = section_storage_by_name . get ( section . name ( ) ) . value_or ( nullptr ) ;
ASSERT ( section_storage ) ;
section . relocations ( ) . for_each_relocation ( [ & ] ( const ELFImage : : Relocation & relocation ) {
auto & patch_ptr = * reinterpret_cast < ptrdiff_t * > ( section_storage + relocation . offset ( ) ) ;
switch ( relocation . type ( ) ) {
case R_386_PC32 : {
// PC-relative relocation
dbg ( ) < < " PC-relative relocation: " < < relocation . symbol ( ) . name ( ) ;
2019-11-28 23:30:20 +03:00
u32 symbol_address = address_for_kernel_symbol ( relocation . symbol ( ) . name ( ) ) ;
2019-12-24 06:09:18 +03:00
if ( symbol_address = = 0 )
missing_symbols = true ;
2019-11-28 22:59:11 +03:00
dbg ( ) < < " Symbol address: " < < ( void * ) symbol_address ;
ptrdiff_t relative_offset = ( char * ) symbol_address - ( ( char * ) & patch_ptr + 4 ) ;
patch_ptr = relative_offset ;
break ;
}
case R_386_32 : // Absolute relocation
dbg ( ) < < " Absolute relocation: ' " < < relocation . symbol ( ) . name ( ) < < " ' value: " < < relocation . symbol ( ) . value ( ) < < " , index: " < < relocation . symbol_index ( ) ;
2019-11-28 23:30:20 +03:00
if ( relocation . symbol ( ) . bind ( ) = = STB_LOCAL ) {
auto * section_storage_containing_symbol = section_storage_by_name . get ( relocation . symbol ( ) . section ( ) . name ( ) ) . value_or ( nullptr ) ;
ASSERT ( section_storage_containing_symbol ) ;
2019-12-24 06:09:18 +03:00
u32 symbol_address = ( ptrdiff_t ) ( section_storage_containing_symbol + relocation . symbol ( ) . value ( ) ) ;
if ( symbol_address = = 0 )
missing_symbols = true ;
dbg ( ) < < " Symbol address: " < < ( void * ) symbol_address ;
patch_ptr + = symbol_address ;
2019-11-28 23:30:20 +03:00
} else if ( relocation . symbol ( ) . bind ( ) = = STB_GLOBAL ) {
2019-12-24 06:09:18 +03:00
u32 symbol_address = address_for_kernel_symbol ( relocation . symbol ( ) . name ( ) ) ;
if ( symbol_address = = 0 )
missing_symbols = true ;
dbg ( ) < < " Symbol address: " < < ( void * ) symbol_address ;
patch_ptr + = symbol_address ;
2019-11-28 23:30:20 +03:00
} else {
ASSERT_NOT_REACHED ( ) ;
}
2019-11-28 22:59:11 +03:00
break ;
}
return IterationDecision : : Continue ;
} ) ;
return IterationDecision : : Continue ;
} ) ;
2019-12-24 06:09:18 +03:00
if ( missing_symbols )
return - ENOENT ;
2019-11-28 22:59:11 +03:00
auto * text_base = section_storage_by_name . get ( " .text " ) . value_or ( nullptr ) ;
if ( ! text_base ) {
dbg ( ) < < " No .text section found in module! " ;
return - EINVAL ;
}
elf_image - > for_each_symbol ( [ & ] ( const ELFImage : : Symbol & symbol ) {
dbg ( ) < < " - " < < symbol . type ( ) < < " ' " < < symbol . name ( ) < < " ' @ " < < ( void * ) symbol . value ( ) < < " , size= " < < symbol . size ( ) ;
if ( ! strcmp ( symbol . name ( ) , " module_init " ) ) {
2019-11-28 23:07:22 +03:00
module - > module_init = ( ModuleInitPtr ) ( text_base + symbol . value ( ) ) ;
} else if ( ! strcmp ( symbol . name ( ) , " module_fini " ) ) {
module - > module_fini = ( ModuleFiniPtr ) ( text_base + symbol . value ( ) ) ;
2019-11-29 23:31:17 +03:00
} else if ( ! strcmp ( symbol . name ( ) , " module_name " ) ) {
const u8 * storage = section_storage_by_name . get ( symbol . section ( ) . name ( ) ) . value_or ( nullptr ) ;
if ( storage )
module - > name = String ( ( const char * ) ( storage + symbol . value ( ) ) ) ;
2019-11-28 22:59:11 +03:00
}
return IterationDecision : : Continue ;
} ) ;
2019-11-28 23:07:22 +03:00
if ( ! module - > module_init )
2019-11-28 22:59:11 +03:00
return - EINVAL ;
2019-12-24 03:40:26 +03:00
if ( g_modules - > contains ( module - > name ) ) {
dbg ( ) < < " a module with the name " < < module - > name < < " is already loaded; please unload it first " ;
return - EEXIST ;
}
2019-11-28 23:07:22 +03:00
module - > module_init ( ) ;
2019-11-28 22:59:11 +03:00
auto name = module - > name ;
g_modules - > set ( name , move ( module ) ) ;
return 0 ;
}
int Process : : sys $ module_unload ( const char * name , size_t name_length )
{
if ( ! is_superuser ( ) )
return - EPERM ;
if ( ! validate_read ( name , name_length ) )
return - EFAULT ;
2019-11-28 23:07:22 +03:00
auto it = g_modules - > find ( name ) ;
if ( it = = g_modules - > end ( ) )
return - ENOENT ;
if ( it - > value - > module_fini )
it - > value - > module_fini ( ) ;
g_modules - > remove ( it ) ;
2019-11-28 22:59:11 +03:00
return 0 ;
}
2019-12-11 22:36:56 +03:00
int Process : : sys $ profiling_enable ( pid_t pid )
{
InterruptDisabler disabler ;
auto * process = Process : : from_pid ( pid ) ;
if ( ! process )
return - ESRCH ;
if ( ! is_superuser ( ) & & process - > uid ( ) ! = m_uid )
return - EPERM ;
Profiling : : start ( * process ) ;
process - > set_profiling ( true ) ;
return 0 ;
}
int Process : : sys $ profiling_disable ( pid_t pid )
{
InterruptDisabler disabler ;
auto * process = Process : : from_pid ( pid ) ;
if ( ! process )
return - ESRCH ;
if ( ! is_superuser ( ) & & process - > uid ( ) ! = m_uid )
return - EPERM ;
process - > set_profiling ( false ) ;
Profiling : : stop ( ) ;
return 0 ;
}
2019-12-15 23:29:26 +03:00
void * Process : : sys $ get_kernel_info_page ( )
{
2019-12-16 00:21:28 +03:00
return s_info_page_address_for_userspace . as_ptr ( ) ;
2019-12-15 23:29:26 +03:00
}
2019-12-22 13:35:02 +03:00
Thread & Process : : any_thread ( )
{
Thread * found_thread = nullptr ;
for_each_thread ( [ & ] ( auto & thread ) {
found_thread = & thread ;
return IterationDecision : : Break ;
} ) ;
ASSERT ( found_thread ) ;
return * found_thread ;
}
2019-12-22 23:29:47 +03:00
WaitQueue & Process : : futex_queue ( i32 * userspace_address )
{
auto & queue = m_futex_queues . ensure ( ( u32 ) userspace_address ) ;
if ( ! queue )
queue = make < WaitQueue > ( ) ;
return * queue ;
}
int Process : : sys $ futex ( const Syscall : : SC_futex_params * params )
{
if ( ! validate_read_typed ( params ) )
return - EFAULT ;
auto & [ userspace_address , futex_op , value , timeout ] = * params ;
if ( ! validate_read_typed ( userspace_address ) )
return - EFAULT ;
if ( timeout & & ! validate_read_typed ( timeout ) )
return - EFAULT ;
switch ( futex_op ) {
case FUTEX_WAIT :
if ( * userspace_address ! = value )
return - EAGAIN ;
// FIXME: This is supposed to be interruptible by a signal, but right now WaitQueue cannot be interrupted.
// FIXME: Support timeout!
current - > wait_on ( futex_queue ( userspace_address ) ) ;
break ;
case FUTEX_WAKE :
if ( value = = 0 )
return 0 ;
if ( value = = 1 ) {
futex_queue ( userspace_address ) . wake_one ( ) ;
} else {
// FIXME: Wake exactly (value) waiters.
futex_queue ( userspace_address ) . wake_all ( ) ;
}
break ;
}
return 0 ;
}
2019-12-30 21:23:13 +03:00
int Process : : sys $ set_thread_boost ( int tid , int amount )
{
if ( amount < 0 | | amount > 20 )
return - EINVAL ;
InterruptDisabler disabler ;
auto * thread = Thread : : from_tid ( tid ) ;
if ( ! thread )
return - ESRCH ;
if ( thread - > state ( ) = = Thread : : State : : Dead | | thread - > state ( ) = = Thread : : State : : Dying )
return - ESRCH ;
if ( ! is_superuser ( ) & & thread - > process ( ) . uid ( ) ! = euid ( ) )
return - EPERM ;
thread - > set_priority_boost ( amount ) ;
return 0 ;
}
2019-12-30 22:10:00 +03:00
int Process : : sys $ set_process_boost ( pid_t pid , int amount )
{
if ( amount < 0 | | amount > 20 )
return - EINVAL ;
InterruptDisabler disabler ;
auto * process = Process : : from_pid ( pid ) ;
if ( ! process | | process - > is_dead ( ) )
return - ESRCH ;
if ( ! is_superuser ( ) & & process - > uid ( ) ! = euid ( ) )
return - EPERM ;
process - > m_priority_boost = amount ;
return 0 ;
}