Skip to content

Commit ff58722

Browse files
authored
Implement getrlimit(RLIMIT_NOFILE) (#23774)
Also, remove the unnecessary setrlimit and ugetrlimit syscalls. Musl will use __syscall_prlimit64 instead of both of these (See musl/src/misc/getrlimit.c and musl/src/misc/setrlimit.c).
1 parent af88c1f commit ff58722

File tree

6 files changed

+42
-19
lines changed

6 files changed

+42
-19
lines changed

system/lib/libc/emscripten_syscall_stubs.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,6 @@ weak int __syscall_umask(int mask) {
126126
return old;
127127
}
128128

129-
weak int __syscall_setrlimit(int resource, intptr_t limit) {
130-
return 0; // no-op
131-
}
132-
133129
weak int __syscall_getrusage(int who, intptr_t usage) {
134130
REPORT(getrusage);
135131
struct rusage *u = (struct rusage *)usage;
@@ -228,21 +224,23 @@ weak int __syscall_munlockall() {
228224
weak int __syscall_prlimit64(int pid, int resource, intptr_t new_limit, intptr_t old_limit) {
229225
REPORT(prlimit64);
230226
struct rlimit *old = (struct rlimit *)old_limit;
231-
if (old) { // just report no limits
232-
old->rlim_cur = RLIM_INFINITY;
233-
old->rlim_max = RLIM_INFINITY;
227+
if (new_limit) {
228+
return -EPERM;
229+
}
230+
if (old) {
231+
if (resource == RLIMIT_NOFILE) {
232+
// See FS.MAX_OPEN_FDS in src/lib/libfs.js
233+
old->rlim_cur = 4096;
234+
old->rlim_max = 4096;
235+
} else {
236+
// Just report no limits
237+
old->rlim_cur = RLIM_INFINITY;
238+
old->rlim_max = RLIM_INFINITY;
239+
}
234240
}
235241
return 0;
236242
}
237243

238-
weak int __syscall_ugetrlimit(int resource, intptr_t rlim) {
239-
REPORT(ugetrlimit);
240-
struct rlimit * limits = (struct rlimit *)rlim;
241-
limits->rlim_cur = RLIM_INFINITY;
242-
limits->rlim_max = RLIM_INFINITY;
243-
return 0; // just report no limits
244-
}
245-
246244
weak int __syscall_setsockopt(int sockfd, int level, int optname, intptr_t optval, size_t optlen, int dummy) {
247245
REPORT(setsockopt);
248246
return -ENOPROTOOPT; // The option is unknown at the level indicated.

system/lib/libc/musl/arch/emscripten/bits/syscall.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#define SYS_umask __syscall_umask
1313
#define SYS_getppid __syscall_getppid
1414
#define SYS_setsid __syscall_setsid
15-
#define SYS_setrlimit __syscall_setrlimit
1615
#define SYS_getrusage __syscall_getrusage
1716
#define SYS_munmap __syscall_munmap
1817
#define SYS_fchmod __syscall_fchmod
@@ -35,7 +34,6 @@
3534
#define SYS_mremap __syscall_mremap
3635
#define SYS_poll __syscall_poll
3736
#define SYS_getcwd __syscall_getcwd
38-
#define SYS_ugetrlimit __syscall_ugetrlimit
3937
#define SYS_mmap2 __syscall_mmap2
4038
#define SYS_truncate64 __syscall_truncate64
4139
#define SYS_ftruncate64 __syscall_ftruncate64

system/lib/libc/musl/arch/emscripten/syscall_arch.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ int __syscall_umask(int mask);
2929
int __syscall_getppid(void);
3030
int __syscall_getpgrp(void);
3131
int __syscall_setsid(void);
32-
int __syscall_setrlimit(int resource, intptr_t limit);
3332
int __syscall_getrusage(int who, intptr_t usage);
3433
int __syscall_munmap(intptr_t addr, size_t len);
3534
int __syscall_fchmod(int fd, int mode);
@@ -53,7 +52,6 @@ int __syscall_munlockall(void);
5352
int __syscall_mremap(intptr_t old_addr, size_t old_size, size_t new_size, int flags, intptr_t new_addr);
5453
int __syscall_poll(intptr_t fds, int nfds, int timeout);
5554
int __syscall_getcwd(intptr_t buf, size_t size);
56-
int __syscall_ugetrlimit(int resource, intptr_t rlim);
5755
intptr_t __syscall_mmap2(intptr_t addr, size_t len, int prot, int flags, int fd, off_t offset);
5856
int __syscall_truncate64(intptr_t path, off_t length);
5957
int __syscall_ftruncate64(int fd, off_t length);

test/other/test_rlimit.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <assert.h>
2+
#include <errno.h>
3+
#include <stdio.h>
4+
#include <sys/resource.h>
5+
6+
int main() {
7+
struct rlimit rlim;
8+
9+
assert(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
10+
printf("RLIMIT_NOFILE: rlim_cur: %lld\n", rlim.rlim_cur);
11+
printf("RLIMIT_NOFILE: rlim_max: %lld\n", rlim.rlim_max);
12+
13+
assert(getrlimit(RLIMIT_CORE, &rlim) == 0);
14+
printf("RLIMIT_CORE: rlim_cur: %lld\n", rlim.rlim_cur);
15+
printf("RLIMIT_CORE: rlim_max: %lld\n", rlim.rlim_max);
16+
17+
// setrlimit should always fail with EPERM
18+
assert(setrlimit(RLIMIT_NOFILE, &rlim) == -1);
19+
assert(errno == EPERM);
20+
21+
return 0;
22+
}

test/other/test_rlimit.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
RLIMIT_NOFILE: rlim_cur: 4096
2+
RLIMIT_NOFILE: rlim_max: 4096
3+
RLIMIT_CORE: rlim_cur: -1
4+
RLIMIT_CORE: rlim_max: -1

test/test_other.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15696,3 +15696,6 @@ def test_rollup(self):
1569615696
self.run_process([EMCC, test_file('hello_world.c'), '-sEXPORT_ES6', '-sEXIT_RUNTIME', '-sENVIRONMENT=node', '-sMODULARIZE', '-o', 'hello.mjs'])
1569715697
self.run_process(shared.get_npm_cmd('rollup') + ['--config'])
1569815698
self.assertContained('hello, world!', self.run_js('bundle.mjs'))
15699+
15700+
def test_rlimit(self):
15701+
self.do_other_test('test_rlimit.c', emcc_args=['-O1'])

0 commit comments

Comments
 (0)