kernel: Add umask
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-05-26 22:27:49 +02:00
parent d89a823924
commit b0506bf88f
Signed by: apio
GPG Key ID: B8A7D06E42258954
10 changed files with 46 additions and 6 deletions

View File

@ -272,6 +272,8 @@ int main()
stdout = fopen("/dev/console", "w"); stdout = fopen("/dev/console", "w");
stderr = fopen("/dev/console", "w"); stderr = fopen("/dev/console", "w");
umask(022);
g_init_log = fopen("/init.log", "w+"); g_init_log = fopen("/init.log", "w+");
fcntl(fileno(g_init_log), F_SETFD, FD_CLOEXEC); fcntl(fileno(g_init_log), F_SETFD, FD_CLOEXEC);

View File

@ -2,6 +2,14 @@
#include <luna/PathParser.h> #include <luna/PathParser.h>
#include <os/ArgumentParser.h> #include <os/ArgumentParser.h>
#include <os/FileSystem.h> #include <os/FileSystem.h>
#include <sys/stat.h>
static mode_t s_umask = 0;
static void read_umask()
{
s_umask = umask(0);
}
Result<void> mkdir_recursively(StringView path, mode_t mode) Result<void> mkdir_recursively(StringView path, mode_t mode)
{ {
@ -15,7 +23,7 @@ begin:
PathParser parser = TRY(PathParser::create(path.chars())); PathParser parser = TRY(PathParser::create(path.chars()));
auto parent = TRY(parser.dirname()); auto parent = TRY(parser.dirname());
TRY(mkdir_recursively(parent.view(), mode)); TRY(mkdir_recursively(parent.view(), (0777 & ~s_umask) | S_IWUSR | S_IXUSR));
goto begin; goto begin;
} }
@ -33,12 +41,18 @@ Result<int> luna_main(int argc, char** argv)
parser.add_description("Create directories."_sv); parser.add_description("Create directories."_sv);
parser.add_system_program_info("mkdir"_sv); parser.add_system_program_info("mkdir"_sv);
parser.add_positional_argument(path, "path"_sv, true); parser.add_positional_argument(path, "path"_sv, true);
parser.add_positional_argument(mode_string, "mode"_sv, "755"_sv); parser.add_value_argument(mode_string, 'm', "mode"_sv, true, "set the mode for the newly created directory");
parser.add_switch_argument(recursive, 'p', "parents"_sv, parser.add_switch_argument(recursive, 'p', "parents"_sv,
"if parent directories do not exist, create them as well"_sv); "if parent directories do not exist, create them as well"_sv);
parser.parse(argc, argv); parser.parse(argc, argv);
mode_t mode = (mode_t)parse_unsigned_integer(mode_string.chars(), nullptr, 8); read_umask();
mode_t mode;
if (mode_string.is_empty()) mode = 0777 & ~s_umask;
else
mode = (mode_t)parse_unsigned_integer(mode_string.chars(), nullptr, 8) & 01777;
if (recursive) if (recursive)
{ {

View File

@ -134,6 +134,7 @@ Result<u64> sys_fork(Registers* regs, SyscallArgs)
thread->auth = current->auth; thread->auth = current->auth;
thread->current_directory = current->current_directory; thread->current_directory = current->current_directory;
thread->current_directory_path = move(current_directory_path); thread->current_directory_path = move(current_directory_path);
thread->umask = current->umask;
thread->parent = current; thread->parent = current;
for (int i = 0; i < FD_MAX; i++) for (int i = 0; i < FD_MAX; i++)

View File

@ -194,3 +194,16 @@ Result<u64> sys_pipe(Registers*, SyscallArgs args)
return 0; return 0;
} }
Result<u64> sys_umask(Registers*, SyscallArgs args)
{
mode_t new_umask = (mode_t)args[0];
auto* current = Scheduler::current();
mode_t old_umask = current->umask;
current->umask = new_umask & 0777;
return old_umask;
}

View File

@ -12,7 +12,7 @@ Result<u64> sys_mkdir(Registers*, SyscallArgs args)
Thread* current = Scheduler::current(); Thread* current = Scheduler::current();
auto inode = TRY(VFS::create_directory(path.chars(), current->auth, current->current_directory)); auto inode = TRY(VFS::create_directory(path.chars(), current->auth, current->current_directory));
inode->chmod(mode); inode->chmod(mode & ~current->umask);
inode->chown(current->auth.euid, current->auth.egid); inode->chown(current->auth.euid, current->auth.egid);
return 0; return 0;

View File

@ -38,7 +38,7 @@ Result<u64> sys_openat(Registers*, SyscallArgs args)
if (error == ENOENT && (flags & O_CREAT) && !path.is_empty()) if (error == ENOENT && (flags & O_CREAT) && !path.is_empty())
{ {
inode = TRY(VFS::create_file(path.chars(), current->auth, parent_inode)); inode = TRY(VFS::create_file(path.chars(), current->auth, parent_inode));
inode->chmod(mode); inode->chmod(mode & ~current->umask);
inode->chown(current->auth.euid, current->auth.egid); inode->chown(current->auth.euid, current->auth.egid);
} }
else else

View File

@ -87,6 +87,8 @@ struct Thread : public LinkedListNode<Thread>
u8 status { 0 }; u8 status { 0 };
mode_t umask { 0 };
StaticString<128> name; StaticString<128> name;
String current_directory_path = {}; String current_directory_path = {};

View File

@ -38,6 +38,9 @@ extern "C"
/* Retrieve information about a file. */ /* Retrieve information about a file. */
int fstatat(int dirfd, const char* path, struct stat* st, int flags); int fstatat(int dirfd, const char* path, struct stat* st, int flags);
/* Change the process's file creation mask. */
mode_t umask(mode_t mask);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -47,4 +47,9 @@ extern "C"
long rc = syscall(SYS_fstatat, dirfd, path, st, flags); long rc = syscall(SYS_fstatat, dirfd, path, st, flags);
__errno_return(rc, int); __errno_return(rc, int);
} }
mode_t umask(mode_t mask)
{
return (mode_t)syscall(SYS_umask, mask);
}
} }

View File

@ -5,7 +5,7 @@
_e(lseek) _e(mkdir) _e(execve) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) _e(geteuid) \ _e(lseek) _e(mkdir) _e(execve) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) _e(geteuid) \
_e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(fchmodat) _e(fchownat) _e(ioctl) \ _e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(fchmodat) _e(fchownat) _e(ioctl) \
_e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2) _e(pipe) _e(mount) \ _e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2) _e(pipe) _e(mount) \
_e(umount) _e(pstat) _e(getrusage) _e(symlinkat) _e(readlinkat) _e(umount) _e(pstat) _e(getrusage) _e(symlinkat) _e(readlinkat) _e(umask)
enum Syscalls enum Syscalls
{ {