Update sortix/process.cpp coding style.

This commit is contained in:
Jonas 'Sortie' Termansen 2013-06-12 01:01:49 +02:00
parent 8d420c9de7
commit 2cb3f2860a
1 changed files with 994 additions and 976 deletions

View File

@ -22,43 +22,44 @@
*******************************************************************************/ *******************************************************************************/
#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <sortix/clock.h>
#include <sortix/fcntl.h>
#include <sortix/fork.h>
#include <sortix/mman.h>
#include <sortix/signal.h>
#include <sortix/stat.h>
#include <sortix/unistd.h>
#include <sortix/wait.h>
#include <sortix/kernel/platform.h> #include <sortix/kernel/platform.h>
#include <sortix/kernel/kthread.h>
#include <sortix/kernel/refcount.h>
#include <sortix/kernel/ioctx.h>
#include <sortix/kernel/copy.h> #include <sortix/kernel/copy.h>
#include <sortix/kernel/descriptor.h> #include <sortix/kernel/descriptor.h>
#include <sortix/kernel/dtable.h> #include <sortix/kernel/dtable.h>
#include <sortix/kernel/mtable.h> #include <sortix/kernel/ioctx.h>
#include <sortix/kernel/worker.h> #include <sortix/kernel/kthread.h>
#include <sortix/kernel/memorymanagement.h> #include <sortix/kernel/memorymanagement.h>
#include <sortix/kernel/string.h> #include <sortix/kernel/mtable.h>
#include <sortix/kernel/syscall.h>
#include <sortix/kernel/sortedlist.h>
#include <sortix/kernel/scheduler.h>
#include <sortix/kernel/symbol.h>
#include <sortix/kernel/process.h> #include <sortix/kernel/process.h>
#include <sortix/kernel/refcount.h>
#include <sortix/kernel/scheduler.h>
#include <sortix/kernel/sortedlist.h>
#include <sortix/kernel/string.h>
#include <sortix/kernel/symbol.h>
#include <sortix/kernel/syscall.h>
#include <sortix/kernel/thread.h> #include <sortix/kernel/thread.h>
#include <sortix/kernel/time.h> #include <sortix/kernel/time.h>
#include <sortix/kernel/worker.h>
#include <sortix/clock.h>
#include <sortix/signal.h>
#include <sortix/unistd.h>
#include <sortix/fcntl.h>
#include <sortix/stat.h>
#include <sortix/fork.h>
#include <sortix/mman.h>
#include <sortix/wait.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include "initrd.h"
#include "elf.h" #include "elf.h"
#include "initrd.h"
namespace Sortix {
namespace Sortix
{
bool ProcessSegment::Intersects(ProcessSegment* segments) bool ProcessSegment::Intersects(ProcessSegment* segments)
{ {
for ( ProcessSegment* tmp = segments; tmp != NULL; tmp = tmp->next ) for ( ProcessSegment* tmp = segments; tmp != NULL; tmp = tmp->next )
@ -405,7 +406,8 @@ namespace Sortix
ScopedLock lock(&childlock); ScopedLock lock(&childlock);
// A process can only wait if it has children. // A process can only wait if it has children.
if ( !firstchild && !zombiechild ) { errno = ECHILD; return -1; } if ( !firstchild && !zombiechild )
return errno = ECHILD, -1;
// Processes can only wait for their own children to exit. // Processes can only wait for their own children to exit.
if ( 0 < thepid ) if ( 0 < thepid )
@ -419,7 +421,8 @@ namespace Sortix
for ( Process* p = zombiechild; !found && p; p = p->nextsibling ) for ( Process* p = zombiechild; !found && p; p = p->nextsibling )
if ( p->pid == thepid ) if ( p->pid == thepid )
found = true; found = true;
if ( !found ) { errno = ECHILD; return -1; } if ( !found )
return errno = ECHILD, -1;
} }
Process* zombie = NULL; Process* zombie = NULL;
@ -435,7 +438,8 @@ namespace Sortix
zombiewaiting++; zombiewaiting++;
unsigned long r = kthread_cond_wait_signal(&zombiecond, &childlock); unsigned long r = kthread_cond_wait_signal(&zombiecond, &childlock);
zombiewaiting--; zombiewaiting--;
if ( !r ) { errno = EINTR; return -1; } if ( !r )
return errno = EINTR, -1;
} }
if ( zombie->prevsibling ) if ( zombie->prevsibling )
@ -468,7 +472,7 @@ namespace Sortix
return thepid; return thepid;
} }
pid_t SysWait(pid_t pid, int* status, int options) pid_t sys_waitpid(pid_t pid, int* status, int options)
{ {
return CurrentProcess()->Wait(pid, status, options); return CurrentProcess()->Wait(pid, status, options);
} }
@ -488,7 +492,7 @@ namespace Sortix
t->DeliverSignal(SIGKILL); t->DeliverSignal(SIGKILL);
} }
int SysExit(int status) int sys_exit(int status)
{ {
CurrentProcess()->Exit(status); CurrentProcess()->Exit(status);
return 0; return 0;
@ -499,8 +503,7 @@ namespace Sortix
// TODO: How to handle signals that kill the process? // TODO: How to handle signals that kill the process?
if ( firstthread ) if ( firstthread )
return firstthread->DeliverSignal(signum); return firstthread->DeliverSignal(signum);
errno = EINIT; return errno = EINIT, false;
return false;
} }
void Process::AddChildProcess(Process* child) void Process::AddChildProcess(Process* child)
@ -572,7 +575,8 @@ namespace Sortix
assert(CurrentProcess() == this); assert(CurrentProcess() == this);
Process* clone = new Process; Process* clone = new Process;
if ( !clone ) { return NULL; } if ( !clone )
return NULL;
ProcessSegment* clonesegments = NULL; ProcessSegment* clonesegments = NULL;
@ -657,7 +661,6 @@ namespace Sortix
clone->symbol_table_length = symbol_table_length; clone->symbol_table_length = symbol_table_length;
} }
if ( pid == 1) if ( pid == 1)
assert(dtable->Get(1)); assert(dtable->Get(1));
@ -695,7 +698,6 @@ namespace Sortix
int envc, const char* const* envp, int envc, const char* const* envp,
CPU::InterruptRegisters* regs) CPU::InterruptRegisters* regs)
{ {
(void) programname;
assert(CurrentProcess() == this); assert(CurrentProcess() == this);
char* programname_clone = String::Clone(programname); char* programname_clone = String::Clone(programname);
@ -730,7 +732,8 @@ namespace Sortix
stackargv[argc] = NULL; stackargv[argc] = NULL;
if ( argvsize % 16UL ) { argvsize += 16 - (argvsize % 16UL); } if ( argvsize % 16UL )
argvsize += 16 - argvsize % 16UL;
// And then move envp onto the stack. // And then move envp onto the stack.
addr_t envppos = argvpos - argvsize - sizeof(char*) * (envc+1); addr_t envppos = argvpos - argvsize - sizeof(char*) * (envc+1);
@ -748,7 +751,8 @@ namespace Sortix
stackenvp[envc] = NULL; stackenvp[envc] = NULL;
if ( envpsize % 16UL ) { envpsize += 16 - (envpsize % 16UL); } if ( envpsize % 16UL )
envpsize += 16 - envpsize % 16UL;
stackpos = envppos - envpsize; stackpos = envppos - envpsize;
@ -768,7 +772,7 @@ namespace Sortix
return dir->open(ctx, path, flags, mode); return dir->open(ctx, path, flags, mode);
} }
int SysExecVE(const char* _filename, char* const _argv[], char* const _envp[]) int sys_execve(const char* _filename, char* const _argv[], char* const _envp[])
{ {
char* filename; char* filename;
int argc; int argc;
@ -843,10 +847,12 @@ namespace Sortix
cleanup_desc: cleanup_desc:
desc.Reset(); desc.Reset();
cleanup_envp: cleanup_envp:
for ( int i = 0; i < envc; i++) { delete[] envp[i]; } for ( int i = 0; i < envc; i++)
delete[] envp[i];
delete[] envp; delete[] envp;
cleanup_argv: cleanup_argv:
for ( int i = 0; i < argc; i++) { delete[] argv[i]; } for ( int i = 0; i < argc; i++)
delete[] argv[i];
delete[] argv; delete[] argv;
cleanup_filename: cleanup_filename:
delete[] filename; delete[] filename;
@ -855,12 +861,14 @@ namespace Sortix
return result; return result;
} }
pid_t SysTFork(int flags, tforkregs_t* regs) pid_t sys_tfork(int flags, tforkregs_t* regs)
{ {
if ( Signal::IsPending() ) { errno = EINTR; return -1; } if ( Signal::IsPending() )
return errno = EINTR, -1;
// TODO: Properly support tfork(2). // TODO: Properly support tfork(2).
if ( flags != SFFORK ) { errno = ENOSYS; return -1; } if ( flags != SFFORK )
return errno = ENOSYS, -1;
CPU::InterruptRegisters cpuregs; CPU::InterruptRegisters cpuregs;
InitializeThreadRegisters(&cpuregs, regs); InitializeThreadRegisters(&cpuregs, regs);
@ -868,7 +876,8 @@ namespace Sortix
// TODO: Is it a hack to create a new kernel stack here? // TODO: Is it a hack to create a new kernel stack here?
Thread* curthread = CurrentThread(); Thread* curthread = CurrentThread();
uint8_t* newkernelstack = new uint8_t[curthread->kernelstacksize]; uint8_t* newkernelstack = new uint8_t[curthread->kernelstacksize];
if ( !newkernelstack ) { return -1; } if ( !newkernelstack )
return -1;
Process* clone = CurrentProcess()->Fork(); Process* clone = CurrentProcess()->Fork();
if ( !clone ) { delete[] newkernelstack; return -1; } if ( !clone ) { delete[] newkernelstack; return -1; }
@ -894,7 +903,7 @@ namespace Sortix
return clone->pid; return clone->pid;
} }
pid_t SysGetPID() pid_t sys_getpid()
{ {
return CurrentProcess()->pid; return CurrentProcess()->pid;
} }
@ -907,7 +916,7 @@ namespace Sortix
return parent->pid; return parent->pid;
} }
pid_t SysGetParentPID() pid_t sys_getppid()
{ {
return CurrentProcess()->GetParentProcessId(); return CurrentProcess()->GetParentProcessId();
} }
@ -940,15 +949,19 @@ namespace Sortix
int ProcessCompare(Process* a, Process* b) int ProcessCompare(Process* a, Process* b)
{ {
if ( a->pid < b->pid ) { return -1; } if ( a->pid < b->pid )
if ( a->pid > b->pid ) { return 1; } return -1;
if ( a->pid > b->pid )
return 1;
return 0; return 0;
} }
int ProcessPIDCompare(Process* a, pid_t pid) int ProcessPIDCompare(Process* a, pid_t pid)
{ {
if ( a->pid < pid ) { return -1; } if ( a->pid < pid )
if ( a->pid > pid ) { return 1; } return -1;
if ( a->pid > pid )
return 1;
return 0; return 0;
} }
@ -958,7 +971,8 @@ namespace Sortix
{ {
ScopedLock lock(&pidalloclock); ScopedLock lock(&pidalloclock);
size_t index = pidlist->Search(ProcessPIDCompare, pid); size_t index = pidlist->Search(ProcessPIDCompare, pid);
if ( index == SIZE_MAX ) { return NULL; } if ( index == SIZE_MAX )
return NULL;
return pidlist->Get(index); return pidlist->Get(index);
} }
@ -978,20 +992,24 @@ namespace Sortix
pidlist->Remove(index); pidlist->Remove(index);
} }
void* SysSbrk(intptr_t increment) void* sys_sbrk(intptr_t increment)
{ {
Process* process = CurrentProcess(); Process* process = CurrentProcess();
ProcessSegment* dataseg = NULL; ProcessSegment* dataseg = NULL;
for ( ProcessSegment* iter = process->segments; iter; iter = iter->next ) for ( ProcessSegment* iter = process->segments; iter; iter = iter->next )
{ {
if ( !iter->type == SEG_DATA ) { continue; } if ( !iter->type == SEG_DATA )
if ( dataseg && iter->position < dataseg->position ) { continue; } continue;
if ( dataseg && iter->position < dataseg->position )
continue;
dataseg = iter; dataseg = iter;
} }
if ( !dataseg ) { errno = ENOMEM; return (void*) -1UL; } if ( !dataseg )
return errno = ENOMEM, (void*) -1UL;
addr_t currentend = dataseg->position + dataseg->size; addr_t currentend = dataseg->position + dataseg->size;
addr_t newend = currentend + increment; addr_t newend = currentend + increment;
if ( newend < dataseg->position ) { errno = EINVAL; return (void*) -1UL; } if ( newend < dataseg->position )
return errno = EINVAL, (void*) -1UL;
if ( newend < currentend ) if ( newend < currentend )
{ {
addr_t unmapfrom = Page::AlignUp(newend); addr_t unmapfrom = Page::AlignUp(newend);
@ -1011,16 +1029,14 @@ namespace Sortix
size_t mapbytes = Page::AlignUp(newend - mapfrom); size_t mapbytes = Page::AlignUp(newend - mapfrom);
int prot = PROT_FORK | PROT_READ | PROT_WRITE | PROT_KREAD | PROT_KWRITE; int prot = PROT_FORK | PROT_READ | PROT_WRITE | PROT_KREAD | PROT_KWRITE;
if ( !Memory::MapRange(mapfrom, mapbytes, prot) ) if ( !Memory::MapRange(mapfrom, mapbytes, prot) )
{
return (void*) -1UL; return (void*) -1UL;
} }
} }
}
dataseg->size += increment; dataseg->size += increment;
return (void*) newend; return (void*) newend;
} }
size_t SysGetPageSize() size_t sys_getpagesize()
{ {
return Page::Size(); return Page::Size();
} }
@ -1036,25 +1052,27 @@ namespace Sortix
void Process::Init() void Process::Init()
{ {
Syscall::Register(SYSCALL_EXEC, (void*) SysExecVE); Syscall::Register(SYSCALL_EXEC, (void*) sys_execve);
Syscall::Register(SYSCALL_TFORK, (void*) SysTFork); Syscall::Register(SYSCALL_EXIT, (void*) sys_exit);
Syscall::Register(SYSCALL_GETPID, (void*) SysGetPID); Syscall::Register(SYSCALL_GET_PAGE_SIZE, (void*) sys_getpagesize);
Syscall::Register(SYSCALL_GETPPID, (void*) SysGetParentPID); Syscall::Register(SYSCALL_GETPID, (void*) sys_getpid);
Syscall::Register(SYSCALL_EXIT, (void*) SysExit); Syscall::Register(SYSCALL_GETPPID, (void*) sys_getppid);
Syscall::Register(SYSCALL_SBRK, (void*) sys_sbrk);
Syscall::Register(SYSCALL_TFORK, (void*) sys_tfork);
Syscall::Register(SYSCALL_UMASK, (void*) sys_umask); Syscall::Register(SYSCALL_UMASK, (void*) sys_umask);
Syscall::Register(SYSCALL_WAIT, (void*) SysWait); Syscall::Register(SYSCALL_WAIT, (void*) sys_waitpid);
Syscall::Register(SYSCALL_SBRK, (void*) SysSbrk);
Syscall::Register(SYSCALL_GET_PAGE_SIZE, (void*) SysGetPageSize);
pidalloclock = KTHREAD_MUTEX_INITIALIZER; pidalloclock = KTHREAD_MUTEX_INITIALIZER;
nextpidtoallocate = 0; nextpidtoallocate = 0;
pidlist = new SortedList<Process*>(ProcessCompare); pidlist = new SortedList<Process*>(ProcessCompare);
if ( !pidlist ) { Panic("could not allocate pidlist\n"); } if ( !pidlist )
Panic("could not allocate pidlist\n");
} }
addr_t Process::AllocVirtualAddr(size_t size) addr_t Process::AllocVirtualAddr(size_t size)
{ {
return (mmapfrom -= size); return mmapfrom -= size;
}
} }
} // namespace Sortix