With O_TMPFILE, open(2) takes a mode argument.
Strictly, the mode isn't really meaningful unless you supply O_EXCL,
but the kernel will take it and fstat will return it even if you
never give the file a name.
Also warn for O_TMPFILE without a mode at compile time where possible.
Bug: N/A
Test: ran tests
Change-Id: I729b6d6e6190676fd017a1190b6200bf9abdbfd8
diff --git a/libc/bionic/open.cpp b/libc/bionic/open.cpp
index 6d179c4..222e5d3 100644
--- a/libc/bionic/open.cpp
+++ b/libc/bionic/open.cpp
@@ -43,6 +43,10 @@
#endif
}
+static inline bool needs_mode(int flags) {
+ return ((flags & O_CREAT) == O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE);
+}
+
int creat(const char* pathname, mode_t mode) {
return open(pathname, O_CREAT | O_TRUNC | O_WRONLY, mode);
}
@@ -51,7 +55,7 @@
int open(const char* pathname, int flags, ...) {
mode_t mode = 0;
- if ((flags & O_CREAT) != 0) {
+ if (needs_mode(flags)) {
va_list args;
va_start(args, flags);
mode = static_cast<mode_t>(va_arg(args, int));
@@ -63,17 +67,14 @@
__strong_alias(open64, open);
int __open_2(const char* pathname, int flags) {
- if (__predict_false((flags & O_CREAT) != 0)) {
- __fortify_fatal("open(O_CREAT): called without specifying a mode");
- }
-
+ if (needs_mode(flags)) __fortify_fatal("open: called with O_CREAT/O_TMPFILE but no mode");
return __openat(AT_FDCWD, pathname, force_O_LARGEFILE(flags), 0);
}
int openat(int fd, const char *pathname, int flags, ...) {
mode_t mode = 0;
- if ((flags & O_CREAT) != 0) {
+ if (needs_mode(flags)) {
va_list args;
va_start(args, flags);
mode = static_cast<mode_t>(va_arg(args, int));
@@ -85,9 +86,6 @@
__strong_alias(openat64, openat);
int __openat_2(int fd, const char* pathname, int flags) {
- if ((flags & O_CREAT) != 0) {
- __fortify_fatal("openat(O_CREAT): called without specifying a mode");
- }
-
+ if (needs_mode(flags)) __fortify_fatal("open: called with O_CREAT/O_TMPFILE but no mode");
return __openat(fd, pathname, force_O_LARGEFILE(flags), 0);
}