Skip to content

Commit d26e762

Browse files
authored
[WasmFS] Stub ioctl()/terminal support (#16677)
We don't have TTY support in emscripten anyhow, so most of this just does nothing. We also do not yet have a concept of TTYs in wasmfs, so just recognize stdin/out/err as such. This is enough for basic things, gets some tests passing, and is almost at parity with the old FS (it had the ability to create new terminals and mark them as such).
1 parent 765ce7f commit d26e762

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

system/lib/standalone/standalone.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ long __syscall_openat(int dirfd, const char* path, long flags, ...) {
9090
return -EPERM;
9191
}
9292

93-
int __syscall_ioctl(int fd, int op, ...) {
93+
__attribute__((__weak__)) int __syscall_ioctl(int fd, int op, ...) {
9494
return -ENOSYS;
9595
}
9696

97-
long __syscall_fcntl64(long fd, long cmd, ...) {
97+
__attribute__((__weak__)) long __syscall_fcntl64(long fd, long cmd, ...) {
9898
return -ENOSYS;
9999
}
100100

system/lib/wasmfs/syscalls.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@
1414
#include <poll.h>
1515
#include <stdarg.h>
1616
#include <stdlib.h>
17+
#include <sys/ioctl.h>
1718
#include <sys/stat.h>
1819
#include <syscall_arch.h>
1920
#include <unistd.h>
2021
#include <utility>
2122
#include <vector>
2223
#include <wasi/api.h>
23-
#include <syscall_arch.h>
2424

2525
#include "backend.h"
2626
#include "file.h"
2727
#include "file_table.h"
2828
#include "paths.h"
2929
#include "pipe_backend.h"
30+
#include "streams.h"
3031
#include "wasmfs.h"
3132

3233
// File permission macros for wasmfs.
@@ -1118,6 +1119,48 @@ int __syscall_ftruncate64(int fd, uint64_t size) {
11181119
return ret;
11191120
}
11201121

1122+
static bool isTTY(std::shared_ptr<File>& file) {
1123+
// TODO: Full TTY support. For now, just see stdin/out/err as terminals and
1124+
// nothing else.
1125+
return file == StdinFile::getSingleton() ||
1126+
file == StdoutFile::getSingleton() ||
1127+
file == StderrFile::getSingleton();
1128+
}
1129+
1130+
int __syscall_ioctl(int fd, int request, ...) {
1131+
auto openFile = wasmFS.getFileTable().locked().getEntry(fd);
1132+
if (!openFile) {
1133+
return -EBADF;
1134+
}
1135+
if (!isTTY(openFile->locked().getFile())) {
1136+
return -ENOTTY;
1137+
}
1138+
// TODO: Full TTY support. For now this is limited, and matches the old FS.
1139+
switch (request) {
1140+
case TCGETA:
1141+
case TCGETS:
1142+
case TCSETA:
1143+
case TCSETAW:
1144+
case TCSETAF:
1145+
case TCSETS:
1146+
case TCSETSW:
1147+
case TCSETSF:
1148+
case TIOCGWINSZ:
1149+
case TIOCSWINSZ: {
1150+
// TTY operations that we do nothing for anyhow can just be ignored.
1151+
return -0;
1152+
}
1153+
case TIOCGPGRP:
1154+
case TIOCSPGRP: {
1155+
// TODO We should get/set the group number here.
1156+
return -EINVAL;
1157+
}
1158+
default: {
1159+
abort();
1160+
}
1161+
}
1162+
}
1163+
11211164
int __syscall_pipe(intptr_t fd) {
11221165
auto* fds = (__wasi_fd_t*)fd;
11231166

tests/test_other.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8163,6 +8163,7 @@ def test_autotools_shared_check(self):
81638163
out = self.run_process([EMCC, '--help'], stdout=PIPE).stdout
81648164
assert re.search(expected, out)
81658165

8166+
@also_with_wasmfs
81668167
def test_ioctl_window_size(self):
81678168
self.do_other_test('test_ioctl_window_size.cpp')
81688169

@@ -10283,6 +10284,7 @@ def test_errno_type(self):
1028310284
''')
1028410285
self.run_process([EMCC, 'errno_type.c'])
1028510286

10287+
@also_with_wasmfs
1028610288
def test_standalone_syscalls(self):
1028710289
self.run_process([EMXX, test_file('other/test_standalone_syscalls.cpp'), '-o', 'test.wasm'])
1028810290
expected = read_file(test_file('other/test_standalone_syscalls.out'))

0 commit comments

Comments
 (0)