Merge "Added proper handling of pty-names for pipe usage."
diff --git a/CleanSpec.mk b/CleanSpec.mk
new file mode 100644
index 0000000..b84e1b6
--- /dev/null
+++ b/CleanSpec.mk
@@ -0,0 +1,49 @@
+# Copyright (C) 2007 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# If you don't need to do a full clean build but would like to touch
+# a file or delete some intermediate files, add a clean step to the end
+# of the list.  These steps will only be run once, if they haven't been
+# run before.
+#
+# E.g.:
+#     $(call add-clean-step, touch -c external/sqlite/sqlite3.h)
+#     $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libz_intermediates)
+#
+# Always use "touch -c" and "rm -f" or "rm -rf" to gracefully deal with
+# files that are missing or have been moved.
+#
+# Use $(PRODUCT_OUT) to get to the "out/target/product/blah/" directory.
+# Use $(OUT_DIR) to refer to the "out" directory.
+#
+# If you need to re-do something that's already mentioned, just copy
+# the command and add it to the bottom of the list.  E.g., if a change
+# that you made last week required touching a file and a change you
+# made today requires touching the same file, just copy the old
+# touch step and add it to the end of the list.
+#
+# ************************************************
+# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
+# ************************************************
+
+# For example:
+#$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/AndroidTests_intermediates)
+#$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/JAVA_LIBRARIES/core_intermediates)
+#$(call add-clean-step, find $(OUT_DIR) -type f -name "IGTalkSession*" -print0 | xargs -0 rm -f)
+#$(call add-clean-step, rm -rf $(PRODUCT_OUT)/data/*)
+
+# ************************************************
+# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
+# ************************************************
diff --git a/android/Android.mk b/android/Android.mk
new file mode 100644
index 0000000..25c4c58
--- /dev/null
+++ b/android/Android.mk
@@ -0,0 +1,25 @@
+#
+# Copyright (C) 2009 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := ip-up-vpn.c
+LOCAL_SHARED_LIBRARIES := libcutils
+LOCAL_MODULE := ip-up-vpn
+LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/ppp
+
+include $(BUILD_EXECUTABLE)
diff --git a/android/ip-up-vpn.c b/android/ip-up-vpn.c
new file mode 100644
index 0000000..afc85f4
--- /dev/null
+++ b/android/ip-up-vpn.c
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <linux/route.h>
+
+#include <android/log.h>
+#include <cutils/properties.h>
+
+static inline struct in_addr *in_addr(struct sockaddr *sa)
+{
+    return &((struct sockaddr_in *)sa)->sin_addr;
+}
+
+int main(int argc, char **argv)
+{
+    struct rtentry route = {
+        .rt_dst     = {.sa_family = AF_INET},
+        .rt_genmask = {.sa_family = AF_INET},
+        .rt_gateway = {.sa_family = AF_INET},
+        .rt_flags   = RTF_UP | RTF_GATEWAY,
+    };
+    FILE *f;
+    int s;
+
+    errno = EINVAL;
+    if (argc > 5 && inet_aton(argv[5], in_addr(&route.rt_gateway)) &&
+        (f = fopen("/proc/net/route", "r")) != NULL &&
+        (s = socket(AF_INET, SOCK_DGRAM, 0)) != -1) {
+        uint32_t *address = &in_addr(&route.rt_dst)->s_addr;
+        uint32_t *netmask = &in_addr(&route.rt_genmask)->s_addr;
+        char device[64];
+
+        fscanf(f, "%*[^\n]\n");
+        while (fscanf(f, "%63s%X%*X%*X%*d%*u%*d%X%*d%*u%*u\n",
+                      device, address, netmask) == 3) {
+            if (strcmp(argv[1], device)) {
+                uint32_t bit = ntohl(*netmask);
+                bit = htonl(bit ^ (1 << 31 | bit >> 1));
+                if (bit) {
+                    *netmask |= bit;
+                    if (ioctl(s, SIOCADDRT, &route) == -1 && errno != EEXIST) {
+                        break;
+                    }
+                    *address ^= bit;
+                    if (ioctl(s, SIOCADDRT, &route) == -1 && errno != EEXIST) {
+                        break;
+                    }
+                    errno = 0;
+                }
+            }
+        }
+    }
+
+    if (!errno) {
+        char *dns = getenv("DNS1");
+        property_set("vpn.dns1", dns ? dns : "");
+        dns = getenv("DNS2");
+        property_set("vpn.dns2", dns ? dns : "");
+        property_set("vpn.status", "ok");
+        __android_log_print(ANDROID_LOG_INFO, "ip-up-vpn",
+                            "All traffic is now redirected to %s", argv[5]);
+    } else {
+        property_set("vpn.status", "error");
+        __android_log_write(ANDROID_LOG_ERROR, "ip-up-vpn", strerror(errno));
+    }
+    return errno;
+}
diff --git a/pppd/Android.mk b/pppd/Android.mk
index 01928b1..dcd5f8d 100644
--- a/pppd/Android.mk
+++ b/pppd/Android.mk
@@ -11,31 +11,29 @@
 	ipcp.c \
 	upap.c \
 	chap-new.c \
-	md5.c \
 	ccp.c \
 	ecp.c \
-	ipxcp.c \
 	auth.c \
 	options.c \
 	sys-linux.c \
-	md4.c \
 	chap_ms.c \
 	demand.c \
 	utils.c \
 	tty.c \
 	eap.c \
-	chap-md5.c
+	chap-md5.c \
+	pppcrypt.c \
+	openssl-hash.c \
+	pppox.c
 
 LOCAL_SHARED_LIBRARIES := \
-	libcutils
+	libcutils libcrypto
 
 LOCAL_C_INCLUDES := \
 	$(LOCAL_PATH)/include
 
-LOCAL_CFLAGS := -DANDROID_CHANGES
+LOCAL_CFLAGS := -DANDROID_CHANGES -DCHAPMS=1 -DMPPE=1 -Iexternal/openssl/include
 
-LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
-LOCAL_MODULE_TAGS := eng
 LOCAL_MODULE:= pppd
 
 include $(BUILD_EXECUTABLE)
diff --git a/pppd/chap-new.c b/pppd/chap-new.c
index b09fa3e..7d1aecd 100644
--- a/pppd/chap-new.c
+++ b/pppd/chap-new.c
@@ -35,6 +35,9 @@
 #include "pppd.h"
 #include "chap-new.h"
 #include "chap-md5.h"
+#ifdef ANDROID_CHANGES
+#include "openssl-hash.h"
+#endif
 
 #ifdef CHAPMS
 #include "chap_ms.h"
@@ -141,6 +144,9 @@
 	memset(&client, 0, sizeof(client));
 	memset(&server, 0, sizeof(server));
 
+#ifdef ANDROID_CHANGES
+	openssl_hash_init();
+#endif
 	chap_md5_init();
 #ifdef CHAPMS
 	chapms_init();
diff --git a/pppd/chap_ms.c b/pppd/chap_ms.c
index fb65d56..5f2c0e2 100644
--- a/pppd/chap_ms.c
+++ b/pppd/chap_ms.c
@@ -89,8 +89,12 @@
 #include "pppd.h"
 #include "chap-new.h"
 #include "chap_ms.h"
+#ifdef ANDROID_CHANGES
+#include "openssl-hash.h"
+#else
 #include "md4.h"
 #include "sha1.h"
+#endif
 #include "pppcrypt.h"
 #include "magic.h"
 
@@ -514,12 +518,17 @@
 static void
 NTPasswordHash(char *secret, int secret_len, u_char hash[MD4_SIGNATURE_SIZE])
 {
+#ifdef ANDROID_CHANGES
+    /* We link with MD4 routines in openssl, we have to take bytes instead */
+    int			mdlen = secret_len;
+#else
 #ifdef __NetBSD__
     /* NetBSD uses the libc md4 routines which take bytes instead of bits */
     int			mdlen = secret_len;
 #else
     int			mdlen = secret_len * 8;
 #endif
+#endif
     MD4_CTX		md4Context;
 
     MD4Init(&md4Context);
diff --git a/pppd/ipcp.c b/pppd/ipcp.c
index 52eb3ca..09aebe3 100644
--- a/pppd/ipcp.c
+++ b/pppd/ipcp.c
@@ -1996,9 +1996,9 @@
 create_resolv(peerdns1, peerdns2)
     u_int32_t peerdns1, peerdns2;
 {
+#ifndef ANDROID_CHANGES
     FILE *f;
 
-#if 0 /* resolv.conf has no meaning for ANDROIDS */
     f = fopen(_PATH_RESOLV, "w");
     if (f == NULL) {
 	error("Failed to create %s: %m", _PATH_RESOLV);
diff --git a/pppd/main.c b/pppd/main.c
index d37d781..64efefe 100644
--- a/pppd/main.c
+++ b/pppd/main.c
@@ -362,11 +362,42 @@
 
     progname = *argv;
 
+#ifdef ANDROID_CHANGES
+    {
+        extern void pppox_init();
+        pppox_init();
+        privileged = 1;
+    }
+    {
+        char *envargs = getenv("envargs");
+        if (envargs) {
+            int i;
+            /* Decode the arguments in-place and count the number of them.
+             * They were hex encoded using [A-P] instead of [0-9A-F]. */
+            for (argc = 0, i = 0; envargs[i] && envargs[i + 1]; i += 2) {
+                char c = ((envargs[i] - 'A') << 4) + (envargs[i + 1] - 'A');
+                if (c == 0) {
+                    ++argc;
+                }
+                envargs[i / 2 + 1] = c;
+            }
+            if (argc == 0 || (argv = malloc(sizeof(char *) * argc)) == NULL) {
+                fatal("Failed to parse envargs!");
+            }
+            for (envargs[0] = 0, i = 0; i < argc; ++envargs) {
+                if (envargs[0] == 0) {
+                    argv[i++] = &envargs[1];
+                }
+            }
+        }
+    }
+#endif
+
     /*
      * Parse, in order, the system options file, the user's options file,
      * and the command line arguments.
      */
-#ifdef ANDROID
+#ifdef ANDROID_CHANGES
     /* Android: only take options from commandline */
     if (!parse_args(argc-1, argv+1))
 	exit(EXIT_OPTION_ERROR);
@@ -391,6 +422,7 @@
     if (debug)
 	setlogmask(LOG_UPTO(LOG_DEBUG));
 
+#ifndef ANDROID_CHANGES
     /*
      * Check that we are running as root.
      */
@@ -399,6 +431,7 @@
 		     argv[0]);
 	exit(EXIT_NOT_ROOT);
     }
+#endif
 
     if (!ppp_available()) {
 	option_error("%s", no_ppp_msg);
@@ -805,8 +838,10 @@
 void
 reopen_log()
 {
+#ifndef ANDROID_CHANGES
     openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
     setlogmask(LOG_UPTO(LOG_INFO));
+#endif
 }
 
 /*
@@ -816,6 +851,7 @@
 create_pidfile(pid)
     int pid;
 {
+#ifndef ANDROID_CHANGES
     FILE *pidfile;
 
     slprintf(pidfilename, sizeof(pidfilename), "%s%s.pid",
@@ -827,12 +863,14 @@
 	error("Failed to create pid file %s: %m", pidfilename);
 	pidfilename[0] = 0;
     }
+#endif
 }
 
 void
 create_linkpidfile(pid)
     int pid;
 {
+#ifndef ANDROID_CHANGES
     FILE *pidfile;
 
     if (linkname[0] == 0)
@@ -849,6 +887,7 @@
 	error("Failed to create pid file %s: %m", linkpidfile);
 	linkpidfile[0] = 0;
     }
+#endif
 }
 
 /*
@@ -856,12 +895,14 @@
  */
 void remove_pidfiles()
 {
+#ifndef ANDROID_CHANGES
     if (pidfilename[0] != 0 && unlink(pidfilename) < 0 && errno != ENOENT)
 	warn("unable to delete pid file %s: %m", pidfilename);
     pidfilename[0] = 0;
     if (linkpidfile[0] != 0 && unlink(linkpidfile) < 0 && errno != ENOENT)
 	warn("unable to delete pid file %s: %m", linkpidfile);
     linkpidfile[0] = 0;
+#endif
 }
 
 /*
@@ -1518,7 +1559,9 @@
 	if (errfd != 2)
 		dup2(errfd, 2);
 
+#ifndef ANDROID_CHANGES
 	closelog();
+#endif
 	if (log_to_fd > 2)
 		close(log_to_fd);
 	if (the_channel->close)
@@ -1625,6 +1668,21 @@
     int pid;
     struct stat sbuf;
 
+#ifdef ANDROID_CHANGES
+    /* Originally linkname is used to create named pid files, which is
+    * meaningless to android. Here we use it as a suffix of program names,
+    * so different users can run their own program by specifying it. For
+    * example, "/etc/ppp/ip-up-vpn" will be executed when IPCP is up and
+    * linkname is "vpn". Note that "/" is not allowed for security reasons. */
+    char file[MAXPATHLEN];
+
+    if (linkname[0] && !strchr(linkname, '/')) {
+        snprintf(file, MAXPATHLEN, "%s-%s", prog, linkname);
+        file[MAXPATHLEN - 1] = '\0';
+        prog = file;
+    }
+#endif
+
     /*
      * First check if the file exists and is executable.
      * We don't use access() because that would use the
@@ -1667,11 +1725,15 @@
     /* run the program */
     execve(prog, args, script_env);
     if (must_exist || errno != ENOENT) {
+#ifndef ANDROID_CHANGES
 	/* have to reopen the log, there's nowhere else
 	   for the message to go. */
 	reopen_log();
 	syslog(LOG_ERR, "Can't execute %s: %m", prog);
 	closelog();
+#else
+	error("Can't execute %s: %m", prog);
+#endif
     }
     _exit(-1);
 }
diff --git a/pppd/openssl-hash.c b/pppd/openssl-hash.c
new file mode 100644
index 0000000..840a68c
--- /dev/null
+++ b/pppd/openssl-hash.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <openssl/evp.h>
+
+const EVP_MD *sha1_md;
+const EVP_MD *md4_md;
+const EVP_MD *md5_md;
+
+void    openssl_hash_init() {
+    /* Use the SHA1 functions in openssl to save the flash space.*/
+    OpenSSL_add_all_digests();
+    sha1_md = EVP_get_digestbyname("sha1");
+    if (!sha1_md) {
+        dbglog("Error Unknown message digest SHA1\n");
+        exit(1);
+    }
+    md4_md = EVP_get_digestbyname("md4");
+    if (!md4_md) {
+        dbglog("Error Unknown message digest MD4\n");
+        exit(1);
+    }
+    md5_md = EVP_get_digestbyname("md5");
+    if (!md5_md) {
+        dbglog("Error Unknown message digest MD5\n");
+        exit(1);
+    }
+}
diff --git a/pppd/openssl-hash.h b/pppd/openssl-hash.h
new file mode 100644
index 0000000..a2a5abe
--- /dev/null
+++ b/pppd/openssl-hash.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __OPENSSL_HASH__
+#define __OPENSSL_HASH__
+
+#include <openssl/evp.h>
+
+extern  const EVP_MD *sha1_md;
+#define SHA1_SIGNATURE_SIZE 20
+#define SHA1_CTX	EVP_MD_CTX
+#define SHA1_Init(ctx)  { \
+    EVP_MD_CTX_init(ctx); \
+    EVP_DigestInit_ex(ctx, sha1_md, NULL); \
+}
+#define SHA1_Update EVP_DigestUpdate
+#define SHA1_Final(digest, ctx)  { \
+    int md_len; \
+    EVP_DigestFinal_ex(ctx, digest, &md_len); \
+}
+
+extern const EVP_MD *md4_md;
+#define MD4_CTX EVP_MD_CTX
+#define MD4Init(ctx)  { \
+    EVP_MD_CTX_init(ctx); \
+    EVP_DigestInit_ex(ctx, md4_md, NULL); \
+}
+#define MD4Update   EVP_DigestUpdate
+#define MD4Final    SHA1_Final
+
+extern const EVP_MD *md5_md;
+#define MD5_CTX EVP_MD_CTX
+#define MD5_Init(ctx)  { \
+    EVP_MD_CTX_init(ctx); \
+    EVP_DigestInit_ex(ctx, md5_md, NULL); \
+}
+#define MD5_Update   EVP_DigestUpdate
+#define MD5_Final    SHA1_Final
+
+extern  void    openssl_hash_init();
+
+#endif
diff --git a/pppd/options.c b/pppd/options.c
index 5375bf3..f8a836b 100644
--- a/pppd/options.c
+++ b/pppd/options.c
@@ -1065,8 +1065,9 @@
     va_end(args);
     if (phase == PHASE_INITIALIZE)
 	fprintf(stderr, "%s: %s\n", progname, buf);
+#ifndef ANDROID_CHANGES
     syslog(LOG_ERR, "%s", buf);
-#ifdef ANDROID_CHANGES
+#else
     error("%s", buf);
 #endif    
 }
diff --git a/pppd/pppcrypt.c b/pppd/pppcrypt.c
index 8b85b13..1302c83 100644
--- a/pppd/pppcrypt.c
+++ b/pppd/pppcrypt.c
@@ -171,7 +171,7 @@
 }
 
 bool
-DesEncrypt(clear, key, cipher)
+DesEncrypt(clear, cipher)
 u_char *clear;	/* IN  8 octets */
 u_char *cipher;	/* OUT 8 octets */
 {
diff --git a/pppd/pppcrypt.h b/pppd/pppcrypt.h
index adcdcbc..33b956d 100644
--- a/pppd/pppcrypt.h
+++ b/pppd/pppcrypt.h
@@ -38,8 +38,12 @@
 #endif
 
 #ifndef USE_CRYPT
+#ifdef ANDROID_CHANGES
+#include <openssl/des.h>
+#else
 #include <des.h>
 #endif
+#endif
 
 extern bool	DesSetkey __P((u_char *));
 extern bool	DesEncrypt __P((u_char *, u_char *));
diff --git a/pppd/pppox.c b/pppd/pppox.c
new file mode 100644
index 0000000..9604fba
--- /dev/null
+++ b/pppd/pppox.c
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <unistd.h>
+#include "pppd.h"
+
+static int pppox_set(char **);
+static int pppox_connect();
+static void pppox_disconnect();
+
+static option_t pppox_options[] = {
+    {"pppox", o_special, pppox_set, "PPPoX socket", OPT_DEVNAM},
+    {NULL},
+};
+
+static struct channel pppox_channel = {
+    .options = pppox_options,
+    .process_extra_options = NULL,
+    .check_options = NULL,
+    .connect = pppox_connect,
+    .disconnect = pppox_disconnect,
+    .establish_ppp = generic_establish_ppp,
+    .disestablish_ppp = generic_disestablish_ppp,
+    .send_config = NULL,
+    .recv_config = NULL,
+    .cleanup = NULL,
+    .close = NULL,
+};
+
+static int pppox = -1;
+
+static int pppox_set(char **argv) {
+    if (!int_option(*argv, &pppox)) {
+        return 0;
+    }
+    info("Using PPPoX (socket = %d)", pppox);
+    the_channel = &pppox_channel;
+    return 1;
+}
+
+static int pppox_connect() {
+    return pppox;
+}
+
+static void pppox_disconnect() {
+    if (pppox != -1) {
+        close(pppox);
+        pppox = -1;
+    }
+}
+
+void pppox_init() {
+    add_options(pppox_options);
+}
diff --git a/pppd/utils.c b/pppd/utils.c
index 6f668b8..1ef4b3b 100644
--- a/pppd/utils.c
+++ b/pppd/utils.c
@@ -665,11 +665,31 @@
     log_write(level, buf);
 }
 
+#ifdef ANDROID_CHANGES
+
+#if LOG_PRIMASK != 7
+#error Syslog.h has been changed! Please fix this table!
+#endif
+
+static int syslog_to_android[] = {
+    [LOG_EMERG] = ANDROID_LOG_FATAL,
+    [LOG_ALERT] = ANDROID_LOG_FATAL,
+    [LOG_CRIT] = ANDROID_LOG_FATAL,
+    [LOG_ERR] = ANDROID_LOG_ERROR,
+    [LOG_WARNING] = ANDROID_LOG_WARN,
+    [LOG_NOTICE] = ANDROID_LOG_INFO,
+    [LOG_INFO] = ANDROID_LOG_INFO,
+    [LOG_DEBUG] = ANDROID_LOG_DEBUG,
+};
+
+#endif
+
 static void
 log_write(level, buf)
     int level;
     char *buf;
 {
+#ifndef ANDROID_CHANGES
     syslog(level, "%s", buf);
 
     fprintf(stderr, buf);
@@ -683,6 +703,9 @@
 	    || write(log_to_fd, "\n", 1) != 1)
 	    log_to_fd = -1;
     }
+#else
+    __android_log_write(syslog_to_android[level], LOG_TAG, buf);
+#endif
 }
 
 /*
@@ -701,11 +724,7 @@
     fmt = va_arg(pvar, char *);
 #endif
 
-#ifdef ANDROID_CHANGES
-    __android_log_vprint(ANDROID_LOG_ERROR, LOG_TAG, fmt, pvar);
-#else
     logit(LOG_ERR, fmt, pvar);
-#endif
     va_end(pvar);
 
     die(1);			/* as promised */
@@ -727,12 +746,7 @@
     fmt = va_arg(pvar, char *);
 #endif
 
-#ifdef ANDROID_CHANGES
-    __android_log_vprint(ANDROID_LOG_ERROR, LOG_TAG, fmt, pvar);
-#else
     logit(LOG_ERR, fmt, pvar);
-#endif
-
     va_end(pvar);
     ++error_count;
 }
@@ -753,12 +767,7 @@
     fmt = va_arg(pvar, char *);
 #endif
 
-#ifdef ANDROID_CHANGES
-    __android_log_vprint(ANDROID_LOG_WARN, LOG_TAG, fmt, pvar);
-#else
     logit(LOG_WARNING, fmt, pvar);
-#endif
-
     va_end(pvar);
 }
 
@@ -778,11 +787,7 @@
     fmt = va_arg(pvar, char *);
 #endif
 
-#ifdef ANDROID_CHANGES
-    __android_log_vprint(ANDROID_LOG_INFO, LOG_TAG, fmt, pvar);
-#else
     logit(LOG_NOTICE, fmt, pvar);
-#endif
     va_end(pvar);
 }
 
@@ -802,12 +807,7 @@
     fmt = va_arg(pvar, char *);
 #endif
 
-#ifdef ANDROID_CHANGES
-    __android_log_vprint(ANDROID_LOG_INFO, LOG_TAG, fmt, pvar);
-#else
     logit(LOG_INFO, fmt, pvar);
-#endif
-
     va_end(pvar);
 }
 
@@ -827,11 +827,7 @@
     fmt = va_arg(pvar, char *);
 #endif
 
-#ifdef ANDROID_CHANGES
-    __android_log_vprint(ANDROID_LOG_DEBUG, LOG_TAG, fmt, pvar);
-#else
     logit(LOG_DEBUG, fmt, pvar);
-#endif
     va_end(pvar);
 }