From ec782f684297be63fc6df61e3706f7db9d7879b9 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Fri, 8 Nov 2024 10:51:46 +0100 Subject: [PATCH 1/2] Make seek work on `/dev/null` Seeking `/dev/null` should work and do nothing instead of returning `EPIPE`. This fixes it. --- src/library_fs.js | 1 + test/other/test_seek_dev_null.c | 21 +++++++++++++++++++++ test/other/test_seek_dev_null.out | 2 ++ test/test_other.py | 3 +++ 4 files changed, 27 insertions(+) create mode 100644 test/other/test_seek_dev_null.c create mode 100644 test/other/test_seek_dev_null.out diff --git a/src/library_fs.js b/src/library_fs.js index d24cdbf1c7500..886efa61f4c45 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -1347,6 +1347,7 @@ FS.staticInit(); FS.registerDevice(FS.makedev(1, 3), { read: () => 0, write: (stream, buffer, offset, length, pos) => length, + llseek: () => 0, }); FS.mkdev('/dev/null', FS.makedev(1, 3)); // setup /dev/tty and /dev/tty1 diff --git a/test/other/test_seek_dev_null.c b/test/other/test_seek_dev_null.c new file mode 100644 index 0000000000000..a72872a17718d --- /dev/null +++ b/test/other/test_seek_dev_null.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include +#include + +int main() { + int fd = open("/dev/null", O_WRONLY); + printf("fd: %d\n", fd); + if (fd == -1) { + printf("open failed: %s\n", strerror(errno)); + return 1; + } + int res = lseek(fd, 10, SEEK_CUR); + printf("res: %d\n", res); + if (res == -1) { + printf("lseek failed: %s\n", strerror(errno)); + return 1; + } + return 0; +} diff --git a/test/other/test_seek_dev_null.out b/test/other/test_seek_dev_null.out new file mode 100644 index 0000000000000..c9882bbf2b2c4 --- /dev/null +++ b/test/other/test_seek_dev_null.out @@ -0,0 +1,2 @@ +fd: 3 +res: 0 diff --git a/test/test_other.py b/test/test_other.py index 594651b6cb33c..2079fd8d44f2b 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -15329,3 +15329,6 @@ def test_fp16(self, opts): def test_embool(self): self.do_other_test('test_embool.c') + + def test_seek_dev_null(self): + self.do_other_test('test_seek_dev_null.c') From 20b1a3275dcb0754e2be287dbc6ef12c34e8bd23 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Thu, 14 Nov 2024 12:06:28 +0100 Subject: [PATCH 2/2] Add test to test_files.c --- test/other/test_seek_dev_null.c | 21 --------------------- test/other/test_seek_dev_null.out | 2 -- test/test_files.c | 2 ++ test/test_files.out | 1 + test/test_other.py | 3 --- 5 files changed, 3 insertions(+), 26 deletions(-) delete mode 100644 test/other/test_seek_dev_null.c delete mode 100644 test/other/test_seek_dev_null.out diff --git a/test/other/test_seek_dev_null.c b/test/other/test_seek_dev_null.c deleted file mode 100644 index a72872a17718d..0000000000000 --- a/test/other/test_seek_dev_null.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include -#include -#include -#include - -int main() { - int fd = open("/dev/null", O_WRONLY); - printf("fd: %d\n", fd); - if (fd == -1) { - printf("open failed: %s\n", strerror(errno)); - return 1; - } - int res = lseek(fd, 10, SEEK_CUR); - printf("res: %d\n", res); - if (res == -1) { - printf("lseek failed: %s\n", strerror(errno)); - return 1; - } - return 0; -} diff --git a/test/other/test_seek_dev_null.out b/test/other/test_seek_dev_null.out deleted file mode 100644 index c9882bbf2b2c4..0000000000000 --- a/test/other/test_seek_dev_null.out +++ /dev/null @@ -1,2 +0,0 @@ -fd: 3 -res: 0 diff --git a/test/test_files.c b/test/test_files.c index 57c8a4c3a12d2..13d4e7ba95574 100644 --- a/test/test_files.c +++ b/test/test_files.c @@ -162,6 +162,8 @@ void test_tempfiles() { char data[5] = { 10, 30, 20, 11, 88 }; FILE *n = fopen("/dev/null", "w"); printf("5 bytes to dev/null: %zu\n", fwrite(data, 1, 5, n)); + int res = fseek(n, 10, SEEK_CUR); + printf("seek /dev/null: %d\n", res); fclose(n); // Test file creation with O_TRUNC (regression test for #16784) diff --git a/test/test_files.out b/test/test_files.out index d8a72d702b275..7b32cdb1c8a38 100644 --- a/test/test_files.out +++ b/test/test_files.out @@ -12,4 +12,5 @@ seeked=ata. seeked=ta. fscanfed: 10 - hello 5 bytes to dev/null: 5 +seek /dev/null: 0 ok. diff --git a/test/test_other.py b/test/test_other.py index 2079fd8d44f2b..594651b6cb33c 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -15329,6 +15329,3 @@ def test_fp16(self, opts): def test_embool(self): self.do_other_test('test_embool.c') - - def test_seek_dev_null(self): - self.do_other_test('test_seek_dev_null.c')