diff --git a/system/lib/libc/emscripten_syscall_stubs.c b/system/lib/libc/emscripten_syscall_stubs.c index c8eb79c02baf..03d8d67ec17a 100644 --- a/system/lib/libc/emscripten_syscall_stubs.c +++ b/system/lib/libc/emscripten_syscall_stubs.c @@ -28,7 +28,7 @@ static int g_pid = 42; static int g_pgid = 42; static int g_ppid = 1; static int g_sid = 42; -static mode_t g_umask = S_IRWXU | S_IRWXG | S_IRWXO; +static mode_t g_umask = S_IWGRP | S_IWOTH; #ifdef NDEBUG #define REPORT(name) diff --git a/test/other/test_umask.c b/test/other/test_umask.c new file mode 100644 index 000000000000..2a6e11d28abd --- /dev/null +++ b/test/other/test_umask.c @@ -0,0 +1,65 @@ +/* + * Copyright 2024 The Emscripten Authors. All rights reserved. + * Emscripten is available under two separate licenses, the MIT license and the + * University of Illinois/NCSA Open Source License. Both these licenses can be + * found in the LICENSE file. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +mode_t get_umask() { + mode_t current = umask(0); // Set umask to 0 and get the old value + umask(current); // Immediately set it back + return current; +} + +void create_file(const char *path, const char *buffer) { + mode_t mode = 0777 - get_umask(); + int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode); + assert(fd >= 0); + + int err = write(fd, buffer, sizeof(char) * strlen(buffer)); + assert(err == (sizeof(char) * strlen(buffer))); + + close(fd); +} + +int main() { + // Get the default umask + mode_t default_umask = get_umask(); + printf("default umask: %o\n", default_umask); + assert(default_umask == 022); + + // Create a new file with default umask + create_file("umask_test_file", "abcdef"); + struct stat st; + stat("umask_test_file", &st); + printf("default_umask - stat: %o\n", st.st_mode); + assert((st.st_mode & 0666) == 0644); + unlink("umask_test_file"); + + // Set new umask + mode_t new_umask = 027; + mode_t old_umask = umask(new_umask); + + // Create a new file with new umask + create_file("umask_test_file", "abcdef"); + stat("umask_test_file", &st); + printf("new_umask - stat: %o\n", st.st_mode); + assert((st.st_mode & 0666) == 0640); + + // Restore the old umask + umask(old_umask); + + puts("success"); + return EXIT_SUCCESS; +} diff --git a/test/test_other.py b/test/test_other.py index c568b563ba8e..69a6aac1c475 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -7669,6 +7669,11 @@ def test_umask_0(self): self.run_process([EMCC, 'src.c']) self.assertContained('hello, world!', self.run_js('a.out.js')) + @crossplatform + @also_with_wasmfs + def test_umask(self): + self.do_runf('other/test_umask.c', 'success') + def test_no_missing_symbols(self): # simple hello world should not show any missing symbols self.run_process([EMCC, test_file('hello_world.c')])