Skip to content

Commit

Permalink
file: implemented F_DUPFD_CLOEXEC and F_DUPFD_CLOFORK
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Jan 6, 2025
1 parent e7bb723 commit 5809902
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ int fcntl(Thread *t, int fd, int cmd, uintptr_t arg) {

switch(cmd) {
case F_DUPFD:
case F_DUPFD_CLOEXEC:
case F_DUPFD_CLOFORK:
if(((int) arg) < 0 || ((int)arg) >= MAX_IO_DESCRIPTORS)
return -EBADF;

IODescriptor *iod = NULL;
int dupfd = openIO(p, (void **) &iod);
if(dupfd < 0) return dupfd;
Expand All @@ -252,6 +257,10 @@ int fcntl(Thread *t, int fd, int cmd, uintptr_t arg) {
socket->refCount++;
}

iod->flags &= ~(FD_CLOEXEC | FD_CLOFORK);
if(cmd == F_DUPFD_CLOEXEC) iod->flags |= FD_CLOEXEC;
else if(cmd == F_DUPFD_CLOFORK) iod->flags |= FD_CLOFORK;

return dupfd;

case F_GETFD:
Expand Down
2 changes: 2 additions & 0 deletions src/include/kernel/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#define F_SETLKW 8
#define F_GETOWN 9
#define F_SETOWN 10
#define F_DUPFD_CLOEXEC 11
#define F_DUPFD_CLOFORK 12

/* fcntl() flags */
#define FD_CLOEXEC (O_CLOEXEC)
Expand Down

0 comments on commit 5809902

Please sign in to comment.