Compare commits
2 Commits
df10544e84
...
0d54d0ece1
Author | SHA1 | Date | |
---|---|---|---|
0d54d0ece1 | |||
6239ed83c7 |
@ -113,6 +113,25 @@ Result<u64> sys_fcntl(Registers*, SyscallArgs args)
|
|||||||
|
|
||||||
return (u64)new_fd;
|
return (u64)new_fd;
|
||||||
}
|
}
|
||||||
|
case F_GETFD: return (u64) !!(descriptor.flags & O_CLOEXEC);
|
||||||
|
case F_SETFD: {
|
||||||
|
int arg = (int)args[2];
|
||||||
|
if (arg == FD_CLOEXEC) descriptor.flags |= O_CLOEXEC;
|
||||||
|
else
|
||||||
|
descriptor.flags &= ~O_CLOEXEC;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
case F_GETFL: return (u64)(descriptor.flags & ~O_CLOEXEC);
|
||||||
|
case F_SETFL: {
|
||||||
|
int arg = (int)args[2];
|
||||||
|
|
||||||
|
descriptor.flags &= ~(O_APPEND | O_NONBLOCK);
|
||||||
|
arg &= (O_APPEND | O_NONBLOCK);
|
||||||
|
|
||||||
|
descriptor.flags |= arg;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
default: return err(EINVAL);
|
default: return err(EINVAL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,5 +5,11 @@
|
|||||||
|
|
||||||
#define F_DUPFD 0
|
#define F_DUPFD 0
|
||||||
#define F_DUPFD_CLOEXEC 1
|
#define F_DUPFD_CLOEXEC 1
|
||||||
|
#define F_GETFD 2
|
||||||
|
#define F_SETFD 3
|
||||||
|
#define F_SETFL 4
|
||||||
|
#define F_GETFL 5
|
||||||
|
|
||||||
|
#define FD_CLOEXEC 1
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -13,4 +13,6 @@
|
|||||||
#define O_NONBLOCK 64
|
#define O_NONBLOCK 64
|
||||||
#define O_CLOEXEC 128
|
#define O_CLOEXEC 128
|
||||||
|
|
||||||
|
#define O_ACCMODE O_RDWR
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -29,6 +29,22 @@ static int fopen_parse_mode(const char* mode)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int fdopen_check_compatible_mode(int fd, int new_flags)
|
||||||
|
{
|
||||||
|
int old_flags = fcntl(fd, F_GETFL);
|
||||||
|
if (old_flags < 0) return -1;
|
||||||
|
|
||||||
|
int old_mode = old_flags & O_ACCMODE;
|
||||||
|
int new_mode = new_flags & O_ACCMODE;
|
||||||
|
if ((old_mode & new_mode) != new_mode)
|
||||||
|
{
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
FILE* fopen(const char* path, const char* mode)
|
FILE* fopen(const char* path, const char* mode)
|
||||||
@ -59,9 +75,7 @@ extern "C"
|
|||||||
|
|
||||||
if ((flags = fopen_parse_mode(mode)) < 0) return nullptr;
|
if ((flags = fopen_parse_mode(mode)) < 0) return nullptr;
|
||||||
|
|
||||||
// FIXME: We do verify that fd is valid, but not that the mode is compatible.
|
if (fdopen_check_compatible_mode(fd, flags) < 0) return nullptr;
|
||||||
long rc = lseek(fd, 0, SEEK_CUR);
|
|
||||||
if (rc < 0) return nullptr;
|
|
||||||
|
|
||||||
FILE* f = (FILE*)malloc(sizeof(FILE));
|
FILE* f = (FILE*)malloc(sizeof(FILE));
|
||||||
if (!f) { return nullptr; }
|
if (!f) { return nullptr; }
|
||||||
|
Loading…
Reference in New Issue
Block a user