-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(fdtable): add CLOEXEC flag #162
Merged
Merged
Conversation
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
2db9013
to
5ead86a
Compare
ken4647
reviewed
Jan 20, 2025
7085fad
to
8f6e1ef
Compare
ken4647
reviewed
Jan 21, 2025
2bbe4d3
to
b4f2490
Compare
chore: apply code style fixes and update documentation
test script:
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
int fd;
pid_t pid;
// Create a temporary file
fd = open("test_file.txt", O_CREAT | O_RDWR, 0644);
if (fd == -1) {
perror("open failed");
exit(EXIT_FAILURE);
}
// Print the file descriptor
printf("Parent process: Opened file descriptor %d\n", fd);
// Set FD_CLOEXEC flag (Close on exec)
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
perror("fcntl F_SETFD failed");
close(fd);
exit(EXIT_FAILURE);
}
// Create a child process
pid = fork();
if (pid == -1) {
perror("fork failed");
close(fd);
exit(EXIT_FAILURE);
}
if (pid == 0) {
// Child process: Execute another program
printf("Child process: Executing 'child_test'...\n");
char fd_str[16];
snprintf(fd_str, sizeof(fd_str), "%d", fd); // Convert file descriptor to string
execl("./test_cloexec_child_aarch64", "./test_cloexec_child_aarch64", fd_str, NULL);
// If execl returns, something went wrong
perror("execl failed");
close(fd);
exit(EXIT_FAILURE);
} else {
// Parent process: Wait for the child process to complete
printf("Parent process: Waiting for child to complete...\n");
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
printf("Parent process: Child exited with status %d\n", WEXITSTATUS(status));
} else {
printf("Parent process: Child did not exit normally\n");
}
// Cleanup
close(fd);
unlink("test_file.txt"); // Delete the temporary file
printf("Parent process: Done.\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
// Check if the file descriptor is passed as an argument
if (argc < 2)
{
fprintf(stderr, "Usage: %s <fd>\n", argv[0]);
exit(EXIT_FAILURE);
}
// Convert the argument to an integer (file descriptor)
int fd = atoi(argv[1]); // Get the file descriptor passed as argument
printf("Child process: Got file descriptor %d\n", fd);
// Try to read from the file descriptor
char buffer[16];
ssize_t n = read(fd, buffer, sizeof(buffer));
if (n == -1)
{
// If reading fails, it means FD_CLOEXEC is working, and the file descriptor is closed on exec
perror("Child process: read failed");
printf("Child process: File descriptor %d is not valid (expected if FD_CLOEXEC works).\n", fd);
exit(EXIT_SUCCESS);
}
else
{
// If reading succeeds, something is wrong because the file descriptor should be closed
printf("Child process: Unexpectedly read %zd bytes from file descriptor %d\n", n, fd);
exit(EXIT_FAILURE);
}
} if Parent process: Opened file descriptor 3
Parent process: Waiting for child to complete...
Child process: Executing 'child_test'...
Child process: read failed: Bad file descriptor
child get fd: 3Child process: File descriptor 3 is not valid (expected if FD_CLOEXEC works).
Parent process: Child exited with status 0
Parent process: Done. else, the output will print: Parent process: Opened file descriptor 3
Parent process: Waiting for child to complete...
Child process: Executing 'child_test'...
child get fd: 3
Child process: Unexpectedly read 0 bytes from file descriptor 3
Parent process: Child exited with status 1
Parent process: Done. |
ken4647
approved these changes
Feb 10, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add support for the CLOEXEC flag in file descriptors.