FORTIFY_SOURCE: Add openat, fix bug

Add fortify_source support for openat(). This change requires that
an argument be supplied when using O_CREAT.

Fix unnecessary call to __open_2. If, at compile time, we know that
"flags" is constant and DOESN'T contain O_CREAT, the call to __open_2
is useless.

Change-Id: Ifcd29c4fb25e25656961d7552d672e161f0cfdbd
diff --git a/libc/unistd/openat.c b/libc/unistd/openat.c
index 6b7b367..fb04e9c 100644
--- a/libc/unistd/openat.c
+++ b/libc/unistd/openat.c
@@ -28,6 +28,8 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <stdarg.h>
+#include <stdlib.h>
+#include <private/logd.h>
 
 extern int  __openat(int, const char*, int, int);
 
@@ -49,3 +51,16 @@
     return __openat(fd, pathname, flags, mode);
 }
 
+int __openat_2(int fd, const char *pathname, int flags)
+{
+    if (flags & O_CREAT) {
+        __libc_android_log_print(ANDROID_LOG_FATAL, "libc",
+            "*** openat(O_CREAT) called without specifying a mode ***\n");
+        abort();
+    }
+
+    flags |= O_LARGEFILE;
+
+    return __openat(fd, pathname, flags, 0);
+}
+