android_hook: fix leaking file descriptor

/dev/tun is opened but never closed, leaking the file descriptor.
Properly close the file descriptor.

Add O_CLOEXEC. As the FD number is never used, it would be meaningless
to attempt to pass it to a child, as the child would have no way to
figure out what FD was passed from parent to child.

Add O_RDONLY. The original open() call used "0" instead of O_RDONLY.
It's the same thing, but it's more readable to use the macro.

Error check the return value from open(). Otherwise, the remaining code
will attempt to continue and perform an ioctl on an invalid file
descriptor.

Test: none
Change-Id: I5083d175fa9b8e3e8d4707a49f29d0cebe9965f9
diff --git a/main.c b/main.c
index 524155a..10c774f 100644
--- a/main.c
+++ b/main.c
@@ -103,7 +103,12 @@
 const char *android_hook(char **envp)
 {
     struct ifreq ifr = {.ifr_flags = IFF_TUN};
-    int tun = open("/dev/tun", 0);
+    int tun = open("/dev/tun", O_RDONLY | O_CLOEXEC);
+
+    if (tun == -1) {
+        do_plog(LLV_ERROR, "error opening /dev/tun: %s\n", strerror(errno));
+        exit(1);
+    }
 
     /* Android does not support INTERNAL_WINS4_LIST, so we just use it. */
     while (*envp && strncmp(*envp, "INTERNAL_WINS4_LIST=", 20)) {
@@ -117,6 +122,7 @@
         do_plog(LLV_ERROR, "Cannot allocate TUN: %s\n", strerror(errno));
         exit(1);
     }
+    close(tun);
     sprintf(*envp, "INTERFACE=%s", ifr.ifr_name);
     return "/etc/ppp/ip-up-vpn";
 }