Merge "Correction to use of TEMP_FAILURE_RETRY in send_prop_msg"
diff --git a/libc/include/sys/atomics.h b/libc/include/sys/atomics.h
index 3ada8de..143bc4b 100644
--- a/libc/include/sys/atomics.h
+++ b/libc/include/sys/atomics.h
@@ -47,20 +47,20 @@
 #define __ATOMIC_INLINE__ static __inline__ __attribute__((always_inline))
 
 __ATOMIC_INLINE__ int
-__atomic_cmpxchg(int old, int _new, volatile int *ptr)
+__atomic_cmpxchg(int old_value, int new_value, volatile int* ptr)
 {
     /* We must return 0 on success */
-    return __sync_val_compare_and_swap(ptr, old, _new) != old;
+    return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value;
 }
 
 __ATOMIC_INLINE__ int
-__atomic_swap(int _new, volatile int *ptr)
+__atomic_swap(int new_value, volatile int *ptr)
 {
-    int prev;
+    int old_value;
     do {
-        prev = *ptr;
-    } while (__sync_val_compare_and_swap(ptr, prev, _new) != prev);
-    return prev;
+        old_value = *ptr;
+    } while (__sync_val_compare_and_swap(ptr, old_value, new_value) != old_value);
+    return old_value;
 }
 
 __ATOMIC_INLINE__ int
diff --git a/libc/include/time.h b/libc/include/time.h
index 8867b32..e280e0a 100644
--- a/libc/include/time.h
+++ b/libc/include/time.h
@@ -103,6 +103,7 @@
 #define CLOCK_THREAD_CPUTIME_ID    3
 #define CLOCK_REALTIME_HR          4
 #define CLOCK_MONOTONIC_HR         5
+#define CLOCK_BOOTTIME             7
 
 extern int  timer_create(int, struct sigevent*, timer_t*);
 extern int  timer_delete(timer_t);
diff --git a/libc/private/bionic_atomic_gcc_builtin.h b/libc/private/bionic_atomic_gcc_builtin.h
index e7c5761..2919f7f 100644
--- a/libc/private/bionic_atomic_gcc_builtin.h
+++ b/libc/private/bionic_atomic_gcc_builtin.h
@@ -31,18 +31,17 @@
 __bionic_cmpxchg(int32_t old_value, int32_t new_value, volatile int32_t* ptr)
 {
     /* We must return 0 on success */
-    return __sync_bool_compare_and_swap(ptr, old_value, new_value) == 0;
+    return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value;
 }
 
 __ATOMIC_INLINE__ int32_t
 __bionic_swap(int32_t new_value, volatile int32_t* ptr)
 {
-    int32_t prev;
+    int32_t old_value;
     do {
-        prev = *ptr;
-        status = __sync_val_compare_and_swap(ptr, prev, new_value);
-    } while (status == 0);
-    return prev;
+        old_value = *ptr;
+    } while (__sync_val_compare_and_swap(ptr, old_value, new_value) != old_value);
+    return old_value;
 }
 
 __ATOMIC_INLINE__ int32_t
diff --git a/libc/private/bionic_atomic_inline.h b/libc/private/bionic_atomic_inline.h
index 821ad39..0b348c3 100644
--- a/libc/private/bionic_atomic_inline.h
+++ b/libc/private/bionic_atomic_inline.h
@@ -53,6 +53,8 @@
 #  include <bionic_atomic_arm.h>
 #elif defined(__i386__)
 #  include <bionic_atomic_x86.h>
+#elif defined(__mips__)
+#  include <bionic_atomic_mips.h>
 #else
 #  include <bionic_atomic_gcc_builtin.h>
 #endif
diff --git a/libc/private/bionic_atomic_mips.h b/libc/private/bionic_atomic_mips.h
new file mode 100644
index 0000000..28fe88d
--- /dev/null
+++ b/libc/private/bionic_atomic_mips.h
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2011 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 BIONIC_ATOMIC_MIPS_H
+#define BIONIC_ATOMIC_MIPS_H
+
+/* Define a full memory barrier, this is only needed if we build the
+ * platform for a multi-core device.
+ */
+#if defined(ANDROID_SMP) && ANDROID_SMP == 1
+__ATOMIC_INLINE__ void
+__bionic_memory_barrier()
+{
+    __asm__ __volatile__ ( "sync" : : : "memory" );
+}
+#else
+__ATOMIC_INLINE__ void
+__bionic_memory_barrier()
+{
+    /* A simple compiler barrier */
+    __asm__ __volatile__ ( "" : : : "memory" );
+}
+#endif
+
+/* Compare-and-swap, without any explicit barriers. Note that this function
+ * returns 0 on success, and 1 on failure. The opposite convention is typically
+ * used on other platforms.
+ */
+__ATOMIC_INLINE__ int
+__bionic_cmpxchg(int32_t old_value, int32_t new_value, volatile int32_t* ptr)
+{
+    int32_t prev, status;
+    __asm__ __volatile__ ("1: move %[status], %[new_value]  \n"
+                          "   ll %[prev], 0(%[ptr])         \n"
+                          "   bne %[old_value], %[prev], 2f \n"
+                          "   sc   %[status], 0(%[ptr])     \n"
+                          "   beqz %[status], 1b            \n"
+                          "2:                               \n"
+                          : [prev]"=&r"(prev), [status]"=&r"(status), "+m"(*ptr)
+                          : [new_value]"r"(new_value), [old_value]"r"(old_value), [ptr]"r"(ptr)
+                          : "memory");
+    return prev != old_value;
+}
+
+
+/* Swap, without any explicit barriers */
+__ATOMIC_INLINE__ int32_t
+__bionic_swap(int32_t new_value, volatile int32_t *ptr)
+{
+   int32_t prev, status;
+    __asm__ __volatile__ ("1:  move %[status], %[new_value] \n"
+                          "    ll %[prev], 0(%[ptr])        \n"
+                          "    sc %[status], 0(%[ptr])      \n"
+                          "    beqz %[status], 1b           \n"
+                          : [prev]"=&r"(prev), [status]"=&r"(status), "+m"(*ptr)
+                          : [ptr]"r"(ptr), [new_value]"r"(new_value)
+                          : "memory");
+    return prev;
+}
+
+/* Atomic increment, without explicit barriers */
+__ATOMIC_INLINE__ int32_t
+__bionic_atomic_inc(volatile int32_t *ptr)
+{
+    int32_t prev, status;
+    __asm__ __volatile__ ("1:  ll %[prev], 0(%[ptr])        \n"
+                          "    addiu %[status], %[prev], 1  \n"
+                          "    sc   %[status], 0(%[ptr])    \n"
+                          "    beqz %[status], 1b           \n"
+                          : [prev]"=&r" (prev), [status]"=&r"(status), "+m" (*ptr)
+                          : [ptr]"r"(ptr)
+                          : "memory");
+    return prev;
+}
+
+/* Atomic decrement, without explicit barriers */
+__ATOMIC_INLINE__ int32_t
+__bionic_atomic_dec(volatile int32_t *ptr)
+{
+    int32_t prev, status;
+    __asm__ __volatile__ ("1:  ll %[prev], 0(%[ptr])        \n"
+                          "    addiu %[status], %[prev], -1 \n"
+                          "    sc   %[status], 0(%[ptr])    \n"
+                          "    beqz %[status], 1b           \n"
+                          : [prev]"=&r" (prev), [status]"=&r"(status), "+m" (*ptr)
+                          : [ptr]"r"(ptr)
+                          : "memory");
+    return prev;
+}
+#endif /* BIONIC_ATOMIC_MIPS_H */
diff --git a/libc/stdio/vfprintf.c b/libc/stdio/vfprintf.c
index dac8496..427fc7f 100644
--- a/libc/stdio/vfprintf.c
+++ b/libc/stdio/vfprintf.c
@@ -559,7 +559,7 @@
 #endif /* FLOATING_POINT */
 /* the Android security team suggests removing support for %n
  * since it has no real practical value, and could lead to
- * running malicious code (for really bugy programs that
+ * running malicious code (for really buggy programs that
  * send to printf() user-generated formatting strings).
  */
 #if 0
diff --git a/libc/tools/zoneinfo/generate b/libc/tools/zoneinfo/generate
index 3e21d0b..ab2617f 100755
--- a/libc/tools/zoneinfo/generate
+++ b/libc/tools/zoneinfo/generate
@@ -1,82 +1,131 @@
-#!/bin/bash
+#!/usr/bin/python
 # Run with no arguments from any directory, with no special setup required.
 
-# Abort if any command returns an error exit status, or if an undefined
-# variable is used.
-set -e
-set -u
+import ftplib
+import hashlib
+import os
+import re
+import shutil
+import string
+import subprocess
+import sys
+import tarfile
+import tempfile
 
-echo "Looking for bionic..."
-bionic_dir=$(cd $(dirname $0)/../../.. && pwd)
-bionic_zoneinfo_dir=$bionic_dir/libc/zoneinfo
-bionic_zoneinfo_tools_dir=$bionic_dir/libc/tools/zoneinfo
-if [[ ! -d "$bionic_zoneinfo_dir" || ! -d "$bionic_zoneinfo_tools_dir" ]]; then
-  echo "Can't find bionic's zoneinfo directories!"
-  exit 1
-fi
+# Find the bionic directory, searching upward from this script.
+bionic_libc_tools_zoneinfo_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
+bionic_libc_tools_dir = os.path.dirname(bionic_libc_tools_zoneinfo_dir)
+bionic_libc_dir = os.path.dirname(bionic_libc_tools_dir)
+bionic_dir = os.path.dirname(bionic_libc_dir)
+bionic_libc_zoneinfo_dir = '%s/libc/zoneinfo' % bionic_dir
+if not os.path.isdir(bionic_libc_tools_zoneinfo_dir) or not os.path.isdir(bionic_libc_zoneinfo_dir):
+  print "Couldn't find bionic/libc/tools/zoneinfo!"
+  sys.exit(1)
+print 'Found bionic in %s...' % bionic_dir
 
-echo "Switching to temporary directory..."
-temp_dir=`mktemp -d`
-cd $temp_dir
-trap "rm -rf $temp_dir; exit" INT TERM EXIT
+
+regions = ['africa', 'antarctica', 'asia', 'australasia', 'backward', 'etcetera', 'europe', 'northamerica', 'southamerica']
+
+
+def current_tzdata_version():
+  return open('%s/zoneinfo.version' % bionic_libc_zoneinfo_dir).readline().rstrip('\n')
+
+
+def md5_file(filename):
+  md5 = hashlib.md5()
+  f = open(filename, 'rb')
+  while True:
+    data = f.read(8192)
+    if not data:
+      break
+    md5.update(data)
+  return md5.hexdigest()
+
+
+def upgrade_to(ftp, filename):
+  version = re.search('tzdata(.+)\.tar\.gz', filename).group(1)
+
+  # Switch to a temporary directory.
+  tmp_dir = tempfile.mkdtemp('-tzdata')
+  os.chdir(tmp_dir)
+  print 'Created temporary directory "%s"...' % tmp_dir
+
+  print 'Downloading %s...' % filename
+  ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
+  print 'MD5: %s' % md5_file(filename)
+
+  print 'Extracting...'
+  os.mkdir('extracted')
+  tar = tarfile.open(filename, 'r')
+  tar.extractall('extracted')
+
+  print 'Calling zic(1)...'
+  os.mkdir('data')
+  for region in regions:
+    if region != 'backward':
+      subprocess.check_call(['zic', '-d', 'data', 'extracted/%s' % region])
+
+  # Collect the data ZoneCompactor needs.
+  links = []
+  zones = []
+  for region in regions:
+    for line in open('extracted/%s' % region).readlines():
+      fields = string.split(line)
+      if len(fields) == 0:
+        continue
+      elif fields[0] == 'Link':
+        links.append('%s %s %s\n' % (fields[0], fields[1], fields[2]))
+        zones.append(fields[2])
+      elif fields[0] == 'Zone':
+        zones.append(fields[1])
+  zones.sort()
+
+  # Write it into the "setup" file.
+  setup = open('setup', 'w')
+  for link in links:
+    setup.write(link)
+  for zone in zones:
+    setup.write('%s\n' % zone)
+  setup.close()
+
+  print 'Calling ZoneCompactor...'
+  subprocess.check_call(['javac', '-d', '.',
+                         '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir,
+                         '%s/ZoneInfo.java' % bionic_libc_tools_zoneinfo_dir])
+  subprocess.check_call(['java', 'ZoneCompactor', 'setup', 'data'])
+
+  print 'Updating bionic from %s to %s...' % (current_tzdata_version(), version)
+  # Move the .dat and .idx files...
+  os.remove('%s/zoneinfo.dat' % bionic_libc_zoneinfo_dir)
+  shutil.move('zoneinfo.dat', bionic_libc_zoneinfo_dir)
+  os.remove('%s/zoneinfo.idx' % bionic_libc_zoneinfo_dir)
+  shutil.move('zoneinfo.idx', bionic_libc_zoneinfo_dir)
+  # Write the .version file...
+  zoneinfo_version = open('%s/zoneinfo.version' % bionic_libc_zoneinfo_dir, 'wb+')
+  zoneinfo_version.write('%s\n' % version)
+  zoneinfo_version.close()
+
 
 # URL from "Sources for Time Zone and Daylight Saving Time Data"
 # http://www.twinsun.com/tz/tz-link.htm
-echo "Looking for new tzdata..."
-wget -N --no-verbose 'ftp://munnari.oz.au/pub/tzdata*.tar.gz'
-zoneinfo_version_file=$bionic_zoneinfo_dir/zoneinfo.version
-if [ -f "$zoneinfo_version_file" ]; then
-  current_version=tzdata`sed s/\n// < $zoneinfo_version_file`
-else
-  current_version=missing
-fi
-latest_archive=`ls -r -v tzdata*.tar.gz | head -n1`
-latest_version=`basename $latest_archive .tar.gz`
-if [ "$current_version" == "$latest_version" ]; then
-  echo "You already have the latest tzdata ($latest_version)!"
-  exit 1
-fi
 
-md5_sum=`md5sum $latest_archive`
-echo "MD5: $md5_sum"
+print 'Looking for new tzdata...'
+ftp = ftplib.FTP('ftp.iana.org')
+ftp.login()
+ftp.cwd('tz/releases')
+tzdata_filenames = []
+for filename in ftp.nlst():
+  if filename.startswith('tzdata20'):
+    tzdata_filenames.append(filename)
+tzdata_filenames.sort()
 
-echo "Extracting $latest_version..."
-mkdir $latest_version
-tar -C $latest_version -zxf $latest_archive
+# If you're several releases behind, we'll walk you through the upgrades one by one.
+current_version = current_tzdata_version()
+current_filename = 'tzdata%s.tar.gz' % current_version
+for filename in tzdata_filenames:
+  if filename > current_filename:
+    upgrade_to(ftp, filename)
+    sys.exit(0)
 
-echo "Compiling $latest_version..."
-mkdir data
-for i in \
-    africa \
-    antarctica \
-    asia \
-    australasia \
-    etcetera \
-    europe \
-    factory \
-    northamerica \
-    solar87 \
-    solar88 \
-    solar89 \
-    southamerica
-do
-    zic -d data $latest_version/$i
-done
-
-echo "Compacting $latest_version..."
-(
-    cat $latest_version/* | grep '^Link' | awk '{print $1, $2, $3}'
-    (
-        cat $latest_version/* | grep '^Zone' | awk '{print $2}'
-        cat $latest_version/* | grep '^Link' | awk '{print $3}'
-    ) | LC_ALL="C" sort
-) | grep -v Riyadh8 > setup
-
-javac -d . \
-    $bionic_zoneinfo_tools_dir/ZoneCompactor.java \
-    $bionic_zoneinfo_tools_dir/ZoneInfo.java
-java ZoneCompactor setup data
-
-echo "Updating bionic to $latest_version..."
-mv zoneinfo.dat zoneinfo.idx $bionic_zoneinfo_dir
-echo $latest_version | sed 's/tzdata//' > $bionic_zoneinfo_dir/zoneinfo.version
+print 'You already have the latest tzdata (%s)!' % current_version
+sys.exit(0)
diff --git a/libc/unistd/abort.c b/libc/unistd/abort.c
index a3f8c54..58d620d 100644
--- a/libc/unistd/abort.c
+++ b/libc/unistd/abort.c
@@ -79,7 +79,12 @@
 
     /* temporary, for bug hunting */
     /* seg fault seems to produce better debuggerd results than SIGABRT */
+#ifdef __mips__
+    /* An access that will generate SIGSEGV rather than SIGBUS. */
+    *((char*)0xdeadc0c0) = 39;
+#else
     *((char*)0xdeadbaad) = 39;
+#endif
     /* -- */
 
 	(void)kill(getpid(), SIGABRT);
diff --git a/libc/zoneinfo/zoneinfo.dat b/libc/zoneinfo/zoneinfo.dat
index cc1cb38..82d6b9c 100644
--- a/libc/zoneinfo/zoneinfo.dat
+++ b/libc/zoneinfo/zoneinfo.dat
Binary files differ
diff --git a/libc/zoneinfo/zoneinfo.idx b/libc/zoneinfo/zoneinfo.idx
index 0483c28..2268706 100644
--- a/libc/zoneinfo/zoneinfo.idx
+++ b/libc/zoneinfo/zoneinfo.idx
Binary files differ
diff --git a/libc/zoneinfo/zoneinfo.version b/libc/zoneinfo/zoneinfo.version
index 9412b94..e606761 100644
--- a/libc/zoneinfo/zoneinfo.version
+++ b/libc/zoneinfo/zoneinfo.version
@@ -1 +1 @@
-2012c
+2012d