diff --git a/libc/umask.cpp b/libc/umask.cpp index d62e99e1..bf043f59 100644 --- a/libc/umask.cpp +++ b/libc/umask.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + Copyright(C) Jonas 'Sortie' Termansen 2013. This file is part of the Sortix C Library. @@ -24,12 +24,10 @@ #include #include -#include -#include -// TODO: Implement this in the kernel. +DEFN_SYSCALL1(mode_t, sys_umask, SYSCALL_UMASK, mode_t); + extern "C" mode_t umask(mode_t mask) { - (void) mask; - return 0; + return sys_umask(mask); } diff --git a/sortix/descriptor.cpp b/sortix/descriptor.cpp index ca1a4f33..08ed411f 100644 --- a/sortix/descriptor.cpp +++ b/sortix/descriptor.cpp @@ -356,6 +356,11 @@ static bool IsLastPathElement(const char* elem) Ref Descriptor::open(ioctx_t* ctx, const char* filename, int flags, mode_t mode) { + Process* process = CurrentProcess(); + kthread_mutex_lock(&process->idlock); + mode &= ~process->umask; + kthread_mutex_unlock(&process->idlock); + // Unlike early Unix systems, the empty path does not mean the current // directory on Sortix, so reject it. if ( !filename[0] ) @@ -444,6 +449,11 @@ Ref Descriptor::open_elem(ioctx_t* ctx, const char* filename, int Descriptor::mkdir(ioctx_t* ctx, const char* filename, mode_t mode) { + Process* process = CurrentProcess(); + kthread_mutex_lock(&process->idlock); + mode &= ~process->umask; + kthread_mutex_unlock(&process->idlock); + char* final; Ref dir = OpenDirContainingPath(ctx, Ref(this), filename, &final); diff --git a/sortix/include/sortix/kernel/process.h b/sortix/include/sortix/kernel/process.h index d74ba467..276a22a7 100644 --- a/sortix/include/sortix/kernel/process.h +++ b/sortix/include/sortix/kernel/process.h @@ -96,6 +96,7 @@ public: kthread_mutex_t idlock; uid_t uid, euid; gid_t gid, egid; + mode_t umask; private: kthread_mutex_t ptrlock; diff --git a/sortix/include/sortix/syscallnum.h b/sortix/include/sortix/syscallnum.h index 664abe06..da74eb5e 100644 --- a/sortix/include/sortix/syscallnum.h +++ b/sortix/include/sortix/syscallnum.h @@ -128,6 +128,7 @@ #define SYSCALL_CLOCK_SETTIMERES 104 #define SYSCALL_CLOCK_NANOSLEEP 105 #define SYSCALL_TIMENS 106 -#define SYSCALL_MAX_NUM 107 /* index of highest constant + 1 */ +#define SYSCALL_UMASK 107 +#define SYSCALL_MAX_NUM 108 /* index of highest constant + 1 */ #endif diff --git a/sortix/process.cpp b/sortix/process.cpp index 84b67bd6..b4c9d2e2 100644 --- a/sortix/process.cpp +++ b/sortix/process.cpp @@ -131,6 +131,7 @@ namespace Sortix pid = AllocatePID(); uid = euid = 0; gid = egid = 0; + umask = 0022; Time::InitializeProcessClocks(this); alarm_timer.Attach(Time::GetClock(CLOCK_MONOTONIC)); Put(this); @@ -611,6 +612,7 @@ namespace Sortix clone->gid = gid; clone->euid = euid; clone->egid = egid; + clone->umask = umask; kthread_mutex_unlock(&idlock); if ( !(clone->program_image_path = String::Clone(program_image_path)) ) @@ -978,6 +980,15 @@ namespace Sortix return Page::Size(); } + mode_t sys_umask(mode_t newmask) + { + Process* process = CurrentProcess(); + ScopedLock lock(&process->idlock); + mode_t oldmask = process->umask; + process->umask = newmask & 0666; + return oldmask; + } + void Process::Init() { Syscall::Register(SYSCALL_EXEC, (void*) SysExecVE); @@ -985,6 +996,7 @@ namespace Sortix Syscall::Register(SYSCALL_GETPID, (void*) SysGetPID); Syscall::Register(SYSCALL_GETPPID, (void*) SysGetParentPID); Syscall::Register(SYSCALL_EXIT, (void*) SysExit); + Syscall::Register(SYSCALL_UMASK, (void*) sys_umask); Syscall::Register(SYSCALL_WAIT, (void*) SysWait); Syscall::Register(SYSCALL_SBRK, (void*) SysSbrk); Syscall::Register(SYSCALL_GET_PAGE_SIZE, (void*) SysGetPageSize); diff --git a/utils/init.cpp b/utils/init.cpp index 9a1f5556..6595766f 100644 --- a/utils/init.cpp +++ b/utils/init.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free @@ -75,6 +75,9 @@ int main(int /*argc*/, char* /*argv*/[]) printf("\r\e[m\e[J"); fflush(stdout); + // Set the default file creation mask. + umask(022); + // By default, compile to the same architecture that the kernel told us that // we are running. setenv("objtype", getenv("cputype"), 0);