-
Notifications
You must be signed in to change notification settings - Fork 831
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close host files when the last FD referencing them is closed
- Loading branch information
Showing
6 changed files
with
213 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/// This test makes sure writing to the same file, when referred to | ||
/// by multiple FDs (some of which are shared), works correctly. The | ||
/// trace logs generated by Wasmer are also inspected to make sure | ||
/// the file is closed properly. | ||
|
||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <sys/wait.h> | ||
|
||
void error_exit(const char *message) | ||
{ | ||
perror(message); | ||
exit(-1); | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int fd1 = open("./output", O_CREAT | O_EXCL | O_WRONLY); | ||
if (fd1 < 0) | ||
{ | ||
error_exit("open"); | ||
} | ||
|
||
int fd2 = dup(fd1); | ||
if (fd2 < 0 || fd2 == fd1) | ||
{ | ||
error_exit("parent dup"); | ||
} | ||
|
||
if (!write(fd2, "parent 2\n", 9)) | ||
{ | ||
error_exit("parent write 2"); | ||
} | ||
|
||
pid_t pid = fork(); | ||
if (pid == -1) | ||
{ | ||
error_exit("fork"); | ||
} | ||
else if (pid == 0) | ||
{ | ||
if (close(fd2)) | ||
{ | ||
error_exit("child close parent fd2"); | ||
} | ||
|
||
fd2 = dup(fd1); | ||
if (fd2 < 0 || fd2 == fd1) | ||
{ | ||
error_exit("child dup"); | ||
} | ||
|
||
if (!write(fd1, "child 1\n", 8)) | ||
{ | ||
error_exit("child write 1"); | ||
} | ||
if (close(fd1)) | ||
{ | ||
error_exit("child close 1"); | ||
} | ||
|
||
if (!write(fd2, "child 2\n", 8)) | ||
{ | ||
error_exit("child write 2"); | ||
} | ||
if (close(fd2)) | ||
{ | ||
error_exit("child close 2"); | ||
} | ||
exit(0); | ||
} | ||
else | ||
{ | ||
if (close(fd2)) | ||
{ | ||
error_exit("parent close 2"); | ||
} | ||
|
||
int status; | ||
waitpid(pid, &status, 0); | ||
if (status) | ||
{ | ||
error_exit("child exit code"); | ||
} | ||
|
||
if (!write(fd1, "parent 1\n", 9)) | ||
{ | ||
error_exit("parent write 1"); | ||
} | ||
|
||
// These prints can be inspected to make sure the file was | ||
// closed as a result of the last close() call, see run.sh | ||
printf("closing last fd\n"); | ||
if (close(fd1)) | ||
{ | ||
error_exit("parent close 1"); | ||
} | ||
printf("last fd closed\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/bin/bash | ||
|
||
set -eu | ||
|
||
rm -f output* | ||
|
||
RUST_LOG=virtual_fs=trace $WASMER run main.wasm --dir . &> output-wasmer-log | ||
|
||
cat output | grep "parent 1" >/dev/null && \ | ||
cat output | grep "parent 2" >/dev/null && \ | ||
cat output | grep "child 1" >/dev/null && \ | ||
cat output | grep "child 2" >/dev/null && \ | ||
awk '/closing last fd/{m1=1} /Closing host file.*shared-fd\/output/{if(m1) m2=1} /last fd closed/{if(m2) print "found all lines"}' output-wasmer-log | grep "found all lines" >/dev/null |